*/ function webtospeech_default_settings() { return array( 'site_key' => '', 'verification_token' => '', 'position' => 'above', 'theme' => 'auto', 'post_types' => array( 'post' ), ); } /** * The saved settings, with defaults filled in for anything missing. * * @return array */ function webtospeech_settings() { $saved = get_option( WEBTOSPEECH_OPTION, array() ); return array_merge( webtospeech_default_settings(), is_array( $saved ) ? $saved : array() ); } /** * One saved setting. * * @param string $name The setting's name. * @return mixed */ function webtospeech_setting( $name ) { $settings = webtospeech_settings(); return isset( $settings[ $name ] ) ? $settings[ $name ] : null; } /** * The public post types a player can be added to automatically, keyed by name. * * @return array */ function webtospeech_available_post_types() { $types = get_post_types( array( 'public' => true ), 'objects' ); unset( $types['attachment'] ); return $types; } /** * Find a site key in what was pasted: the key itself, or the whole snippet from webtospeech.app. * * @param string $value What was entered. * @return string The key, or an empty string when there isn't one. */ function webtospeech_parse_site_key( $value ) { return preg_match( '/\bws_[A-Za-z0-9]+\b/', (string) $value, $matches ) ? $matches[0] : ''; } /** * Find a verification token in what was pasted: the token itself, the whole meta tag, or the DNS record. * * @param string $value What was entered. * @return string The token, or an empty string when there isn't one. */ function webtospeech_parse_verification_token( $value ) { $value = trim( (string) $value ); if ( preg_match( '/content\s*=\s*["\']?([A-Za-z0-9]+)/i', $value, $matches ) ) { return $matches[1]; } if ( preg_match( '/webtospeech-verification\s*=\s*([A-Za-z0-9]+)/i', $value, $matches ) ) { return $matches[1]; } return preg_match( '/^[A-Za-z0-9]+$/', $value ) ? $value : ''; } /** * Clean the settings submitted from the settings page before they're saved. * * @param mixed $input The submitted values. * @return array */ function webtospeech_sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $previous = webtospeech_settings(); $clean = webtospeech_default_settings(); // The key is picked out with a strict pattern, so what was pasted (perhaps the whole snippet, tags and all) is // searched as it is, and only the key is kept. $raw_key = isset( $input['site_key'] ) ? trim( wp_unslash( (string) $input['site_key'] ) ) : ''; $key = webtospeech_parse_site_key( $raw_key ); if ( '' !== $raw_key && '' === $key ) { add_settings_error( WEBTOSPEECH_OPTION, 'webtospeech_site_key', __( 'That doesn’t look like a WebtoSpeech site key. Keys start with “ws_”. Your previous key was kept.', 'webtospeech' ) ); $key = $previous['site_key']; } $clean['site_key'] = $key; $raw_token = isset( $input['verification_token'] ) ? trim( wp_unslash( (string) $input['verification_token'] ) ) : ''; $token = webtospeech_parse_verification_token( $raw_token ); if ( '' !== $raw_token && '' === $token ) { add_settings_error( WEBTOSPEECH_OPTION, 'webtospeech_verification_token', __( 'That doesn’t look like a verification token. Copy it from Settings → Sites on webtospeech.app. Your previous token was kept.', 'webtospeech' ) ); $token = $previous['verification_token']; } $clean['verification_token'] = $token; if ( isset( $input['position'] ) && in_array( $input['position'], array( 'above', 'below' ), true ) ) { $clean['position'] = $input['position']; } if ( isset( $input['theme'] ) && in_array( $input['theme'], array( 'auto', 'light', 'dark' ), true ) ) { $clean['theme'] = $input['theme']; } $submitted_types = isset( $input['post_types'] ) && is_array( $input['post_types'] ) ? array_map( 'sanitize_key', $input['post_types'] ) : array(); $clean['post_types'] = array_values( array_intersect( $submitted_types, array_keys( webtospeech_available_post_types() ) ) ); return $clean; }