PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
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 / includes / class-ad-type-adsense.php
advanced-ads / modules / gadsense / includes Last commit date
types 1 year ago adsense-report-api.php 1 year ago class-ad-type-adsense.php 3 months ago class-adsense-report-data.php 1 year ago class-adsense-report.php 1 year ago class-gadsense-data.php 1 year ago class-mapi.php 1 year ago class-network-adsense.php 1 year ago
class-ad-type-adsense.php
395 lines
1 <?php // phpcs:ignore WordPress.Files.FileName
2 /**
3 * AdSense Ad Type
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 */
8
9 use AdvancedAds\Abstracts\Ad;
10 use AdvancedAds\Utilities\Conditional;
11 use AdvancedAds\Interfaces\Ad_Interface;
12
13 /**
14 * Adsense ad type
15 *
16 * phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
17 */
18 class Advanced_Ads_Ad_Type_Adsense extends Ad implements Ad_Interface {
19
20 /**
21 * Return an array with AdSense ad type keys and readable labels
22 *
23 * @return array
24 */
25 public static function get_ad_types() {
26 return [
27 'normal' => __( 'Normal', 'advanced-ads' ),
28 'responsive' => __( 'Responsive', 'advanced-ads' ),
29 'matched-content' => __( 'Multiplex', 'advanced-ads' ),
30 'link' => __( 'Link ads', 'advanced-ads' ),
31 'link-responsive' => __( 'Link ads (Responsive)', 'advanced-ads' ),
32 'in-article' => __( 'In-article', 'advanced-ads' ),
33 'in-feed' => __( 'In-feed', 'advanced-ads' ),
34 ];
35 }
36
37 /**
38 * Get readable names for each AdSense ad type
39 *
40 * @param string $ad_type ad type key.
41 * @return string
42 */
43 public static function get_ad_type_label( $ad_type ) {
44 $ad_types = self::get_ad_types();
45 return $ad_types[ $ad_type ] ?? __( 'Normal', 'advanced-ads' );
46 }
47
48 /**
49 * Output for the ad parameters metabox
50 * this will be loaded using ajax when changing the ad type radio buttons
51 * echo the output right away here
52 * name parameters must be in the "advanced_ads" array
53 *
54 * @param object $ad ad object.
55 *
56 * @since 1.4
57 */
58 public function render_parameters( $ad ) {
59 // TODO: THIS IS JUST A QUICK AND DIRTY HACK. Create a dedicated method to handle this properly.
60 ?>
61 <script>
62 jQuery( function () {
63 <?php
64 $mapi_options = Advanced_Ads_AdSense_MAPI::get_option();
65 $json_ad_codes = wp_json_encode( $mapi_options['ad_codes'] );
66 ?>
67 const adsense = new AdvancedAdsNetworkAdsense(<?php echo $json_ad_codes; // phpcs:ignore ?>)
68 AdvancedAdsAdmin.AdImporter.setup( adsense )
69 } )
70 </script>
71 <?php
72 $content = (string) $ad->get_content();
73 $unit_id = '';
74 $unit_pubid = '';
75 $unit_code = '';
76 $unit_type = 'responsive';
77 $unit_width = 0;
78 $unit_height = 0;
79 $json_content = '';
80 $unit_resize = '';
81 $extra_params = [
82 'default_width' => '',
83 'default_height' => '',
84 'at_media' => [],
85 ];
86
87 $db = Advanced_Ads_AdSense_Data::get_instance();
88 $pub_id = trim( $db->get_adsense_id( $ad ) );
89
90 // check pub_id for errors.
91 $pub_id_errors = false;
92 if ( '' !== $pub_id && 0 !== strpos( $pub_id, 'pub-' ) ) {
93 $pub_id_errors = __( 'The Publisher ID has an incorrect format. (must start with "pub-")', 'advanced-ads' );
94 }
95
96 global $external_ad_unit_id, $use_dashicons, $closeable;
97 $closeable = true;
98 $use_dashicons = false;
99 $external_ad_unit_id = '';
100 if ( trim( $content ) !== '' ) {
101
102 $json_content = stripslashes( $content );
103
104 // get json content striped by slashes.
105 $content = json_decode( stripslashes( $content ) );
106
107 if ( isset( $content->unitType ) ) {
108 $content->json = $json_content;
109 $unit_type = $content->unitType;
110 $unit_code = $content->slotId;
111 $unit_pubid = ! empty( $content->pubId ) ? $content->pubId : $pub_id;
112 $layout = $content->layout ?? '';
113 $layout_key = $content->layout_key ?? '';
114
115 if ( 'responsive' !== $content->unitType && 'link-responsive' !== $content->unitType && 'matched-content' !== $content->unitType ) {
116 // Normal ad unit.
117 $unit_width = $ad->get_width();
118 $unit_height = $ad->get_height();
119 } else {
120 // Responsive && multiplex ads.
121 $unit_resize = $content->resize ?? 'auto';
122 if ( 'auto' !== $unit_resize ) {
123 $extra_params = apply_filters( 'advanced-ads-gadsense-ad-param-data', $extra_params, $content, $ad );
124 }
125 }
126 if ( ! empty( $unit_pubid ) ) {
127 $unit_id = 'ca-' . $unit_pubid . ':' . $unit_code;
128 }
129 $external_ad_unit_id = $unit_id;
130 }
131 }
132
133 if ( '' === trim( $pub_id ) && '' !== trim( $unit_code ) ) {
134 $pub_id_errors = __( 'Your AdSense Publisher ID is missing.', 'advanced-ads' );
135 }
136
137 $default_template = GADSENSE_BASE_PATH . 'admin/views/adsense-ad-parameters.php';
138 /**
139 * Inclusion of other UI template is done here. The content is passed in order to allow the inclusion of different
140 * templates file, depending of the ad. It's up to the developer to verify that $content is not an empty
141 * variable (which is the case for a new ad).
142 *
143 * Inclusion of .js and .css files for the ad creation/editon page are done by another hook. See
144 * 'advanced-ads-gadsense-ad-param-script' and 'advanced-ads-gadsense-ad-param-style' in "../admin/class-gadsense-admin.php".
145 */
146 $template = apply_filters( 'advanced-ads-gadsense-ad-param-template', $default_template, $content );
147
148 require $template;
149 }
150
151 /**
152 * Render icon on the ad overview list
153 *
154 * @param Ad $ad Ad instance.
155 */
156 public function render_icon( Ad $ad ) {
157 $image = 'adsense-display.svg';
158
159 $content = json_decode( wp_unslash( $ad->get_content() ), true );
160 if ( isset( $content['unitType'] ) ) {
161 switch ( $content['unitType'] ) {
162 case 'matched-content':
163 $image = 'adsense-multiplex.svg';
164 break;
165 case 'in-article':
166 $image = 'adsense-in-article.svg';
167 break;
168 case 'in-feed':
169 $image = 'adsense-in-feed.svg';
170 break;
171 }
172 }
173
174 echo '<img src="' . esc_url( ADVADS_BASE_URL ) . '/modules/gadsense/admin/assets/img/' . esc_attr( $image ) . '" width="50" height="50">';
175 }
176
177 /**
178 * Render additional information in the ad type tooltip on the ad overview page
179 *
180 * @param Ad $ad Ad instance.
181 */
182 public function render_ad_type_tooltip( Ad $ad ) {
183 $content = json_decode( stripslashes( $ad->get_content() ), true );
184 if ( isset( $content['unitType'] ) ) {
185 echo esc_html( self::get_ad_type_label( $content['unitType'] ) );
186 }
187 }
188
189 /**
190 * Sanitize content field on save
191 *
192 * @param string $content ad content.
193 *
194 * @return string $content sanitized ad content
195 * @since 1.0.0
196 */
197 public function sanitize_content( $content = '' ) {
198 $content = wp_unslash( $content );
199 $ad_unit = json_decode( $content, true );
200 if ( empty( $ad_unit ) ) {
201 $ad_unit = [];
202 }
203
204 // Remove this slotId from unsupported_ads.
205 $mapi_options = Advanced_Ads_AdSense_MAPI::get_option();
206 if ( array_key_exists( 'slotId', $ad_unit ) && array_key_exists( $ad_unit['slotId'], $mapi_options['unsupported_units'] ) ) {
207 unset( $mapi_options['unsupported_units'][ $ad_unit['slotId'] ] );
208 update_option( Advanced_Ads_AdSense_MAPI::OPTION_KEY, $mapi_options );
209 }
210
211 return $content;
212 }
213
214 /**
215 * Prepare output for frontend.
216 *
217 * @return string
218 */
219 public function prepare_frontend_output(): string {
220 global $gadsense;
221
222 $ad_args = $this->get_prop( 'ad_args' );
223 $content = json_decode( stripslashes( $this->get_content() ) );
224
225 if (
226 isset( $ad_args['wp_the_query']['is_404'] ) &&
227 $ad_args['wp_the_query']['is_404'] &&
228 ! defined( 'ADVADS_ALLOW_ADSENSE_ON_404' )
229 ) {
230 return '';
231 }
232
233 $output = '';
234 $db = Advanced_Ads_AdSense_Data::get_instance();
235 $pub_id = $db->get_adsense_id( $this );
236
237 if ( ! isset( $content->unitType ) || empty( $pub_id ) ) {
238 return $output;
239 }
240
241 // deprecated since the adsbygoogle.js file is now always loaded.
242 if ( ! isset( $gadsense['google_loaded'] ) || ! $gadsense['google_loaded'] ) {
243 $gadsense['google_loaded'] = true;
244 }
245
246 // check if passive cb is used.
247 if ( isset( $gadsense['adsense_count'] ) ) {
248 ++$gadsense['adsense_count'];
249 } else {
250 $gadsense['adsense_count'] = 1;
251 }
252
253 // "link" was a static format until AdSense stopped filling them in March 2021. Their responsive format serves as a fallback recommended by AdSense
254 // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
255 $is_static_normal_content = ! in_array( $content->unitType, [ 'responsive', 'link', 'link-responsive', 'matched-content', 'in-article', 'in-feed' ], true );
256
257 $output = apply_filters( 'advanced-ads-gadsense-output', false, $this, $pub_id, $content );
258 if ( false !== $output ) {
259 return $output;
260 }
261
262 // Prevent output on AMP pages.
263 if ( Conditional::is_amp() ) {
264 return '';
265 }
266
267 $output = '';
268
269 // Add notice when a link unit is used.
270 if ( in_array( $content->unitType, [ 'link', 'link-responsive' ], true ) ) {
271 Advanced_Ads_Ad_Health_Notices::get_instance()->add( 'adsense_link_units_deprecated' );
272 }
273
274 // build static normal content ads first.
275 if ( $is_static_normal_content ) {
276 $output .= $this->get_script_tag( $pub_id );
277 $output .= '<ins class="adsbygoogle" ';
278 $output .= 'style="display:inline-block;width:' . $this->get_width() . 'px;height:' . $this->get_height() . 'px;" ' . "\n";
279 $output .= 'data-ad-client="ca-' . $pub_id . '" ' . "\n";
280 $output .= 'data-ad-slot="' . $content->slotId . '"';
281 // ad type for static link unit.
282 if ( 'link' === $content->unitType ) {
283 $output .= "\n" . 'data-ad-format="link"';
284 }
285 $output .= '></ins> ' . "\n";
286 $output .= '<script> ' . "\n";
287 $output .= '(adsbygoogle = window.adsbygoogle || []).push({}); ' . "\n";
288 $output .= '</script>' . "\n";
289 } else {
290 /**
291 * The value of $ad->content->resize should be tested to format the output correctly
292 */
293 $unmodified = $output;
294 $output = apply_filters( 'advanced-ads-gadsense-responsive-output', $output, $this, $pub_id );
295 if ( $unmodified === $output ) {
296 /**
297 * If the output has not been modified, perform a default responsive output.
298 * A simple did_action check isn't sufficient, some hooks may be attached and fired but didn't touch the output
299 */
300 $this->append_defaut_responsive_content( $output, $pub_id, $content );
301
302 // Remove float setting if this is a responsive ad unit without custom sizes.
303 unset( $this->wrapper['style']['float'] );
304 }
305 }
306
307 return $output;
308 }
309
310 /**
311 * Check if a string looks like an AdSense ad code.
312 *
313 * @param string $content The string that need to be checked.
314 *
315 * @return boolean
316 */
317 public static function content_is_adsense( $content = '' ) {
318 return false !== stripos( $content, 'googlesyndication.com' ) &&
319 ( false !== stripos( $content, 'google_ad_client' ) || false !== stripos( $content, 'data-ad-client' ) );
320 }
321
322 /**
323 * Build AdSense script tag.
324 *
325 * @param string $pub_id AdSense publisher ID.
326 *
327 * @return string
328 */
329 protected function get_script_tag( $pub_id ) {
330 return sprintf(
331 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- don't allow any changes on Google AdSense code.
332 '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js%s" crossorigin="anonymous"></script>',
333 /**
334 * Filter the output of the publisher ID appended to the AdSense JavaScript Code.
335 *
336 * @param boolean
337 */
338 apply_filters( 'advanced-ads-adsense-publisher-id', true ) ? '?client=ca-' . $pub_id : ''
339 );
340 }
341
342 /**
343 * Append responsive content
344 *
345 * @param string $output Current ad unit code.
346 * @param string $pub_id AdSense publisher ID.
347 * @param object $content Ad unit content with all parameters.
348 */
349 protected function append_defaut_responsive_content( &$output, $pub_id, $content ) {
350 $format = '';
351 $style = 'display:block;';
352 switch ( $content->unitType ) {
353 case 'matched-content':
354 $format = 'autorelaxed';
355 break;
356 case 'link-responsive':
357 case 'link':
358 $format = 'link';
359 break;
360 case 'in-feed':
361 $format = 'fluid';
362 $layout_key = $content->layout_key;
363 break;
364 case 'in-article':
365 $format = 'fluid';
366 $layout = 'in-article';
367 $style = 'display:block; text-align:center;';
368 break;
369 default:
370 $format = 'auto';
371 }
372
373 $output .= $this->get_script_tag( $pub_id );
374 $output .= '<ins class="adsbygoogle" ';
375 $output .= 'style="' . $style . '" ';
376 $output .= 'data-ad-client="ca-' . $pub_id . '" ' . "\n";
377 $output .= 'data-ad-slot="' . $content->slotId . '" ' . "\n";
378 $output .= isset( $layout ) ? 'data-ad-layout="' . $layout . '"' . "\n" : '';
379 $output .= isset( $layout_key ) ? 'data-ad-layout-key="' . $layout_key . '"' . "\n" : '';
380 $output .= 'data-ad-format="';
381 $output .= $format;
382
383 $options = Advanced_Ads_AdSense_Data::get_instance()->get_options();
384 $fw = ! empty( $options['fullwidth-ads'] ) ? $options['fullwidth-ads'] : 'default';
385 if ( 'default' !== $fw ) {
386 $output .= 'enable' === $fw ? '" data-full-width-responsive="true' : '" data-full-width-responsive="false';
387 }
388
389 $output .= '"></ins>' . "\n";
390 $output .= '<script> ' . "\n";
391 $output .= apply_filters( 'advanced-ads-gadsense-responsive-adsbygoogle', '(adsbygoogle = window.adsbygoogle || []).push({}); ' . "\n" );
392 $output .= '</script>' . "\n";
393 }
394 }
395