PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.24
Advanced Ads – Ad Manager & AdSense v2.0.24
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / modules / gadsense / public / public.php
advanced-ads / modules / gadsense / public Last commit date
templates 4 years ago public.php 1 month ago
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