| 1 |
<?php |
| 2 |
/** |
| 3 |
* Author: Alin Marcu |
| 4 |
* Author URI: https://deconf.com |
| 5 |
* Copyright 2013 Alin Marcu |
| 6 |
* License: GPLv2 or later |
| 7 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( ! defined( 'ABSPATH' ) ) |
| 12 |
exit(); |
| 13 |
|
| 14 |
if ( ! class_exists( 'CAWP_Backend_Ajax' ) ) { |
| 15 |
|
| 16 |
final class CAWP_Backend_Ajax { |
| 17 |
|
| 18 |
private $cawp; |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
$this->cawp = CAWP(); |
| 22 |
|
| 23 |
if ( CAWP_Tools::check_roles( $this->cawp->config->options['access_back'] ) && ( ( 1 == $this->cawp->config->options['backend_item_reports'] ) || ( 1 == $this->cawp->config->options['dashboard_widget'] ) ) ) { |
| 24 |
/** |
| 25 |
* Items action |
| 26 |
*/ |
| 27 |
add_action( 'wp_ajax_cawp_backend_item_reports', array( $this, 'ajax_item_reports' ) ); |
| 28 |
} |
| 29 |
if ( current_user_can( 'manage_options' ) ) { |
| 30 |
/** |
| 31 |
* Admin Widget action |
| 32 |
*/ |
| 33 |
add_action( 'wp_ajax_cawp_dismiss_notices', array( $this, 'ajax_dismiss_notices' ) ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Ajax handler for Item Reports |
| 39 |
* |
| 40 |
* @return JsonSerializable|int |
| 41 |
*/ |
| 42 |
public function ajax_item_reports() { |
| 43 |
if ( ! isset( $_POST['cawp_security_backend_item_reports'] ) || ! wp_verify_nonce( $_POST['cawp_security_backend_item_reports'], 'cawp_backend_item_reports' ) ) { |
| 44 |
wp_die( 630 ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( isset( $_POST['projectId'] ) && 'false' !== $_POST['projectId'] ) { |
| 48 |
$projectId = sanitize_text_field( $_POST['projectId'] ); |
| 49 |
} else { |
| 50 |
$projectId = false; |
| 51 |
} |
| 52 |
$from = sanitize_option( 'date_format', $_POST['from'] ); |
| 53 |
$to = sanitize_option( 'date_format', $_POST['to'] ); |
| 54 |
$query = sanitize_text_field( $_POST['query'] ); |
| 55 |
if ( isset( $_POST['filter'] ) ) { |
| 56 |
$filter_id = (int) $_POST['filter']; |
| 57 |
} else { |
| 58 |
$filter_id = false; |
| 59 |
} |
| 60 |
if ( isset( $_POST['metric'] ) ) { |
| 61 |
$metric = sanitize_text_field( $_POST['metric'] ); |
| 62 |
} else { |
| 63 |
$metric = 'visitors'; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ob_get_length() ) { |
| 67 |
ob_clean(); |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! ( CAWP_Tools::check_roles( $this->cawp->config->options['access_back'] ) && ( ( 1 == $this->cawp->config->options['backend_item_reports'] ) || ( 1 == $this->cawp->config->options['dashboard_widget'] ) ) ) ) { |
| 71 |
wp_die( 631 ); |
| 72 |
} |
| 73 |
if ( $this->cawp->config->options['sitekey'] && $this->cawp->config->options['siteid'] && $from && $to ) { |
| 74 |
if ( null === $this->cawp->capi_controller ) { |
| 75 |
$this->cawp->capi_controller = new CAWP_CAPI_Controller(); |
| 76 |
} |
| 77 |
} else { |
| 78 |
wp_die( 624 ); |
| 79 |
} |
| 80 |
if ( false == $projectId ) { |
| 81 |
$projectId = $this->cawp->config->options['siteid']; |
| 82 |
} |
| 83 |
|
| 84 |
$this->cawp->capi_controller->timeshift = (int) current_time( 'timestamp' ) - time(); |
| 85 |
|
| 86 |
if ( $filter_id ) { |
| 87 |
$uri = get_permalink( $filter_id ); |
| 88 |
/** |
| 89 |
* allow URL correction before sending an API request |
| 90 |
*/ |
| 91 |
$filter = apply_filters( 'cawp_backenditem_uri', $uri, $filter_id ); |
| 92 |
} else { |
| 93 |
$filter = false; |
| 94 |
} |
| 95 |
|
| 96 |
$queries = explode( ',', $query ); |
| 97 |
|
| 98 |
$results = array(); |
| 99 |
|
| 100 |
foreach ( $queries as $value ) { |
| 101 |
$results[] = $this->cawp->capi_controller->get( $projectId, $value, $from, $to, $filter, $metric ); |
| 102 |
} |
| 103 |
|
| 104 |
wp_send_json( $results ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Ajax handler for dismissing Admin notices |
| 109 |
*/ |
| 110 |
public function ajax_dismiss_notices() { |
| 111 |
if ( ! isset( $_POST['cawp_security_dismiss_notices'] ) || ! wp_verify_nonce( $_POST['cawp_security_dismiss_notices'], 'cawp_dismiss_notices' ) ) { |
| 112 |
wp_die( 630 ); |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 116 |
wp_die( 631 ); |
| 117 |
} |
| 118 |
|
| 119 |
delete_option( 'cawp_got_updated' ); |
| 120 |
|
| 121 |
wp_die(); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|