public.php
220 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName |
| 2 | |
| 3 | use AdvancedAds\Utilities\Conditional; |
| 4 | |
| 5 | /** |
| 6 | * Class Advanced_Ads_AdSense_Public. |
| 7 | */ |
| 8 | class Advanced_Ads_AdSense_Public { |
| 9 | |
| 10 | /** |
| 11 | * AdSense account related data |
| 12 | * |
| 13 | * @var Advanced_Ads_AdSense_Data |
| 14 | */ |
| 15 | private $data; |
| 16 | |
| 17 | /** |
| 18 | * Instance of Advanced_Ads_AdSense_Public |
| 19 | * |
| 20 | * @var Advanced_Ads_AdSense_Public |
| 21 | */ |
| 22 | private static $instance; |
| 23 | |
| 24 | /** |
| 25 | * Advanced_Ads_AdSense_Public constructor. |
| 26 | */ |
| 27 | private function __construct() { |
| 28 | $this->data = Advanced_Ads_AdSense_Data::get_instance(); |
| 29 | add_action( 'wp_head', [ $this, 'inject_header' ], 20 ); |
| 30 | // Fires before cache-busting frontend is initialized and tracking method is set. |
| 31 | add_action( 'wp', [ $this, 'inject_amp_code' ], 20 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get singleton instance. |
| 36 | * |
| 37 | * @return self |
| 38 | */ |
| 39 | public static function get_instance() { |
| 40 | if ( is_null( self::$instance ) ) { |
| 41 | self::$instance = new self(); |
| 42 | } |
| 43 | |
| 44 | return self::$instance; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Print data in the head tag on the front end. |
| 49 | */ |
| 50 | public function inject_header() { |
| 51 | $options = $this->data->get_options(); |
| 52 | |
| 53 | // Inject CSS to make AdSense background transparent. |
| 54 | if ( ! empty( $options['background'] ) ) { |
| 55 | // Some themes not only get the background wrong, but also add some padding to ins element. |
| 56 | echo '<style>ins.adsbygoogle { background-color: transparent; padding: 0; }</style>'; |
| 57 | } |
| 58 | |
| 59 | if ( Conditional::is_ad_disabled() || Conditional::is_amp() ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | $privacy = Advanced_Ads_Privacy::get_instance(); |
| 64 | $privacy_options = $privacy->options(); |
| 65 | $privacy_enabled = $privacy->get_state() !== 'not_needed'; |
| 66 | $npa_enabled = ( ! empty( $privacy_options['enabled'] ) && 'custom' === $privacy_options['consent-method'] ) && ! empty( $privacy_options['show-non-personalized-adsense'] ); |
| 67 | |
| 68 | // Show non-personalized Adsense ads if non-personalized ads are enabled and consent was not given. |
| 69 | if ( $privacy_enabled && $npa_enabled ) { |
| 70 | echo '<script>'; |
| 71 | // If the page is not from a cache. |
| 72 | if ( $privacy->get_state() === 'unknown' ) { |
| 73 | echo '(adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=1;'; |
| 74 | } |
| 75 | // If the page is from a cache, wait until 'advads.privacy' is available. Execute before cache-busting. |
| 76 | echo '( window.advanced_ads_ready || jQuery( document ).ready ).call( null, function() { |
| 77 | var state = ( advads.privacy ) ? advads.privacy.get_state() : ""; |
| 78 | var use_npa = ( state === "unknown" ) ? 1 : 0; |
| 79 | (adsbygoogle=window.adsbygoogle||[]).requestNonPersonalizedAds=use_npa; |
| 80 | } )'; |
| 81 | echo '</script>'; |
| 82 | } |
| 83 | |
| 84 | if ( ! apply_filters( 'advanced-ads-can-display-ads-in-header', true ) ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | $pub_id = trim( $this->data->get_adsense_id() ); |
| 89 | |
| 90 | if ( $pub_id && isset( $options['page-level-enabled'] ) && $options['page-level-enabled'] ) { |
| 91 | $pub_id = $this->data->get_adsense_id(); |
| 92 | $client_id = 'ca-' . $pub_id; |
| 93 | $top_anchor = isset( $options['top-anchor-ad'] ) && $options['top-anchor-ad']; |
| 94 | $top_anchor_code = sprintf( |
| 95 | '(adsbygoogle = window.adsbygoogle || []).push({ |
| 96 | google_ad_client: "%s", |
| 97 | enable_page_level_ads: true, |
| 98 | overlays: {bottom: true} |
| 99 | });', |
| 100 | esc_attr( $client_id ) |
| 101 | ); |
| 102 | /** |
| 103 | * Filter the output of the publisher ID appended to the AdSense JavaScript Code. |
| 104 | * |
| 105 | * @param boolean |
| 106 | */ |
| 107 | $add_publisher_id = apply_filters( 'advanced-ads-adsense-publisher-id', true ); |
| 108 | $script_src = add_query_arg( |
| 109 | [ 'client' => $add_publisher_id ? esc_attr( $client_id ) : false ], |
| 110 | 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js' |
| 111 | ); |
| 112 | |
| 113 | /** |
| 114 | * Allows to override the page-level code. |
| 115 | * |
| 116 | * The Pro add-on uses this filter to inject a drop-in replacement for the page-level header code. |
| 117 | * |
| 118 | * @param string $code Existing page level code. |
| 119 | * @param array $parameters { |
| 120 | * Parameters of the AdSense code. |
| 121 | * |
| 122 | * @type string $client_id The Google AdSense client ID. |
| 123 | * @type bool $top_anchor AdSense anchor ad on top of pages. |
| 124 | * @type string $top_anchor_code The code for top anchor ads. |
| 125 | * @type string $script_src AdSense script url. |
| 126 | * } |
| 127 | */ |
| 128 | $custom_code = apply_filters( |
| 129 | 'advanced-ads-gadsense-page-level-code', |
| 130 | '', |
| 131 | compact( [ 'client_id', 'top_anchor', 'top_anchor_code', 'script_src' ] ) |
| 132 | ); |
| 133 | |
| 134 | if ( $custom_code ) { |
| 135 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- the snippet has already been escaped. |
| 136 | echo $custom_code; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // inject page-level header code. |
| 141 | include GADSENSE_BASE_PATH . 'public/templates/page-level.php'; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Handle AdSense AMP code |
| 147 | */ |
| 148 | public function inject_amp_code() { |
| 149 | // Early bail!! |
| 150 | if ( wp_doing_ajax() ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // for non-AMP pages we do this on the `template_redirect` hook, this has not fired yet. |
| 155 | wp_advads()->frontend->run_checks(); |
| 156 | |
| 157 | if ( |
| 158 | Conditional::is_ad_disabled() |
| 159 | // check if this an AMP page, we're inside `wp` action so it's safe to use. |
| 160 | || ! Conditional::is_amp() |
| 161 | ) { |
| 162 | return; |
| 163 | } |
| 164 | // The is_amp_endpoint function is used for multiple plugins. |
| 165 | if ( function_exists( 'is_amp_endpoint' ) ) { |
| 166 | $adsense_data = Advanced_Ads_AdSense_Data::get_instance(); |
| 167 | $adsense_options = $adsense_data->get_options(); |
| 168 | |
| 169 | // AMP Auto ads was removed from Responsive add-on version 1.10.0. |
| 170 | if ( |
| 171 | ( defined( 'AAR_VERSION' ) && 1 === version_compare( '1.10.0', AAR_VERSION ) ) || |
| 172 | empty( $adsense_options['amp']['auto_ads_enabled'] ) |
| 173 | ) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // Adds the AdSense Auto ads AMP code to the page (head) in "Reader" mode. |
| 178 | add_action( 'amp_post_template_data', [ $this, 'add_auto_ads_amp_head_script' ] ); |
| 179 | |
| 180 | // SmartMag theme (http://theme-sphere.com/smart-mag/documentation/). |
| 181 | add_action( 'bunyad_amp_pre_main', [ $this, 'add_auto_ads_amp_body_script' ] ); |
| 182 | |
| 183 | /** |
| 184 | * Add AMP Auto ads body code to footer for `AMP` plugin ( https://wordpress.org/plugins/amp/ ) |
| 185 | * |
| 186 | * Adds the Auto ads `body` tag to `wp_footer` because there is no WordPress right hook after `body` |
| 187 | * The AdSense Auto ads code is added automatically to the `head` section using the amp_post_template_data hook above. |
| 188 | * |
| 189 | * use `wp_footer` in Transition and Standard mode |
| 190 | * use `amp_post_template_footer` in Reader mode |
| 191 | */ |
| 192 | add_action( 'wp_footer', [ $this, 'add_auto_ads_amp_body_script' ] ); |
| 193 | add_action( 'amp_post_template_footer', [ $this, 'add_auto_ads_amp_body_script' ] ); |
| 194 | |
| 195 | // Other AMP plugins. |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Add AdSense AMP Auto ads code to the header. |
| 201 | * |
| 202 | * @param array $data AMP components. |
| 203 | */ |
| 204 | public function add_auto_ads_amp_head_script( $data ) { |
| 205 | $data['amp_component_scripts']['amp-auto-ads'] = 'https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js'; |
| 206 | |
| 207 | return $data; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Add Adsense Auto Ads body script. |
| 212 | */ |
| 213 | public function add_auto_ads_amp_body_script() { |
| 214 | $pub_id = $this->data->get_adsense_id(); |
| 215 | if ( $pub_id ) { |
| 216 | printf( '<amp-auto-ads type="adsense" data-ad-client="ca-%s"></amp-auto-ads>', esc_attr( $pub_id ) ); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 |