| 1 |
<?php //phpcs:ignore |
| 2 |
|
| 3 |
/** |
| 4 |
* Analytics REST API — Intent Endpoints |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @subpackage \Rest\Analytics |
| 8 |
* @since 1.1.13 |
| 9 |
* @category Rest |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\Rest\Analytics; |
| 13 |
|
| 14 |
use Disco\App\Analytics\Controllers\IntentPerformanceController; |
| 15 |
use WP_REST_Server; |
| 16 |
|
| 17 |
/** |
| 18 |
* Class IntentApi |
| 19 |
* |
| 20 |
* Registers and handles the following REST API route: |
| 21 |
* |
| 22 |
* - GET /disco/v1/analytics/intents-performance — Intent-wise performance (no comparison). |
| 23 |
* |
| 24 |
* @package Disco |
| 25 |
* @subpackage \Rest\Analytics |
| 26 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 27 |
* @link https://webappick.com |
| 28 |
* @license https://opensource.org/licenses/gpl-license.php GNU Public License |
| 29 |
* @category Rest |
| 30 |
*/ |
| 31 |
class IntentApi extends Base { |
| 32 |
|
| 33 |
/** |
| 34 |
* Registers the REST API routes for intent endpoints. |
| 35 |
*/ |
| 36 |
public function register_routes(): void { |
| 37 |
// GET /analytics/intents-performance |
| 38 |
register_rest_route( |
| 39 |
$this->namespace, |
| 40 |
'/' . $this->rest_base . '/intents-performance', |
| 41 |
array( |
| 42 |
array( |
| 43 |
'methods' => WP_REST_Server::READABLE, |
| 44 |
'callback' => array( $this, 'get_intents_performance' ), |
| 45 |
'permission_callback' => array( $this, 'permissions_check' ), |
| 46 |
'args' => $this->get_date_params(), |
| 47 |
), |
| 48 |
'schema' => array( $this, 'get_intent_performance_schema' ), |
| 49 |
) |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
// ========================================================================= |
| 54 |
// Callbacks |
| 55 |
// ========================================================================= |
| 56 |
|
| 57 |
/** |
| 58 |
* GET /disco/v1/analytics/intents-performance |
| 59 |
* |
| 60 |
* Returns intent-wise disco sales for the current period only. |
| 61 |
* Data items: intent, sales, orders, percentage. |
| 62 |
* No comparison period — use /intents for growth deltas. |
| 63 |
* |
| 64 |
* Query params: |
| 65 |
* date_from (string Y-m-d) Start of range. Default: 30 days ago. |
| 66 |
* date_to (string Y-m-d) End of range. Default: today. |
| 67 |
* |
| 68 |
* @param \WP_REST_Request $request Incoming REST request. |
| 69 |
* @return \WP_REST_Response|\WP_Error |
| 70 |
*/ |
| 71 |
public function get_intents_performance( $request ) { |
| 72 |
$error = $this->validate_date_params( $request ); |
| 73 |
|
| 74 |
if ( is_wp_error( $error ) ) { |
| 75 |
return $error; |
| 76 |
} |
| 77 |
|
| 78 |
$args = array( |
| 79 |
'date_from' => is_string( $request['date_from'] ) ? sanitize_text_field( $request['date_from'] ) : '', |
| 80 |
'date_to' => is_string( $request['date_to'] ) ? sanitize_text_field( $request['date_to'] ) : '', |
| 81 |
); |
| 82 |
|
| 83 |
return rest_ensure_response( IntentPerformanceController::get_intent_performance( $args ) ); |
| 84 |
} |
| 85 |
|
| 86 |
// ========================================================================= |
| 87 |
// Schemas |
| 88 |
// ========================================================================= |
| 89 |
|
| 90 |
/** |
| 91 |
* Retrieves the intent-performance schema, conforming to JSON Schema. |
| 92 |
* |
| 93 |
* @return array |
| 94 |
*/ |
| 95 |
public function get_intent_performance_schema() { //phpcs:ignore |
| 96 |
return array( |
| 97 |
'$schema' => 'http://json-schema.org/draft-04/schema#', |
| 98 |
'title' => 'analytics-intents-performance', |
| 99 |
'type' => 'object', |
| 100 |
'properties' => array( |
| 101 |
'current_period' => array( |
| 102 |
'description' => __( 'Current period date range.', 'disco' ), |
| 103 |
'type' => 'object', |
| 104 |
'context' => array( 'view' ), |
| 105 |
'readonly' => true, |
| 106 |
), |
| 107 |
'compare_period' => array( |
| 108 |
'description' => __( 'Comparison period date range.', 'disco' ), |
| 109 |
'type' => 'object', |
| 110 |
'context' => array( 'view' ), |
| 111 |
'readonly' => true, |
| 112 |
), |
| 113 |
'total_revenue' => array( |
| 114 |
'description' => __( 'Total revenue across all intents.', 'disco' ), |
| 115 |
'type' => 'number', |
| 116 |
'context' => array( 'view' ), |
| 117 |
'readonly' => true, |
| 118 |
), |
| 119 |
'data' => array( |
| 120 |
'description' => __( 'Intent-wise sales performance.', 'disco' ), |
| 121 |
'type' => 'array', |
| 122 |
'context' => array( 'view' ), |
| 123 |
'readonly' => true, |
| 124 |
'items' => array( |
| 125 |
'type' => 'object', |
| 126 |
'properties' => array( |
| 127 |
'intent' => array( |
| 128 |
'description' => __( 'Campaign intent label, or "Others" for deleted campaigns.', 'disco' ), |
| 129 |
'type' => 'string', |
| 130 |
), |
| 131 |
'revenue' => array( |
| 132 |
'description' => __( 'Total order revenue for this intent.', 'disco' ), |
| 133 |
'type' => 'number', |
| 134 |
), |
| 135 |
'orders' => array( |
| 136 |
'description' => __( 'Count of completed orders for this intent.', 'disco' ), |
| 137 |
'type' => 'integer', |
| 138 |
), |
| 139 |
'percentage' => array( |
| 140 |
'description' => __( 'Share of total disco revenue (0–100).', 'disco' ), |
| 141 |
'type' => 'number', |
| 142 |
), |
| 143 |
), |
| 144 |
), |
| 145 |
), |
| 146 |
), |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|