true, 'file_storage_location' => 'local', 'dropbox_app_key' => '', 'dropbox_app_secret' => '', 'dropbox_access_token' => '', 'dropbox_token_expires' => '', 'voice_model' => 'gpt-4o-mini-tts', 'audio_language' => '', 'default_voice_instructions' => '', 'show_player_label' => true, 'player_label' => 'Listen to the audio version of this article (generated by AI).', 'post_types' => 'all', 'post_types_to_enable' => array(), 'include_title' => true, 'include_author' => true, 'include_date' => true, 'custom_text_before_content' => '', 'custom_text_after_content' => '', 'custom_pronunciations' => '', 'auto_generate_tts' => false, 'default_voice' => '', 'player_background_color' => '', 'player_background_color_mode' => 'default', 'player_color' => '', 'player_color_mode' => 'default', 'player_height' => 'default', 'player_speed' => '1', 'enable_statistics' => true, 'last_usage_check' => '', 'error_log' => '', 'user_roles' => 'all', 'user_roles_to_enable' => array(), ); if ( isset( $default_options[$option] ) ) { return ( is_array( $default_options[$option] ) ? array_map( 'sanitize_text_field', $default_options[$option] ) : sanitize_textarea_field( $default_options[$option] ) ); } return ''; } // Update specific option function ai_tts_update_option( $option, $value ) { $options = get_option( 'ai_tts_settings' ); if ( !$options ) { $options = array(); } $options[$option] = $value; update_option( 'ai_tts_settings', $options ); } // Options page callback function ai_tts_options_page() { ?>
'sanitize_text_field', ) ); register_setting( 'ai_tts_settings', 'ai_tts_settings', 'ai_tts_sanitise_settings' ); } /** * Sanitize AI TTS plugin settings before saving. * * @param array $input Raw input settings array. * @return array Sanitised settings array. */ function ai_tts_sanitise_settings( $input ) { // Ensure input is an array if ( !is_array( $input ) ) { return array(); } // Preserve existing values for fields intentionally absent from the form submission // (e.g. disabled inputs for Dropbox credentials when already connected). $existing = get_option( 'ai_tts_settings', array() ); if ( !is_array( $existing ) ) { $existing = array(); } $preserve_if_missing = array( 'dropbox_app_key', 'dropbox_app_secret', 'dropbox_access_token', 'dropbox_token_expires' ); foreach ( $preserve_if_missing as $key ) { if ( !isset( $input[$key] ) && isset( $existing[$key] ) ) { $input[$key] = $existing[$key]; } } // Sanitize checkbox arrays $checkbox_arrays = array('post_types_to_enable', 'user_roles_to_enable'); foreach ( $checkbox_arrays as $key ) { if ( isset( $input[$key] ) && is_array( $input[$key] ) ) { $input[$key] = array_map( 'sanitize_text_field', $input[$key] ); } else { $input[$key] = array(); } } // Sanitize custom_pronunciations (nested array) if ( isset( $input['custom_pronunciations'] ) && is_array( $input['custom_pronunciations'] ) ) { foreach ( $input['custom_pronunciations'] as $index => $pair ) { $input['custom_pronunciations'][$index] = array( 'word' => ( isset( $pair['word'] ) ? sanitize_text_field( $pair['word'] ) : '' ), 'pronunciation' => ( isset( $pair['pronunciation'] ) ? sanitize_text_field( $pair['pronunciation'] ) : '' ), ); } } else { $input['custom_pronunciations'] = array(); } // Sanitize text/textarea inputs $text_fields = array( 'voice_model', 'player_label', 'custom_text_before_content', 'custom_text_after_content', 'default_voice', 'default_voice_style', 'file_storage_location', 'dropbox_app_key', 'dropbox_app_secret', 'dropbox_access_token', 'dropbox_token_expires', 'player_background_color', 'player_background_color_mode', 'player_color', 'player_color_mode', 'player_height', 'player_speed', 'post_types', 'user_roles', 'last_usage_check' ); foreach ( $text_fields as $key ) { if ( isset( $input[$key] ) ) { $input[$key] = sanitize_text_field( $input[$key] ); } } // Audio language: automatic (empty), the site language, or a supported language code if ( isset( $input['audio_language'] ) ) { $audio_language = sanitize_key( $input['audio_language'] ); $input['audio_language'] = ( 'site' === $audio_language || array_key_exists( $audio_language, ai_tts_get_audio_languages() ) ? $audio_language : '' ); } if ( isset( $input['default_voice_instructions'] ) ) { $input['default_voice_instructions'] = sanitize_textarea_field( $input['default_voice_instructions'] ); } // The error log holds one entry per line. if ( isset( $input['error_log'] ) ) { $input['error_log'] = sanitize_textarea_field( $input['error_log'] ); } // Sanitize boolean checkboxes (cast to bool) $checkboxes = array( 'show_pricing_estimate', 'show_player_label', 'include_title', 'include_author', 'include_date', 'auto_generate_tts', 'enable_statistics' ); // A missing checkbox means "unchecked" only when the settings form was submitted. Other saves // (e.g. ai_tts_add_to_error_log()) leave it missing, so its default still applies. // phpcs:ignore WordPress.Security.NonceVerification.Missing -- options.php verifies the nonce for this form. $is_settings_form = isset( $_POST['option_page'] ) && 'ai_tts_settings' === sanitize_key( wp_unslash( $_POST['option_page'] ) ); foreach ( $checkboxes as $key ) { if ( isset( $input[$key] ) ) { $input[$key] = (bool) $input[$key]; } elseif ( $is_settings_form ) { $input[$key] = false; } } return $input; } /** * Add a message to the AI TTS error log. */ function ai_tts_add_to_error_log( $message ) { // Newest entry first, one entry per line, keeping the last 10. $entry = current_time( 'mysql' ) . ' - ' . preg_replace( '/\\s*[\\r\\n]+\\s*/', ' ', (string) $message ); $ai_tts_error_log = trim( (string) ai_tts_get_option( 'error_log' ) ); $ai_tts_error_log = ( '' !== $ai_tts_error_log ? explode( "\n", $ai_tts_error_log ) : array() ); array_unshift( $ai_tts_error_log, $entry ); $ai_tts_error_log = implode( "\n", array_slice( $ai_tts_error_log, 0, 10 ) ); $options = get_option( 'ai_tts_settings' ); if ( !$options ) { $options = array(); } $options['error_log'] = $ai_tts_error_log; update_option( 'ai_tts_settings', $options ); }