} }`. * * WP dependencies are constructor-injectable so the permission + response * shaping can be unit-tested without WordPress loaded. */ class ContentMarkingController { /** @var callable */ private $current_user_can; /** @var callable */ private $verify_nonce; /** @var callable */ private $reset_service; /** @var callable */ private $count_service; /** * @param array{current_user_can?:callable, verify_nonce?:callable, reset_service?:callable, count_service?:callable} $deps */ public function __construct( array $deps = array() ) { $this->current_user_can = $deps['current_user_can'] ?? 'current_user_can'; $this->verify_nonce = $deps['verify_nonce'] ?? static function ( \WP_REST_Request $request ): bool { $nonce = (string) $request->get_header( 'X-WP-Nonce' ); if ( '' === $nonce ) { $nonce = (string) $request->get_param( '_wpnonce' ); } return (bool) wp_verify_nonce( $nonce, 'wp_rest' ); }; $this->reset_service = $deps['reset_service'] ?? array( ContentMarkReset::class, 'reset' ); $this->count_service = $deps['count_service'] ?? array( ContentMarkReset::class, 'markedCount' ); } public function register_routes(): void { register_rest_route( 'aipt/v1', '/content-marking/reset', array( 'methods' => 'POST', 'callback' => array( $this, 'reset_marks' ), 'permission_callback' => array( $this, 'check_permissions' ), ) ); register_rest_route( 'aipt/v1', '/content-marking/status', array( 'methods' => 'GET', 'callback' => array( $this, 'count_marks' ), 'permission_callback' => array( $this, 'check_permissions' ), ) ); } /** * Shared by both the GET /status read and the POST /reset write: a read * needs no nonce, and a write needs one only from a cookie-authenticated * request (an Application-Password/Basic-Auth client is already * authenticated and has no way to ever obtain one, @see * \AIPT\Utils\RestNonceGuard). * * @return true|\WP_Error */ public function check_permissions( \WP_REST_Request $request ) { $capable = (bool) call_user_func( $this->current_user_can, 'manage_options' ); $nonce_required = $capable && 'GET' !== $request->get_method() && \AIPT\Utils\RestNonceGuard::isCookieAuthenticated(); $allowed = $capable && ( ! $nonce_required || (bool) call_user_func( $this->verify_nonce, $request ) ); if ( $allowed ) { return true; } $message = '/aipt/v1/content-marking/status' === $request->get_route() ? __( 'You are not allowed to view AI content marking status.', 'ai-product-tools' ) : __( 'You are not allowed to reset AI content marking.', 'ai-product-tools' ); return new \WP_Error( 'rest_forbidden', $message, array( 'status' => 403 ) ); } public function reset_marks( \WP_REST_Request $request ): \WP_REST_Response { unset( $request ); $deleted = (int) call_user_func( $this->reset_service ); return new \WP_REST_Response( array( 'success' => true, 'data' => array( 'deleted' => $deleted ), ), 200 ); } public function count_marks( \WP_REST_Request $request ): \WP_REST_Response { unset( $request ); $marked = (int) call_user_func( $this->count_service ); return new \WP_REST_Response( array( 'success' => true, 'data' => array( 'marked' => $marked ), ), 200 ); } }