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.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Provide public ajax interface. |
| 5 | * |
| 6 | * @since 1.5.0 |
| 7 | */ |
| 8 | class Advanced_Ads_Ajax { |
| 9 | |
| 10 | private function __construct() |
| 11 | { |
| 12 | add_action( 'wp_ajax_advads_ad_select', array( $this, 'advads_ajax_ad_select' ) ); |
| 13 | add_action( 'wp_ajax_nopriv_advads_ad_select', array( $this, 'advads_ajax_ad_select' ) ); |
| 14 | add_action( 'wp_ajax_advads-ad-health-notice-push', array( $this, 'ad_health_notice_push' ) ); |
| 15 | add_action( 'wp_ajax_nopriv_advads-ad-health-notice-push', array( $this, 'ad_health_notice_push' ) ); |
| 16 | } |
| 17 | |
| 18 | private static $instance; |
| 19 | |
| 20 | public static function get_instance() |
| 21 | { |
| 22 | if ( ! isset(self::$instance) ) { |
| 23 | self::$instance = new self; |
| 24 | } |
| 25 | |
| 26 | return self::$instance; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Simple wp ajax interface for ad selection. |
| 31 | */ |
| 32 | public function advads_ajax_ad_select() { |
| 33 | // set proper header |
| 34 | header( 'Content-Type: application/json; charset: utf-8' ); |
| 35 | |
| 36 | // allow modules / add ons to test (this is rather late but should happen before anything important is called) |
| 37 | do_action( 'advanced-ads-ajax-ad-select-init' ); |
| 38 | |
| 39 | $adIds = isset( $_REQUEST['ad_ids'] ) ? $_REQUEST['ad_ids'] : null; |
| 40 | if ( is_string( $adIds ) ) { |
| 41 | $adIds = json_decode( $adIds, true ); |
| 42 | } |
| 43 | if (is_array($adIds)) { // ads loaded previously and passed by query |
| 44 | Advanced_Ads::get_instance()->current_ads += $adIds; |
| 45 | } |
| 46 | |
| 47 | $deferedAds = isset( $_REQUEST['deferedAds'] ) ? $_REQUEST['deferedAds'] : null; |
| 48 | if ( $deferedAds ) { // load all ajax ads with a single request |
| 49 | $response = array(); |
| 50 | |
| 51 | $requests_by_blog = array(); |
| 52 | foreach ( (array) $deferedAds as $request ) { |
| 53 | $blog_id = isset( $request['blog_id'] ) ? $request['blog_id'] : get_current_blog_id(); |
| 54 | $requests_by_blog[ $blog_id ][] = $request; |
| 55 | } |
| 56 | foreach ( $requests_by_blog as $blog_id => $requests ) { |
| 57 | if ( $blog_id !== get_current_blog_id() ) { Advanced_Ads::get_instance()->switch_to_blog( $blog_id ); } |
| 58 | |
| 59 | foreach ( $requests as $request ) { |
| 60 | $result = $this->select_one( $request ); |
| 61 | $result['elementId'] = ! empty( $request['elementId'] ) ? $request['elementId'] : null; |
| 62 | $response[] = $result; |
| 63 | } |
| 64 | |
| 65 | if ( $blog_id !== get_current_blog_id() ) { Advanced_Ads::get_instance()->restore_current_blog(); } |
| 66 | } |
| 67 | |
| 68 | echo json_encode( $response ); |
| 69 | die(); |
| 70 | } |
| 71 | |
| 72 | $response = $this->select_one( $_REQUEST ); |
| 73 | echo json_encode( $response ); |
| 74 | die(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Provides a single ad (ad, group, placement) given ID and selection method. |
| 79 | * |
| 80 | * @param $request array |
| 81 | */ |
| 82 | private function select_one( $request ) { |
| 83 | // init handlers |
| 84 | $selector = Advanced_Ads_Select::get_instance(); |
| 85 | $methods = $selector->get_methods(); |
| 86 | $method = isset( $request['ad_method'] ) ? (string) $request['ad_method'] : null; |
| 87 | $id = isset( $request['ad_id'] ) ? (string) $request['ad_id'] : null; |
| 88 | $arguments = isset( $request['ad_args'] ) ? $request['ad_args'] : array(); |
| 89 | if (is_string($arguments)) { |
| 90 | $arguments = stripslashes($arguments); |
| 91 | $arguments = json_decode($arguments, true); |
| 92 | } |
| 93 | if ( ! empty( $request['elementId'] ) ) { |
| 94 | $arguments['cache_busting_elementid'] = $request['elementId']; |
| 95 | } |
| 96 | |
| 97 | $response = array(); |
| 98 | if ( isset( $methods[ $method ] ) && isset( $id ) ) { |
| 99 | $advads = Advanced_Ads::get_instance(); |
| 100 | $l = count( $advads->current_ads ); |
| 101 | |
| 102 | // build content |
| 103 | $content = $selector->get_ad_by_method( $id, $method, $arguments ); |
| 104 | $adIds = array_slice( $advads->current_ads, $l ); // ads loaded by this request |
| 105 | |
| 106 | return array( 'status' => 'success', 'item' => $content, 'id' => $id, 'method' => $method, 'ads' => $adIds, 'blog_id' => get_current_blog_id() ); |
| 107 | } else { |
| 108 | // report error |
| 109 | return array( 'status' => 'error', 'message' => 'No valid ID or METHOD found.' ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Push an Ad Health notice to the queue in the backend |
| 115 | */ |
| 116 | public function ad_health_notice_push(){ |
| 117 | |
| 118 | check_ajax_referer( 'advanced-ads-ad-health-ajax-nonce', 'nonce' ); |
| 119 | |
| 120 | if ( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | $key = ( !empty( $_REQUEST['key'] ) ) ? esc_attr( $_REQUEST['key'] ) : false; |
| 125 | $attr = ( !empty( $_REQUEST['attr'] ) && is_array( $_REQUEST['attr'] ) ) ? $_REQUEST['attr'] : array(); |
| 126 | |
| 127 | // update or new entry? |
| 128 | if( isset( $attr['mode'] ) && 'update' === $attr['mode'] ){ |
| 129 | Advanced_Ads_Ad_Health_Notices::get_instance()->update( $key, $attr ); |
| 130 | } else { |
| 131 | Advanced_Ads_Ad_Health_Notices::get_instance()->add( $key, $attr ); |
| 132 | } |
| 133 | |
| 134 | die(); |
| 135 | } |
| 136 | } |
| 137 |