'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => array( $this, 'can_settings' ), ), array( 'methods' => 'PATCH', 'callback' => array( $this, 'update' ), 'permission_callback' => array( $this, 'can_settings' ), ), ) ); if ( class_exists( ProviderRegistry::class ) ) { register_rest_route( Router::NAMESPACE, '/providers/(?Popenai|gemini|anthropic|openrouter|xai|mistral)/models', array( 'methods' => 'GET', 'callback' => array( $this, 'provider_models' ), 'permission_callback' => array( $this, 'can_settings' ), 'args' => array( 'provider' => array( 'type' => 'string', 'required' => true, 'enum' => array( 'openai', 'gemini', 'anthropic', 'openrouter', 'xai', 'mistral' ) ), 'refresh' => array( 'type' => 'boolean', 'default' => false ), ), ) ); register_rest_route( Router::NAMESPACE, '/providers/test', array( array( 'methods' => 'POST', 'callback' => array( $this, 'test_provider' ), 'permission_callback' => array( $this, 'can_settings' ), ), ) ); } if ( class_exists( WhiteLabel::class ) ) { register_rest_route( Router::NAMESPACE, '/settings/white-label', array( array( 'methods' => 'POST', 'callback' => array( $this, 'upload_white_label' ), 'permission_callback' => array( $this, 'can_settings' ), ), array( 'methods' => 'DELETE', 'callback' => array( $this, 'remove_white_label' ), 'permission_callback' => array( $this, 'can_settings' ), ), ) ); } } public function show(): WP_REST_Response { $settings = get_option( 'frkt_settings', array() ); $keys = class_exists( SecretStore::class ) ? ( new SecretStore() )->status_map() : array(); $providers = array(); if ( class_exists( ProviderRegistry::class ) ) { foreach ( ProviderRegistry::catalog() as $entry ) { $id = (string) $entry['id']; $providers[] = array( 'id' => $id, 'label' => (string) $entry['label'], 'configured' => ! empty( $keys[ $id ] ), 'kind' => (string) ( $entry['kind'] ?? '' ), ); } } $uninstall_mode = get_option( 'frkt_uninstall_mode', 'keep' ); if ( ! in_array( $uninstall_mode, array( 'keep', 'delete' ), true ) ) { $uninstall_mode = 'keep'; } return new WP_REST_Response( array( 'settings' => $settings, 'providers' => $providers, 'featureFlags' => \FoldRocket\Domain\Features\FeatureRegistry::enabled_map(), 'dbVersion' => get_option( 'frkt_db_version', '' ), 'uninstallMode' => $uninstall_mode, 'whiteLabel' => class_exists( WhiteLabel::class ) ? WhiteLabel::payload() : array( 'logoUrl' => '', 'iconUrl' => '', ), ) ); } public function upload_white_label( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'white_label' ) ) { return $denied; } $file = $this->extract_uploaded_file( $request ); if ( ! $file ) { return $this->error( 'invalid_image', __( 'Choose a PNG, JPEG, WebP, or GIF image.', 'foldrocket' ) ); } try { $payload = WhiteLabel::store( sanitize_key( (string) $request->get_param( 'kind' ) ), $file ); } catch ( \InvalidArgumentException $e ) { return $this->error( 'invalid_image', $e->getMessage() ); } catch ( \Throwable $e ) { return $this->error( 'upload_failed', __( 'Could not save the brand image.', 'foldrocket' ), 500 ); } return new WP_REST_Response( $payload ); } public function remove_white_label( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'white_label' ) ) { return $denied; } try { $payload = WhiteLabel::remove( sanitize_key( (string) $request->get_param( 'kind' ) ) ); } catch ( \InvalidArgumentException $e ) { return $this->error( 'invalid_image', $e->getMessage() ); } catch ( \Throwable $e ) { return $this->error( 'upload_failed', __( 'Could not save the brand image.', 'foldrocket' ), 500 ); } return new WP_REST_Response( $payload ); } public function update( WP_REST_Request $request ): WP_REST_Response { $current = get_option( 'frkt_settings', array() ); $current = is_array( $current ) ? $current : array(); $incoming = $request->get_param( 'settings' ); if ( is_array( $incoming ) ) { $current = $this->merge_settings( $current, $this->sanitize_settings( $incoming ) ); update_option( 'frkt_settings', $current, false ); } $keys = $request->get_param( 'apiKeys' ); if ( is_array( $keys ) && class_exists( SecretStore::class ) ) { $store = new SecretStore(); foreach ( $keys as $provider => $value ) { if ( ! is_string( $value ) || $value === '' ) { continue; } // Never accept empty overwrite markers from clients that don't have the key. if ( $value === '********' ) { continue; } $store->set( sanitize_key( (string) $provider ), sanitize_text_field( $value ) ); } } $uninstall = $request->get_param( 'uninstallMode' ); if ( in_array( $uninstall, array( 'keep', 'delete' ), true ) ) { update_option( 'frkt_uninstall_mode', $uninstall, false ); $current['uninstall_mode'] = $uninstall; update_option( 'frkt_settings', $current, false ); } return $this->show(); } public function provider_models( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'ai_analysis' ) ) { return $denied; } $provider = (string) $request->get_param( 'provider' ); if ( ! in_array( $provider, array_column( ProviderRegistry::catalog(), 'id' ), true ) ) { return $this->error( 'unknown_provider', __( 'Unknown provider.', 'foldrocket' ) ); } return new WP_REST_Response( ( new \FoldRocket\Infrastructure\Providers\ModelCatalog() )->list( $provider, rest_sanitize_boolean( $request->get_param( 'refresh' ) ) ) ); } public function test_provider( WP_REST_Request $request ): WP_REST_Response|\WP_Error { if ( $denied = $this->require_feature( 'ai_analysis' ) ) { return $denied; } $provider_id = sanitize_key( (string) $request->get_param( 'provider' ) ); $registry = new ProviderRegistry(); $provider = $registry->get( $provider_id ); if ( ! $provider ) { return $this->error( 'unknown_provider', __( 'Unknown provider.', 'foldrocket' ) ); } if ( ! $provider->supports_vision() ) { return $this->error( 'not_configured', __( 'Provider API key is missing.', 'foldrocket' ) ); } try { $message = $registry->ping( $provider_id ); return new WP_REST_Response( array( 'ok' => true, 'provider' => $provider->id(), 'label' => $provider->label(), 'message' => $message, ) ); } catch ( \Throwable $e ) { return $this->exception_error( 'provider_unreachable', $e, __( 'Could not reach the AI provider. Check the API key and network, then try again.', 'foldrocket' ), 502, true ); } } /** * @param array $settings * @return array */ private function sanitize_settings( array $settings ): array { $out = array(); foreach ( $settings as $section => $values ) { if ( ! is_array( $values ) ) { continue; } $section_key = sanitize_key( (string) $section ); $out[ $section_key ] = array(); foreach ( $values as $key => $value ) { $k = sanitize_key( (string) $key ); if ( 'general' === $section_key && 'items_per_page' === $k ) { $out[ $section_key ][ $k ] = min( 100, max( 1, (int) $value ) ); continue; } if ( 'general' === $section_key && 'large_file_mb' === $k ) { $out[ $section_key ][ $k ] = min( 500, max( 1, (int) $value ) ); continue; } if ( 'general' === $section_key && 'resize_presets' === $k ) { $out[ $section_key ][ $k ] = $this->sanitize_resize_presets( $value ); continue; } if ( 'general' === $section_key && 'folder_color_palette' === $k ) { $colors = array(); if ( is_array( $value ) ) { foreach ( array_slice( $value, 0, 24 ) as $color ) { $hex = strtoupper( trim( (string) $color ) ); if ( preg_match( '/^#[0-9A-F]{6}$/', $hex ) ) { $colors[] = $hex; } } } $out[ $section_key ][ $k ] = array_values( array_unique( $colors ) ); continue; } if ( 'general' === $section_key && 'theme' === $k ) { $theme = strtolower( sanitize_text_field( (string) $value ) ); $out[ $section_key ][ $k ] = in_array( $theme, array( 'light', 'dark', 'system' ), true ) ? $theme : 'system'; continue; } if ( 'general' === $section_key && in_array( $k, array( 'white_label_logo', 'white_label_icon' ), true ) ) { continue; } if ( 'general' === $section_key && 'media_tab_label' === $k ) { if ( \FoldRocket\Support\Distribution::is_free_package() ) { continue; } $label = sanitize_text_field( (string) $value ); $label = function_exists( 'mb_substr' ) ? mb_substr( $label, 0, 40 ) : substr( $label, 0, 40 ); $out[ $section_key ][ $k ] = trim( $label ); continue; } if ( 'general' === $section_key && 'default_view' === $k ) { $view = strtolower( sanitize_text_field( (string) $value ) ); $out[ $section_key ][ $k ] = in_array( $view, array( 'grid', 'list' ), true ) ? $view : 'grid'; continue; } if ( 'setup' === $section_key && 'completed' === $k ) { $out[ $section_key ][ $k ] = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); continue; } if ( 'setup' === $section_key && 'version' === $k ) { $out[ $section_key ][ $k ] = max( 0, (int) $value ); continue; } if ( 'setup' === $section_key && 'overview' === $k ) { $overview = sanitize_key( (string) $value ); $out[ $section_key ][ $k ] = in_array( $overview, array( 'pending', 'done' ), true ) ? $overview : 'done'; continue; } if ( 'privacy' === $section_key && in_array( $k, array( 'strip_exif', 'send_reduced_only', 'never_send_originals' ), true ) ) { $out[ $section_key ][ $k ] = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); continue; } if ( 'languages' === $section_key && 'ui' === $k ) { $locale = sanitize_text_field( (string) $value ); if ( 'auto' === $locale || preg_match( '/^[a-z]{2,3}(_[A-Z]{2})?$/', $locale ) ) { $out[ $section_key ][ $k ] = $locale; } else { $out[ $section_key ][ $k ] = 'auto'; } continue; } if ( 'languages' === $section_key && 'default' === $k ) { $locale = sanitize_text_field( (string) $value ); $out[ $section_key ][ $k ] = \FoldRocket\Domain\Languages\SupportedLocales::is_supported( $locale ) ? $locale : 'en_US'; continue; } if ( 'languages' === $section_key && 'enabled' === $k && is_array( $value ) ) { $locales = array(); foreach ( $value as $locale ) { $code = sanitize_text_field( (string) $locale ); if ( \FoldRocket\Domain\Languages\SupportedLocales::is_supported( $code ) ) { $locales[] = $code; } } $out[ $section_key ][ $k ] = array_values( array_unique( $locales ) ); continue; } if ( 'ai' === $section_key && 'generation_instructions' === $k ) { $text = sanitize_textarea_field( (string) $value ); $max = \FoldRocket\Infrastructure\AI\AnalysisPrompts::MAX_INSTRUCTIONS_LENGTH; if ( function_exists( 'mb_substr' ) ) { $text = mb_substr( $text, 0, $max ); } else { $text = substr( $text, 0, $max ); } $out[ $section_key ][ $k ] = $text; continue; } if ( is_bool( $value ) ) { $out[ $section_key ][ $k ] = $value; } elseif ( is_int( $value ) || is_float( $value ) ) { $out[ $section_key ][ $k ] = $value; } elseif ( is_array( $value ) ) { $out[ $section_key ][ $k ] = array_map( 'sanitize_text_field', array_map( 'strval', $value ) ); } else { $out[ $section_key ][ $k ] = sanitize_text_field( (string) $value ); } } } return $out; } /** @param mixed $value @return array> */ private function sanitize_resize_presets( $value ): array { if ( ! is_array( $value ) ) { return array(); } $presets = array(); foreach ( array_slice( $value, 0, 8 ) as $preset ) { if ( ! is_array( $preset ) ) { continue; } $label = sanitize_text_field( (string) ( $preset['label'] ?? '' ) ); $label = function_exists( 'mb_substr' ) ? mb_substr( trim( $label ), 0, 20 ) : substr( trim( $label ), 0, 20 ); if ( $label === '' ) { continue; } if ( isset( $preset['scale'] ) ) { $scale = (float) $preset['scale']; if ( $scale <= 0 || $scale >= 1 ) { continue; } $presets[] = array( 'label' => $label, 'scale' => $scale ); } else { $width = (int) ( $preset['width'] ?? 0 ); if ( $width < 1 || $width > 10000 ) { continue; } $presets[] = array( 'label' => $label, 'width' => $width ); } } return $presets; } /** * Merge settings patches. List fields (numeric arrays) replace wholesale — * array_replace_recursive would keep stale indexes when shortening lists * (e.g. languages.enabled after unchecking a locale). * * @param array $current * @param array $incoming * @return array */ private function merge_settings( array $current, array $incoming ): array { foreach ( $incoming as $section => $values ) { if ( ! is_array( $values ) ) { $current[ $section ] = $values; continue; } if ( ! isset( $current[ $section ] ) || ! is_array( $current[ $section ] ) ) { $current[ $section ] = $values; continue; } foreach ( $values as $key => $value ) { if ( is_array( $value ) && $this->is_list_array( $value ) ) { $current[ $section ][ $key ] = $value; continue; } if ( is_array( $value ) ) { $base = isset( $current[ $section ][ $key ] ) && is_array( $current[ $section ][ $key ] ) ? $current[ $section ][ $key ] : array(); $current[ $section ][ $key ] = array_replace_recursive( $base, $value ); continue; } $current[ $section ][ $key ] = $value; } } // Default metadata locale must always stay in the enabled list. if ( isset( $current['languages'] ) && is_array( $current['languages'] ) ) { $default = isset( $current['languages']['default'] ) ? (string) $current['languages']['default'] : ''; $enabled = isset( $current['languages']['enabled'] ) && is_array( $current['languages']['enabled'] ) ? $current['languages']['enabled'] : array(); if ( $default !== '' && ! in_array( $default, $enabled, true ) ) { array_unshift( $enabled, $default ); $current['languages']['enabled'] = array_values( array_unique( $enabled ) ); } } return $current; } /** * @param array $value */ private function is_list_array( array $value ): bool { if ( function_exists( 'array_is_list' ) ) { return array_is_list( $value ); } $i = 0; foreach ( $value as $k => $_v ) { if ( $k !== $i ) { return false; } ++$i; } return true; } }