, with the service patterns it needs. * It must run before any other script, so it cannot be an enqueued file. */ final class Guard { /** * Compact configuration: per service its category and lower-cased patterns. * Keys are short because this is printed on every page: s = script src, i = inline code, * f = iframe src, c = category. * * @param array $settings Settings. * @return array */ public static function config( array $settings ) { $services = array(); foreach ( Blocker::candidates( Services::active(), $settings ) as $service ) { $scripts = array_values( array_filter( array_map( 'strtolower', (array) $service['scripts'] ) ) ); $services[] = array( 'id' => $service['id'], 'c' => $service['category'], 's' => $settings['block_scripts'] ? $scripts : array(), 'i' => $settings['block_scripts'] ? array_values( array_filter( array_map( array( Blocker::class, 'normalise' ), (array) $service['scripts'] ) ) ) : array(), 'f' => $settings['block_embeds'] ? array_values( array_filter( array_map( 'strtolower', (array) $service['iframes'] ) ) ) : array(), ); } return array( 'cookie' => Banner::COOKIE, 'revision' => (int) $settings['revision'], 'services' => $services, 'perSvc' => (bool) $settings['service_choice'], ); } /** * Prints the guard. */ public static function print_guard() { $settings = Settings::get(); if ( ! $settings['block_scripts'] && ! $settings['block_embeds'] ) { return; } $code = self::code(); if ( '' === $code ) { return; } $js = 'window.ariconsentGuardConfig=' . wp_json_encode( self::config( $settings ) ) . ";\n" . $code; wp_print_inline_script_tag( $js, array( 'id' => 'ariconsent-guard' ) + Plugin::no_optimize_attributes() ); } /** * Guard source, read once per request from the plugin's own file. * * @return string */ private static function code() { static $code = null; if ( null === $code ) { $path = ARICONSENT_DIR . 'assets/js/guard.js'; $code = is_readable( $path ) ? (string) file_get_contents( $path ) : ''; // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local plugin file. } return $code; } }