$value ) { unset( $data[ $value ] ); } if ( isset( $data['credentials'] ) ) { $security_tools = new \RWPAI\Components\Tools\Rwpai_Security_Tools(); $data['credentials'] = $security_tools->decrypt( $data['credentials'] ); } return $data; } /** * Unified function to set plugin config * * @param array $config Plugin config. * @return void */ function rwpai_set_plugin_config( $config ) { // Encrypt credentials before saving. if ( isset( $config['credentials'] ) ) { $security_tools = new \RWPAI\Components\Tools\Rwpai_Security_Tools(); $config['credentials'] = $security_tools->encrypt( $config['credentials'] ); } update_option( RWPAI_KV_STORAGE, $config ); } /** * Sets default options upon plugin activation. */ function rwpai_set_default_plugin_config() { // Only add the option if it doesn't already exist. if ( false === get_option( RWPAI_KV_STORAGE ) ) { rwpai_set_plugin_config( RWPAI_DEFAULT_CONFIG ); } } /** * REST API handler to get settings. * * @return WP_REST_Response JSON response. */ function rwpai_rest_get_settings() { $settings = rwpai_get_plugin_config(); return rest_ensure_response( array( 'status' => true, 'message' => 'Plugin config retrieved successfully.', 'data' => $settings, ) ); } /** * REST permission callback for saving settings. * * @param WP_REST_Request $request Request object. * @return bool|WP_Error True if permission granted, WP_Error otherwise. */ function rwpai_rest_only_admin( $request ) { $result = rwpai_verify_request_using_nonce_rest( $request, RWPAI_NONCE_NAME_ADMIN ); return true === $result ? true : $result; } /** * REST API handler to save settings. * * @param WP_REST_Request $request Request object. * @return WP_REST_Response|WP_Error Response or error. */ function rwpai_rest_save_settings( $request ) { $settings = $request->get_param( 'settings' ); if ( empty( $settings ) ) { return new WP_Error( 'missing_settings', 'No settings data provided.', array( 'status' => 400 ) ); } // Validate that settings is an array or object, not a string. if ( ! is_array( $settings ) && ! is_object( $settings ) ) { return new WP_REST_Response( array( 'status' => false, 'message' => 'Settings must be an object or array, not a string or primitive value.', ), 400 ); } $validated = rwpai_validate_and_sanitize_settings( $settings ); if ( is_wp_error( $validated ) ) { return new WP_REST_Response( array( 'status' => false, 'message' => $validated->get_error_message(), ), 400 ); } $config_before = rwpai_get_plugin_config(); // Override old config with new settings. $new_config = array_merge( $config_before, $validated['settings'] ); rwpai_set_plugin_config( $new_config ); $response = array( 'status' => true, 'message' => empty( $validated['unrecognized_keys'] ) ? 'Settings saved successfully.' : 'Settings saved, but some keys were not recognized.', ); if ( ! empty( $validated['unrecognized_keys'] ) ) { $response['unrecognized_keys'] = $validated['unrecognized_keys']; } return rest_ensure_response( $response ); } /** * Validate and sanitize plugin settings. * * @param array $settings Raw settings input. * @return array|WP_Error Sanitized settings or WP_Error. */ function rwpai_validate_and_sanitize_settings( $settings ) { $sanitized = array(); $string_array_keys = array( 'exclude_from_post_categories', 'auto_add_for_post_types', 'exclude_elements', 'exclude_texts' ); $integrations_config = array( 'credentials', 'credentials_valid', 'ai_source', 'ai_config', ); $look_config = array( // 'player_style', ); // String / number / any. $tts_config_keys = array( 'plan', ...$integrations_config, ...$look_config, ); $allowed_keys = array_merge( $string_array_keys, $tts_config_keys, ); $unrecognized_keys = array(); // Track extra/unknown keys. foreach ( $settings as $key => $value ) { if ( ! in_array( $key, $allowed_keys, true ) ) { $unrecognized_keys[] = $key; } } // Sanitize string array fields. foreach ( $string_array_keys as $key ) { if ( isset( $settings[ $key ] ) ) { if ( ! is_array( $settings[ $key ] ) ) { return new WP_Error( 'invalid_field', "Invalid field: {$key}" ); } $sanitized[ $key ] = array_map( 'sanitize_text_field', $settings[ $key ] ); } } // Sanitize TTS config fields. foreach ( $tts_config_keys as $key ) { if ( isset( $settings[ $key ] ) ) { $sanitized[ $key ] = $settings[ $key ]; } } return array( 'settings' => $sanitized, 'unrecognized_keys' => $unrecognized_keys, ); } /** * Allow CORS from localhost:3001 and localhost:3002 * Only active when WordPress runs on localhost (for development only) */ function rwpai_allow_localhost_cors_headers() { $allowed_origins = array( 'http://localhost:3001', 'http://localhost:3002', ); // Only run when WP is on localhost. $site_url = get_site_url(); $is_local_site = ( str_contains( $site_url, 'localhost' ) || str_contains( $site_url, '127.0.0.1' ) ); if ( ! $is_local_site ) { return; } // Sanitize and validate HTTP_ORIGIN. $origin = ''; if ( isset( $_SERVER['HTTP_ORIGIN'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash $origin = esc_url_raw( wp_unslash( $_SERVER['HTTP_ORIGIN'] ) ); } // If origin is allowed → set headers. if ( $origin && in_array( $origin, $allowed_origins, true ) ) { header( 'Access-Control-Allow-Origin: ' . $origin ); header( 'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE' ); header( 'Access-Control-Allow-Credentials: true' ); header( 'Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With' ); } // Handle preflight OPTIONS. if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'OPTIONS' === $_SERVER['REQUEST_METHOD'] ) { status_header( 200 ); exit; } } add_action( 'init', 'rwpai_allow_localhost_cors_headers' );