| 1 |
<?php |
| 2 |
/** |
| 3 |
* Analytify REST Bootstrap Trait |
| 4 |
* |
| 5 |
* This trait contains the REST API bootstrap functionality for the Analytify plugin. |
| 6 |
* It handles REST API initialization, request handling, and permission checking. |
| 7 |
* |
| 8 |
* @package WP_Analytify |
| 9 |
* @subpackage REST |
| 10 |
* @since 8.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; } |
| 15 |
|
| 16 |
trait Analytify_Rest_Bootstrap { |
| 17 |
/** |
| 18 |
* Instance of the class. |
| 19 |
* |
| 20 |
* @var self|null |
| 21 |
*/ |
| 22 |
private static $instance; |
| 23 |
|
| 24 |
/** |
| 25 |
* WP Analytify instance. |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
private $wp_analytify; |
| 30 |
|
| 31 |
/** |
| 32 |
* Google Analytics mode. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
private $ga_mode; |
| 37 |
|
| 38 |
/** |
| 39 |
* Start date for analytics. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
private $start_date; |
| 44 |
|
| 45 |
/** |
| 46 |
* End date for analytics. |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
private $end_date; |
| 51 |
|
| 52 |
/** |
| 53 |
* Date difference in days. |
| 54 |
* |
| 55 |
* @var int |
| 56 |
*/ |
| 57 |
private $date_differ; |
| 58 |
|
| 59 |
/** |
| 60 |
* Post ID. |
| 61 |
* |
| 62 |
* @var int |
| 63 |
*/ |
| 64 |
private $post_id; |
| 65 |
|
| 66 |
/** |
| 67 |
* Compare start date. |
| 68 |
* |
| 69 |
* @var string|null |
| 70 |
*/ |
| 71 |
private $compare_start_date = null; |
| 72 |
|
| 73 |
/** |
| 74 |
* Compare end date. |
| 75 |
* |
| 76 |
* @var string|null |
| 77 |
*/ |
| 78 |
private $compare_end_date = null; |
| 79 |
|
| 80 |
/** |
| 81 |
* Compare days. |
| 82 |
* |
| 83 |
* @var int|null |
| 84 |
*/ |
| 85 |
private $compare_days = null; |
| 86 |
|
| 87 |
/** |
| 88 |
* Get instance of the class. |
| 89 |
* |
| 90 |
* @return self |
| 91 |
*/ |
| 92 |
public static function get_instance() { |
| 93 |
if ( empty( self::$instance ) ) { |
| 94 |
self::$instance = new self(); |
| 95 |
} |
| 96 |
return self::$instance; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Constructor. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
private function __construct() { |
| 105 |
add_action( 'rest_api_init', array( $this, 'analytify_rest_api_init' ) ); |
| 106 |
add_filter( 'analytify_general_stats_footer', array( $this, 'general_stats_footer' ), 10, 2 ); |
| 107 |
add_filter( 'analytify_single_post_sections', array( $this, 'single_post_sections' ), 10, 3 ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Initialize REST API. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function analytify_rest_api_init() { |
| 116 |
$this->wp_analytify = $GLOBALS['WP_ANALYTIFY']; |
| 117 |
$this->ga_mode = WPANALYTIFY_Utils::get_ga_mode(); |
| 118 |
register_rest_route( |
| 119 |
'wp-analytify/v1', |
| 120 |
'/get_report/(?P<request_type>[a-zA-Z0-9-]+)', |
| 121 |
array( |
| 122 |
array( |
| 123 |
'methods' => WP_REST_Server::READABLE, |
| 124 |
'callback' => array( $this, 'handle_request' ), |
| 125 |
'permission_callback' => array( $this, 'permission_check' ), |
| 126 |
), |
| 127 |
) |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Check permissions for REST API requests. |
| 133 |
* |
| 134 |
* @param WP_REST_Request $data The REST request object. |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public function permission_check( $data ) { |
| 138 |
$route = $data->get_route(); |
| 139 |
if ( strpos( $route, 'single-post-stats' ) !== false ) { |
| 140 |
$is_access_level = $this->wp_analytify->settings->get_option( 'show_analytics_roles_back_end', 'wp-analytify-admin', array( 'administrator' ) ); |
| 141 |
return (bool) $this->wp_analytify->pa_check_roles( $is_access_level ); |
| 142 |
} |
| 143 |
$is_access_level = $this->wp_analytify->settings->get_option( 'show_analytics_roles_dashboard', 'wp-analytify-dashboard', array( 'administrator' ) ); |
| 144 |
return (bool) $this->wp_analytify->pa_check_roles( $is_access_level ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Handle REST API requests. |
| 149 |
* |
| 150 |
* @param WP_REST_Request $request The REST request object. |
| 151 |
* @return WP_REST_Response|WP_Error |
| 152 |
*/ |
| 153 |
public function handle_request( WP_REST_Request $request ) { |
| 154 |
$request_type = $request->get_param( 'request_type' ); |
| 155 |
$this->start_date = $request->get_param( 'sd' ) ? $request->get_param( 'sd' ) : wp_date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- current_time is acceptable for date calculations |
| 156 |
$this->end_date = $request->get_param( 'ed' ) ? $request->get_param( 'ed' ) : wp_date( 'Y-m-d', current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested -- current_time is acceptable for date calculations |
| 157 |
$this->date_differ = $request->get_param( 'd_diff' ); |
| 158 |
$this->post_id = $request->get_param( 'post_id' ); |
| 159 |
switch ( $request_type ) { |
| 160 |
case 'general-stats': |
| 161 |
return new WP_REST_Response( $this->general_stats() ); |
| 162 |
case 'top-pages-stats': |
| 163 |
return new WP_REST_Response( $this->top_pages_stats() ); |
| 164 |
case 'geographic-stats': |
| 165 |
return new WP_REST_Response( $this->geographic_stats() ); |
| 166 |
case 'system-stats': |
| 167 |
return new WP_REST_Response( $this->system_stats() ); |
| 168 |
case 'keyword-stats': |
| 169 |
return new WP_REST_Response( $this->keyword_stats() ); |
| 170 |
case 'social-stats': |
| 171 |
return new WP_REST_Response( $this->social_stats() ); |
| 172 |
case 'referer-stats': |
| 173 |
return new WP_REST_Response( $this->get_referer_stats() ); |
| 174 |
case 'what-is-happening-stats': |
| 175 |
return new WP_REST_Response( $this->get_what_is_happening_stats() ); |
| 176 |
case 'single-post-stats': |
| 177 |
return new WP_REST_Response( $this->get_single_post_stats() ); |
| 178 |
} |
| 179 |
return new WP_Error( 'analytify_invalid_endpoint', esc_html__( 'Invalid endpoint.', 'wp-analytify' ), array( 'status' => 404 ) ); |
| 180 |
} |
| 181 |
} |
| 182 |
|