allowed_generator_types, true ); } /** * Validate and normalize prompt_data based on generator type. * * - description: requires a non-empty custom_prompt (2.8.1). A request * still carrying the pre-2.8.1 system_prompt/user_prompt pair instead * is merged into custom_prompt the same way CustomPrompts::merge() * merges stored options, so an older caller keeps working. * - title/faq/seo_meta/taxonomy: require additional_prompt only (system + user are built-in) * * @param mixed $prompt_data Raw prompt data from the request. * @param string $generator_type Generator type for schema selection. * @return array|WP_Error Normalized prompt data or error. */ private function validate_prompt_data( $prompt_data, string $generator_type = '' ) { if ( ! is_array( $prompt_data ) ) { return new WP_Error( 'invalid_prompt_data', __( 'prompt_data must be a JSON object.', 'ai-product-tools' ), [ 'status' => 400 ] ); } $additional_prompt = $prompt_data['additional_prompt'] ?? null; $uses_additional_only = in_array( $generator_type, [ 'title', 'faq', 'seo_meta', 'taxonomy' ], true ); if ( $uses_additional_only ) { if ( ! is_string( $additional_prompt ) || '' === trim( $additional_prompt ) ) { return new WP_Error( 'invalid_prompt_schema', __( 'additional_prompt must not be empty for title/faq/seo_meta/taxonomy templates.', 'ai-product-tools' ), [ 'status' => 400 ] ); } $normalized = [ 'additional_prompt' => $additional_prompt, ]; } else { $custom_prompt = $prompt_data['custom_prompt'] ?? null; if ( ! is_string( $custom_prompt ) || '' === trim( $custom_prompt ) ) { $system_prompt = $prompt_data['system_prompt'] ?? null; $user_prompt = $prompt_data['user_prompt'] ?? null; $merged = CustomPrompts::merge( is_string( $system_prompt ) ? $system_prompt : '', is_string( $user_prompt ) ? $user_prompt : '' ); $custom_prompt = '' === $merged ? null : $merged; } if ( ! is_string( $custom_prompt ) || '' === trim( $custom_prompt ) ) { return new WP_Error( 'invalid_prompt_schema', __( 'custom_prompt must not be empty for description templates.', 'ai-product-tools' ), [ 'status' => 400 ] ); } $normalized = [ 'custom_prompt' => $custom_prompt, ]; } $encoded = wp_json_encode( $normalized ); if ( ! is_string( $encoded ) || strlen( $encoded ) > 102400 ) { return new WP_Error( 'prompt_data_too_large', __( 'prompt_data exceeds the maximum allowed size of 100KB.', 'ai-product-tools' ), [ 'status' => 400 ] ); } return $normalized; } }