| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX\Core\Rest; |
| 4 |
|
| 5 |
use NotificationX\Admin\Settings; |
| 6 |
use NotificationX\Core\Analytics as CoreAnalytics; |
| 7 |
use NotificationX\GetInstance; |
| 8 |
use NotificationX\NotificationX; |
| 9 |
use WP_REST_Controller; |
| 10 |
use WP_REST_Response; |
| 11 |
use WP_REST_Server; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* @method static Analytics get_instance($args = null) |
| 16 |
*/ |
| 17 |
class Analytics { |
| 18 |
/** |
| 19 |
* Instance of Analytics |
| 20 |
* |
| 21 |
* @var Analytics |
| 22 |
*/ |
| 23 |
use GetInstance; |
| 24 |
|
| 25 |
/** |
| 26 |
* Post type. |
| 27 |
* |
| 28 |
* @since 4.7.0 |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $post_type; |
| 32 |
public $namespace; |
| 33 |
public $rest_base; |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor. |
| 37 |
* |
| 38 |
* @since 4.7.0 |
| 39 |
* |
| 40 |
* @param string $post_type Post type. |
| 41 |
*/ |
| 42 |
public function __construct() { |
| 43 |
$this->namespace = 'notificationx/v1'; |
| 44 |
$this->rest_base = 'analytics'; |
| 45 |
add_action('rest_api_init', [$this, 'register_routes']); |
| 46 |
} |
| 47 |
|
| 48 |
public function get_rest_url(){ |
| 49 |
return rest_url($this->namespace . '/' . $this->rest_base); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Registers the routes for the objects of the controller. |
| 54 |
* |
| 55 |
* @since 4.7.0 |
| 56 |
* |
| 57 |
* @see register_rest_route() |
| 58 |
*/ |
| 59 |
public function register_routes() { |
| 60 |
register_rest_route( |
| 61 |
$this->namespace, |
| 62 |
'/' . $this->rest_base, |
| 63 |
// For Frontend analytics |
| 64 |
array( |
| 65 |
'methods' => WP_REST_Server::EDITABLE, |
| 66 |
'callback' => array($this, 'insert_analytics'), |
| 67 |
'permission_callback' => [$this, 'can_insert_analytics'], |
| 68 |
'args' => array( |
| 69 |
'nx_id' => array( |
| 70 |
'required' => true, |
| 71 |
'description' => __( 'Unique identifier for the object.', 'notificationx' ), |
| 72 |
'type' => 'integer', |
| 73 |
), |
| 74 |
'type' => array( |
| 75 |
'required' => false, |
| 76 |
'description' => __( 'Click or View', 'notificationx' ), |
| 77 |
'type' => 'string', |
| 78 |
), |
| 79 |
), |
| 80 |
) |
| 81 |
); |
| 82 |
register_rest_route( |
| 83 |
$this->namespace, |
| 84 |
"/{$this->rest_base}/get", |
| 85 |
// For backend analytics |
| 86 |
array( |
| 87 |
'methods' => WP_REST_Server::EDITABLE, |
| 88 |
'callback' => array( $this, 'get_analytics' ), |
| 89 |
// maybe use. |
| 90 |
'permission_callback' => [ $this, 'can_read_analytics' ], |
| 91 |
'args' => array( |
| 92 |
'startDate' => array( |
| 93 |
'required' => true, |
| 94 |
'description' => __( 'Start of the date range.', 'notificationx' ), |
| 95 |
'type' => 'string', |
| 96 |
), |
| 97 |
'endDate' => array( |
| 98 |
'required' => true, |
| 99 |
'description' => __( 'End of the date range.', 'notificationx' ), |
| 100 |
'type' => 'string', |
| 101 |
), |
| 102 |
), |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
public function can_read_analytics( $request ) { |
| 109 |
return current_user_can('read_notificationx_analytics') && Settings::get_instance()->get('settings.enable_analytics', true); |
| 110 |
} |
| 111 |
|
| 112 |
public function can_insert_analytics( $request ) { |
| 113 |
return Settings::get_instance()->get('settings.enable_analytics', true); |
| 114 |
} |
| 115 |
|
| 116 |
public function get_analytics($request){ |
| 117 |
$params = $request->get_params(); |
| 118 |
$result = CoreAnalytics::get_instance()->get_stats($params); |
| 119 |
wp_send_json($result); |
| 120 |
} |
| 121 |
|
| 122 |
public function insert_analytics($request){ |
| 123 |
$params = $request->get_params(); |
| 124 |
$type = !empty( $params['type'] ) && in_array( $params['type'], ['clicks', 'views', 'ctr'] ) ? esc_sql( $params['type'] ) : 'clicks'; |
| 125 |
$result = CoreAnalytics::get_instance()->insert_analytics( absint( $params['nx_id'] ), $type ); |
| 126 |
return ['success' => true]; |
| 127 |
} |
| 128 |
} |
| 129 |
|