get( 'adsense_account', 'adsense' ); } /** * Get the base URL for AdSense JS * * @return string */ public function adsense_js_base_url() { /** * Filter: adcmdr_adsense_js_base_url * * Filters the AdSense URL allowing you to change or add parameters if needed. */ return apply_filters( 'adcmdr_adsense_js_base_url', 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' ); } /** * The full AdSense JS URL with parameters appended. * * @param null|string $publisher_id The publisher ID to append to the AdSense URL. * * @return string */ public function adsense_js_url( $publisher_id = null ) { $url = $this->adsense_js_base_url(); if ( $publisher_id ) { $client_id = 'ca-' . $publisher_id; $url = add_query_arg( array( 'client' => esc_attr( $client_id ), ), $url ); } /** * Filter: adcmdr_adsense_js_url * * Filters the AdSense URL allowing you to change or add parameters if needed. */ return apply_filters( 'adcmdr_adsense_js_url', $url ); } /** * The full AdSense script tag. * * @param bool $fallback_id A fake publisher_id used for display purposes. * * @return string */ public function get_adsense_script_url( $fallback_id = false ) { $publisher_id = $this->get_adsense_publisher_id(); if ( ! $publisher_id && $fallback_id ) { $publisher_id = $fallback_id; } if ( $publisher_id ) { return $this->adsense_js_url( $publisher_id ); } return ''; } /** * Maybe insert the AdSense script into wp_head. */ public function wp_enqueue_scripts() { if ( ! is_admin() && Options::instance()->get( 'insert_adsense_head_code', 'adsense', true ) ) { $adsense_url = $this->get_adsense_script_url(); if ( $adsense_url != '' ) { $handle = Util::ns( 'adsense' ); wp_register_script( $handle, $adsense_url, array(), // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- The version is intentionally null on this script, because we want to match Google AdSense's typical script tag. null, array( 'in_footer' => false, 'strategy' => 'async', ) ); wp_enqueue_script( $handle ); } } } /** * Filters the AdSense script tag after enqueue. * * @param string $tag The script tag. * @param string $handle The script handle. * @param string $src The script src. * * @return string */ public function script_loader_tag( $tag, $handle, $src ) { if ( $handle === Util::ns( 'adsense' ) ) { $tag = str_ireplace( '>', ' crossorigin="anonymous">', $tag ); } return $tag; } }