1, 'enable_twitter' => 1, 'default_image' => 0, 'twitter_site' => '', 'fb_app_id' => '', 'skip_if_seo_plugin' => 1, ); } /** * Returns the cleaned settings, read once per request. * * @return array */ public static function get() { if ( null === self::$cache ) { self::$cache = self::clean( get_option( self::OPTION, array() ) ); } return self::$cache; } /** * Forgets the in-memory copy of the settings. * * @return void */ public static function flush_cache() { self::$cache = null; } /** * Cleans a settings array. Unknown keys are dropped, missing keys get their * default, and every value is forced to the right type. * * @param mixed $raw Whatever was stored or submitted. * @param array|null $errors Pass an array (by reference) to collect * messages about values that were rejected. * Each entry is array( code, message ). * @return array Settings with exactly the keys of defaults(). */ public static function clean( $raw, &$errors = null ) { $report = is_array( $errors ); $raw = is_array( $raw ) ? $raw : array(); $raw = array_merge( self::defaults(), $raw ); $clean = array(); foreach ( self::SWITCHES as $key ) { // Stored as 0/1. A string "0" counts as off. $clean[ $key ] = empty( $raw[ $key ] ) ? 0 : 1; } $clean['default_image'] = is_scalar( $raw['default_image'] ) ? absint( $raw['default_image'] ) : 0; $clean['twitter_site'] = self::clean_handle( $raw['twitter_site'] ); if ( $report && '' === $clean['twitter_site'] && self::has_input( $raw['twitter_site'] ) ) { $errors[] = array( 'devlab_og_twitter_site', __( 'The X (Twitter) username was not saved. Use 1 to 15 letters, numbers or underscores, for example @devlab.', 'devlab-open-graph' ), ); } $clean['fb_app_id'] = self::clean_app_id( $raw['fb_app_id'] ); if ( $report && '' === $clean['fb_app_id'] && self::has_input( $raw['fb_app_id'] ) ) { $errors[] = array( 'devlab_og_fb_app_id', __( 'The Facebook App ID was not saved. It is a number with 5 to 20 digits.', 'devlab-open-graph' ), ); } return $clean; } /** * Whether the user typed something, before any cleaning. Used to tell * "left empty" from "typed something that had to be rejected" (for * example markup, which sanitize_text_field() reduces to nothing). * * @param mixed $value Raw value. * @return bool */ private static function has_input( $value ) { return is_scalar( $value ) && '' !== trim( (string) $value ); } /** * Turns a scalar into a trimmed single-line string, or an empty string for * anything else (arrays, objects). * * @param mixed $value Raw value. * @return string */ private static function flat_string( $value ) { return is_scalar( $value ) ? trim( sanitize_text_field( (string) $value ) ) : ''; } /** * Normalises an X (Twitter) username to "@name", or '' when it is invalid. * * Accepts "@name", "name" and a pasted profile address on x.com or * twitter.com. * * @param mixed $value Raw value. * @return string */ private static function clean_handle( $value ) { $value = self::flat_string( $value ); if ( '' === $value ) { return ''; } if ( preg_match( '~^(?:https?://)?(?:www\.|mobile\.)?(?:twitter|x)\.com/@?([A-Za-z0-9_]{1,15})/?(?:[?#].*)?$~i', $value, $match ) ) { $value = $match[1]; } $value = ltrim( $value, '@' ); if ( ! preg_match( '/^[A-Za-z0-9_]{1,15}$/', $value ) ) { return ''; } return '@' . $value; } /** * Validates a Facebook App ID: digits only. * * @param mixed $value Raw value. * @return string The digits, or '' when invalid. */ private static function clean_app_id( $value ) { $value = self::flat_string( $value ); return preg_match( '/^[0-9]{5,20}$/', $value ) ? $value : ''; } /** * Registers the option with the Settings API. * * @return void */ public function register() { register_setting( self::GROUP, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => self::defaults(), 'show_in_rest' => false, ) ); } /** * Settings API callback: cleans the submitted form and reports problems. * * @param mixed $input Submitted value (already unslashed by options.php). * @return array */ public function sanitize( $input ) { $input = is_array( $input ) ? $input : array(); // An unticked checkbox is missing from the form: that means "off". // On a second pass the keys are already there, so nothing changes. foreach ( self::SWITCHES as $key ) { if ( ! array_key_exists( $key, $input ) ) { $input[ $key ] = 0; } } $errors = array(); $clean = self::clean( $input, $errors ); if ( $clean['default_image'] && ! DevLab_OG_Image::is_usable_attachment( $clean['default_image'] ) ) { $clean['default_image'] = 0; $errors[] = array( 'devlab_og_default_image', __( 'The default image was not saved. Choose a JPG, PNG, GIF or WebP image from the media library.', 'devlab-open-graph' ), ); } // The callback can run twice for one save; report each problem once. static $reported = array(); foreach ( $errors as $error ) { if ( isset( $reported[ $error[0] ] ) ) { continue; } $reported[ $error[0] ] = true; add_settings_error( self::MESSAGES, $error[0], $error[1], 'error' ); } return $clean; } }