*/ final class Script { /** * The FastCMP layout that renders the Decline button outside the consent * popup, top left. * * The local twin of the translation Krull owns for a synced site: the * `external-decline` template with the thin, left-aligned button every * production publisher on that template runs today. * * @since 4.4.0 * @var array */ const DECLINE_OUTSIDE_LAYOUT = array( 'template' => 'external-decline', 'refuseButtonSize' => 'thin', 'refuseButtonAlignment' => 'left', ); /** * The unique identifier of this plugin. * * @since 2.0.0 * @access protected * @var string $plugin_name The string used to uniquely identify this plugin. */ protected $plugin_name; /** * The current version of the plugin. * * @since 2.0.0 * @access protected * @var string $version The current version of the plugin. */ private $version; /** * No pub Class * * @since 2.0.0 * @access private * @var NoPub $no_pub The no pub class. */ private $no_pub; /** * Define the core functionality of the plugin. * * @since 2.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. * @param NoPub $no_pub The no pub class. */ public function __construct( $plugin_name, $version, $no_pub ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->no_pub = $no_pub; if ( ACTIRISE_CRON !== 'true' ) { $this->update_fastcmp(); } } /** * Register all of the hooks related to the public-facing functionality * of the plugin. * * @since 2.0.0 * @return void */ public function head_integration() { /** @var string $uuid */ $uuid = Options::get( 'settings-uuid', '' ); /** @var string $type_tag */ $type_tag = Options::get( 'settings-uuid-type', 'boot' ); /** @var string $custom_value1 */ $custom_value1 = Options::get( 'custom1', 'author_display_name' ); /** @var string $custom_value2 */ $custom_value2 = Options::get( 'custom2', 'category_0_slug' ); /** @var string $custom_value3 */ $custom_value3 = Options::get( 'custom3', 'post_ID' ); /** @var string $custom_value4 */ $custom_value4 = Options::get( 'custom4', '' ); /** @var string $custom_value5 */ $custom_value5 = Options::get( 'custom5', '' ); $script_option = version_compare( get_bloginfo( 'version' ), '6.3', '>=' ) ? array( 'in_footer' => false, ) : false; if ( Options::get( 'fastcmp-enabled', false ) === true ) { $fastcmp_options = Helpers::get_fastcmp_options( true ); if ( $fastcmp_options['privacyPolicy'] !== '' && $fastcmp_options['uuid'] !== '' ) { // Actirise holds this site's configuration and the banner reads it from // there, so the page identifies the domain and says nothing else. A site // the sync has not reached — every free publisher among them — has no such // configuration to read, so it keeps emitting its own, unchanged. So does // a site in the rollout with nothing published yet (version 0). if ( ConfigSync::has_published_config() ) { $fastcmp_config = sprintf( "window.FAST_CMP_OPTIONS = { configType: 'actirise-wp', domainUid: '%s', };", esc_js( $fastcmp_options['uuid'] ) ); } else { $fastcmp_config = $this->build_fastcmp_options( $fastcmp_options ); } $stublight = $fastcmp_options['stubLight']; $fast_cmp_options_script = array( 'data-no-optimize' => '1', 'data-wpmeteor-nooptimize' => 'true', 'data-cfasync' => 'false', 'nowprocket' => '', ); if ( defined( 'WP_ROCKET_VERSION' ) === false ) { $fast_cmp_options_script['charset'] = 'UTF-8'; } wp_enqueue_script( 'fastcmp', ACTIRISE_URL_FASTCMP_STATIC . 'fast-cmp-stub.js', array(), 'latest', $script_option ); wp_add_inline_script( 'fastcmp', $fastcmp_config . "\n" . $stublight, 'before' ); $fastcmp_options = Helpers::get_fastcmp_options( true ); $this->add_fastcmp_style( $fastcmp_options ); } } $this->add_presized_style(); if ( $uuid === '' ) { return; } $presized_slots_json = wp_json_encode( $this->get_presized_slot_active() ); if ( ! is_string( $presized_slots_json ) ) { $presized_slots_json = '[]'; } $no_pub = ! $this->no_pub->ads_enabled() ? 'no_pub: true,' : ''; wp_enqueue_script( 'actirise', ACTIRISE_URL_FLASHBID . esc_js( $type_tag ) . '/' . esc_js( $uuid ) . '.js', array(), 'latest', $script_option ); $data_var = sprintf( "window._hbdbrk=window._hbdbrk||[];window._hbdbrk.push(['_vars', {page_type:'%s',pid:3,custom1:'%s',custom2:'%s',custom3:'%s',custom4:'%s',custom5:'%s',%s}]);", esc_attr( Helpers::get_page_type() ), esc_attr( Helpers::get_custom_value( $custom_value1, 'custom1' ) ), esc_attr( Helpers::get_custom_value( $custom_value2, 'custom2' ) ), esc_attr( Helpers::get_custom_value( $custom_value3, 'custom3' ) ), esc_attr( Helpers::get_custom_value( $custom_value4, 'custom4' ) ), esc_attr( Helpers::get_custom_value( $custom_value5, 'custom5' ) ), esc_attr( $no_pub ) ); /** @var string $adstxt_active */ $adstxt_active = Options::get( 'adstxt-active', false ) ? '1' : '0'; /** @var string $presizeddiv_active */ $presizeddiv_active = Options::get( 'presizeddiv-active', false ) ? '1' : '0'; /** @var string $adstxt_last_update */ $adstxt_last_update = Options::get( 'adstxt-lastupdate', '-1' ); /** @var string $presizeddiv_last_update */ $presizeddiv_last_update = Options::get( 'presizeddiv-lastupdate', '-1' ); /** @var string $presizeddiv_last_update */ $version_lastupdate = Options::get( 'version-lastupdate', '-1' ); $actirise_plugin = sprintf( "window.actirisePlugin=window.actirisePlugin||{};window.actirisePlugin.version='%s';window.actirisePlugin.version_lastupdate=%s;window.actirisePlugin.adsTxt={active:%s,lastUpdate:%s};window.actirisePlugin.presized={active:%s,lastUpdate:%s,slots:%s};window.actirisePlugin.cron=%s;window.actirisePlugin.fastcmp=%s;window.actirisePlugin.autoUpdate=%s;window.actirisePlugin.debug=%s;", esc_attr( $this->version ), esc_attr( $version_lastupdate === '-1' ? '-1' : 'new Date(' . $version_lastupdate . ' * 1000)' ), esc_attr( $adstxt_active ), esc_attr( $adstxt_last_update === '-1' ? '-1' : 'new Date(' . $adstxt_last_update . ' * 1000)' ), esc_attr( $presizeddiv_active ), esc_attr( $presizeddiv_last_update === '-1' ? '-1' : 'new Date(' . $presizeddiv_last_update . ' * 1000)' ), $presized_slots_json, esc_attr( ( defined( 'ACTIRISE_CRON' ) && ACTIRISE_CRON === 'true' ) === true ? 'true' : 'false' ), esc_attr( Options::get( 'fastcmp-enabled', false ) === true ? 'true' : 'false' ), esc_attr( Helpers::has_auto_update() ? 'true' : 'false' ), esc_attr( Options::get( 'debug-enabled', false ) === true ? 'true' : 'false' ) ); wp_add_inline_script( 'actirise', $data_var . "\n" . $actirise_plugin, 'after' ); } /** * Change js attribute. * * @since 2.5.7 * @param string $tag The tag. * @param string $handle The handle. * @return string */ public function change_js_attribute( $tag, $handle ) { if ( 'actirise' === $handle ) { /** @var string $tag */ $tag = preg_replace( '#\sid=([\'"])actirise-js([\'"])#', '', $tag ); /** @var string $tag */ $tag = str_replace( 'src=', 'data-cfasync="false" defer src=', $tag ); /** @var string $tag */ $tag = preg_replace( '#\sid=([\'"])actirise-js-after([\'"])#', '', $tag ); /** @var string $tag */ $tag = preg_replace( '/\?ver=[0-9.a-z]+/', '', $tag ); } if ( 'fastcmp' === $handle ) { /** @var string $tag */ $tag = preg_replace( '#\sid=([\'"])fastcmp-js-before([\'"])#', ' data-no-optimize="1" data-wpmeteor-nooptimize="true" data-cfasync="false" nowprocket="" charset="UTF-8"', $tag ); /** @var string $tag */ $tag = str_replace( 'src=', 'async data-no-optimize="1" data-wpmeteor-nooptimize="true" data-cfasync="false" nowprocket="" charset="UTF-8" src=', $tag ); /** @var string $tag */ $tag = preg_replace( '#\sid=([\'"])fastcmp-js([\'"])#', '', $tag ); /** @var string $tag */ $tag = preg_replace( '/\?ver=[0-9.a-z]+/', '', $tag ); } return $tag; } /** * Add custom CSS style for FastCMP. * * Only the CSS the publisher wrote themselves, on every site: it is theirs, * and nothing on the Actirise side replaces it. The button styles are not * emitted from here — a site the sync has not reached carries them in the * `appearance` block of its own options, and a synced one is themed from the * configuration Actirise holds. * * @since 2.4.0 * @param array{customStyle: string} $fastcmp_options The fastcmp options. * @return void */ public function add_fastcmp_style( $fastcmp_options ) { $custom_style = preg_replace( '/\s\s+/', '', $fastcmp_options['customStyle'] ); if ( is_null( $custom_style ) || $custom_style === '' ) { return; } wp_register_style( 'fast-cmp-custom-styles', false, array(), $this->version ); wp_enqueue_style( 'fast-cmp-custom-styles' ); wp_add_inline_style( 'fast-cmp-custom-styles', trim( $custom_style ) ); } /** * Inject CSS for Presized Div * * @since 3.2.0 * * @return void */ public function add_presized_style() { $version = intval( time() / 3600 ); wp_register_style( 'actirise-presized', false, array(), strval( $version ) ); wp_enqueue_style( 'actirise-presized', '', array(), strval( $version ) ); /** @var array $css_code */ $css_code = array(); /** @var array $slots */ $slots = Options::get( 'presizeddiv-actirise', array() ); foreach ( $slots as $slot ) { if ( ! isset( $slot['cssCode'] ) || ! is_string( $slot['cssCode'] ) || $slot['cssCode'] === '' ) { continue; } // Defense in depth: stylesheets are validated at write time, but a // value stored before 4.4 or written directly in the database has // not been. Nothing may escape the