EDD_SL_Plugin_Updater.php
6 years ago
ad-ajax.php
6 years ago
ad-debug.php
8 years ago
ad-health-notices.php
6 years ago
ad-model.php
8 years ago
ad-select.php
9 years ago
ad.php
6 years ago
ad_ajax_callbacks.php
6 years ago
ad_group.php
6 years ago
ad_placements.php
6 years ago
ad_type_abstract.php
8 years ago
ad_type_content.php
6 years ago
ad_type_dummy.php
6 years ago
ad_type_group.php
8 years ago
ad_type_image.php
6 years ago
ad_type_plain.php
6 years ago
checks.php
6 years ago
compatibility.php
6 years ago
display-conditions.php
6 years ago
filesystem.php
8 years ago
frontend_checks.php
6 years ago
plugin.php
6 years ago
upgrades.php
6 years ago
utils.php
6 years ago
visitor-conditions.php
6 years ago
widget.php
6 years ago
ad_ajax_callbacks.php
501 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Advanced Ads. |
| 5 | * |
| 6 | * @package Advanced_Ads |
| 7 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 8 | * @license GPL-2.0+ |
| 9 | * @link http://webgilde.com |
| 10 | * @copyright 2013-2018 Thomas Maier, webgilde GmbH |
| 11 | */ |
| 12 | |
| 13 | /** |
| 14 | * This class is used to bundle all ajax callbacks |
| 15 | * |
| 16 | * @package Advanced_Ads_Ajax_Callbacks |
| 17 | * @author Thomas Maier <thomas.maier@webgilde.com> |
| 18 | */ |
| 19 | class Advanced_Ads_Ad_Ajax_Callbacks { |
| 20 | |
| 21 | public function __construct() { |
| 22 | |
| 23 | // NOTE: admin only! |
| 24 | //add_action( 'wp_ajax_load_content_editor', array( $this, 'load_content_editor' ) ); |
| 25 | add_action( 'wp_ajax_load_ad_parameters_metabox', array( $this, 'load_ad_parameters_metabox' ) ); |
| 26 | add_action( 'wp_ajax_load_visitor_conditions_metabox', array( $this, 'load_visitor_condition' ) ); |
| 27 | add_action( 'wp_ajax_load_display_conditions_metabox', array( $this, 'load_display_condition' ) ); |
| 28 | add_action( 'wp_ajax_advads-terms-search', array( $this, 'search_terms' ) ); |
| 29 | add_action( 'wp_ajax_advads-close-notice', array( $this, 'close_notice' ) ); |
| 30 | add_action( 'wp_ajax_advads-hide-notice', array( $this, 'hide_notice' ) ); |
| 31 | add_action( 'wp_ajax_advads-subscribe-notice', array( $this, 'subscribe' ) ); |
| 32 | add_action( 'wp_ajax_advads-activate-license', array( $this, 'activate_license' ) ); |
| 33 | add_action( 'wp_ajax_advads-deactivate-license', array( $this, 'deactivate_license' ) ); |
| 34 | add_action( 'wp_ajax_advads-adblock-rebuild-assets', array( $this, 'adblock_rebuild_assets' ) ); |
| 35 | add_action( 'wp_ajax_advads-post-search', array( $this, 'post_search' ) ); |
| 36 | add_action( 'wp_ajax_advads-ad-injection-content', array( $this, 'inject_placement' ) ); |
| 37 | add_action( 'wp_ajax_advads-save-hide-wizard-state', array( $this, 'save_wizard_state' ) ); |
| 38 | add_action( 'wp_ajax_advads-adsense-enable-pla', array( $this, 'adsense_enable_pla' ) ); |
| 39 | add_action( 'wp_ajax_advads-ad-health-notice-display', array( $this, 'ad_health_notice_display' ) ); |
| 40 | add_action( 'wp_ajax_advads-ad-health-notice-push', array( $this, 'ad_health_notice_push' ) ); |
| 41 | add_action( 'wp_ajax_advads-ad-health-notice-hide', array( $this, 'ad_health_notice_hide' ) ); |
| 42 | add_action( 'wp_ajax_advads-ad-health-notice-unignore', array( $this, 'ad_health_notice_unignore' ) ); |
| 43 | add_action( 'wp_ajax_advads-ad-health-notice-solved', array( $this, 'ad_health_notice_solved' ) ); |
| 44 | |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * load content of the ad parameter metabox |
| 49 | * |
| 50 | * @since 1.0.0 |
| 51 | */ |
| 52 | public function load_ad_parameters_metabox() { |
| 53 | |
| 54 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 55 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | $types = Advanced_Ads::get_instance()->ad_types; |
| 60 | $type_string = $_REQUEST['ad_type']; |
| 61 | $ad_id = absint( $_REQUEST['ad_id'] ); |
| 62 | if ( empty($ad_id) ) { die(); } |
| 63 | |
| 64 | $ad = new Advanced_Ads_Ad( $ad_id ); |
| 65 | |
| 66 | if ( ! empty($types[$type_string]) && method_exists( $types[$type_string], 'render_parameters' ) ) { |
| 67 | $type = $types[ $type_string ]; |
| 68 | $type->render_parameters( $ad ); |
| 69 | |
| 70 | $types_without_size = array('dummy'); |
| 71 | $types_without_size = apply_filters( 'advanced-ads-types-without-size', $types_without_size ); |
| 72 | if ( ! in_array($type_string, $types_without_size) ) { |
| 73 | include ADVADS_BASE_PATH . 'admin/views/ad-parameters-size.php'; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | die(); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * load interface for single visitor condition |
| 83 | * |
| 84 | * @since 1.5.4 |
| 85 | */ |
| 86 | public function load_visitor_condition() { |
| 87 | |
| 88 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 89 | |
| 90 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | // get visitor condition types |
| 95 | $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; |
| 96 | $condition = array(); |
| 97 | $condition['type'] = isset( $_POST['type'] ) ? $_POST['type'] : ''; |
| 98 | $index = isset( $_POST['index'] ) ? $_POST['index'] : 0; |
| 99 | |
| 100 | if( isset( $visitor_conditions[$condition['type']] ) ) { |
| 101 | $metabox = $visitor_conditions[$condition['type']]['metabox']; |
| 102 | } else { |
| 103 | die(); |
| 104 | } |
| 105 | |
| 106 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 107 | call_user_func( array($metabox[0], $metabox[1]), $condition, $index ); |
| 108 | } |
| 109 | |
| 110 | die(); |
| 111 | } |
| 112 | /** |
| 113 | * load interface for single display condition |
| 114 | * |
| 115 | * @since 1.7 |
| 116 | */ |
| 117 | public function load_display_condition() { |
| 118 | |
| 119 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 120 | |
| 121 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // get display condition types |
| 126 | $conditions = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 127 | $condition = array(); |
| 128 | |
| 129 | $condition['type'] = isset( $_POST['type'] ) ? $_POST['type'] : ''; |
| 130 | |
| 131 | $index = isset( $_POST['index'] ) ? $_POST['index'] : 0; |
| 132 | |
| 133 | if( isset( $conditions[$condition['type']] ) ) { |
| 134 | $metabox = $conditions[$condition['type']]['metabox']; |
| 135 | } else { |
| 136 | die(); |
| 137 | } |
| 138 | |
| 139 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 140 | call_user_func( array($metabox[0], $metabox[1]), $condition, $index ); |
| 141 | } |
| 142 | |
| 143 | die(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * search terms belonging to a specific taxonomy |
| 148 | * |
| 149 | * @sinc 1.4.7 |
| 150 | */ |
| 151 | public function search_terms(){ |
| 152 | |
| 153 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 154 | |
| 155 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | $args = array(); |
| 160 | $taxonomy = $_POST['tax']; |
| 161 | $args = array('hide_empty' => false, 'number' => 20); |
| 162 | |
| 163 | if ( !isset( $_POST['search'] ) || $_POST['search'] === '' ) { die(); } |
| 164 | |
| 165 | // if search is an id, search for the term id, else do a full text search |
| 166 | if( 0 !== absint($_POST['search'] ) && strlen( $_POST['search'] ) == strlen ( absint($_POST['search'] ) ) ){ |
| 167 | $args['include'] = array(absint($_POST['search'])); |
| 168 | } else { |
| 169 | $args['search'] = $_POST['search']; |
| 170 | } |
| 171 | |
| 172 | $results = get_terms( $taxonomy, $args ); |
| 173 | // $results = _WP_Editors::wp_link_query( $args ); |
| 174 | echo wp_json_encode( $results ); |
| 175 | echo "\n"; |
| 176 | die(); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * close a notice for good |
| 181 | * |
| 182 | * @since 1.5.3 |
| 183 | */ |
| 184 | public function close_notice(){ |
| 185 | |
| 186 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 187 | |
| 188 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) |
| 189 | || empty( $_POST['notice'] ) |
| 190 | ) { |
| 191 | die(); |
| 192 | } |
| 193 | |
| 194 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue($_POST['notice']); |
| 195 | die(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * hide a notice for some time (7 days right now) |
| 200 | * |
| 201 | * @since 1.8.17 |
| 202 | */ |
| 203 | public function hide_notice(){ |
| 204 | |
| 205 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 206 | |
| 207 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) |
| 208 | || empty( $_POST['notice'] ) |
| 209 | ) { |
| 210 | die(); |
| 211 | } |
| 212 | |
| 213 | Advanced_Ads_Admin_Notices::get_instance()->hide_notice( $_POST['notice'] ); |
| 214 | die(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * subscribe to newsletter |
| 219 | * |
| 220 | * @since 1.5.3 |
| 221 | */ |
| 222 | public function subscribe(){ |
| 223 | |
| 224 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 225 | |
| 226 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface') ) |
| 227 | || empty( $_POST['notice'] ) |
| 228 | ) { |
| 229 | die(); |
| 230 | } |
| 231 | |
| 232 | echo Advanced_Ads_Admin_Notices::get_instance()->subscribe($_POST['notice']); |
| 233 | die(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * activate license of an add-on |
| 238 | * |
| 239 | * @since 1.5.7 |
| 240 | */ |
| 241 | public function activate_license(){ |
| 242 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | // check nonce |
| 247 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 248 | |
| 249 | if ( !isset( $_POST['addon'] ) || $_POST['addon'] === '' ) { die(); } |
| 250 | |
| 251 | echo Advanced_Ads_Admin_Licenses::get_instance()->activate_license( $_POST['addon'], $_POST['pluginname'], $_POST['optionslug'], $_POST['license'] ); |
| 252 | |
| 253 | die(); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * deactivate license of an add-on |
| 258 | * |
| 259 | * @since 1.6.11 |
| 260 | */ |
| 261 | public function deactivate_license(){ |
| 262 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // check nonce |
| 267 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 268 | |
| 269 | if ( !isset( $_POST['addon'] ) || $_POST['addon'] === '' ) { die(); } |
| 270 | |
| 271 | echo Advanced_Ads_Admin_Licenses::get_instance()->deactivate_license( $_POST['addon'], $_POST['pluginname'], $_POST['optionslug'] ); |
| 272 | |
| 273 | die(); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * rebuild assets for ad-blocker module |
| 278 | * |
| 279 | */ |
| 280 | public function adblock_rebuild_assets(){ |
| 281 | |
| 282 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 283 | |
| 284 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | Advanced_Ads_Ad_Blocker_Admin::get_instance()->add_asset_rebuild_form(); |
| 289 | die(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * post search (used in Display conditions) |
| 294 | * |
| 295 | */ |
| 296 | public function post_search(){ |
| 297 | |
| 298 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 299 | |
| 300 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | add_filter( 'wp_link_query_args', array( 'Advanced_Ads_Display_Conditions', 'modify_post_search' ) ); |
| 305 | add_filter( 'posts_search', array( 'Advanced_Ads_Display_Conditions', 'modify_post_search_sql' ) ); |
| 306 | |
| 307 | wp_ajax_wp_link_ajax(); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * inject an ad and a placement |
| 312 | * |
| 313 | * @since 1.7.3 |
| 314 | */ |
| 315 | public function inject_placement(){ |
| 316 | |
| 317 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 318 | |
| 319 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 320 | die(); |
| 321 | } |
| 322 | |
| 323 | $ad_id = absint( $_REQUEST['ad_id'] ); |
| 324 | if ( empty( $ad_id ) ) { die(); } |
| 325 | |
| 326 | // use existing placement |
| 327 | if ( isset( $_REQUEST['placement_slug'] ) ) { |
| 328 | $xml_array[] = '<placements type="array">'; |
| 329 | $xml_array[] = '<item key="0" type="array">'; |
| 330 | $xml_array[] = '<item type="string">ad_' . $ad_id . '</item>'; |
| 331 | $xml_array[] = '<key type="string">' . $_REQUEST['placement_slug'] . '</key>'; |
| 332 | $xml_array[] = '<use_existing type="boolean">1</use_existing>'; |
| 333 | $xml_array[] = '</item>'; |
| 334 | $xml_array[] = '</placements>'; |
| 335 | |
| 336 | $xml = '<advads-export>' . implode( '', $xml_array ) . '</advads-export>'; |
| 337 | |
| 338 | Advanced_Ads_Import::get_instance()->import( $xml ); |
| 339 | if ( count( Advanced_Ads_Import::get_instance()->imported_data['placements'] ) ) { |
| 340 | // if the ad was assigned |
| 341 | echo $_REQUEST['placement_slug']; |
| 342 | }; |
| 343 | die(); |
| 344 | } |
| 345 | |
| 346 | // create new placement |
| 347 | $placements = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array(); |
| 348 | |
| 349 | $type = esc_attr( $_REQUEST['placement_type'] ); |
| 350 | |
| 351 | $item = 'ad_' . $ad_id; |
| 352 | |
| 353 | $options = array(); |
| 354 | |
| 355 | // check type |
| 356 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 357 | if( ! isset( $placement_types[ $type ] ) ){ |
| 358 | die(); |
| 359 | } |
| 360 | |
| 361 | $title = $placement_types[ $type ]['title']; |
| 362 | |
| 363 | $new_placement = array( |
| 364 | 'type' => $type, |
| 365 | 'item' => $item, |
| 366 | 'name' => $title, |
| 367 | ); |
| 368 | |
| 369 | // set content specific options |
| 370 | if( 'post_content' === $type ){ |
| 371 | $index = isset( $_REQUEST['options']['index'] ) ? absint( $_REQUEST['options']['index'] ) : 1; |
| 372 | $new_placement['options'] = array( |
| 373 | 'position' => 'after', |
| 374 | 'index' => $index, |
| 375 | 'tag' => 'p' |
| 376 | ); |
| 377 | } |
| 378 | |
| 379 | $slug = Advanced_Ads_Placements::save_new_placement( $new_placement ); |
| 380 | // return potential slug |
| 381 | echo $slug; |
| 382 | |
| 383 | die(); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * save ad wizard state for each user individually |
| 388 | * |
| 389 | * @since 1.7.4 |
| 390 | */ |
| 391 | public function save_wizard_state(){ |
| 392 | |
| 393 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 394 | |
| 395 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | $state = ( isset( $_REQUEST['hide_wizard'] ) && 'true' === $_REQUEST['hide_wizard'] ) ? 'true' : 'false'; |
| 400 | |
| 401 | // get current user |
| 402 | $user_id = get_current_user_id(); |
| 403 | if( ! $user_id ) { |
| 404 | die(); |
| 405 | } |
| 406 | |
| 407 | update_user_meta( $user_id, 'advanced-ads-hide-wizard', $state ); |
| 408 | |
| 409 | die(); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Enable Adsense Auto ads, previously "Page-Level ads" |
| 414 | */ |
| 415 | public function adsense_enable_pla(){ |
| 416 | |
| 417 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 418 | |
| 419 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | $options = get_option( GADSENSE_OPT_NAME, array() ); |
| 424 | $options['page-level-enabled'] = true; |
| 425 | update_option( GADSENSE_OPT_NAME, $options ); |
| 426 | die(); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Display list of Ad Health notices |
| 431 | */ |
| 432 | public function ad_health_notice_display(){ |
| 433 | |
| 434 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 435 | |
| 436 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | Advanced_Ads_Ad_Health_Notices::get_instance()->render_widget(); |
| 441 | die(); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Push an Ad Health notice to the queue |
| 446 | */ |
| 447 | public function ad_health_notice_push(){ |
| 448 | |
| 449 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 450 | |
| 451 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | $key = ( !empty( $_REQUEST['key'] ) ) ? esc_attr( $_REQUEST['key'] ) : false; |
| 456 | $attr = ( !empty( $_REQUEST['attr'] ) && is_array( $_REQUEST['attr'] ) ) ? $_REQUEST['attr'] : array(); |
| 457 | |
| 458 | // update or new entry? |
| 459 | if( isset( $attr['mode'] ) && 'update' === $attr['mode'] ){ |
| 460 | Advanced_Ads_Ad_Health_Notices::get_instance()->update( $key, $attr ); |
| 461 | } else { |
| 462 | Advanced_Ads_Ad_Health_Notices::get_instance()->add( $key, $attr ); |
| 463 | } |
| 464 | |
| 465 | die(); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Hide Ad Health notice |
| 470 | */ |
| 471 | public function ad_health_notice_hide(){ |
| 472 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 473 | |
| 474 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | $notice_key = ( !empty( $_REQUEST['notice'] ) ) ? esc_attr( $_REQUEST['notice'] ) : false; |
| 479 | |
| 480 | Advanced_Ads_Ad_Health_Notices::get_instance()->hide( $notice_key ); |
| 481 | die(); |
| 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Show all ignored notices of a given type |
| 486 | */ |
| 487 | public function ad_health_notice_unignore(){ |
| 488 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 489 | |
| 490 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | // $notice_key = ( !empty( $_REQUEST['type'] ) ) ? esc_attr( $_REQUEST['type'] ) : false; |
| 495 | |
| 496 | // Advanced_Ads_Ad_Health_Notices::get_instance()->unignore_by_type( $notice_key ); |
| 497 | Advanced_Ads_Ad_Health_Notices::get_instance()->unignore(); |
| 498 | die(); |
| 499 | } |
| 500 | } |
| 501 |