EDD_SL_Plugin_Updater.php
7 years ago
ad-ajax.php
8 years ago
ad-debug.php
8 years ago
ad-model.php
8 years ago
ad-select.php
9 years ago
ad.php
7 years ago
ad_ajax_callbacks.php
7 years ago
ad_group.php
7 years ago
ad_placements.php
7 years ago
ad_type_abstract.php
8 years ago
ad_type_content.php
8 years ago
ad_type_dummy.php
8 years ago
ad_type_group.php
8 years ago
ad_type_image.php
7 years ago
ad_type_plain.php
7 years ago
checks.php
7 years ago
compatibility.php
7 years ago
display-conditions.php
7 years ago
filesystem.php
8 years ago
frontend_checks.php
7 years ago
plugin.php
7 years ago
upgrades.php
9 years ago
utils.php
7 years ago
visitor-conditions.php
7 years ago
widget.php
7 years ago
frontend_checks.php
724 lines
| 1 | <?php |
| 2 | |
| 3 | class Advanced_Ads_Frontend_Checks { |
| 4 | /** |
| 5 | * True if 'the_content' was invoked, false otherwise. |
| 6 | * |
| 7 | * @var bool |
| 8 | */ |
| 9 | private $did_the_content = false; |
| 10 | |
| 11 | public function __construct() { |
| 12 | add_action( 'init', array( $this, 'init' ) ); |
| 13 | } |
| 14 | |
| 15 | public function init() { |
| 16 | $enabled = false; |
| 17 | |
| 18 | if ( ! is_admin() |
| 19 | && is_admin_bar_showing() |
| 20 | && current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ) ) |
| 21 | ) { |
| 22 | add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 1000 ); |
| 23 | add_filter( 'the_content', array( $this, 'set_did_the_content' ) ); |
| 24 | add_filter( 'wp_footer', array( $this, 'footer_checks' ), -101 ); |
| 25 | add_filter( 'advanced-ads-ad-select-args', array( $this, 'ad_select_args_callback' ) ); |
| 26 | $enabled = true; |
| 27 | } |
| 28 | |
| 29 | if ( $enabled || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 30 | add_filter( 'advanced-ads-ad-output', array( $this, 'after_ad_output' ), 10, 2 ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Notify ads loaded with AJAX. |
| 36 | * |
| 37 | * @param array $args |
| 38 | * @return array $args |
| 39 | */ |
| 40 | public function ad_select_args_callback( $args ) { |
| 41 | $args['frontend-check'] = true; |
| 42 | return $args; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * List current ad situation on the page in the admin-bar. |
| 47 | * |
| 48 | * @param obj $wp_admin_bar WP_Admin_Bar |
| 49 | */ |
| 50 | public function add_admin_bar_menu( $wp_admin_bar ) { |
| 51 | global $wp_the_query, $post, $wp_scripts; |
| 52 | |
| 53 | $options = Advanced_Ads_Plugin::get_instance()->options(); |
| 54 | |
| 55 | // load AdSense related options |
| 56 | $adsense_options = Advanced_Ads_AdSense_Data::get_instance()->get_options(); |
| 57 | |
| 58 | // check if jQuery is loaded in the header |
| 59 | // Hidden, will be shown using js. |
| 60 | // message removed after we fixed all issues we know of |
| 61 | /*$wp_admin_bar->add_node( array( |
| 62 | 'parent' => 'advanced_ads_ad_health', |
| 63 | 'id' => 'advanced_ads_ad_health_jquery', |
| 64 | 'title' => __( 'jQuery not in header', 'advanced-ads' ), |
| 65 | 'href' => ADVADS_URL . 'manual/common-issues#frontend-issues-javascript', |
| 66 | 'meta' => array( |
| 67 | 'class' => 'hidden advanced_ads_ad_health_warning', |
| 68 | 'target' => '_blank' |
| 69 | ) |
| 70 | ) );*/ |
| 71 | |
| 72 | // check if AdSense loads Auto Ads ads |
| 73 | // Hidden, will be shown using js. |
| 74 | if( ! isset( $adsense_options['violation-warnings-disable'] ) ) { |
| 75 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 76 | 'parent' => 'advanced_ads_ad_health', |
| 77 | 'id' => 'advanced_ads_autoads_displayed', |
| 78 | 'title' => __( 'Random AdSense ads', 'advanced-ads' ), |
| 79 | 'href' => ADVADS_URL . 'adsense-in-random-positions-auto-ads/#utm_source=advancedads&utm_medium=link&utm_campaign=frontend-autoads-ads', |
| 80 | 'meta' => array( |
| 81 | 'class' => 'hidden advanced_ads_ad_health_warning', |
| 82 | 'target' => '_blank' |
| 83 | ) |
| 84 | ) ); |
| 85 | } |
| 86 | |
| 87 | // check if current user was identified as a bot |
| 88 | if( Advanced_Ads::get_instance()->is_bot() ) { |
| 89 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 90 | 'parent' => 'advanced_ads_ad_health', |
| 91 | 'id' => 'advanced_ads_user_is_bot', |
| 92 | 'title' => __( 'You look like a bot', 'advanced-ads' ), |
| 93 | 'href' => ADVADS_URL . 'manual/ad-health/#look-like-bot', |
| 94 | 'meta' => array( |
| 95 | 'class' => 'advanced_ads_ad_health_warning', |
| 96 | 'target' => '_blank' |
| 97 | ) |
| 98 | ) ); |
| 99 | } |
| 100 | |
| 101 | // check if an ad blocker is enabled |
| 102 | // Hidden, will be shown using js. |
| 103 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 104 | 'parent' => 'advanced_ads_ad_health', |
| 105 | 'id' => 'advanced_ads_ad_health_adblocker_enabled', |
| 106 | 'title' => __( 'Ad blocker enabled', 'advanced-ads' ), |
| 107 | // 'href' => 'https://wpadvancedads.com/support', |
| 108 | 'meta' => array( |
| 109 | 'class' => 'hidden advanced_ads_ad_health_warning', |
| 110 | 'target' => '_blank' |
| 111 | ) |
| 112 | ) ); |
| 113 | |
| 114 | if ( $wp_the_query->is_singular() ) { |
| 115 | if ( ! $this->did_the_content ) { |
| 116 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 117 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 118 | // Find a placement that depends on 'the_content' filter. |
| 119 | foreach ( $placements as $placement ) { |
| 120 | if ( isset ( $placement['type'] ) |
| 121 | && ! empty( $placement_types[ $placement['type'] ]['options']['uses_the_content'] ) ) { |
| 122 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 123 | 'parent' => 'advanced_ads_ad_health', |
| 124 | 'id' => 'advanced_ads_ad_health_the_content_not_invoked', |
| 125 | 'title' => sprintf( __( '<em>%s</em> filter does not exist', 'advanced-ads' ), 'the_content' ), |
| 126 | 'href' => ADVADS_URL . 'manual/ads-not-showing-up/#the_content-filter-missing', |
| 127 | 'meta' => array( |
| 128 | 'class' => 'advanced_ads_ad_health_warning', |
| 129 | 'target' => '_blank' |
| 130 | ) |
| 131 | ) ); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if ( ! empty( $post->ID ) ) { |
| 138 | $ad_settings = get_post_meta( $post->ID, '_advads_ad_settings', true ); |
| 139 | |
| 140 | if ( ! empty( $ad_settings['disable_ads'] ) ) { |
| 141 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 142 | 'parent' => 'advanced_ads_ad_health', |
| 143 | 'id' => 'advanced_ads_ad_health_disabled_on_page', |
| 144 | 'title' => __( 'Ads are disabled on this page', 'advanced-ads' ), |
| 145 | 'href' => get_edit_post_link( $post->ID ) . '#advads-ad-settings', |
| 146 | 'meta' => array( |
| 147 | 'class' => 'advanced_ads_ad_health_warning', |
| 148 | 'target' => '_blank' |
| 149 | ) |
| 150 | ) ); |
| 151 | } |
| 152 | |
| 153 | if ( ! empty( $ad_settings['disable_the_content'] ) ) { |
| 154 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 155 | 'parent' => 'advanced_ads_ad_health', |
| 156 | 'id' => 'advanced_ads_ad_health_disabled_in_content', |
| 157 | 'title' => __( 'Ads are disabled in the content of this page', 'advanced-ads' ), |
| 158 | 'href' => get_edit_post_link( $post->ID ) . '#advads-ad-settings', |
| 159 | 'meta' => array( |
| 160 | 'class' => 'advanced_ads_ad_health_warning', |
| 161 | 'target' => '_blank' |
| 162 | ) |
| 163 | ) ); |
| 164 | } |
| 165 | } else { |
| 166 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 167 | 'parent' => 'advanced_ads_ad_health', |
| 168 | 'id' => 'advanced_ads_ad_health_post_zero', |
| 169 | 'title' => __( 'the current post ID is 0 ', 'advanced-ads' ), |
| 170 | 'href' => ADVADS_URL . 'manual/ad-health/#post-id-0', |
| 171 | 'meta' => array( |
| 172 | 'class' => 'advanced_ads_ad_health_warning', |
| 173 | 'target' => '_blank' |
| 174 | ) |
| 175 | ) ); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if ( ! empty( $options['disabled-ads']['all'] ) ) { |
| 180 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 181 | 'parent' => 'advanced_ads_ad_health', |
| 182 | 'id' => 'advanced_ads_ad_health_no_all', |
| 183 | 'title' => __( 'Ads are disabled on all pages', 'advanced-ads' ), |
| 184 | 'href' => admin_url( 'admin.php?page=advanced-ads-settings' ), |
| 185 | 'meta' => array( |
| 186 | 'class' => 'advanced_ads_ad_health_warning', |
| 187 | 'target' => '_blank' |
| 188 | ) |
| 189 | ) ); |
| 190 | } |
| 191 | |
| 192 | if ( $wp_the_query->is_404() && ! empty( $options['disabled-ads']['404'] ) ) { |
| 193 | $nodes[] = array( 1, array( |
| 194 | 'parent' => 'advanced_ads_ad_health', |
| 195 | 'id' => 'advanced_ads_ad_health_no_404', |
| 196 | 'title' => __( 'Ads are disabled on 404 pages', 'advanced-ads' ), |
| 197 | 'href' => admin_url( 'admin.php?page=advanced-ads-settings' ), |
| 198 | 'meta' => array( |
| 199 | 'class' => 'advanced_ads_ad_health_warning', |
| 200 | 'target' => '_blank' |
| 201 | ) |
| 202 | ) ); |
| 203 | } |
| 204 | |
| 205 | if ( ! $wp_the_query->is_singular() && ! empty( $options['disabled-ads']['archives'] ) ){ |
| 206 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 207 | 'parent' => 'advanced_ads_ad_health', |
| 208 | 'id' => 'advanced_ads_ad_health_no_archive', |
| 209 | 'title' => __( 'Ads are disabled on non singular pages', 'advanced-ads' ), |
| 210 | 'href' => admin_url( 'admin.php?page=advanced-ads-settings' ), |
| 211 | 'meta' => array( |
| 212 | 'class' => 'advanced_ads_ad_health_warning', |
| 213 | 'target' => '_blank' |
| 214 | ) |
| 215 | ) ); |
| 216 | } |
| 217 | |
| 218 | if ( ! extension_loaded( 'dom' ) ) { |
| 219 | $nodes[] = array( 'type' => 1, 'data' => array( |
| 220 | 'parent' => 'advanced_ads_ad_health', |
| 221 | 'id' => 'advanced_ads_ad_health_no_dom_document', |
| 222 | 'title' => sprintf( __( 'The %s extension(s) is not loaded', 'advanced-ads' ), 'dom' ), |
| 223 | 'href' => 'http://php.net/manual/en/book.dom.php', |
| 224 | 'meta' => array( |
| 225 | 'class' => 'advanced_ads_ad_health_warning', |
| 226 | 'target' => '_blank' |
| 227 | ) |
| 228 | ) ); |
| 229 | } |
| 230 | |
| 231 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 232 | 'parent' => 'advanced_ads_ad_health', |
| 233 | 'id' => 'advanced_ads_ad_health_has_http', |
| 234 | 'title' => sprintf( '%s %s', |
| 235 | __( 'Your website is using HTTPS, but the ad code contains HTTP and might not work.', 'advanced-ads' ), |
| 236 | sprintf( __( 'Ad IDs: %s', 'advanced-ads' ), '<i></i>' ) |
| 237 | ), |
| 238 | 'href' => 'https://wpadvancedads.com/manual/ad-health/?utm_source=advanced-ads&utm_medium=link&utm_campaign=adhealth-https-ads#https-ads', |
| 239 | 'meta' => array( |
| 240 | 'class' => 'hidden advanced_ads_ad_health_warning advanced_ads_ad_health_has_http', |
| 241 | 'target' => '_blank' |
| 242 | ) |
| 243 | ) ); |
| 244 | |
| 245 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 246 | 'parent' => 'advanced_ads_ad_health', |
| 247 | 'id' => 'advanced_ads_ad_health_incorrect_head', |
| 248 | 'title' => sprintf( __( 'Visible ads should not use the Header placement: %s', 'advanced-ads' ), '<i></i>' ), |
| 249 | 'href' => 'https://wpadvancedads.com/manual/ad-health/?utm_source=advanced-ads&utm_medium=link&utm_campaign=adhealth-visible-ad-in-header#header-ads', |
| 250 | 'meta' => array( |
| 251 | 'class' => 'hidden advanced_ads_ad_health_warning advanced_ads_ad_health_incorrect_head', |
| 252 | 'target' => '_blank' |
| 253 | ) |
| 254 | ) ); |
| 255 | |
| 256 | // warn if an AdSense ad seems to be hidden |
| 257 | if( ! isset( $adsense_options['violation-warnings-disable'] ) ) { |
| 258 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 259 | 'parent' => 'advanced_ads_ad_health', |
| 260 | 'id' => 'advanced_ads_ad_health_hidden_adsense', |
| 261 | 'title' => sprintf( '%s: %s. %s', |
| 262 | __( 'AdSense violation', 'advanced-ads' ), |
| 263 | __( 'Ad is hidden', 'advanced-ads' ), |
| 264 | sprintf( __( 'IDs: %s', 'advanced-ads' ), '<i></i>' ) |
| 265 | ), |
| 266 | 'href' => 'https://wpadvancedads.com/manual/ad-health/?utm_source=advanced-ads&utm_medium=link&utm_campaign=adhealth-frontend-adsense-hidden#adsense-hidden', |
| 267 | 'meta' => array( |
| 268 | 'class' => 'hidden advanced_ads_ad_health_warning advanced_ads_ad_health_hidden_adsense', |
| 269 | 'target' => '_blank' |
| 270 | ) |
| 271 | ) ); |
| 272 | } |
| 273 | |
| 274 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 275 | 'parent' => 'advanced_ads_ad_health', |
| 276 | 'id' => 'advanced_ads_ad_health_floated_responsive_adsense', |
| 277 | 'title' => sprintf( __( 'The following responsive AdSense ads are not showing up: %s', 'advanced-ads' ), '<i></i>' ), |
| 278 | 'href' => 'https://wpadvancedads.com/manual/ad-health/?utm_source=advanced-ads&utm_medium=link&utm_campaign=adhealth-adsense-responsive-not-showing#The_following_responsive_AdSense_ads_arenot_showing_up', |
| 279 | 'meta' => array( |
| 280 | 'class' => 'hidden advanced_ads_ad_health_warning advanced_ads_ad_health_floated_responsive_adsense', |
| 281 | 'target' => '_blank' |
| 282 | ) |
| 283 | ) ); |
| 284 | |
| 285 | // warn if consent was not given |
| 286 | $privacy_state = Advanced_Ads_Privacy::get_instance()->get_state(); |
| 287 | if( 'not_needed' !== $privacy_state ) { |
| 288 | $nodes[] = array( 'type' => 2, 'data' => array( |
| 289 | 'parent' => 'advanced_ads_ad_health', |
| 290 | 'id' => 'advanced_ads_ad_health_consent_missing', |
| 291 | 'title' => __( 'Consent not given', 'advanced-ads' ), |
| 292 | 'href' => admin_url( 'admin.php?page=advanced-ads-settings#top#privacy' ), |
| 293 | 'meta' => array( |
| 294 | 'class' => 'hidden advanced_ads_ad_health_warning advanced_ads_ad_health_consent_missing', |
| 295 | 'target' => '_blank' |
| 296 | ) |
| 297 | ) ); |
| 298 | } |
| 299 | |
| 300 | $nodes[] = array( 'type' => 3, 'data' => array( |
| 301 | 'parent' => 'advanced_ads_ad_health', |
| 302 | 'id' => 'advanced_ads_ad_health_debug_dfp', |
| 303 | 'title' => __( 'debug DFP ads', 'advanced-ads' ), |
| 304 | 'href' => esc_url( add_query_arg( 'googfc', '' ) ), |
| 305 | 'meta' => array( |
| 306 | 'class' => 'hidden advanced_ads_ad_health_debug_dfp_link', |
| 307 | 'target' => '_blank', |
| 308 | ) |
| 309 | ) ); |
| 310 | |
| 311 | $nodes[] = array( 'type' => 3, 'data' => array( |
| 312 | 'parent' => 'advanced_ads_ad_health', |
| 313 | 'id' => 'advanced_ads_ad_health_highlight_ads', |
| 314 | 'title' => sprintf( '<label style="color: inherit;"><input id="advanced_ads_highlight_ads_checkbox" type="checkbox"> %s</label>', __( 'highlight ads', 'advanced-ads' ) ) |
| 315 | ) ); |
| 316 | |
| 317 | // search for AdSense Verification and Auto ads code |
| 318 | $nodes[] = array( 'type' => 3, 'data' => array( |
| 319 | 'parent' => 'advanced_ads_ad_health', |
| 320 | 'id' => 'advanced_ads_ad_health_auto_ads_found', |
| 321 | 'title' => __( 'Auto ads code found', 'advanced-ads' ), |
| 322 | 'href' => 'https://wpadvancedads.com/manual/ad-health/?utm_source=advanced-ads&utm_medium=link&utm_campaign=adhealth-adsense-auto-ads-found#Auto_ads_code_found', |
| 323 | 'meta' => array( |
| 324 | 'class' => 'hidden advanced_ads_ad_health_highlight_ads', |
| 325 | 'target' => '_blank', |
| 326 | ) |
| 327 | ) ); |
| 328 | |
| 329 | /** |
| 330 | * Add new node. |
| 331 | * |
| 332 | * @param array $node An array that contains: |
| 333 | * 'type' => 1 - warning, 2 - hidden warning that will be shown using JS, 3 - info message |
| 334 | * 'data': @see WP_Admin_Bar->add_node |
| 335 | * @param obj $wp_admin_bar |
| 336 | */ |
| 337 | $nodes = apply_filters( 'advanced-ads-ad-health-nodes', $nodes ); |
| 338 | usort( $nodes, array( $this, 'sort_nodes' ) ); |
| 339 | |
| 340 | $wp_admin_bar->add_node( array( |
| 341 | 'id' => 'advanced_ads_ad_health', |
| 342 | 'title' => __( 'Ad Health', 'advanced-ads' ), |
| 343 | ) ); |
| 344 | |
| 345 | $display_fine = true; |
| 346 | |
| 347 | foreach ( $nodes as $node ) { |
| 348 | if ( ! isset( $node['type'] ) || ! isset( $node['data'] ) ) { continue; } |
| 349 | if ( $node['type'] === 1 ) { $display_fine = false; } |
| 350 | $wp_admin_bar->add_node( $node['data'] ); |
| 351 | } |
| 352 | |
| 353 | if ( $display_fine ) { |
| 354 | $wp_admin_bar->add_node( array( |
| 355 | 'parent' => 'advanced_ads_ad_health', |
| 356 | 'id' => 'advanced_ads_ad_health_fine', |
| 357 | 'title' => __( 'Everything is fine', 'advanced-ads' ), |
| 358 | 'href' => false, |
| 359 | 'meta' => array( |
| 360 | 'target' => '_blank', |
| 361 | ) |
| 362 | ) ); |
| 363 | } |
| 364 | |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Sort nodes. |
| 369 | */ |
| 370 | function sort_nodes( $a, $b ) { |
| 371 | if ( ! isset( $a['type'] ) || ! isset( $b['type'] ) ) { |
| 372 | return 0; |
| 373 | } |
| 374 | if ( $a['type'] == $b['type'] ) { |
| 375 | return 0; |
| 376 | } |
| 377 | return ( $a['type'] < $b['type'] ) ? -1 : 1; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Set variable to 'true' when 'the_content' filter is invoked. |
| 382 | * |
| 383 | * @param string $content |
| 384 | * @return string $content |
| 385 | */ |
| 386 | public function set_did_the_content( $content ) { |
| 387 | if ( ! $this->did_the_content ) { |
| 388 | $this->did_the_content = true; |
| 389 | } |
| 390 | return $content; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Check conditions and display warning. |
| 395 | * Conditions: |
| 396 | * AdBlocker enabled, |
| 397 | * jQuery is included in header |
| 398 | * AdSense Quick Start ads are running |
| 399 | */ |
| 400 | public function footer_checks() { |
| 401 | $adsense_options = Advanced_Ads_AdSense_Data::get_instance()->get_options(); |
| 402 | ob_start(); |
| 403 | ?><!-- Advanced Ads: <?php _e( 'the following code is used for automatic error detection and only visible to admins', 'advanced-ads' ); ?>--> |
| 404 | <style>.hidden { display: none; } .advads-adminbar-is-warnings { background: #a54811 ! important; color: #fff !important; } |
| 405 | #wp-admin-bar-advanced_ads_ad_health-default a:after { content: "\25BA"; margin-left: .5em; font-size: smaller; } |
| 406 | .advanced-ads-highlight-ads { outline:4px solid blue !important; }</style> |
| 407 | <script type="text/javascript" src="<?php echo ADVADS_BASE_URL . 'admin/assets/js/advertisement.js' ?>"></script> |
| 408 | <script> |
| 409 | var advanced_ads_frontend_checks = { |
| 410 | showCount: function() { |
| 411 | try { |
| 412 | // Count only warnings that have the 'advanced_ads_ad_health_warning' class. |
| 413 | var warning_count = document.querySelectorAll( '.advanced_ads_ad_health_warning:not(.hidden)' ).length; |
| 414 | var fine_item = document.getElementById( 'wp-admin-bar-advanced_ads_ad_health_fine' ); |
| 415 | } catch ( e ) { return; } |
| 416 | |
| 417 | if ( warning_count ) { |
| 418 | var header = document.querySelector( '#wp-admin-bar-advanced_ads_ad_health > div' ); |
| 419 | |
| 420 | if ( fine_item ) { |
| 421 | // Hide 'fine' item. |
| 422 | fine_item.className += ' hidden'; |
| 423 | } |
| 424 | |
| 425 | if ( header ) { |
| 426 | header.innerHTML = header.innerHTML.replace(/ <i>(.*?)<\/i>/, '') + ' <i>(' + warning_count + ')</i>'; |
| 427 | header.className += ' advads-adminbar-is-warnings'; |
| 428 | } |
| 429 | } |
| 430 | }, |
| 431 | |
| 432 | array_unique: function( array ) { |
| 433 | var r= []; |
| 434 | for ( var i = 0; i < array.length; i++ ) { |
| 435 | if ( r.indexOf( array[ i ] ) === -1 ) { |
| 436 | r.push( array[ i ] ); |
| 437 | } |
| 438 | } |
| 439 | return r; |
| 440 | }, |
| 441 | |
| 442 | /** |
| 443 | * Add item to Ad Health node. |
| 444 | * |
| 445 | * @param string selector Selector of the node. |
| 446 | * @param string/array Item(s) to add. |
| 447 | */ |
| 448 | add_item_to_node: function( selector, item ) { |
| 449 | if ( typeof item === 'string' ) { |
| 450 | item = item.split(); |
| 451 | } |
| 452 | var selector = document.querySelector( selector ); |
| 453 | if ( selector ) { |
| 454 | selector.className = selector.className.replace( 'hidden', '' ); |
| 455 | selector.innerHTML = selector.innerHTML.replace( /(<i>)(.*?)(<\/i>)/, function( match, p1, p2, p3 ) { |
| 456 | p2 = ( p2 ) ? p2.split( ', ' ) : []; |
| 457 | p2 = p2.concat( item ); |
| 458 | p2 = advanced_ads_frontend_checks.array_unique( p2 ); |
| 459 | return p1 + p2.join( ', ' ) + p3; |
| 460 | } ); |
| 461 | advanced_ads_frontend_checks.showCount(); |
| 462 | } |
| 463 | }, |
| 464 | |
| 465 | /** |
| 466 | * Search for hidden AdSense. |
| 467 | * |
| 468 | * @param string context Context for search. |
| 469 | */ |
| 470 | advads_highlight_hidden_adsense: function( context ) { |
| 471 | if ( ! context ) { |
| 472 | context = 'html' |
| 473 | } |
| 474 | if ( window.jQuery ) { |
| 475 | var advads_ad_health_check_adsense_hidden_ids = []; |
| 476 | var responsive_zero_width = []; |
| 477 | jQuery( 'ins.adsbygoogle', context ).each( function() { |
| 478 | // The parent container is invisible. |
| 479 | if( ! jQuery( this ).parent().is(':visible') ){ |
| 480 | advads_ad_health_check_adsense_hidden_ids.push( this.dataset.adSlot ); |
| 481 | } |
| 482 | |
| 483 | // Zero width, perhaps because a parent container is floated |
| 484 | if ( jQuery( this ).attr( 'data-ad-format' ) && 0 === jQuery( this ).width() ) { |
| 485 | responsive_zero_width.push( this.dataset.adSlot ); |
| 486 | } |
| 487 | }); |
| 488 | if( advads_ad_health_check_adsense_hidden_ids.length ){ |
| 489 | advanced_ads_frontend_checks.add_item_to_node( '.advanced_ads_ad_health_hidden_adsense', advads_ad_health_check_adsense_hidden_ids ); |
| 490 | } |
| 491 | if ( responsive_zero_width.length ) { |
| 492 | advanced_ads_frontend_checks.add_item_to_node( '.advanced_ads_ad_health_floated_responsive_adsense', responsive_zero_width ); |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | }; |
| 497 | |
| 498 | (function(d, w) { |
| 499 | // var not_head_jQuery = typeof jQuery === 'undefined'; |
| 500 | |
| 501 | var addEvent = function( obj, type, fn ) { |
| 502 | if ( obj.addEventListener ) |
| 503 | obj.addEventListener( type, fn, false ); |
| 504 | else if ( obj.attachEvent ) |
| 505 | obj.attachEvent( 'on' + type, function() { return fn.call( obj, window.event ); } ); |
| 506 | }; |
| 507 | |
| 508 | function highlight_ads() { |
| 509 | try { |
| 510 | var ad_wrappers = document.querySelectorAll('div[id^="<?php echo Advanced_Ads_Plugin::get_instance()->get_frontend_prefix();?>"]') |
| 511 | } catch ( e ) { return; } |
| 512 | for ( i = 0; i < ad_wrappers.length; i++ ) { |
| 513 | if ( this.checked ) { |
| 514 | ad_wrappers[i].className += ' advanced-ads-highlight-ads'; |
| 515 | } else { |
| 516 | ad_wrappers[i].className = ad_wrappers[i].className.replace( 'advanced-ads-highlight-ads', '' ); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | advanced_ads_ready( function() { |
| 522 | var adblock_item = d.getElementById( 'wp-admin-bar-advanced_ads_ad_health_adblocker_enabled' ); |
| 523 | // jQuery_item = d.getElementById( 'wp-admin-bar-advanced_ads_ad_health_jquery' ), |
| 524 | |
| 525 | var highlight_checkbox = d.getElementById( 'advanced_ads_highlight_ads_checkbox' ); |
| 526 | if ( highlight_checkbox ) { |
| 527 | addEvent( highlight_checkbox, 'change', highlight_ads ); |
| 528 | } |
| 529 | |
| 530 | if ( adblock_item && typeof advanced_ads_adblocker_test === 'undefined' ) { |
| 531 | // show hidden item |
| 532 | adblock_item.className = adblock_item.className.replace( /hidden/, '' ); |
| 533 | } |
| 534 | |
| 535 | /* if ( jQuery_item && not_head_jQuery ) { |
| 536 | // show hidden item |
| 537 | jQuery_item.className = jQuery_item.className.replace( /hidden/, '' ); |
| 538 | }*/ |
| 539 | |
| 540 | advanced_ads_frontend_checks.showCount(); |
| 541 | }); |
| 542 | |
| 543 | <?php if( ! isset( $adsense_options['violation-warnings-disable'] ) ) : ?> |
| 544 | // show warning if AdSense ad is hidden |
| 545 | // show hint if AdSense Auto ads are enabled |
| 546 | setTimeout( function(){ |
| 547 | advanced_ads_ready( advanced_ads_frontend_checks.advads_highlight_hidden_adsense ); |
| 548 | advads_highlight_adsense_auto_ads(); |
| 549 | }, 2000 ); |
| 550 | |
| 551 | // highlight AdSense Auto Ads ads 3 seconds after site loaded |
| 552 | setTimeout( function(){ |
| 553 | advanced_ads_ready( advads_highlight_adsense_autoads ) |
| 554 | }, 3000 ); |
| 555 | function advads_highlight_adsense_autoads(){ |
| 556 | if ( ! window.jQuery ) { |
| 557 | window.console && window.console.log( 'Advanced Ads: jQuery not found. Some Ad Health warnings will not be shown' ); |
| 558 | return; |
| 559 | } |
| 560 | var autoads_ads = jQuery(document).find('.google-auto-placed'); |
| 561 | jQuery( '<p class="advads-autoads-hint" style="background-color:#0085ba;color:#fff;font-size:0.8em;padding:5px;"><?php |
| 562 | printf(__( 'This ad was automatically placed here by AdSense. <a href="%s" target="_blank" style="color:#fff;border-color:#fff;">Click here to learn more</a>.', 'advanced-ads' ), ADVADS_URL . 'adsense-in-random-positions-auto-ads/#utm_source=advanced-ads&utm_medium=link&utm_campaign=frontend-autoads-ads' ); |
| 563 | ?></p>' ).prependTo( autoads_ads ); |
| 564 | // show Auto Ads warning in Adhealth Bar if relevant |
| 565 | if( autoads_ads.length ){ |
| 566 | var advads_autoads_link = document.querySelector( '#wp-admin-bar-advanced_ads_autoads_displayed.hidden' ); |
| 567 | if ( advads_autoads_link ) { |
| 568 | advads_autoads_link.className = advads_autoads_link.className.replace( 'hidden', '' ); |
| 569 | } |
| 570 | advanced_ads_frontend_checks.showCount(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | // inform the user that AdSense Auto ads code was found |
| 575 | function advads_highlight_adsense_auto_ads(){ |
| 576 | var auto_ads_pattern = /enable_page_level_ads: true/m |
| 577 | if (auto_ads_pattern.exec( jQuery('head').text() ) ){ |
| 578 | var advads_autoads_code_link = document.querySelector( '#wp-admin-bar-advanced_ads_ad_health_auto_ads_found' ); |
| 579 | advads_autoads_code_link.className = advads_autoads_code_link.className.replace( 'hidden', '' ); |
| 580 | } |
| 581 | } |
| 582 | <?php endif; |
| 583 | /** |
| 584 | * code to check if current user gave consent to show ads |
| 585 | */ |
| 586 | $privacy_state = Advanced_Ads_Privacy::get_instance()->get_state(); |
| 587 | if( 'not_needed' !== $privacy_state ) : |
| 588 | ?>advanced_ads_ready( function() { |
| 589 | var state = ( advads.privacy ) ? advads.privacy.get_state() : ""; |
| 590 | var advads_consent_link = document.querySelector( '#wp-admin-bar-advanced_ads_ad_health_consent_missing.hidden' ); |
| 591 | if ( 'unknown' === state && advads_consent_link ) { |
| 592 | advads_consent_link.className = advads_consent_link.className.replace( 'hidden', '' ); |
| 593 | } |
| 594 | |
| 595 | advanced_ads_frontend_checks.showCount(); |
| 596 | }); |
| 597 | <?php endif; ?> |
| 598 | })(document, window); |
| 599 | </script> |
| 600 | <?php echo Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Inject JS after ad content. |
| 605 | * |
| 606 | * @param str $content ad content |
| 607 | * @param obj $ad Advanced_Ads_Ad |
| 608 | * @return str $content ad content |
| 609 | */ |
| 610 | public function after_ad_output( $content = '', Advanced_Ads_Ad $ad ) { |
| 611 | if ( ! isset( $ad->args['frontend-check'] ) ) { return $content; } |
| 612 | |
| 613 | // Allow DFP debugging by showing a link that points to the current URL with the 'googfc' parameter. |
| 614 | if ( $ad->type === 'plain' && preg_match( '/gpt\.js/', $content ) ) { |
| 615 | ob_start(); ?> |
| 616 | <script>advanced_ads_ready( function() { |
| 617 | var advads_dfp_link = document.querySelector( '.advanced_ads_ad_health_debug_dfp_link.hidden' ); |
| 618 | if ( advads_dfp_link ) { |
| 619 | advads_dfp_link.className = advads_dfp_link.className.replace( 'hidden', '' ); |
| 620 | advanced_ads_frontend_checks.showCount(); |
| 621 | } |
| 622 | });</script> |
| 623 | <?php |
| 624 | $content .= Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 625 | } |
| 626 | |
| 627 | if ( Advanced_Ads_Ad_Debug::is_https_and_http( $ad ) ) { |
| 628 | ob_start(); ?> |
| 629 | <script>advanced_ads_ready( function() { |
| 630 | var ad_id = '<?php echo $ad->id; ?>'; |
| 631 | advanced_ads_frontend_checks.add_item_to_node( '.advanced_ads_ad_health_has_http', ad_id ); |
| 632 | });</script> |
| 633 | <?php |
| 634 | $content .= Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 635 | } |
| 636 | |
| 637 | if ( ! $this->can_use_head_placement( $content, $ad ) ) { |
| 638 | ob_start(); ?> |
| 639 | <script>advanced_ads_ready( function() { |
| 640 | var ad_id = '<?php echo $ad->id; ?>'; |
| 641 | advanced_ads_frontend_checks.add_item_to_node( '.advanced_ads_ad_health_incorrect_head', ad_id ); |
| 642 | });</script> |
| 643 | <?php |
| 644 | $content .= Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 645 | } |
| 646 | |
| 647 | $adsense_options = Advanced_Ads_AdSense_Data::get_instance()->get_options(); |
| 648 | if ( 'adsense' === $ad->type |
| 649 | && ! empty( $ad->args['cache_busting_elementid'] ) |
| 650 | && ! isset( $adsense_options['violation-warnings-disable'] ) |
| 651 | ) { |
| 652 | ob_start(); ?> |
| 653 | <script>advanced_ads_ready( function() { |
| 654 | var ad_id = '<?php echo $ad->id; ?>'; |
| 655 | var wrapper = '#<?php echo $ad->args['cache_busting_elementid']; ?>'; |
| 656 | advanced_ads_frontend_checks.advads_highlight_hidden_adsense( wrapper ); |
| 657 | });</script> |
| 658 | <?php |
| 659 | $content .= Advanced_Ads_Utils::get_inline_asset( ob_get_clean() ); |
| 660 | } |
| 661 | |
| 662 | return $content; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | /** |
| 667 | * Check if the 'Header Code' placement can be used to delived the ad. |
| 668 | * |
| 669 | * @param string $content Ad content. |
| 670 | * @param obj $ad Advanced_Ads_Ad |
| 671 | * @return bool |
| 672 | */ |
| 673 | private function can_use_head_placement( $content, Advanced_Ads_Ad $ad ) { |
| 674 | |
| 675 | if ( ! $ad->is_head_placement ) { |
| 676 | return true; |
| 677 | } |
| 678 | if ( ! $dom = $this->get_ad_dom( $content ) ) { |
| 679 | return true; |
| 680 | } |
| 681 | |
| 682 | $body = $dom->getElementsByTagName( 'body' )->item( 0 ); |
| 683 | |
| 684 | $count = $body->childNodes->length; |
| 685 | for ( $i = 0; $i < $count; $i++ ) { |
| 686 | $node = $body->childNodes->item( $i ); |
| 687 | |
| 688 | if ( XML_TEXT_NODE === $node->nodeType ) { |
| 689 | return false; |
| 690 | } |
| 691 | |
| 692 | if ( XML_ELEMENT_NODE === $node->nodeType |
| 693 | && ! in_array( $node->nodeName, array( 'meta', 'link', 'title', 'style', 'script', 'noscript', 'base' ) ) ) { |
| 694 | return false; |
| 695 | } |
| 696 | } |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * Convert ad content to a DOMDocument. |
| 702 | * |
| 703 | * @param string $content |
| 704 | * @return DOMDocument|false |
| 705 | */ |
| 706 | private function get_ad_dom( $content ) { |
| 707 | if ( ! extension_loaded( 'dom' ) ) { |
| 708 | return false; |
| 709 | } |
| 710 | $libxml_previous_state = libxml_use_internal_errors( true ); |
| 711 | $dom = new DOMDocument(); |
| 712 | $result = $dom->loadHTML( '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body>' . $content . '</body></html>' ); |
| 713 | |
| 714 | libxml_clear_errors(); |
| 715 | libxml_use_internal_errors( $libxml_previous_state ); |
| 716 | |
| 717 | if ( ! $result ) { |
| 718 | return false; |
| 719 | } |
| 720 | |
| 721 | return $dom; |
| 722 | } |
| 723 | } |
| 724 |