array( type, parent, tab, rest_writable, site_specific ). * * Built on first use from the option arrays options.php defines, so it must not be * called before options.php has loaded. The result is cached for the request. * * - type: toggle | text | html | url | number | email | email_list | * user_list | plugin_list | key_list * - parent: the toggle this sub-setting belongs to (full option name), or null * - tab: general | security | performance | email_notifications * - rest_writable: whether the REST route may change it (reads cover everything) * - site_specific: never exported or imported (one site's inbox must not travel) * * @return array */ function inspry_toolkit_settings_schema() { static $schema = null; if ( null !== $schema ) { return $schema; } global $toolkit_options, $toolkit_security_options, $toolkit_performance_options, $toolkit_email_notification_options; $tabs = array( 'general' => $toolkit_options, 'security' => $toolkit_security_options, 'performance' => $toolkit_performance_options, 'email_notifications' => $toolkit_email_notification_options, ); $built = array(); foreach ( $tabs as $tab => $options ) { foreach ( is_array( $options ) ? $options : array() as $option ) { if ( ! is_array( $option ) || empty( $option['name'] ) || ! is_string( $option['name'] ) ) { continue; } $built[ 'inspry_toolkit_' . $option['name'] ] = array( 'type' => 'toggle', 'parent' => null, 'tab' => $tab, 'rest_writable' => false, 'site_specific' => false, ); } } // Called before options.php loaded (nothing to describe yet): answer empty, cache nothing. if ( empty( $built ) ) { return array(); } // Sub-settings: name without the prefix => array( type, parent toggle without the prefix ). $sub_settings = array( // General tab. 'limit_admins_list' => array( 'user_list', 'limit_admins' ), 'maintenance_mode_heading' => array( 'html', 'maintenance_mode' ), 'maintenance_mode_sentence' => array( 'html', 'maintenance_mode' ), 'backend_footer_agency_name' => array( 'text', 'backend_footer' ), 'backend_footer_agency_url' => array( 'url', 'backend_footer' ), 'backend_footer_agency_email' => array( 'email', 'backend_footer' ), 'backend_logo_image' => array( 'url', 'backend_logo' ), 'development_environment_user_list' => array( 'user_list', 'manage_environment_indicator' ), 'development_environment_host_name' => array( 'text', 'manage_environment_indicator' ), 'manage_development_environment_banner_color' => array( 'key_list', 'manage_environment_indicator' ), // Security tab. 'disable_installs_and_updates_list' => array( 'user_list', 'disable_installs_and_updates' ), 'remove_site_health_user_list' => array( 'user_list', 'remove_site_health' ), 'checksum_verification_for_wordpress_core_files_email_send' => array( 'email', 'checksum_verification_for_wordpress_core_files' ), 'limit_users_modify_admin_email_user_list' => array( 'user_list', 'limit_users_modify_admin_email' ), 'disable_admin_notice_user_list' => array( 'user_list', 'disable_admin_notice' ), // Performance tab. 'limit_post_revisions_number' => array( 'number', 'limit_post_revisions' ), // Email Notifications tab. 'change_email_sender_address' => array( 'email', 'change_email_sender' ), 'change_email_sender_name' => array( 'text', 'change_email_sender' ), 'change_user_notifications_email_sender' => array( 'email', 'change_user_notifications_email' ), 'enable_plugin_update_admin_notifications_list' => array( 'user_list', 'enable_plugin_update_admin_notifications' ), 'enable_plugin_update_admin_notifications_plugin_list' => array( 'plugin_list', 'enable_plugin_update_admin_notifications' ), 'enable_plugin_activations_admin_notifications_list' => array( 'user_list', 'enable_plugin_activations_admin_notifications' ), 'enable_plugin_activations_admin_notifications_plugin_list' => array( 'plugin_list', 'enable_plugin_activations_admin_notifications' ), 'payments_test_mode_check_admin_notifications_list' => array( 'user_list', 'payments_test_mode_check' ), 'payments_disable_check_admin_notifications_list' => array( 'user_list', 'payments_disable_check' ), 'comment_notification_recipients_list' => array( 'email_list', 'comment_notification_recipients' ), ); foreach ( $sub_settings as $name => $spec ) { $parent = 'inspry_toolkit_' . $spec[1]; $built[ 'inspry_toolkit_' . $name ] = array( 'type' => $spec[0], 'parent' => $parent, 'tab' => isset( $built[ $parent ] ) ? $built[ $parent ]['tab'] : null, 'rest_writable' => false, 'site_specific' => false, ); } // One client's inbox must never travel to another site in a settings file (see tools.php). $built['inspry_toolkit_comment_notification_recipients_list']['site_specific'] = true; /* * What the REST route may change. Deliberately small: these three have no side * effect beyond the email text they alter, and none can lock an administrator * out, take the site offline, disable updates, write to .htaccess, or stop a * user's own emails (disable_auto_update_emails replaces * wp_new_user_notification, so it is NOT here). Widening this list is a reviewed * change to this one place. */ $writable = array( 'change_user_notifications_email', 'change_user_notifications_email_sender', 'disable_admin_email_verification', ); foreach ( $writable as $name ) { if ( isset( $built[ 'inspry_toolkit_' . $name ] ) ) { $built[ 'inspry_toolkit_' . $name ]['rest_writable'] = true; } } $schema = $built; return $schema; } /** * The sanitize callback (options.php) for a value shape. * * @param string $type Schema type. * @return string Function name. */ function inspry_toolkit_settings_sanitizer( $type ) { $map = array( 'toggle' => 'inspry_toolkit_sanitize_toggle', 'text' => 'inspry_toolkit_sanitize_text', 'html' => 'inspry_toolkit_sanitize_html', 'url' => 'inspry_toolkit_sanitize_url', 'number' => 'inspry_toolkit_sanitize_number', 'email' => 'inspry_toolkit_sanitize_email', 'email_list' => 'inspry_toolkit_sanitize_email_list', 'user_list' => 'inspry_toolkit_sanitize_user_list', 'plugin_list' => 'inspry_toolkit_sanitize_plugin_list', 'key_list' => 'inspry_toolkit_sanitize_key_list', ); return isset( $map[ $type ] ) ? $map[ $type ] : 'inspry_toolkit_sanitize_text'; } /** * Whether a stored toggle value means "on", the way the rest of the plugin reads it. * * The plugin compares toggles loosely (`== 1`), so '1', 1 and true are on and * everything else, including a never-saved false, is off. * * @param mixed $raw Stored value. * @return bool */ function inspry_toolkit_settings_is_on( $raw ) { return is_scalar( $raw ) && 1 == $raw; // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- the plugin's own comparison. } /** * A stored value in one canonical form, for comparing and for the REST response. * * A toggle becomes '1', '0' or ''. A list becomes an array of strings, or '' when * empty. Anything else becomes a string. This is what the etag is computed over, * so that an object cache returning int 1 where the database holds '1' does not * change it. * * @param string $type Schema type. * @param mixed $raw Stored value. * @return string|string[] */ function inspry_toolkit_settings_normalize( $type, $raw ) { switch ( $type ) { case 'toggle': if ( inspry_toolkit_settings_is_on( $raw ) ) { return '1'; } return ( 0 === $raw || '0' === $raw ) ? '0' : ''; case 'user_list': case 'plugin_list': case 'key_list': if ( ! is_array( $raw ) && ! is_object( $raw ) ) { return ''; } $out = array(); foreach ( (array) $raw as $item ) { if ( is_scalar( $item ) ) { $out[] = (string) $item; } } return $out ? $out : ''; default: return is_scalar( $raw ) ? (string) $raw : ''; } } /** * Why a stored value could misbehave at runtime, or null when it is fine. * * This is the question the fleet check asks ("is anything on this site in a * state the plugin cannot handle?"), so it is deliberately lenient: a never-saved * value (false), '' and a toggle stored as int 1 or 0 are all fine. A sub-setting * is only judged while its toggle is on, since nothing reads it otherwise. * * @param string $type Schema type. * @param mixed $raw Stored value. * @param bool $parent_on Whether the setting's toggle is on (true for a toggle itself). * @return string|null */ function inspry_toolkit_settings_shape_error( $type, $raw, $parent_on ) { if ( false === $raw || null === $raw || '' === $raw ) { return null; } if ( 'toggle' === $type ) { if ( is_bool( $raw ) || ( is_scalar( $raw ) && in_array( (string) $raw, array( '1', '0' ), true ) ) ) { return null; } return 'not an on/off value'; } if ( ! $parent_on ) { return null; } switch ( $type ) { case 'user_list': case 'plugin_list': case 'key_list': return ( is_array( $raw ) || is_object( $raw ) ) ? null : 'expected a list'; case 'email': return ( is_scalar( $raw ) && is_email( trim( (string) $raw ) ) ) ? null : 'not a valid email address'; default: return is_scalar( $raw ) ? null : 'expected text'; } } /** * Check a value sent to the REST route and return its canonical form. * * Stricter than the save path: nothing is coerced. A toggle must be a boolean, * 1/0, or '1'/'0'/''; an email must be empty or valid. * * @param string $type Schema type. * @param mixed $value Submitted value. * @return array array( 'error' => string|null, 'value' => canonical value ) */ function inspry_toolkit_settings_validate_rest( $type, $value ) { switch ( $type ) { case 'toggle': if ( true === $value || 1 === $value || '1' === $value ) { return array( 'error' => null, 'value' => '1' ); } if ( 0 === $value || '0' === $value ) { return array( 'error' => null, 'value' => '0' ); } if ( false === $value || '' === $value || null === $value ) { return array( 'error' => null, 'value' => '' ); } return array( 'error' => 'must be true or false', 'value' => null ); case 'email': if ( null === $value ) { $value = ''; } if ( ! is_string( $value ) ) { return array( 'error' => 'must be a string', 'value' => null ); } $value = trim( $value ); if ( '' === $value || is_email( $value ) ) { return array( 'error' => null, 'value' => $value ); } return array( 'error' => 'not a valid email address', 'value' => null ); default: return array( 'error' => 'this setting cannot be changed through the REST route', 'value' => null ); } }