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 |