namespace, '/wc/analytics', [ 'methods' => \WP_REST_Server::CREATABLE, 'callback' => [ $this, 'record_event' ], 'permission_callback' => function ( WP_REST_Request $request ) { // Rate limit the public analytics endpoint. $policy = new \Aisentic\Public\Policy(); $settings = new \Aisentic\Api\Models\Settings(); $rate_limit = $settings->get( 'general.rateLimitPerMinute', 20 ); // Namespace the identifier so analytics traffic gets its own // rate-limit bucket and cannot consume the chat endpoint's budget. $identifier = 'wc_analytics_' . $policy->getRequestIdentifier(); if ( $policy->isRateLimited( $identifier, $rate_limit * 5 ) ) { // 5x default — analytics is chatty. return new \WP_Error( 'aisentic_analytics_rate_limited', __( 'Too many requests.', 'aisentic' ), [ 'status' => 429 ] ); } // Validate session_id format. $session_id = $request->get_param( 'session_id' ); if ( ! is_string( $session_id ) || strlen( $session_id ) > 64 || ! preg_match( '/^[a-zA-Z0-9_\-]+$/', $session_id ) ) { return new \WP_Error( 'aisentic_invalid_session', __( 'Invalid session id.', 'aisentic' ), [ 'status' => 400 ] ); } return true; }, 'args' => [ 'event' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], 'session_id' => [ 'required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ], 'product_id' => [ 'required' => false, 'type' => 'integer', ], 'variation_id' => [ 'required' => false, 'type' => 'integer', ], 'quantity' => [ 'required' => false, 'type' => 'integer', ], // total + currency are derived server-side (see record_event), never // accepted from the client. product_id/quantity are still // client-provided, so figures are best-effort — quantity is clamped // in record_event to bound abuse of this public endpoint. ], ] ); // GET /aisentic/v1/wc/analytics/summary — get aggregated stats. register_rest_route( $this->namespace, '/wc/analytics/summary', [ 'methods' => \WP_REST_Server::READABLE, 'callback' => [ $this, 'get_summary' ], 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'args' => [ 'days' => [ 'required' => false, 'type' => 'integer', 'default' => 7, ], ], ] ); } /** * Record an analytics event. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function record_event( WP_REST_Request $request ): WP_REST_Response { $event = $request->get_param( 'event' ); $session_id = $request->get_param( 'session_id' ); $product_id = $request->get_param( 'product_id' ); $variation_id = $request->get_param( 'variation_id' ); $quantity = $request->get_param( 'quantity' ); // Never trust client-supplied money. Derive the line total server-side from // the product's display price, and only for the cart_added event. For a // variable product the resolved variation carries the real price, so prefer // it over the parent. Quantity is clamped so a fabricated event cannot // record an absurd total. $total = null; $currency = null; if ( 'cart_added' === $event && $product_id && function_exists( 'wc_get_product' ) ) { $product = wc_get_product( (int) ( $variation_id ? $variation_id : $product_id ) ); if ( $product ) { $qty = min( max( 1, (int) $quantity ), 100 ); $total = (float) wc_get_price_to_display( $product ) * $qty; $currency = get_woocommerce_currency(); } } $repo = new Wc_Analytics_Repository(); $result = $repo->record( $event, $session_id, $product_id, $quantity, $total, $currency ); return new WP_REST_Response( [ 'success' => $result ], $result ? 200 : 400 ); } /** * Get aggregated analytics summary. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public function get_summary( WP_REST_Request $request ): WP_REST_Response { $repo = new Wc_Analytics_Repository(); $days = max( 1, min( 365, (int) $request->get_param( 'days' ) ) ); $to = current_time( 'mysql' ); $from = gmdate( 'Y-m-d H:i:s', strtotime( "-{$days} days", strtotime( $to ) ) ); $summary = $repo->summary( $from, $to ); return new WP_REST_Response( [ 'success' => true, 'summary' => $summary, ] ); } }