*/ public static function script_attributes() { return array( 'data-site-key' => self::site_key(), 'data-consent' => self::consent_attr(), 'data-auto-forms' => self::auto_forms_attr(), ); } /** * Is the module switched on in settings? * * Absent from a stored modules map means off: a site that connected * before this module existed does not start loading a new script on * upgrade. module_enabled() also returns false while workspace features * are off. * * @return bool */ public static function is_enabled() { if ( class_exists( 'AdvisorPPC_Options' ) && method_exists( 'AdvisorPPC_Options', 'module_enabled' ) ) { return (bool) AdvisorPPC_Options::module_enabled( 'tracktor' ); } return false; } /** * Front-end request test, shared with the click-ID module when present. * * @return bool */ public static function should_load() { if ( class_exists( 'AdvisorPPC_ClickIDs' ) && method_exists( 'AdvisorPPC_ClickIDs', 'should_load' ) ) { return (bool) AdvisorPPC_ClickIDs::should_load(); } return ! is_admin(); } /** * Switched on, connected, holding a usable site key, and on a public page. * * @return bool */ public static function is_active() { if ( class_exists( 'AdvisorPPC_Options' ) && method_exists( 'AdvisorPPC_Options', 'workspace_enabled' ) && ! AdvisorPPC_Options::workspace_enabled() ) { return false; } if ( ! self::is_enabled() ) { return false; } if ( ! self::should_load() ) { return false; } if ( class_exists( 'AdvisorPPC_Options' ) && method_exists( 'AdvisorPPC_Options', 'is_connected' ) ) { if ( ! AdvisorPPC_Options::is_connected() ) { return false; } } elseif ( ! self::opt( 'connected', false ) ) { return false; } if ( '' === self::site_key() ) { return false; } if ( '' === self::api_base() ) { return false; } return true; } /** * Plugin URL for a bundled asset. * * @param string $relative Path relative to the plugin root. * @return string */ private static function asset_url( $relative ) { if ( class_exists( 'AdvisorPPC_Consent' ) && method_exists( 'AdvisorPPC_Consent', 'asset_url' ) ) { return AdvisorPPC_Consent::asset_url( $relative ); } if ( defined( 'ADVISORPPC_PLUGIN_URL' ) ) { return ADVISORPPC_PLUGIN_URL . ltrim( $relative, '/' ); } return plugin_dir_url( __DIR__ ) . ltrim( $relative, '/' ); } /** * Asset cache-busting version. * * @return string */ private static function asset_version() { if ( class_exists( 'AdvisorPPC_Consent' ) && method_exists( 'AdvisorPPC_Consent', 'asset_version' ) ) { return AdvisorPPC_Consent::asset_version(); } return defined( 'ADVISORPPC_VERSION' ) ? ADVISORPPC_VERSION : '0.1.0'; } /** * Enqueue the remote tag and the local bridge. Once per page, front end * only, never in wp-admin, never before Connect. * * @return void */ public static function enqueue() { if ( ! self::is_active() ) { return; } $src = self::script_url(); if ( '' === $src ) { return; } if ( function_exists( 'wp_script_is' ) && wp_script_is( self::HANDLE, 'enqueued' ) ) { return; } $tag_deps = array(); if ( class_exists( 'AdvisorPPC_Consent' ) ) { $tag_deps[] = AdvisorPPC_Consent::HANDLE; } wp_enqueue_script( self::HANDLE, $src, $tag_deps, null, // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- remote tag versioned by the service. array( 'in_footer' => false, 'strategy' => 'async', ) ); $gate = self::gate_signals(); if ( '' !== $gate && class_exists( 'AdvisorPPC_Consent' ) ) { AdvisorPPC_Consent::register_gated( self::HANDLE, $gate ); } $bridge_deps = array( self::HANDLE ); if ( class_exists( 'AdvisorPPC_Consent' ) ) { $bridge_deps[] = AdvisorPPC_Consent::HANDLE; } wp_enqueue_script( self::BRIDGE_HANDLE, self::asset_url( 'public/js/tracktor-bridge.js' ), $bridge_deps, self::asset_version(), array( 'in_footer' => false, ) ); $pending = 'advisorppc_pending'; if ( class_exists( 'AdvisorPPC_Tags' ) && defined( 'AdvisorPPC_Tags::PENDING_COOKIE' ) ) { $pending = (string) constant( 'AdvisorPPC_Tags::PENDING_COOKIE' ); } $config = array( 'pendingCookie' => $pending, ); if ( '' !== $gate && class_exists( 'AdvisorPPC_Consent' ) ) { AdvisorPPC_Consent::register_gated( self::BRIDGE_HANDLE, $gate ); } wp_add_inline_script( self::BRIDGE_HANDLE, 'window.advisorppcTracktorCfg = ' . wp_json_encode( $config ) . ';', 'before' ); } /** * Add data-site-key, data-consent and data-auto-forms to the remote tag. * * @param string $tag Full script tag markup. * @param string $handle Script handle. * @param string $src Script source URL. * @return string */ public static function filter_script_tag( $tag, $handle, $src = '' ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- WP passes $src; the handle is the discriminator. if ( self::HANDLE !== $handle ) { return $tag; } if ( false !== strpos( $tag, 'data-site-key=' ) ) { return $tag; } $extra = ''; foreach ( self::script_attributes() as $name => $value ) { $extra .= ' ' . $name . '="' . esc_attr( $value ) . '"'; } $replaced = preg_replace( '/ 'advisorppc-tracktor-consume-js' ) ); return; } echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- literal JS, no user input. } } add_action( 'advisorppc_loaded', array( 'AdvisorPPC_Tracktor', 'init' ) );