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
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_ajax_callbacks.php
423 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 | |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * load content of the ad parameter metabox |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | public function load_ad_parameters_metabox() { |
| 48 | |
| 49 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 50 | |
| 51 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | $types = Advanced_Ads::get_instance()->ad_types; |
| 56 | $type_string = $_REQUEST['ad_type']; |
| 57 | $ad_id = absint( $_REQUEST['ad_id'] ); |
| 58 | if ( empty($ad_id) ) { die(); } |
| 59 | |
| 60 | $ad = new Advanced_Ads_Ad( $ad_id ); |
| 61 | |
| 62 | if ( ! empty($types[$type_string]) && method_exists( $types[$type_string], 'render_parameters' ) ) { |
| 63 | $type = $types[ $type_string ]; |
| 64 | $type->render_parameters( $ad ); |
| 65 | |
| 66 | if( 'dummy' !== $type_string ) : |
| 67 | include ADVADS_BASE_PATH . 'admin/views/ad-parameters-size.php'; |
| 68 | endif; |
| 69 | } |
| 70 | |
| 71 | die(); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * load interface for single visitor condition |
| 77 | * |
| 78 | * @since 1.5.4 |
| 79 | */ |
| 80 | public function load_visitor_condition() { |
| 81 | |
| 82 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 83 | |
| 84 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // get visitor condition types |
| 89 | $visitor_conditions = Advanced_Ads_Visitor_Conditions::get_instance()->conditions; |
| 90 | $condition = array(); |
| 91 | $condition['type'] = isset( $_POST['type'] ) ? $_POST['type'] : ''; |
| 92 | $index = isset( $_POST['index'] ) ? $_POST['index'] : 0; |
| 93 | |
| 94 | if( isset( $visitor_conditions[$condition['type']] ) ) { |
| 95 | $metabox = $visitor_conditions[$condition['type']]['metabox']; |
| 96 | } else { |
| 97 | die(); |
| 98 | } |
| 99 | |
| 100 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 101 | call_user_func( array($metabox[0], $metabox[1]), $condition, $index ); |
| 102 | } |
| 103 | |
| 104 | die(); |
| 105 | } |
| 106 | /** |
| 107 | * load interface for single display condition |
| 108 | * |
| 109 | * @since 1.7 |
| 110 | */ |
| 111 | public function load_display_condition() { |
| 112 | |
| 113 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 114 | |
| 115 | if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | // get display condition types |
| 120 | $conditions = Advanced_Ads_Display_Conditions::get_instance()->conditions; |
| 121 | $condition = array(); |
| 122 | |
| 123 | $condition['type'] = isset( $_POST['type'] ) ? $_POST['type'] : ''; |
| 124 | |
| 125 | $index = isset( $_POST['index'] ) ? $_POST['index'] : 0; |
| 126 | |
| 127 | if( isset( $conditions[$condition['type']] ) ) { |
| 128 | $metabox = $conditions[$condition['type']]['metabox']; |
| 129 | } else { |
| 130 | die(); |
| 131 | } |
| 132 | |
| 133 | if ( method_exists( $metabox[0], $metabox[1] ) ) { |
| 134 | call_user_func( array($metabox[0], $metabox[1]), $condition, $index ); |
| 135 | } |
| 136 | |
| 137 | die(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * search terms belonging to a specific taxonomy |
| 142 | * |
| 143 | * @sinc 1.4.7 |
| 144 | */ |
| 145 | public function search_terms(){ |
| 146 | |
| 147 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 148 | |
| 149 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | $args = array(); |
| 154 | $taxonomy = $_POST['tax']; |
| 155 | $args = array('hide_empty' => false, 'number' => 20); |
| 156 | |
| 157 | if ( !isset( $_POST['search'] ) || $_POST['search'] === '' ) { die(); } |
| 158 | |
| 159 | // if search is an id, search for the term id, else do a full text search |
| 160 | if( 0 !== absint($_POST['search'] ) && strlen( $_POST['search'] ) == strlen ( absint($_POST['search'] ) ) ){ |
| 161 | $args['include'] = array(absint($_POST['search'])); |
| 162 | } else { |
| 163 | $args['search'] = $_POST['search']; |
| 164 | } |
| 165 | |
| 166 | $results = get_terms( $taxonomy, $args ); |
| 167 | // $results = _WP_Editors::wp_link_query( $args ); |
| 168 | echo wp_json_encode( $results ); |
| 169 | echo "\n"; |
| 170 | die(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * close a notice for good |
| 175 | * |
| 176 | * @since 1.5.3 |
| 177 | */ |
| 178 | public function close_notice(){ |
| 179 | |
| 180 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 181 | |
| 182 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) |
| 183 | || empty( $_POST['notice'] ) |
| 184 | ) { |
| 185 | die(); |
| 186 | } |
| 187 | |
| 188 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue($_POST['notice']); |
| 189 | die(); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * hide a notice for some time (7 days right now) |
| 194 | * |
| 195 | * @since 1.8.17 |
| 196 | */ |
| 197 | public function hide_notice(){ |
| 198 | |
| 199 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 200 | |
| 201 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) |
| 202 | || empty( $_POST['notice'] ) |
| 203 | ) { |
| 204 | die(); |
| 205 | } |
| 206 | |
| 207 | Advanced_Ads_Admin_Notices::get_instance()->hide_notice( $_POST['notice'] ); |
| 208 | die(); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * subscribe to newsletter |
| 213 | * |
| 214 | * @since 1.5.3 |
| 215 | */ |
| 216 | public function subscribe(){ |
| 217 | |
| 218 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 219 | |
| 220 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface') ) |
| 221 | || empty( $_POST['notice'] ) |
| 222 | ) { |
| 223 | die(); |
| 224 | } |
| 225 | |
| 226 | echo Advanced_Ads_Admin_Notices::get_instance()->subscribe($_POST['notice']); |
| 227 | die(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * activate license of an add-on |
| 232 | * |
| 233 | * @since 1.5.7 |
| 234 | */ |
| 235 | public function activate_license(){ |
| 236 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | // check nonce |
| 241 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 242 | |
| 243 | if ( !isset( $_POST['addon'] ) || $_POST['addon'] === '' ) { die(); } |
| 244 | |
| 245 | echo Advanced_Ads_Admin_Licenses::get_instance()->activate_license( $_POST['addon'], $_POST['pluginname'], $_POST['optionslug'], $_POST['license'] ); |
| 246 | |
| 247 | die(); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * deactivate license of an add-on |
| 252 | * |
| 253 | * @since 1.6.11 |
| 254 | */ |
| 255 | public function deactivate_license(){ |
| 256 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // check nonce |
| 261 | check_ajax_referer( 'advads_ajax_license_nonce', 'security' ); |
| 262 | |
| 263 | if ( !isset( $_POST['addon'] ) || $_POST['addon'] === '' ) { die(); } |
| 264 | |
| 265 | echo Advanced_Ads_Admin_Licenses::get_instance()->deactivate_license( $_POST['addon'], $_POST['pluginname'], $_POST['optionslug'] ); |
| 266 | |
| 267 | die(); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * rebuild assets for ad-blocker module |
| 272 | * |
| 273 | */ |
| 274 | public function adblock_rebuild_assets(){ |
| 275 | |
| 276 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 277 | |
| 278 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | Advanced_Ads_Ad_Blocker_Admin::get_instance()->add_asset_rebuild_form(); |
| 283 | die(); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * post search (used in Display conditions) |
| 288 | * |
| 289 | */ |
| 290 | public function post_search(){ |
| 291 | |
| 292 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 293 | |
| 294 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | add_filter( 'wp_link_query_args', array( 'Advanced_Ads_Display_Conditions', 'modify_post_search' ) ); |
| 299 | add_filter( 'posts_search', array( 'Advanced_Ads_Display_Conditions', 'modify_post_search_sql' ) ); |
| 300 | |
| 301 | wp_ajax_wp_link_ajax(); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * inject an ad and a placement |
| 306 | * |
| 307 | * @since 1.7.3 |
| 308 | */ |
| 309 | public function inject_placement(){ |
| 310 | |
| 311 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 312 | |
| 313 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 314 | die(); |
| 315 | } |
| 316 | |
| 317 | $ad_id = absint( $_REQUEST['ad_id'] ); |
| 318 | if ( empty( $ad_id ) ) { die(); } |
| 319 | |
| 320 | // use existing placement |
| 321 | if ( isset( $_REQUEST['placement_slug'] ) ) { |
| 322 | $xml_array[] = '<placements type="array">'; |
| 323 | $xml_array[] = '<item key="0" type="array">'; |
| 324 | $xml_array[] = '<item type="string">ad_' . $ad_id . '</item>'; |
| 325 | $xml_array[] = '<key type="string">' . $_REQUEST['placement_slug'] . '</key>'; |
| 326 | $xml_array[] = '<use_existing type="boolean">1</use_existing>'; |
| 327 | $xml_array[] = '</item>'; |
| 328 | $xml_array[] = '</placements>'; |
| 329 | |
| 330 | $xml = '<advads-export>' . implode( '', $xml_array ) . '</advads-export>'; |
| 331 | |
| 332 | Advanced_Ads_Import::get_instance()->import( $xml ); |
| 333 | if ( count( Advanced_Ads_Import::get_instance()->imported_data['placements'] ) ) { |
| 334 | // if the ad was assigned |
| 335 | echo $_REQUEST['placement_slug']; |
| 336 | }; |
| 337 | die(); |
| 338 | } |
| 339 | |
| 340 | // create new placement |
| 341 | $placements = Advanced_Ads::get_instance()->get_model()->get_ad_placements_array(); |
| 342 | |
| 343 | $type = esc_attr( $_REQUEST['placement_type'] ); |
| 344 | |
| 345 | $item = 'ad_' . $ad_id; |
| 346 | |
| 347 | $options = array(); |
| 348 | |
| 349 | // check type |
| 350 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 351 | if( ! isset( $placement_types[ $type ] ) ){ |
| 352 | die(); |
| 353 | } |
| 354 | |
| 355 | $title = $placement_types[ $type ]['title']; |
| 356 | |
| 357 | $new_placement = array( |
| 358 | 'type' => $type, |
| 359 | 'item' => $item, |
| 360 | 'name' => $title, |
| 361 | ); |
| 362 | |
| 363 | // set content specific options |
| 364 | if( 'post_content' === $type ){ |
| 365 | $index = isset( $_REQUEST['options']['index'] ) ? absint( $_REQUEST['options']['index'] ) : 1; |
| 366 | $new_placement['options'] = array( |
| 367 | 'position' => 'after', |
| 368 | 'index' => $index, |
| 369 | 'tag' => 'p' |
| 370 | ); |
| 371 | } |
| 372 | |
| 373 | $slug = Advanced_Ads_Placements::save_new_placement( $new_placement ); |
| 374 | // return potential slug |
| 375 | echo $slug; |
| 376 | |
| 377 | die(); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * save ad wizard state for each user individually |
| 382 | * |
| 383 | * @since 1.7.4 |
| 384 | */ |
| 385 | public function save_wizard_state(){ |
| 386 | |
| 387 | check_ajax_referer('advanced-ads-admin-ajax-nonce', 'nonce'); |
| 388 | |
| 389 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | $state = ( isset( $_REQUEST['hide_wizard'] ) && 'true' === $_REQUEST['hide_wizard'] ) ? 'true' : 'false'; |
| 394 | |
| 395 | // get current user |
| 396 | $user_id = get_current_user_id(); |
| 397 | if( ! $user_id ) { |
| 398 | die(); |
| 399 | } |
| 400 | |
| 401 | update_user_meta( $user_id, 'advanced-ads-hide-wizard', $state ); |
| 402 | |
| 403 | die(); |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Enable Adsense Auto ads, previously "Page-Level ads" |
| 408 | */ |
| 409 | public function adsense_enable_pla(){ |
| 410 | |
| 411 | check_ajax_referer( 'advanced-ads-admin-ajax-nonce', 'nonce' ); |
| 412 | |
| 413 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ) { |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | $options = get_option( GADSENSE_OPT_NAME, array() ); |
| 418 | $options['page-level-enabled'] = true; |
| 419 | update_option( GADSENSE_OPT_NAME, $options ); |
| 420 | die(); |
| 421 | } |
| 422 | } |
| 423 |