'GET', 'callback' => array($this, 'get_current_user'), 'permission_callback' => array($this, 'check_combined_auth'), )); register_rest_route('a2cai/v1', '/products/batch', array( 'methods' => 'GET', 'callback' => array($this, 'get_product_batch'), 'permission_callback' => array($this, 'check_batch_permissions'), )); } public function get_current_user() { $user_id = get_current_user_id(); return new WP_REST_Response(array( 'currentUserId' => $user_id, ), 200); } public function get_product_batch(WP_REST_Request $request) { $batch_size = 500; $batch_number = $request->get_param('batch_number') ? max(1, intval($request->get_param('batch_number'))) : 1; $offset = ($batch_number - 1) * $batch_size; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $batch_size, 'offset' => $offset, 'fields' => 'ids', // Only get post IDs for efficiency ); $products_query = new WP_Query($args); $products = array_map(function ($post_id) { $product = wc_get_product($post_id); $image_id = get_post_thumbnail_id($post_id); $image_src = wp_get_attachment_image_src($image_id, 'full'); $image_url = $image_src ? $image_src[0] : null; $in_stock = $product->is_in_stock(); $is_published = $product->get_status() === 'publish'; $visible = $is_published && $product->get_catalog_visibility() !== 'hidden'; return array( 'id' => $post_id, 'title' => $product->get_title(), 'price' => $product->get_price(), 'imageUrl' => $image_url, 'inStock' => $in_stock, 'visible' => $visible, ); }, $products_query->posts); $total_products = $products_query->found_posts; $more_products = $total_products > $offset + count($products); return rest_ensure_response(array( 'products' => $products, 'moreBatches' => $more_products, 'totalProducts' => $total_products, 'currentBatch' => $batch_number, 'totalBatches' => ceil($total_products / $batch_size) )); } public function check_batch_permissions(WP_REST_Request $request) { $auth_header = $request->get_header('Authorization'); if (!$auth_header || strpos($auth_header, 'Bearer ') !== 0) { return new WP_Error('invalid_token', esc_html__('Invalid or missing bearer token', 'add-to-cart-ai'), array('status' => 401)); } $token = substr($auth_header, 7); $hashed_key = get_option('a2cai_activation_key'); if (!wp_check_password($token, $hashed_key)) { return new WP_Error('invalid_token', esc_html__('Invalid bearer token', 'add-to-cart-ai'), array('status' => 401)); } return true; } public function check_combined_auth(WP_REST_Request $request) { // First try to determine and set the current user $user_id = get_current_user_id(); // If we got a valid user ID, they're authenticated if ($user_id) { return true; } // If no valid user, check for bearer token $auth_header = $request->get_header('Authorization'); if ($auth_header && strpos($auth_header, 'Bearer ') === 0) { $token = substr($auth_header, 7); $hashed_key = get_option('a2cai_activation_key'); if (wp_check_password($token, $hashed_key)) { return true; } } return new WP_Error('unauthorized', esc_html__('You must be logged in or provide a valid bearer token', 'add-to-cart-ai'), array('status' => 401)); } } new ACAI_API();