'POST', 'callback' => array( __CLASS__, 'ayudawp_track_click' ), 'permission_callback' => '__return_true', 'args' => array( 'platform' => array( 'type' => 'string', 'required' => true, 'sanitize_callback' => 'sanitize_text_field', ), 'post_id' => array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ), ), ) ); // Stats endpoint (admin only). register_rest_route( self::API_NAMESPACE, '/stats', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_stats' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => self::ayudawp_get_date_args(), ) ); // Timeline endpoint (admin only). register_rest_route( self::API_NAMESPACE, '/timeline', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_timeline' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => self::ayudawp_get_date_args(), ) ); // Timeline broken down by platform (admin only). register_rest_route( self::API_NAMESPACE, '/timeline-by-platform', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_timeline_by_platform' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => self::ayudawp_get_date_args(), ) ); // Top platforms endpoint (admin only). register_rest_route( self::API_NAMESPACE, '/platforms', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_platforms' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => array_merge( self::ayudawp_get_date_args(), self::ayudawp_get_pagination_args() ), ) ); // Top content endpoint (admin only). register_rest_route( self::API_NAMESPACE, '/content', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_content' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => array_merge( self::ayudawp_get_date_args(), self::ayudawp_get_pagination_args() ), ) ); // Export endpoint (admin only) - returns all data for the date range. register_rest_route( self::API_NAMESPACE, '/export', array( 'methods' => 'GET', 'callback' => array( __CLASS__, 'ayudawp_get_export' ), 'permission_callback' => array( __CLASS__, 'ayudawp_check_admin_permission' ), 'args' => self::ayudawp_get_date_args(), ) ); } /** * Check admin permission * * @return bool */ public static function ayudawp_check_admin_permission() { return current_user_can( 'manage_options' ); } /** * Build a REST response with no-cache headers * * Prevents caching plugins (SiteGround Speed Optimizer, etc.) * from serving stale analytics data on admin endpoints. * * @param mixed $data Response data. * @param int $status HTTP status code. * @return WP_REST_Response */ private static function ayudawp_nocache_response( $data, $status = 200 ) { $response = new WP_REST_Response( $data, $status ); $response->header( 'Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0' ); $response->header( 'Pragma', 'no-cache' ); $response->header( 'Expires', '0' ); return $response; } /** * Get date args definition * * @return array */ private static function ayudawp_get_date_args() { return array( 'days' => array( 'type' => 'integer', 'default' => 30, 'sanitize_callback' => 'absint', ), 'start_date' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), 'end_date' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field', ), ); } /** * Get pagination args definition * * @return array */ private static function ayudawp_get_pagination_args() { return array( 'limit' => array( 'type' => 'integer', 'default' => 10, 'sanitize_callback' => 'absint', ), 'offset' => array( 'type' => 'integer', 'default' => 0, 'sanitize_callback' => 'absint', ), ); } /** * Calculate date range from request params * * @param WP_REST_Request $request Request object. * @return array With start_date and end_date. */ private static function ayudawp_get_date_range( $request ) { $start_date = $request->get_param( 'start_date' ); $end_date = $request->get_param( 'end_date' ); if ( $start_date && $end_date ) { return array( 'start_date' => $start_date, 'end_date' => $end_date, ); } $days = (int) $request->get_param( 'days' ); // Use wp_date() to respect WordPress timezone setting (data is stored in local time). $end_date = wp_date( 'Y-m-d' ); if ( 0 === $days ) { $start_date = '2020-01-01'; } else { $start_date = wp_date( 'Y-m-d', strtotime( '-' . $days . ' days' ) ); } return array( 'start_date' => $start_date, 'end_date' => $end_date, ); } /** * Track a button click from frontend * * No nonce verification: this is a public, non-destructive endpoint * (anonymous click counter). Nonces break with full-page caching * (SiteGround, WP Super Cache, etc.) because they are baked into * the cached HTML and expire. The endpoint is protected by platform * whitelist validation and input sanitization instead. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_track_click( $request ) { $platform = $request->get_param( 'platform' ); $post_id = $request->get_param( 'post_id' ); // Validate platform against known list. $valid_platforms = array_keys( ayudawp_aiss_get_available_buttons() ); // Also accept hyphenated versions. $platform_clean = str_replace( '-', '_', $platform ); if ( ! in_array( $platform_clean, $valid_platforms, true ) ) { return new WP_REST_Response( array( 'error' => 'Invalid platform' ), 400 ); } $post_title = ''; if ( $post_id > 0 ) { $post_title = get_the_title( $post_id ); } $result = AyudaWP_AISS_Database::ayudawp_insert_click( array( 'platform' => $platform_clean, 'post_id' => $post_id, 'post_title' => $post_title, ) ); if ( $result ) { return new WP_REST_Response( array( 'success' => true ), 200 ); } return new WP_REST_Response( array( 'error' => 'Failed to track' ), 500 ); } /** * Get stats for the date range * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_stats( $request ) { $range = self::ayudawp_get_date_range( $request ); $stats = AyudaWP_AISS_Database::ayudawp_get_stats( $range['start_date'], $range['end_date'] ); return self::ayudawp_nocache_response( $stats ); } /** * Get timeline data * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_timeline( $request ) { $range = self::ayudawp_get_date_range( $request ); $timeline = AyudaWP_AISS_Database::ayudawp_get_clicks_over_time( $range['start_date'], $range['end_date'] ); return self::ayudawp_nocache_response( $timeline ); } /** * Get timeline data grouped by platform (for chart per-platform breakdown) * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_timeline_by_platform( $request ) { $range = self::ayudawp_get_date_range( $request ); $rows = AyudaWP_AISS_Database::ayudawp_get_clicks_by_platform_over_time( $range['start_date'], $range['end_date'] ); return self::ayudawp_nocache_response( $rows ); } /** * Get top platforms * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_platforms( $request ) { $range = self::ayudawp_get_date_range( $request ); $limit = $request->get_param( 'limit' ); $offset = $request->get_param( 'offset' ); $platforms = AyudaWP_AISS_Database::ayudawp_get_top_platforms( $range['start_date'], $range['end_date'], $limit, $offset ); $total = AyudaWP_AISS_Database::ayudawp_get_platforms_count( $range['start_date'], $range['end_date'] ); return self::ayudawp_nocache_response( array( 'items' => $platforms, 'total' => $total, ) ); } /** * Get top content * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_content( $request ) { $range = self::ayudawp_get_date_range( $request ); $limit = $request->get_param( 'limit' ); $offset = $request->get_param( 'offset' ); $content = AyudaWP_AISS_Database::ayudawp_get_top_content( $range['start_date'], $range['end_date'], $limit, $offset ); $total = AyudaWP_AISS_Database::ayudawp_get_content_count( $range['start_date'], $range['end_date'] ); // If VigIA is active, add crawl data per post. $vigia_active = class_exists( 'VigIA_Database' ); $crawl_data = array(); if ( $vigia_active && ! empty( $content ) ) { $post_ids = wp_list_pluck( $content, 'post_id' ); $crawl_data = AyudaWP_AISS_Database::ayudawp_get_vigia_crawls_for_posts( $post_ids, $range['start_date'], $range['end_date'] ); } return self::ayudawp_nocache_response( array( 'items' => $content, 'total' => $total, 'vigia_active' => $vigia_active, 'crawl_data' => $crawl_data, ) ); } /** * Get all data for CSV export * * @param WP_REST_Request $request Request object. * @return WP_REST_Response */ public static function ayudawp_get_export( $request ) { $range = self::ayudawp_get_date_range( $request ); $rows = AyudaWP_AISS_Database::ayudawp_get_export_data( $range['start_date'], $range['end_date'] ); return self::ayudawp_nocache_response( array( 'rows' => $rows, 'start_date' => $range['start_date'], 'end_date' => $range['end_date'], ) ); } }