$spec ) { register_rest_route( 'aipt/v1', $route, array( array( 'methods' => $spec['methods'], 'callback' => array( __CLASS__, 'review_action_required' ), 'permission_callback' => array( __CLASS__, $spec['permission'] ), 'args' => $spec['args'], ), ) ); } foreach ( self::business_route_args() as $route => $args ) { register_rest_route( 'aipt/v1', $route, array( array( 'methods' => 'POST', 'callback' => array( __CLASS__, 'review_action_required' ), 'permission_callback' => array( __CLASS__, 'check_permissions_write' ), 'args' => $args, ), ) ); } } /** * The review queue's reads and its bulk delete, by route: the method, * the permission check and the arguments. This class registers each one * to refuse; the Business build registers its handler on the same route * with the same method, check and arguments. * * @return array>}> */ public static function business_queue_routes(): array { return array( '/content-review' => array( 'methods' => 'GET', 'permission' => 'check_permissions_read', 'args' => self::listArgs(), ), '/content-review/summary' => array( 'methods' => 'GET', 'permission' => 'check_permissions_read', 'args' => array(), ), '/content-review/(?P\d+)/current-value' => array( 'methods' => 'GET', 'permission' => 'check_permissions_read', 'args' => array( 'id' => array( 'required' => true, 'sanitize_callback' => 'absint', ), ), ), '/content-review/bulk-delete' => array( 'methods' => 'POST', 'permission' => 'check_permissions_write', 'args' => array( 'ids' => array( 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), 'maxItems' => self::MAX_BULK_IDS, ), ), ), ); } /** * The review actions only the Business build answers, by route, with * their arguments. This class registers each one to refuse; the * Business build registers its handler on the same route with these * same arguments. * * @return array>> */ public static function business_route_args(): array { $id = array( 'id' => array( 'required' => true, 'sanitize_callback' => 'absint', ), ); $ids = array( 'ids' => array( 'required' => true, 'type' => 'array', 'items' => array( 'type' => 'integer' ), 'maxItems' => self::MAX_BULK_IDS, ), ); return array( '/content-review/approve' => $id, '/content-review/edit' => $id + array( 'value' => array( 'required' => true, 'type' => 'string', 'maxLength' => self::MAX_EDIT_VALUE_LENGTH, ), ), '/content-review/reject' => $id, '/content-review/bulk-approve' => $ids, '/content-review/bulk-reject' => $ids, ); } /** * Read permission: edit_products OR manage_woocommerce. * * No nonce: being a GET-only route family, a read is never the * cross-site write a nonce protects against, and WP core's own * cookie-auth gate already covers a cookie-authenticated reader * (@see \AIPT\Utils\RestNonceGuard). The plan comes after it: the free * build's routes refuse, and the Business build's handlers refuse a site * without the entitlement. * * @param WP_REST_Request $request Request object. * @return true|WP_Error */ public static function check_permissions_read( $request ) { if ( ! current_user_can( 'manage_woocommerce' ) && ! current_user_can( 'edit_products' ) ) { return new WP_Error( 'rest_forbidden', __( 'You do not have permission to view content review items.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return true; } /** * Write permission: manage_woocommerce + nonce. * * The Business build's handlers check edit_post on each row's product * and ask the Business gate (mirrors SnapshotRestController pattern); * the free build's routes only refuse. The nonce is required only * from a cookie-authenticated request — an Application-Password/Basic-Auth * (or other non-cookie) client is already authenticated and has no way * to ever obtain one (@see \AIPT\Utils\RestNonceGuard). * * @param WP_REST_Request $request Request object. * @return true|WP_Error */ public static function check_permissions_write( $request ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { return new WP_Error( 'rest_forbidden', __( 'You do not have permission to perform this action.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return \AIPT\Utils\RestNonceGuard::check( $request, __( 'Invalid or missing security nonce.', 'ai-product-tools' ) ); } /** * Return the reason-aware premium_required WP_Error for the Business * 'contentReview' gate. * * @return WP_Error */ protected static function premiumRequiredError(): WP_Error { return PlanDenial::error( 'contentReview', PlanDenial::PLAN_BUSINESS, __( 'Business plan adds content review actions.', 'ai-product-tools' ) ); } /** * Refuse a review route. The callback of every route this class * registers: the free build answers them with this alone, whatever the * entitlement filter says. * * @param WP_REST_Request $request Request object. * @return WP_Error */ public static function review_action_required( $request ): WP_Error { return self::premiumRequiredError(); } /** * List endpoint argument schema. * * @return array> */ private static function listArgs(): array { return array( 'tool' => array( 'sanitize_callback' => 'sanitize_key' ), 'product_id' => array( 'sanitize_callback' => 'absint' ), 'status' => array( 'sanitize_callback' => 'sanitize_key' ), 'cursor' => array( 'sanitize_callback' => array( __CLASS__, 'sanitizeCursor' ) ), 'limit' => array( 'default' => 25, 'sanitize_callback' => 'absint' ), ); } /** * Sanitize the keyset cursor. * * @param mixed $value Raw array or JSON cursor. * @return array|null */ public static function sanitizeCursor( $value ): ?array { if ( is_string( $value ) ) { $value = json_decode( $value, true ); } if ( ! is_array( $value ) || empty( $value['created_at'] ) || empty( $value['id'] ) ) { return null; } return array( 'created_at' => sanitize_text_field( (string) $value['created_at'] ), 'id' => absint( $value['id'] ), ); } }