PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.26
Advanced Ads – Ad Manager & AdSense v2.0.26
2.0.26 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 4 weeks ago adsense-report-api.php 4 weeks ago class-ad-type-adsense.php 4 weeks ago class-adsense-report-data.php 4 weeks ago class-adsense-report.php 4 weeks ago class-gadsense-data.php 4 weeks ago class-mapi.php 4 weeks ago class-network-adsense.php 4 weeks ago
class-ad-type-adsense.php
394 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\Interfaces\Ad_Interface;
11 use AdvancedAds\Utilities\Conditional;
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<string, string>
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 $json_content = stripslashes( $content );
102
103 // get json content striped by slashes.
104 $content = json_decode( stripslashes( $content ) );
105
106 if ( isset( $content->unitType ) ) {
107 $content->json = $json_content;
108 $unit_type = $content->unitType;
109 $unit_code = $content->slotId;
110 $unit_pubid = ! empty( $content->pubId ) ? $content->pubId : $pub_id;
111 $layout = $content->layout ?? '';
112 $layout_key = $content->layout_key ?? '';
113
114 if ( 'responsive' !== $content->unitType && 'link-responsive' !== $content->unitType && 'matched-content' !== $content->unitType ) {
115 // Normal ad unit.
116 $unit_width = $ad->get_width();
117 $unit_height = $ad->get_height();
118 } else {
119 // Responsive && multiplex ads.
120 $unit_resize = $content->resize ?? 'auto';
121 if ( 'auto' !== $unit_resize ) {
122 $extra_params = apply_filters( 'advanced-ads-gadsense-ad-param-data', $extra_params, $content, $ad );
123 }
124 }
125 if ( ! empty( $unit_pubid ) ) {
126 $unit_id = 'ca-' . $unit_pubid . ':' . $unit_code;
127 }
128 $external_ad_unit_id = $unit_id;
129 }
130 }
131
132 if ( '' === trim( $pub_id ) && '' !== trim( $unit_code ) ) {
133 $pub_id_errors = __( 'Your AdSense Publisher ID is missing.', 'advanced-ads' );
134 }
135
136 $default_template = GADSENSE_BASE_PATH . 'admin/views/adsense-ad-parameters.php';
137 /**
138 * Inclusion of other UI template is done here. The content is passed in order to allow the inclusion of different
139 * templates file, depending of the ad. It's up to the developer to verify that $content is not an empty
140 * variable (which is the case for a new ad).
141 *
142 * Inclusion of .js and .css files for the ad creation/editon page are done by another hook. See
143 * 'advanced-ads-gadsense-ad-param-script' and 'advanced-ads-gadsense-ad-param-style' in "../admin/class-gadsense-admin.php".
144 */
145 $template = apply_filters( 'advanced-ads-gadsense-ad-param-template', $default_template, $content );
146
147 require $template;
148 }
149
150 /**
151 * Render icon on the ad overview list
152 *
153 * @param Ad $ad Ad instance.
154 */
155 public function render_icon( Ad $ad ) {
156 $image = 'adsense-display.svg';
157
158 $content = json_decode( wp_unslash( $ad->get_content() ), true );
159 if ( isset( $content['unitType'] ) ) {
160 switch ( $content['unitType'] ) {
161 case 'matched-content':
162 $image = 'adsense-multiplex.svg';
163 break;
164 case 'in-article':
165 $image = 'adsense-in-article.svg';
166 break;
167 case 'in-feed':
168 $image = 'adsense-in-feed.svg';
169 break;
170 }
171 }
172
173 echo '<img src="' . esc_url( ADVADS_BASE_URL ) . '/modules/gadsense/admin/assets/img/' . esc_attr( $image ) . '" width="50" height="50">';
174 }
175
176 /**
177 * Render additional information in the ad type tooltip on the ad overview page
178 *
179 * @param Ad $ad Ad instance.
180 */
181 public function render_ad_type_tooltip( Ad $ad ) {
182 $content = json_decode( stripslashes( $ad->get_content() ), true );
183 if ( isset( $content['unitType'] ) ) {
184 echo esc_html( self::get_ad_type_label( $content['unitType'] ) );
185 }
186 }
187
188 /**
189 * Sanitize content field on save
190 *
191 * @param string $content ad content.
192 *
193 * @return string $content sanitized ad content
194 * @since 1.0.0
195 */
196 public function sanitize_content( $content = '' ) {
197 $content = wp_unslash( $content );
198 $ad_unit = json_decode( $content, true );
199 if ( empty( $ad_unit ) ) {
200 $ad_unit = [];
201 }
202
203 // Remove this slotId from unsupported_ads.
204 $mapi_options = Advanced_Ads_AdSense_MAPI::get_option();
205 if ( array_key_exists( 'slotId', $ad_unit ) && array_key_exists( $ad_unit['slotId'], $mapi_options['unsupported_units'] ) ) {
206 unset( $mapi_options['unsupported_units'][ $ad_unit['slotId'] ] );
207 update_option( Advanced_Ads_AdSense_MAPI::OPTION_KEY, $mapi_options );
208 }
209
210 return $content;
211 }
212
213 /**
214 * Prepare output for frontend.
215 *
216 * @return string
217 */
218 public function prepare_frontend_output(): string {
219 global $gadsense;
220
221 $ad_args = $this->get_prop( 'ad_args' );
222 $content = json_decode( stripslashes( $this->get_content() ) );
223
224 if (
225 isset( $ad_args['wp_the_query']['is_404'] ) &&
226 $ad_args['wp_the_query']['is_404'] &&
227 ! defined( 'ADVADS_ALLOW_ADSENSE_ON_404' )
228 ) {
229 return '';
230 }
231
232 $output = '';
233 $db = Advanced_Ads_AdSense_Data::get_instance();
234 $pub_id = $db->get_adsense_id( $this );
235
236 if ( ! isset( $content->unitType ) || empty( $pub_id ) ) {
237 return $output;
238 }
239
240 // deprecated since the adsbygoogle.js file is now always loaded.
241 if ( ! isset( $gadsense['google_loaded'] ) || ! $gadsense['google_loaded'] ) {
242 $gadsense['google_loaded'] = true;
243 }
244
245 // check if passive cb is used.
246 if ( isset( $gadsense['adsense_count'] ) ) {
247 ++$gadsense['adsense_count'];
248 } else {
249 $gadsense['adsense_count'] = 1;
250 }
251
252 // "link" was a static format until AdSense stopped filling them in March 2021. Their responsive format serves as a fallback recommended by AdSense
253 // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
254 $is_static_normal_content = ! in_array( $content->unitType, [ 'responsive', 'link', 'link-responsive', 'matched-content', 'in-article', 'in-feed' ], true );
255
256 $output = apply_filters( 'advanced-ads-gadsense-output', false, $this, $pub_id, $content );
257 if ( false !== $output ) {
258 return $output;
259 }
260
261 // Prevent output on AMP pages.
262 if ( Conditional::is_amp() ) {
263 return '';
264 }
265
266 $output = '';
267
268 // Add notice when a link unit is used.
269 if ( in_array( $content->unitType, [ 'link', 'link-responsive' ], true ) ) {
270 Advanced_Ads_Ad_Health_Notices::get_instance()->add( 'adsense_link_units_deprecated' );
271 }
272
273 // build static normal content ads first.
274 if ( $is_static_normal_content ) {
275 $output .= $this->get_script_tag( $pub_id );
276 $output .= '<ins class="adsbygoogle" ';
277 $output .= 'style="display:inline-block;width:' . $this->get_width() . 'px;height:' . $this->get_height() . 'px;" ' . "\n";
278 $output .= 'data-ad-client="ca-' . $pub_id . '" ' . "\n";
279 $output .= 'data-ad-slot="' . $content->slotId . '"';
280 // ad type for static link unit.
281 if ( 'link' === $content->unitType ) {
282 $output .= "\n" . 'data-ad-format="link"';
283 }
284 $output .= '></ins> ' . "\n";
285 $output .= '<script> ' . "\n";
286 $output .= '(adsbygoogle = window.adsbygoogle || []).push({}); ' . "\n";
287 $output .= '</script>' . "\n";
288 } else {
289 /**
290 * The value of $ad->content->resize should be tested to format the output correctly
291 */
292 $unmodified = $output;
293 $output = apply_filters( 'advanced-ads-gadsense-responsive-output', $output, $this, $pub_id );
294 if ( $unmodified === $output ) {
295 /**
296 * If the output has not been modified, perform a default responsive output.
297 * A simple did_action check isn't sufficient, some hooks may be attached and fired but didn't touch the output
298 */
299 $this->append_defaut_responsive_content( $output, $pub_id, $content );
300
301 // Remove float setting if this is a responsive ad unit without custom sizes.
302 unset( $this->wrapper['style']['float'] );
303 }
304 }
305
306 return $output;
307 }
308
309 /**
310 * Check if a string looks like an AdSense ad code.
311 *
312 * @param string $content The string that need to be checked.
313 *
314 * @return boolean
315 */
316 public static function content_is_adsense( $content = '' ) {
317 return false !== stripos( $content, 'googlesyndication.com' ) &&
318 ( false !== stripos( $content, 'google_ad_client' ) || false !== stripos( $content, 'data-ad-client' ) );
319 }
320
321 /**
322 * Build AdSense script tag.
323 *
324 * @param string $pub_id AdSense publisher ID.
325 *
326 * @return string
327 */
328 protected function get_script_tag( $pub_id ) {
329 return sprintf(
330 // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript -- don't allow any changes on Google AdSense code.
331 '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js%s" crossorigin="anonymous"></script>',
332 /**
333 * Filter the output of the publisher ID appended to the AdSense JavaScript Code.
334 *
335 * @param boolean
336 */
337 apply_filters( 'advanced-ads-adsense-publisher-id', true ) ? '?client=ca-' . $pub_id : ''
338 );
339 }
340
341 /**
342 * Append responsive content
343 *
344 * @param string $output Current ad unit code.
345 * @param string $pub_id AdSense publisher ID.
346 * @param object $content Ad unit content with all parameters.
347 */
348 protected function append_defaut_responsive_content( &$output, $pub_id, $content ) {
349 $format = '';
350 $style = 'display:block;';
351 switch ( $content->unitType ) {
352 case 'matched-content':
353 $format = 'autorelaxed';
354 break;
355 case 'link-responsive':
356 case 'link':
357 $format = 'link';
358 break;
359 case 'in-feed':
360 $format = 'fluid';
361 $layout_key = $content->layout_key;
362 break;
363 case 'in-article':
364 $format = 'fluid';
365 $layout = 'in-article';
366 $style = 'display:block; text-align:center;';
367 break;
368 default:
369 $format = 'auto';
370 }
371
372 $output .= $this->get_script_tag( $pub_id );
373 $output .= '<ins class="adsbygoogle" ';
374 $output .= 'style="' . $style . '" ';
375 $output .= 'data-ad-client="ca-' . $pub_id . '" ' . "\n";
376 $output .= 'data-ad-slot="' . $content->slotId . '" ' . "\n";
377 $output .= isset( $layout ) ? 'data-ad-layout="' . $layout . '"' . "\n" : '';
378 $output .= isset( $layout_key ) ? 'data-ad-layout-key="' . $layout_key . '"' . "\n" : '';
379 $output .= 'data-ad-format="';
380 $output .= $format;
381
382 $options = Advanced_Ads_AdSense_Data::get_instance()->get_options();
383 $fw = ! empty( $options['fullwidth-ads'] ) ? $options['fullwidth-ads'] : 'default';
384 if ( 'default' !== $fw ) {
385 $output .= 'enable' === $fw ? '" data-full-width-responsive="true' : '" data-full-width-responsive="false';
386 }
387
388 $output .= '"></ins>' . "\n";
389 $output .= '<script> ' . "\n";
390 $output .= apply_filters( 'advanced-ads-gadsense-responsive-adsbygoogle', '(adsbygoogle = window.adsbygoogle || []).push({}); ' . "\n" );
391 $output .= '</script>' . "\n";
392 }
393 }
394