callbacks = array_replace( array( 'current_user_can' => 'current_user_can', 'wp_verify_nonce' => 'wp_verify_nonce', ), $callbacks ); $this->service = $service ?? new ContentDefaultsService( array(), $this->callbacks ); } public function register_routes(): void { \register_rest_route( self::NAMESPACE, self::ROUTE, array( array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_content_defaults' ), 'permission_callback' => array( $this, 'check_permissions' ), ), array( 'methods' => \WP_REST_Server::CREATABLE, 'callback' => array( $this, 'post_content_defaults' ), 'permission_callback' => array( $this, 'check_permissions_write' ), ), ) ); } public function get_content_defaults( \WP_REST_Request $request ) { unset( $request ); return new \WP_REST_Response( $this->format_response( $this->service->get() ), 200 ); } public function post_content_defaults( \WP_REST_Request $request ) { $params = array(); foreach ( array( 'provider', 'model', 'language', 'writing_style', 'brand_context', 'preserveContentImages' ) as $key ) { $value = $request->get_param( $key ); if ( null !== $value ) { $params[ $key ] = $value; } } $defaults = $this->service->update( $params ); if ( \is_wp_error( $defaults ) ) { return $defaults; } return new \WP_REST_Response( $this->format_response( $defaults ), 200 ); } /** * Read permission: edit_products only. GET is a read, so it never needs * the nonce — a cookie-authenticated reader is already covered by WP * core's own cookie-auth gate (@see \AIPT\Utils\RestNonceGuard). */ public function check_permissions( \WP_REST_Request $request ) { unset( $request ); if ( ! call_user_func( $this->callbacks['current_user_can'], 'edit_products' ) ) { return new \WP_Error( 'rest_forbidden', \__( 'You do not have permission to access this endpoint.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return true; } public function check_permissions_write( \WP_REST_Request $request ) { if ( ! call_user_func( $this->callbacks['current_user_can'], 'manage_aipt_settings' ) ) { return new \WP_Error( 'rest_forbidden', \__( 'You do not have permission to update content defaults.', 'ai-product-tools' ), array( 'status' => 403 ) ); } if ( ! $this->verify_nonce( $request ) ) { return new \WP_Error( 'rest_forbidden', \__( 'Invalid or missing REST nonce.', 'ai-product-tools' ), array( 'status' => 403 ) ); } return true; } private function format_response( array $defaults ): array { return array( 'provider' => $defaults['provider'], 'model' => $defaults['model'], 'language' => $defaults['language'], 'writing_style' => $defaults['writing_style'], 'brand_context' => $defaults['brand_context'], 'preserveContentImages' => $defaults['preserveContentImages'], 'canUpdate' => $this->service->can_update(), ); } /** * Required only from a cookie-authenticated request: an * Application-Password/Basic-Auth (or other non-cookie) REST client is * already authenticated and has no way to ever obtain one * (@see \AIPT\Utils\RestNonceGuard). */ private function verify_nonce( \WP_REST_Request $request ): bool { if ( ! \AIPT\Utils\RestNonceGuard::isCookieAuthenticated() ) { return true; } $nonce = $request->get_header( 'X-WP-Nonce' ); if ( empty( $nonce ) ) { $nonce = $request->get_param( '_wpnonce' ); } return (bool) call_user_func( $this->callbacks['wp_verify_nonce'], $nonce, 'wp_rest' ); } }