403 ) ); } if ( ! \AIPT\Utils\RestNonceGuard::isCookieAuthenticated() ) { return true; } $nonce = ''; if ( is_object( $_request ) && method_exists( $_request, 'get_header' ) ) { $nonce = (string) $_request->get_header( 'X-WP-Nonce' ); } if ( '' === $nonce && is_object( $_request ) && method_exists( $_request, 'get_param' ) ) { $nonce = (string) $_request->get_param( '_wpnonce' ); } if ( '' === $nonce || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie check failed', 'ai-product-tools' ), array( 'status' => 403 ) ); } return true; } /** * Check write permission for Product Editor bulk-generation endpoints. * * Reuses the shared capability and nonce checks before enforcing the * Product Editor bulk-generation entitlement. * * Filter-bridge gate (G9) — always-shipped code never consults plan state * directly: it applies `aipt_editor_bulk_generation_enabled` with a false * default. The premium module registers the Business-tier entitlement * callback on that filter; on a stripped free build no registrant exists, * so every editor-bulk route degrades to the same 403 below. * * @param mixed $_request The REST request object. * @return bool|WP_Error True if authorized, WP_Error with 403 otherwise. */ public function check_permissions_editor_bulk_write( $_request = null ) { $permission = $this->check_permissions_write( $_request ); if ( true !== $permission ) { return $permission; } if ( ! apply_filters( 'aipt_editor_bulk_generation_enabled', false ) ) { return PlanDenial::error( 'editorBulkGeneration', PlanDenial::PLAN_BUSINESS, __( 'Business plan is required for Product Editor bulk generation.', 'ai-product-tools' ) ); } return true; } /** * Check whether AI is supported on this WordPress installation. * * All AI-firing endpoints MUST call this at the top of their callback. * Returns `null` when AI is available, or a `WP_Error` (503) when the site * administrator has disabled AI via `WP_AI_SUPPORT` constant or the * `wp_supports_ai` filter. * * @return WP_Error|null Null when AI is available, WP_Error otherwise. */ protected function check_ai_support_gate(): ?WP_Error { $result = \AIPT\Core\Services\AiSupportGate::guard(); return ( $result instanceof WP_Error ) ? $result : null; } /** * Convenience wrapper around RateLimitTrait::checkRateLimit(). * * Resolves the scope from the calling subclass's `RATE_LIMIT_SCOPE` * constant via late static binding, so each generator type gets its own * isolated rate-limit window. Callers may pass an explicit `$scope` to * override the constant (useful in tests). * * @param int $user_id Authenticated user ID. * @param string|null $scope Optional scope override; defaults to static::RATE_LIMIT_SCOPE. * @return array{allowed: bool, remaining: int, reset_in: int} */ protected function check_rate_limit( int $user_id, ?string $scope = null ): array { return $this->checkRateLimit( $user_id, $scope ?? static::RATE_LIMIT_SCOPE ); } /** * Check whether a product may be staged for content review. * * Encapsulates the product-existence and edit-capability guard shared by * every bulk generator's review-staging loop. A product that fails this * check (deleted, trashed, or user lacks edit_post) must never reach * ContentReviewStore::stage() — the apply path would reject it later, * silently stranding the generated content. * * @param int $product_id Product ID to validate. * @return bool True when the product exists and the current user may edit it. */ protected function can_stage_review_for_product( int $product_id ): bool { return (bool) wc_get_product( $product_id ) && current_user_can( 'edit_post', $product_id ); } /** * Whether a saved site-level additional prompt will be left out of generation. * * The premium option filters read the stored prompt only for an entitled * site. The G9 bridge filter mirrors that gate (false by default, so the * free build reports it too), letting the response say why saved * instructions were not used instead of ignoring them silently. The stored * value is read only to report, never applied here. A run on content * templates never uses the saved prompt, on any plan, so it is not * reported there. * * @param string $generator Content structure generator key of the run. * @param string $option Option name of the saved prompt. * @param string $bridge Bridge filter that allows the saved prompt. * @return bool True when a non-empty saved prompt is not allowed. */ protected static function site_additional_prompt_skipped( string $generator, string $option, string $bridge ): bool { if ( BlueprintIntegration::usesBlueprint( $generator, BlueprintIntegration::jobTrustedFacts() ) ) { return false; } $saved = get_option( $option, '' ); return is_string( $saved ) && '' !== trim( $saved ) && ! apply_filters( $bridge, false ); } /** * Build a uniform success response envelope. * * @param array $data Response payload. * @param string $message Optional human-readable success message. * @return WP_REST_Response HTTP 200 with `{ success: true, data: ..., [message: ...] }`. */ protected function success_response( array $data, string $message = '' ): WP_REST_Response { $response = array( 'success' => true, 'data' => $data, ); if ( ! empty( $message ) ) { $response['message'] = $message; } return new WP_REST_Response( $response, 200 ); } /** * Build a uniform error WP_Error. * * @param string $code Machine-readable error code. * @param string $message Human-readable error message. * @param int $status HTTP status code (default 400). * @return WP_Error */ protected function error_response( string $code, string $message, int $status = 400 ): WP_Error { return new WP_Error( $code, $message, array( 'status' => $status ) ); } /** * Format an AI provider API error message with the provider name prefix. * * Delegates to `AIPT\Core\ErrorHandler` for consistent error presentation * across all controllers. * * @param string $provider Provider name (e.g. 'OpenAI', 'Gemini'). * @param string $error_message Raw error message from the provider client. * @return string Formatted error string ready for display. */ protected function format_api_error_message( string $provider, string $error_message ): string { return \AIPT\Core\ErrorHandler::format_api_error_message( $provider, $error_message ); } }