EDD_SL_Plugin_Updater.php
8 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
8 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
ad-debug.php
278 lines
| 1 | <?php |
| 2 | class Advanced_Ads_Ad_Debug { |
| 3 | /** |
| 4 | * Prepare debug mode output. |
| 5 | * |
| 6 | * @param obj Advanced_Ads_Ad |
| 7 | */ |
| 8 | public function prepare_debug_output( Advanced_Ads_Ad $ad ) { |
| 9 | global $post, $wp_query; |
| 10 | |
| 11 | // set size |
| 12 | if ( $ad->width > 100 && $ad->height > 100 ){ |
| 13 | $width = $ad->width; |
| 14 | $height = $ad->height; |
| 15 | } else { |
| 16 | $width = 300; |
| 17 | $height = 250; |
| 18 | } |
| 19 | |
| 20 | $style = "width:{$width}px;height:{$height}px;background-color:#ddd;overflow:scroll;"; |
| 21 | $style_full = 'width: 100%; height: 100vh; background-color: #ddd; overflow: scroll; position: fixed; top: 0; left: 0; min-width: 600px; z-index: 99999;'; |
| 22 | |
| 23 | if ( ! empty( $ad->wrapper['id']) ) { |
| 24 | $wrapper_id = $ad->wrapper['id']; |
| 25 | } else { |
| 26 | $wrapper_id = Advanced_Ads_Plugin::get_instance()->get_frontend_prefix() . mt_rand(); |
| 27 | } |
| 28 | |
| 29 | $content = array(); |
| 30 | |
| 31 | if ( $ad->can_display( array( 'ignore_debugmode' => true ) ) ) { |
| 32 | $content[] = __( 'The ad is displayed on the page', 'advanced-ads' ); |
| 33 | } else { |
| 34 | $content[] = __( 'The ad is not displayed on the page', 'advanced-ads' ); |
| 35 | } |
| 36 | |
| 37 | // compare current wp_query with global wp_main_query |
| 38 | if ( ! $wp_query->is_main_query() ) { |
| 39 | $content[] = sprintf( '<span style="color: red;">%s</span>', __( 'Current query is not identical to main query.', 'advanced-ads' ) ); |
| 40 | // output differences |
| 41 | $content[] = $this->build_query_diff_table(); |
| 42 | } |
| 43 | |
| 44 | if ( isset( $post->post_title ) && isset( $post->ID ) ) { |
| 45 | $content[] = sprintf( '%s: %s, %s: %s', __( 'current post', 'advanced-ads' ), $post->post_title, 'ID', $post->ID ); |
| 46 | } |
| 47 | // compare current post with global post |
| 48 | if ( $wp_query->post !== $post ){ |
| 49 | $error = sprintf( '<span style="color: red;">%s</span>', __( 'Current post is not identical to main post.', 'advanced-ads' ) ); |
| 50 | if ( isset( $wp_query->post->post_title ) && $wp_query->post->ID ) { |
| 51 | $error .= sprintf( '<br />%s: %s, %s: %s', __( 'main post', 'advanced-ads' ), $wp_query->post->post_title, 'ID', $wp_query->post->ID ); |
| 52 | } |
| 53 | $content[] = $error; |
| 54 | } |
| 55 | |
| 56 | $content[] = $this->build_call_chain( $ad ); |
| 57 | $content[] = $this->build_display_conditions_table( $ad ); |
| 58 | $content[] = $this->build_visitor_conditions_table( $ad ); |
| 59 | |
| 60 | if ( $message = self::is_https_and_http( $ad ) ) { |
| 61 | $content[] = sprintf( '<span style="color: red;">%s</span>', $message ); |
| 62 | } |
| 63 | |
| 64 | $content = apply_filters( 'advanced-ads-ad-output-debug-content', $content, $ad ); |
| 65 | |
| 66 | ob_start(); |
| 67 | |
| 68 | include( ADVADS_BASE_PATH . '/public/views/ad-debug.php' ); |
| 69 | |
| 70 | $output = ob_get_clean(); |
| 71 | |
| 72 | // apply a custom filter by ad type |
| 73 | $output = apply_filters( 'advanced-ads-ad-output-debug', $output, $ad ); |
| 74 | $output = apply_filters( 'advanced-ads-ad-output', $output, $ad ); |
| 75 | |
| 76 | return $output; |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * build table with differences between current and main query |
| 82 | * |
| 83 | * @since 1.7.0.3 |
| 84 | */ |
| 85 | protected function build_query_diff_table(){ |
| 86 | |
| 87 | global $wp_query, $wp_the_query; |
| 88 | |
| 89 | $diff_current = array_diff_assoc( $wp_query->query_vars, $wp_the_query->query_vars ); |
| 90 | $diff_main = array_diff_assoc( $wp_the_query->query_vars, $wp_query->query_vars ); |
| 91 | |
| 92 | if( ! is_array( $diff_current ) || ! is_array( $diff_main ) ){ |
| 93 | return ''; |
| 94 | } |
| 95 | |
| 96 | ob_start(); |
| 97 | |
| 98 | ?><table><thead><tr><th></th><th><?php _e( 'current query', 'advanced-ads'); ?></th><th><?php _e( 'main query', 'advanced-ads'); ?></th></tr></thead><?php |
| 99 | foreach( $diff_current as $_key => $_value ){ |
| 100 | ?><tr><td><?php echo $_key; ?></td><td><?php echo $_value; ?></td><td><?php if( isset( $diff_main[$_key] ) ) echo $diff_main[$_key]; ?></td></tr><?php |
| 101 | } |
| 102 | ?></table><?php |
| 103 | |
| 104 | return ob_get_clean(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Build call chain (placement->group->ad) |
| 109 | * |
| 110 | * @param obj Advanced_Ads_Ad |
| 111 | * @return string |
| 112 | */ |
| 113 | protected function build_call_chain( Advanced_Ads_Ad $ad ) { |
| 114 | ob_start(); |
| 115 | |
| 116 | $options = $ad->options(); |
| 117 | |
| 118 | printf( '%s: %s (%s)', __( 'Ad', 'advanced-ads' ), esc_html( $ad->title ), $ad->id ); |
| 119 | |
| 120 | if ( isset( $options['group_info']['id'] ) && isset( $options['group_info']['name'] ) ) { |
| 121 | printf( '<br />%s: %s (%s)', _x( 'Ad Group', 'ad group singular name', 'advanced-ads' ), esc_html( $options['group_info']['name'] ), $options['group_info']['id'] ); |
| 122 | } |
| 123 | |
| 124 | if ( isset( $options['output']['placement_id'] ) ) { |
| 125 | $placements = Advanced_Ads::get_ad_placements_array(); |
| 126 | $placement_id = $options['output']['placement_id']; |
| 127 | $placement_name = isset( $placements[ $placement_id ]['name'] ) ? $placements[ $placement_id ]['name'] : ''; |
| 128 | printf( '<br />%s: %s (%s)', __( 'Placement', 'advanced-ads' ), esc_html( $placement_name ), esc_html( $placement_id ) ); |
| 129 | } |
| 130 | |
| 131 | return ob_get_clean(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Build display conditions table. |
| 136 | * |
| 137 | * @param obj Advanced_Ads_Ad |
| 138 | * @return string |
| 139 | */ |
| 140 | protected function build_display_conditions_table( Advanced_Ads_Ad $ad ) { |
| 141 | $options = $ad->options(); |
| 142 | |
| 143 | if ( ! isset( $options['conditions'] ) |
| 144 | || ! is_array( $options['conditions'] ) |
| 145 | || ! count( $options['conditions'] ) ) { return; } |
| 146 | |
| 147 | $conditions = array_values( $options['conditions'] ); |
| 148 | $display_conditions = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 149 | $the_query = Advanced_Ads_Display_Conditions::get_instance()->ad_select_args_callback( array() ); |
| 150 | |
| 151 | ob_start(); |
| 152 | _e( 'Display Conditions', 'advanced-ads' ); ?> |
| 153 | <?php |
| 154 | foreach ( $conditions as $_condition ) { |
| 155 | if ( ! is_array( $_condition ) |
| 156 | || ! isset( $_condition['type'] ) |
| 157 | || ! isset( $display_conditions[ $_condition['type'] ]['check'][1] ) |
| 158 | ) { continue; } |
| 159 | |
| 160 | |
| 161 | printf( '<div style="margin-bottom: 20px; white-space: pre-wrap; font-family: monospace; width: 100%%; background: %s;"><strong>%s</strong>', |
| 162 | Advanced_Ads_Display_Conditions::frontend_check( $_condition, $ad ) ? '#e9ffe9' : '#ffe9e9', |
| 163 | $display_conditions[ $_condition['type'] ]['label'] ); |
| 164 | |
| 165 | $check = $display_conditions[ $_condition['type'] ]['check'][1]; |
| 166 | if ( $check === 'check_general' ) { |
| 167 | printf( '<table border="1"><thead><tr><th></th><th>%s</th><th>%s</th></tr></thead>', __( 'Ad', 'advanced-ads' ), 'wp_the_query' ); |
| 168 | } else { |
| 169 | printf( '<table border="1"><thead><tr><th>%s</th><th>%s</th></tr></thead>', __( 'Ad', 'advanced-ads' ), 'wp_the_query' ); |
| 170 | } |
| 171 | |
| 172 | switch( $check ) { |
| 173 | case 'check_post_type': |
| 174 | printf( '<tr><td>%s</td><td>%s</td></tr>', |
| 175 | ( isset( $_condition['value'] ) && is_array( $_condition['value'] ) ) ? esc_html( implode( ',', $_condition['value'] ) ) : '', |
| 176 | isset( $the_query['post']['post_type'] ) ? $the_query['post']['post_type'] : '' ); |
| 177 | break; |
| 178 | case 'check_general': |
| 179 | if ( isset( $the_query['wp_the_query'] ) && is_array( $the_query['wp_the_query'] ) ) { |
| 180 | $ad_vars = ( isset( $_condition['value'] ) && is_array( $_condition['value'] ) ) ? $_condition['value'] : array(); |
| 181 | |
| 182 | if ( in_array( 'is_front_page', $ad_vars ) ) { |
| 183 | $ad_vars[] = 'is_home'; |
| 184 | } |
| 185 | |
| 186 | foreach ( $the_query['wp_the_query'] as $_var => $_flag ) { |
| 187 | printf( '<tr><td>%s</td><td>%s</td><td>%s</td></tr>', |
| 188 | $_var, |
| 189 | in_array( $_var, $ad_vars ) ? 1 : 0, |
| 190 | $_flag ); |
| 191 | } |
| 192 | } |
| 193 | break; |
| 194 | case 'check_author': |
| 195 | printf( '<tr><td>%s</td><td>%s</td></tr>', |
| 196 | ( isset( $_condition['value'] ) && is_array( $_condition['value'] ) ) ? esc_html( implode( ',', $_condition['value'] ) ) : '', |
| 197 | isset( $the_query['post']['author'] ) ? $the_query['post']['author'] : '' ); |
| 198 | break; |
| 199 | case 'check_post_ids': |
| 200 | case 'check_taxonomies': |
| 201 | printf( '<tr><td>%s</td><td>post_id: %s<br />is_singular: %s</td></tr>', |
| 202 | ( isset( $_condition['value'] ) && is_array( $_condition['value'] ) ) ? esc_html( implode( ',', $_condition['value'] ) ) : '', |
| 203 | isset( $the_query['post']['id']) ? $the_query['post']['id'] : '', |
| 204 | ! empty( $the_query['wp_the_query']['is_singular'] ) ); |
| 205 | break; |
| 206 | case 'check_taxonomy_archive': |
| 207 | printf( '<tr><td>%s</td><td>term_id: %s<br />is_archive: %s</td></tr>', |
| 208 | ( isset( $_condition['value'] ) && is_array( $_condition['value'] ) ) ? esc_html( implode( ',', $_condition['value'] ) ) : '', |
| 209 | isset( $the_query['wp_the_query']['term_id'] ) ? $the_query['wp_the_query']['term_id'] : '', |
| 210 | ! empty( $the_query['wp_the_query']['is_archive'] ) ); |
| 211 | break; |
| 212 | default: |
| 213 | printf( '<tr><td>%s</td><td>%s</td></tr>', esc_html( print_r( $_condition, true ) ), print_r( $the_query, true ) ); |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | echo '</table></div>'; |
| 218 | } |
| 219 | |
| 220 | return ob_get_clean(); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Build visitor conditions table. |
| 225 | * |
| 226 | * @param obj Advanced_Ads_Ad |
| 227 | * @return string |
| 228 | */ |
| 229 | protected function build_visitor_conditions_table( Advanced_Ads_Ad $ad ) { |
| 230 | $options = $ad->options(); |
| 231 | |
| 232 | if ( ! isset( $options['visitors'] ) |
| 233 | || ! is_array( $options['visitors'] ) |
| 234 | || ! count( $options['visitors'] ) ) { return; } |
| 235 | |
| 236 | ob_start(); |
| 237 | |
| 238 | $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; |
| 239 | ?><?php _e( 'Visitor Conditions', 'advanced-ads' ); |
| 240 | |
| 241 | foreach ( $options['visitors'] as $_condition ) { |
| 242 | if ( ! is_array( $_condition ) |
| 243 | || ! isset( $_condition['type'] ) |
| 244 | || ! isset( $visitor_conditions[ $_condition['type'] ]['check'][1] ) |
| 245 | ) { continue; } |
| 246 | |
| 247 | $content = ''; |
| 248 | foreach ( $_condition as $_k => $_v ) { |
| 249 | $content .= esc_html( $_k ) . ': ' . esc_html( $_v ) . '<br>'; |
| 250 | } |
| 251 | |
| 252 | printf( '<div style="margin-bottom: 20px; white-space: pre-wrap; font-family: monospace; width: 100%%; background: %s;">%s</div>', |
| 253 | Advanced_Ads_Visitor_Conditions::frontend_check( $_condition, $ad ) ? '#e9ffe9' : '#ffe9e9', |
| 254 | $content ); |
| 255 | } |
| 256 | |
| 257 | return ob_get_clean(); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Check if the current URL is HTTPS, but the ad code contains HTTP. |
| 262 | * |
| 263 | * @param obj Advanced_Ads_Ad |
| 264 | * @return bool false/string |
| 265 | */ |
| 266 | public static function is_https_and_http( Advanced_Ads_Ad $ad ) { |
| 267 | if ( is_ssl() |
| 268 | && ( $ad->type === 'plain' || $ad->type === 'content' ) |
| 269 | // Find img, iframe, script. '\\\\' denotes a single backslash |
| 270 | && preg_match( '#\ssrc=\\\\?[\'"]http:\\\\?/\\\\?/#i', $ad->content ) |
| 271 | ) { |
| 272 | return __( 'Your website is using HTTPS, but the ad code contains HTTP and might not work.', 'advanced-ads' ); |
| 273 | } |
| 274 | |
| 275 | return false; |
| 276 | } |
| 277 | } |
| 278 |