ads
3 months ago
groups
3 months ago
metaboxes
1 year ago
pages
3 months ago
placements
2 months ago
class-action-links.php
1 year ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
3 months ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
3 months ago
class-authors.php
1 year ago
class-compatibility.php
1 year ago
class-edd-updater.php
3 months ago
class-list-filters.php
2 months ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 year ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
3 months ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-ajax.php
919 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AJAX Ads |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use Advanced_Ads_Pro; |
| 13 | use Advanced_Ads_Privacy; |
| 14 | use AdvancedAds\Constants; |
| 15 | use AdvancedAds\Abstracts\Ad; |
| 16 | use Advanced_Ads_Admin_Notices; |
| 17 | use AdvancedAds\Frontend\Stats; |
| 18 | use Advanced_Ads_Admin_Licenses; |
| 19 | use Advanced_Ads_Ad_Blocker_Admin; |
| 20 | use Advanced_Ads_Ad_Health_Notices; |
| 21 | use Advanced_Ads_Display_Conditions; |
| 22 | use Advanced_Ads_Visitor_Conditions; |
| 23 | use AdvancedAds\Utilities\Conditional; |
| 24 | use AdvancedAds\Framework\Utilities\Arr; |
| 25 | use AdvancedAds\Framework\Utilities\Params; |
| 26 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 27 | |
| 28 | defined( 'ABSPATH' ) || exit; |
| 29 | |
| 30 | /** |
| 31 | * Frontend AJAX. |
| 32 | */ |
| 33 | class AJAX implements Integration_Interface { |
| 34 | |
| 35 | /** |
| 36 | * Hook into WordPress. |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function hooks(): void { |
| 41 | add_action( 'wp_ajax_advads_ad_select', [ $this, 'ad_select' ] ); |
| 42 | add_action( 'wp_ajax_nopriv_advads_ad_select', [ $this, 'ad_select' ] ); |
| 43 | add_action( 'wp_ajax_advads-ad-health-notice-push', [ $this, 'ad_health_notice_push' ] ); |
| 44 | add_action( 'wp_ajax_nopriv_advads-ad-health-notice-push', [ $this, 'ad_health_notice_push' ] ); |
| 45 | add_action( 'wp_ajax_advads_dismiss_welcome', [ $this, 'dismiss_welcome' ] ); |
| 46 | add_action( 'wp_ajax_advads_newsletter', [ $this, 'subscribe_to_newsletter' ] ); |
| 47 | add_action( 'wp_ajax_advads_activate_addon', [ $this, 'activate_add_on' ] ); |
| 48 | add_action( 'wp_ajax_advads-multiple-subscribe', [ $this, 'multiple_subscribe' ] ); |
| 49 | |
| 50 | add_action( 'wp_ajax_load_ad_parameters_metabox', [ $this, 'load_ad_parameters_metabox' ] ); |
| 51 | add_action( 'wp_ajax_load_visitor_conditions_metabox', [ $this, 'load_visitor_condition' ] ); |
| 52 | add_action( 'wp_ajax_load_display_conditions_metabox', [ $this, 'load_display_condition' ] ); |
| 53 | add_action( 'wp_ajax_advads-terms-search', [ $this, 'search_terms' ] ); |
| 54 | add_action( 'wp_ajax_advads-authors-search', [ $this, 'search_authors' ] ); |
| 55 | add_action( 'wp_ajax_advads-close-notice', [ $this, 'close_notice' ] ); |
| 56 | add_action( 'wp_ajax_advads-hide-notice', [ $this, 'hide_notice' ] ); |
| 57 | add_action( 'wp_ajax_advads-subscribe-notice', [ $this, 'subscribe' ] ); |
| 58 | add_action( 'wp_ajax_advads-activate-license', [ $this, 'activate_license' ] ); |
| 59 | add_action( 'wp_ajax_advads-deactivate-license', [ $this, 'deactivate_license' ] ); |
| 60 | add_action( 'wp_ajax_advads-adblock-rebuild-assets', [ $this, 'adblock_rebuild_assets' ] ); |
| 61 | add_action( 'wp_ajax_advads-post-search', [ $this, 'post_search' ] ); |
| 62 | add_action( 'wp_ajax_advads-ad-injection-content', [ $this, 'inject_placement' ] ); |
| 63 | add_action( 'wp_ajax_advads-save-hide-wizard-state', [ $this, 'save_wizard_state' ] ); |
| 64 | add_action( 'wp_ajax_advads-adsense-enable-pla', [ $this, 'adsense_enable_pla' ] ); |
| 65 | add_action( 'wp_ajax_advads-ad-health-notice-display', [ $this, 'ad_health_notice_display' ] ); |
| 66 | add_action( 'wp_ajax_advads-ad-health-notice-push-adminui', [ $this, 'ad_health_notice_push_adminui' ] ); |
| 67 | add_action( 'wp_ajax_advads-ad-health-notice-hide', [ $this, 'ad_health_notice_hide' ] ); |
| 68 | add_action( 'wp_ajax_advads-ad-health-notice-unignore', [ $this, 'ad_health_notice_unignore' ] ); |
| 69 | add_action( 'wp_ajax_advads-ad-health-notice-solved', [ $this, 'ad_health_notice_solved' ] ); |
| 70 | add_action( 'wp_ajax_advads-update-frontend-element', [ $this, 'update_frontend_element' ] ); |
| 71 | add_action( 'wp_ajax_advads-get-block-hints', [ $this, 'get_block_hints' ] ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Background plugin activation from the add-on box |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function activate_add_on(): void { |
| 80 | wp_ajax_activate_plugin(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Subscribe to the newsletter |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function subscribe_to_newsletter(): void { |
| 89 | if ( ! wp_verify_nonce( sanitize_text_field( Params::post( 'nonce' ), '' ), 'advads-newsletter-subscribe' ) ) { |
| 90 | wp_send_json_error( 'Not Authorized', 401 ); |
| 91 | } |
| 92 | if ( ! Conditional::user_can( 'advanced_ads_see_interface' ) ) { |
| 93 | wp_send_json_error( |
| 94 | [ |
| 95 | /* translators: %s is a URL. */ |
| 96 | 'message' => sprintf( __( 'An error occurred. Please use <a href="%s" target="_blank">this form</a> to sign up.', 'advanced-ads' ), 'http://eepurl.com/bk4z4P' ), |
| 97 | ], |
| 98 | 403 |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | wp_send_json_success( \Advanced_Ads_Admin_Notices::get_instance()->subscribe( 'nl_free_addons' ), 200 ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Stop showing the welcome after a click on the dismiss icon |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public function dismiss_welcome(): void { |
| 111 | Welcome::get()->dismiss(); |
| 112 | wp_send_json_success( 'OK', 200 ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Simple wp ajax interface for ad selection. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function ad_select(): void { |
| 121 | add_filter( 'advanced-ads-output-inline-css', '__return_false' ); |
| 122 | |
| 123 | // Allow modules / add-ons to test (this is rather late but should happen before anything important is called). |
| 124 | do_action( 'advanced-ads-ajax-ad-select-init' ); |
| 125 | |
| 126 | $ad_ids = Params::request( 'ad_ids', [], FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY ); |
| 127 | $defered_ads = Params::request( 'deferedAds', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 128 | |
| 129 | if ( is_array( $ad_ids ) ) { |
| 130 | foreach ( $ad_ids as $ad_id ) { |
| 131 | Stats::get()->add_entity( 'ad', is_array( $ad_id ) ? $ad_id['id'] : $ad_id, '' ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if ( $defered_ads ) { |
| 136 | $response = []; |
| 137 | |
| 138 | $requests_by_blog = []; |
| 139 | foreach ( $defered_ads as $request ) { |
| 140 | $blog_id = $request['blog_id'] ?? get_current_blog_id(); |
| 141 | $requests_by_blog[ $blog_id ][] = $request; |
| 142 | } |
| 143 | |
| 144 | foreach ( $requests_by_blog as $blog_id => $requests ) { |
| 145 | if ( get_current_blog_id() !== $blog_id && is_multisite() ) { |
| 146 | switch_to_blog( $blog_id ); |
| 147 | } |
| 148 | |
| 149 | foreach ( $requests as $request ) { |
| 150 | $result = $this->select_one( $request ); |
| 151 | $result['elementId'] = $request['elementId'] ?? null; |
| 152 | $response[] = $result; |
| 153 | } |
| 154 | |
| 155 | if ( get_current_blog_id() !== $blog_id && is_multisite() ) { |
| 156 | restore_current_blog(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | wp_send_json( $response ); |
| 161 | } |
| 162 | |
| 163 | $response = $this->select_one( $_REQUEST ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 164 | wp_send_json( $response ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Push an Ad Health notice to the queue in the backend |
| 169 | * |
| 170 | * @return void |
| 171 | */ |
| 172 | public function ad_health_notice_push(): void { |
| 173 | check_ajax_referer( 'advanced-ads-ad-health-ajax-nonce', 'nonce' ); |
| 174 | |
| 175 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | $key = ! empty( $_REQUEST['key'] ) ? esc_attr( Params::request( 'key' ) ) : false; |
| 180 | $attr = Params::request( 'attr', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 181 | |
| 182 | // Update or new entry? |
| 183 | if ( isset( $attr['mode'] ) && 'update' === $attr['mode'] ) { |
| 184 | Advanced_Ads_Ad_Health_Notices::get_instance()->update( $key, $attr ); |
| 185 | } else { |
| 186 | Advanced_Ads_Ad_Health_Notices::get_instance()->add( $key, $attr ); |
| 187 | } |
| 188 | |
| 189 | die(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Check if AJAX ad can be displayed, with consent information sent in request. |
| 194 | * |
| 195 | * @param bool $can_display Whether this ad can be displayed. |
| 196 | * @param Ad $ad The ad object. |
| 197 | * |
| 198 | * @return bool |
| 199 | */ |
| 200 | public function can_display_by_consent( $can_display, $ad ) { |
| 201 | |
| 202 | // Early bail!! |
| 203 | if ( ! $can_display ) { |
| 204 | return $can_display; |
| 205 | } |
| 206 | |
| 207 | // If consent is overridden for the ad. |
| 208 | $privacy_props = $ad->get_prop( 'privacy' ); |
| 209 | if ( ! empty( $privacy_props['ignore-consent'] ) ) { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | // If privacy module is not active, we can display. |
| 214 | if ( empty( Advanced_Ads_Privacy::get_instance()->options()['enabled'] ) ) { |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | $consent_state = Params::request( 'consent', 'not_allowed' ); |
| 219 | |
| 220 | // Consent is either given or not needed. |
| 221 | if ( in_array( $consent_state, [ 'not_needed', 'accepted' ], true ) ) { |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | if ( 'not_allowed' === $consent_state ) { |
| 226 | // Allow image ads without custom code. |
| 227 | if ( $ad->get_type() === 'image' ) { |
| 228 | $has_custom_code = class_exists( 'Advanced_Ads_Pro' ) |
| 229 | && ! empty( Advanced_Ads_Pro::get_instance()->get_custom_code( $ad ) ); |
| 230 | |
| 231 | if ( ! $has_custom_code ) { |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // If there is custom code, don't display the ad (unless it's a group). |
| 240 | if ( |
| 241 | class_exists( 'Advanced_Ads_Pro' ) && |
| 242 | ! empty( Advanced_Ads_Pro::get_instance()->get_custom_code( $ad ) ) && |
| 243 | ! $ad->is_type( 'group' ) |
| 244 | ) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | if ( $can_display && $ad->get_type() === 'adsense' ) { |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | // See if this ad type needs consent. |
| 253 | return ! Advanced_Ads_Privacy::get_instance()->ad_type_needs_consent( $ad->get_type() ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Subscribe to multiple newsletters |
| 258 | */ |
| 259 | public function multiple_subscribe() { |
| 260 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 261 | |
| 262 | $groups = ! empty( Params::post( 'groups' ) ) ? json_decode( Params::post( 'groups' ), true ) : []; |
| 263 | |
| 264 | if ( ! Conditional::user_can( 'advanced_ads_see_interface' ) || empty( $groups ) ) { |
| 265 | wp_send_json_error( |
| 266 | [ |
| 267 | /* translators: %s is a URL. */ |
| 268 | 'message' => sprintf( __( 'An error occurred. Please use <a href="%s" target="_blank">this form</a> to sign up.', 'advanced-ads' ), 'http://eepurl.com/bk4z4P' ), |
| 269 | ], |
| 270 | 400 |
| 271 | ); |
| 272 | } |
| 273 | |
| 274 | foreach ( $groups as $group ) { |
| 275 | $message = Advanced_Ads_Admin_Notices::get_instance()->subscribe( $group ); |
| 276 | } |
| 277 | |
| 278 | wp_send_json_success( [ 'message' => $message ?? '' ] ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Provides a single ad (ad, group, placement) given ID and selection method. |
| 283 | * |
| 284 | * @param array $request Request. |
| 285 | * |
| 286 | * @return array |
| 287 | */ |
| 288 | private function select_one( $request ) { |
| 289 | $method = (string) $request['ad_method'] ?? null; |
| 290 | if ( 'id' === $method ) { |
| 291 | $method = 'ad'; |
| 292 | } |
| 293 | |
| 294 | // Early bail!! |
| 295 | if ( ! Conditional::is_entity_allowed( $method ) ) { |
| 296 | return [ |
| 297 | 'status' => 'error', |
| 298 | 'message' => __( 'The method is not allowed to render.', 'advanced-ads' ), |
| 299 | ]; |
| 300 | } |
| 301 | |
| 302 | $function = "get_the_$method"; |
| 303 | $id = (string) $request['ad_id'] ?? null; |
| 304 | $arguments = $request['ad_args'] ?? []; |
| 305 | |
| 306 | if ( is_string( $arguments ) ) { |
| 307 | $arguments = stripslashes( $arguments ); |
| 308 | $arguments = json_decode( $arguments, true ); |
| 309 | } |
| 310 | |
| 311 | if ( ! empty( $request['elementId'] ) ) { |
| 312 | $arguments['cache_busting_elementid'] = $request['elementId']; |
| 313 | } |
| 314 | |
| 315 | // Report error. |
| 316 | if ( empty( $id ) || ! function_exists( $function ) ) { |
| 317 | return [ |
| 318 | 'status' => 'error', |
| 319 | 'message' => 'No valid ID or METHOD found.', |
| 320 | ]; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Filters the received arguments before passing them to ads/groups/placements. |
| 325 | * |
| 326 | * @param array $arguments Existing arguments. |
| 327 | * @param array $request Request data. |
| 328 | */ |
| 329 | $arguments = apply_filters( 'advanced-ads-ajax-ad-select-arguments', $arguments, $request ); |
| 330 | $previous_ads = Stats::get()->entities; |
| 331 | add_filter( 'advanced-ads-can-display-ad', [ $this, 'can_display_by_consent' ], 10, 2 ); |
| 332 | $content = $function( (int) $id, '', $arguments ); |
| 333 | |
| 334 | if ( empty( $content ) ) { |
| 335 | return [ |
| 336 | 'status' => 'error', |
| 337 | 'message' => 'No displayable ad found for privacy settings.', |
| 338 | ]; |
| 339 | } |
| 340 | |
| 341 | $response = [ |
| 342 | 'status' => 'success', |
| 343 | 'item' => $content, |
| 344 | 'id' => $id, |
| 345 | 'method' => $method, |
| 346 | 'ads' => array_slice( Stats::get()->entities, count( $previous_ads ) ), |
| 347 | 'blog_id' => get_current_blog_id(), |
| 348 | ]; |
| 349 | |
| 350 | return apply_filters( |
| 351 | 'advanced-ads-cache-busting-item', |
| 352 | $response, |
| 353 | [ |
| 354 | 'id' => $id, |
| 355 | 'method' => $method, |
| 356 | 'args' => $arguments, |
| 357 | ] |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Load content of the ad parameter metabox |
| 363 | * |
| 364 | * @since 1.0.0 |
| 365 | */ |
| 366 | public function load_ad_parameters_metabox() { |
| 367 | |
| 368 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 369 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | $type_string = Params::post( 'ad_type' ); |
| 374 | $ad_id = Params::post( 'ad_id', 0, FILTER_VALIDATE_INT ); |
| 375 | if ( empty( $ad_id ) ) { |
| 376 | die(); |
| 377 | } |
| 378 | |
| 379 | if ( wp_advads_has_ad_type( $type_string ) ) { |
| 380 | $ad = wp_advads_get_ad( $ad_id, $type_string ); |
| 381 | $ad_type = wp_advads_get_ad_type( $type_string ); |
| 382 | if ( method_exists( $ad_type, 'render_parameters' ) ) { |
| 383 | $ad_type->render_parameters( $ad ); |
| 384 | } |
| 385 | |
| 386 | if ( $ad_type->has_size() ) { |
| 387 | include ADVADS_ABSPATH . 'views/admin/metaboxes/ads/ad-parameters-size.php'; |
| 388 | } |
| 389 | |
| 390 | // Extend the AJAX-loaded parameters form by ad type. |
| 391 | do_action( "advanced-ads-ad-params-after-{$ad->get_type()}", $ad ); |
| 392 | } |
| 393 | |
| 394 | die(); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Load interface for single visitor condition |
| 399 | * |
| 400 | * @since 1.5.4 |
| 401 | */ |
| 402 | public function load_visitor_condition() { |
| 403 | |
| 404 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 405 | |
| 406 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | // get visitor condition types. |
| 411 | $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; |
| 412 | $condition = []; |
| 413 | $condition['type'] = Params::post( 'type', '' ); |
| 414 | $index = Params::post( 'index', 0, FILTER_VALIDATE_INT ); |
| 415 | |
| 416 | $form_name = Params::post( 'form_name', Advanced_Ads_Visitor_Conditions::FORM_NAME ); |
| 417 | |
| 418 | if ( ! isset( $visitor_conditions[ $condition['type'] ] ) ) { |
| 419 | die(); |
| 420 | } |
| 421 | |
| 422 | $metabox = $visitor_conditions[ $condition['type'] ]['metabox']; |
| 423 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 424 | call_user_func( [ $metabox[0], $metabox[1] ], $condition, $index, $form_name ); |
| 425 | } |
| 426 | |
| 427 | die(); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Load interface for single display condition |
| 432 | * |
| 433 | * @since 1.7 |
| 434 | */ |
| 435 | public function load_display_condition() { |
| 436 | |
| 437 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 438 | |
| 439 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | // get display condition types. |
| 444 | $conditions = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 445 | $condition = []; |
| 446 | $condition['type'] = Params::post( 'type', '' ); |
| 447 | $index = Params::post( 'index', 0, FILTER_VALIDATE_INT ); |
| 448 | $form_name = Params::post( 'form_name', Advanced_Ads_Display_Conditions::FORM_NAME ); |
| 449 | |
| 450 | if ( ! isset( $conditions[ $condition['type'] ] ) ) { |
| 451 | die(); |
| 452 | } |
| 453 | |
| 454 | $metabox = $conditions[ $condition['type'] ]['metabox']; |
| 455 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 456 | call_user_func( [ $metabox[0], $metabox[1] ], $condition, $index, $form_name ); |
| 457 | } |
| 458 | |
| 459 | die(); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Search terms belonging to a specific taxonomy |
| 464 | * |
| 465 | * @since 1.4.7 |
| 466 | */ |
| 467 | public function search_terms() { |
| 468 | |
| 469 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 470 | |
| 471 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | $args = [ |
| 476 | 'taxonomy' => Params::post( 'tax', '' ), |
| 477 | 'hide_empty' => false, |
| 478 | 'number' => 20, |
| 479 | ]; |
| 480 | |
| 481 | $search = Params::post( 'search', '' ); |
| 482 | if ( '' === $search ) { |
| 483 | die(); |
| 484 | } |
| 485 | |
| 486 | // if search is an id, search for the term id, else do a full text search. |
| 487 | if ( 0 !== absint( $search ) && strlen( $search ) === strlen( absint( $search ) ) ) { |
| 488 | $args['include'] = [ absint( $search ) ]; |
| 489 | } else { |
| 490 | $args['search'] = $search; |
| 491 | } |
| 492 | |
| 493 | $results = get_terms( $args ); |
| 494 | echo wp_json_encode( $results ); |
| 495 | echo "\n"; |
| 496 | die(); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Search authors |
| 501 | * |
| 502 | * @since 1.47.5 |
| 503 | */ |
| 504 | public function search_authors() { |
| 505 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 506 | |
| 507 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | $args['search_columns'] = [ 'ID', 'user_login', 'user_nicename', 'display_name' ]; |
| 512 | |
| 513 | if ( version_compare( get_bloginfo( 'version' ), '5.9' ) > -1 ) { |
| 514 | $args['capability'] = [ 'edit_posts' ]; |
| 515 | } else { |
| 516 | $args['who'] = 'authors'; |
| 517 | } |
| 518 | |
| 519 | $search = Params::post( 'search', '' ); |
| 520 | if ( '' === $search ) { |
| 521 | die(); |
| 522 | } |
| 523 | |
| 524 | $args['search'] = '*' . sanitize_text_field( wp_unslash( $search ) ) . '*'; |
| 525 | |
| 526 | $results = get_users( $args ); |
| 527 | |
| 528 | echo wp_json_encode( $results ); |
| 529 | die(); |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Close a notice for good |
| 534 | * |
| 535 | * @since 1.5.3 |
| 536 | */ |
| 537 | public function close_notice() { |
| 538 | |
| 539 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 540 | $notice = Params::request( 'notice' ); |
| 541 | |
| 542 | if ( |
| 543 | ! Conditional::user_can( 'advanced_ads_manage_options' ) |
| 544 | || empty( $notice ) |
| 545 | ) { |
| 546 | die(); |
| 547 | } |
| 548 | |
| 549 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( $notice ); |
| 550 | |
| 551 | // permanent dismissed. |
| 552 | if ( 'monetize_wizard' === Params::request( 'notice' ) ) { |
| 553 | update_user_meta( get_current_user_id(), Constants::USER_WIZARD_DISMISS, true ); |
| 554 | } |
| 555 | |
| 556 | $redirect = Params::request( 'redirect' ); |
| 557 | if ( $redirect && wp_safe_redirect( $redirect ) ) { |
| 558 | exit(); |
| 559 | } |
| 560 | |
| 561 | die(); |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * Hide a notice for some time (7 days right now) |
| 566 | * |
| 567 | * @since 1.8.17 |
| 568 | */ |
| 569 | public function hide_notice() { |
| 570 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 571 | $notice = Params::request( 'notice' ); |
| 572 | |
| 573 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) |
| 574 | || empty( $notice ) |
| 575 | ) { |
| 576 | die(); |
| 577 | } |
| 578 | |
| 579 | Advanced_Ads_Admin_Notices::get_instance()->hide_notice( $notice ); |
| 580 | die(); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Subscribe to newsletter |
| 585 | * |
| 586 | * @since 1.5.3 |
| 587 | */ |
| 588 | public function subscribe() { |
| 589 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 590 | $notice = Params::request( 'notice' ); |
| 591 | |
| 592 | if ( |
| 593 | ! Conditional::user_can( 'advanced_ads_see_interface' ) |
| 594 | || empty( $notice ) |
| 595 | ) { |
| 596 | wp_send_json_error( |
| 597 | [ |
| 598 | /* translators: %s is a URL. */ |
| 599 | 'message' => sprintf( __( 'An error occurred. Please use <a href="%s" target="_blank">this form</a> to sign up.', 'advanced-ads' ), 'http://eepurl.com/bk4z4P' ), |
| 600 | ], |
| 601 | 400 |
| 602 | ); |
| 603 | } |
| 604 | |
| 605 | wp_send_json_success( [ 'message' => Advanced_Ads_Admin_Notices::get_instance()->subscribe( $notice ) ] ); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Activate license of an add-on |
| 610 | * |
| 611 | * @since 1.5.7 |
| 612 | */ |
| 613 | public function activate_license() { |
| 614 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 619 | |
| 620 | $addon = Params::post( 'addon' ); |
| 621 | if ( '' === $addon ) { |
| 622 | die(); |
| 623 | } |
| 624 | |
| 625 | // phpcs:disable |
| 626 | echo Advanced_Ads_Admin_Licenses::get_instance()->activate_license( |
| 627 | $addon, |
| 628 | Params::post( 'pluginname' ), |
| 629 | Params::post( 'optionslug' ), |
| 630 | Params::post( 'license' ) |
| 631 | ); |
| 632 | // phpcs:enable |
| 633 | |
| 634 | die(); |
| 635 | } |
| 636 | |
| 637 | /** |
| 638 | * Deactivate license of an add-on |
| 639 | * |
| 640 | * @since 1.6.11 |
| 641 | */ |
| 642 | public function deactivate_license() { |
| 643 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 648 | |
| 649 | $addon = Params::post( 'addon' ); |
| 650 | if ( '' === $addon ) { |
| 651 | die(); |
| 652 | } |
| 653 | |
| 654 | // phpcs:disable |
| 655 | echo Advanced_Ads_Admin_Licenses::get_instance()->deactivate_license( |
| 656 | $addon, |
| 657 | Params::post( 'pluginname' ), |
| 658 | Params::post( 'optionslug' ) |
| 659 | ); |
| 660 | // phpcs:enable |
| 661 | |
| 662 | die(); |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Rebuild assets for ad-blocker module |
| 667 | */ |
| 668 | public function adblock_rebuild_assets() { |
| 669 | |
| 670 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 671 | |
| 672 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | Advanced_Ads_Ad_Blocker_Admin::get_instance()->add_asset_rebuild_form(); |
| 677 | die(); |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Post search (used in Display conditions) |
| 682 | */ |
| 683 | public function post_search() { |
| 684 | |
| 685 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 686 | |
| 687 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 688 | return; |
| 689 | } |
| 690 | |
| 691 | add_filter( 'wp_link_query_args', [ 'Advanced_Ads_Display_Conditions', 'modify_post_search' ] ); |
| 692 | add_filter( 'posts_search', [ 'Advanced_Ads_Display_Conditions', 'modify_post_search_sql' ] ); |
| 693 | |
| 694 | wp_ajax_wp_link_ajax(); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Inject an ad and a placement |
| 699 | * |
| 700 | * @since 1.7.3 |
| 701 | */ |
| 702 | public function inject_placement() { |
| 703 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 704 | |
| 705 | $ad_id = Params::request( 'ad_id', 0, FILTER_VALIDATE_INT ); |
| 706 | |
| 707 | // Early bail!! |
| 708 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) || ! $ad_id ) { |
| 709 | die(); |
| 710 | } |
| 711 | |
| 712 | // use existing placement. |
| 713 | $placement_id = Params::request( 'placement_id', 0, FILTER_VALIDATE_INT ); |
| 714 | if ( $placement_id ) { |
| 715 | $placement = wp_advads_get_placement( $placement_id ); |
| 716 | |
| 717 | if ( $placement ) { |
| 718 | $current_item = $placement->get_item(); |
| 719 | // Check if current item is a group and new item is an ad. |
| 720 | if ( is_string( $current_item ) && strpos( $current_item, 'group_' ) === 0 ) { |
| 721 | $group = wp_advads_get_group( (int) str_replace( 'group_', '', $current_item ) ); |
| 722 | if ( $group ) { |
| 723 | $ad_weights = $group->get_ad_weights(); |
| 724 | $ad_weights[ $ad_id ] = Constants::GROUP_AD_DEFAULT_WEIGHT; |
| 725 | $group->set_ad_weights( $ad_weights ); |
| 726 | $group->save(); |
| 727 | } |
| 728 | } else { |
| 729 | $placement->set_item( 'ad_' . $ad_id ); |
| 730 | $placement->save(); |
| 731 | } |
| 732 | echo esc_attr( $placement_id ); |
| 733 | } |
| 734 | |
| 735 | die(); |
| 736 | } |
| 737 | |
| 738 | $type = esc_attr( Params::request( 'placement_type' ) ); |
| 739 | if ( ! wp_advads_has_placement_type( $type ) ) { |
| 740 | die(); |
| 741 | } |
| 742 | |
| 743 | $new_placement = wp_advads_create_new_placement( $type ); |
| 744 | |
| 745 | $props = [ |
| 746 | 'item' => 'ad_' . $ad_id, |
| 747 | 'title' => wp_advads_get_placement_type( $type )->get_title(), |
| 748 | ]; |
| 749 | |
| 750 | // set content specific options. |
| 751 | if ( $new_placement->is_type( 'post_content' ) ) { |
| 752 | $options = Params::request( 'options', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 753 | $index = (int) Arr::get( $options, 'index', 1 ); |
| 754 | $props['position'] = 'after'; |
| 755 | $props['index'] = $index; |
| 756 | $props['tag'] = 'p'; |
| 757 | } |
| 758 | |
| 759 | $new_placement->set_props( $props ); |
| 760 | echo $new_placement->save();; // phpcs:ignore |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Save ad wizard state for each user individually |
| 765 | * |
| 766 | * @since 1.7.4 |
| 767 | */ |
| 768 | public function save_wizard_state() { |
| 769 | |
| 770 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 771 | |
| 772 | if ( ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | $user_id = get_current_user_id(); |
| 777 | |
| 778 | if ( ! $user_id ) { |
| 779 | die(); |
| 780 | } |
| 781 | |
| 782 | $state = 'true' === Params::request( 'hideWizard' ) ? 'true' : 'false'; |
| 783 | update_user_meta( $user_id, 'advanced-ads-hide-wizard', $state ); |
| 784 | |
| 785 | die(); |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * Enable Adsense Auto ads, previously "Page-Level ads" |
| 790 | */ |
| 791 | public function adsense_enable_pla() { |
| 792 | |
| 793 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 794 | |
| 795 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | $options = get_option( GADSENSE_OPT_NAME, [] ); |
| 800 | $options['page-level-enabled'] = true; |
| 801 | update_option( GADSENSE_OPT_NAME, $options ); |
| 802 | die(); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * Display list of Ad Health notices |
| 807 | */ |
| 808 | public function ad_health_notice_display() { |
| 809 | |
| 810 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 811 | |
| 812 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 813 | return; |
| 814 | } |
| 815 | |
| 816 | Advanced_Ads_Ad_Health_Notices::get_instance()->render_widget(); |
| 817 | die(); |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Push an Ad Health notice to the queue |
| 822 | */ |
| 823 | public function ad_health_notice_push_adminui() { |
| 824 | |
| 825 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 826 | |
| 827 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | $key = Params::request( 'key' ); |
| 832 | $attr = Params::request( 'attr', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 833 | $attr = ! empty( $attr ) && is_array( $attr ) ? $attr : []; |
| 834 | |
| 835 | // update or new entry? |
| 836 | if ( isset( $attr['mode'] ) && 'update' === $attr['mode'] ) { |
| 837 | Advanced_Ads_Ad_Health_Notices::get_instance()->update( $key, $attr ); |
| 838 | } else { |
| 839 | Advanced_Ads_Ad_Health_Notices::get_instance()->add( $key, $attr ); |
| 840 | } |
| 841 | |
| 842 | die(); |
| 843 | } |
| 844 | |
| 845 | /** |
| 846 | * Hide Ad Health notice |
| 847 | */ |
| 848 | public function ad_health_notice_hide() { |
| 849 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 850 | |
| 851 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | $notice = Params::request( 'notice', '' ); |
| 856 | $notice_key = ! empty( $notice ) ? esc_attr( $notice ) : false; |
| 857 | |
| 858 | Advanced_Ads_Ad_Health_Notices::get_instance()->hide( $notice_key ); |
| 859 | die(); |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * Show all ignored notices of a given type |
| 864 | */ |
| 865 | public function ad_health_notice_unignore() { |
| 866 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 867 | |
| 868 | if ( ! Conditional::user_can( 'advanced_ads_manage_options' ) ) { |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | Advanced_Ads_Ad_Health_Notices::get_instance()->unignore(); |
| 873 | die(); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * After the user has selected a new frontend element, update the corresponding placement. |
| 878 | */ |
| 879 | public function update_frontend_element() { |
| 880 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 881 | |
| 882 | if ( ! Conditional::user_can( 'advanced_ads_manage_placements' ) ) { |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | $return = wp_update_post( $_POST ); |
| 887 | |
| 888 | if ( is_wp_error( $return ) ) { |
| 889 | wp_send_json_error( [ 'error' => $return->get_error_message() ], 400 ); |
| 890 | } |
| 891 | |
| 892 | wp_send_json_success( [ 'id' => $return ] ); |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Get hints related to the Gutenberg block. |
| 897 | */ |
| 898 | public function get_block_hints() { |
| 899 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 900 | |
| 901 | $item = Params::post( 'itemID' ); |
| 902 | if ( ! $item || ! Conditional::user_can( 'advanced_ads_edit_ads' ) ) { |
| 903 | die; |
| 904 | } |
| 905 | |
| 906 | $item = explode( '_', $item ); |
| 907 | if ( ! isset( $item[0] ) || 'group' !== $item[0] ) { |
| 908 | die; |
| 909 | } |
| 910 | |
| 911 | $group = wp_advads_get_group( absint( $item[1] ) ); |
| 912 | if ( ! $group ) { |
| 913 | die; |
| 914 | } |
| 915 | |
| 916 | wp_send_json_success( $group->get_hints() ); |
| 917 | } |
| 918 | } |
| 919 |