* array( tab, section, field ) in 2.0. * * @var array */ private const FEATURES = array( 'emojis' => array( 'performance', 'frontend_performance', 'emojis' ), 'embed' => array( 'performance', 'frontend_performance', 'embed' ), 'heartbeat' => array( 'performance', 'frontend_performance', 'heartbeat' ), 'dashicons' => array( 'performance', 'frontend_performance', 'dashicons' ), 'blocks' => array( 'performance', 'frontend_performance', 'block_library' ), 'oembed' => array( 'performance', 'frontend_performance', 'oembed' ), 'generator' => array( 'performance', 'head_cleanup', 'generator' ), 'rsdlink' => array( 'performance', 'head_cleanup', 'rsd_link' ), 'manifest' => array( 'performance', 'head_cleanup', 'wlw_manifest' ), 'shortlink' => array( 'performance', 'head_cleanup', 'shortlink' ), 'rssfeeds' => array( 'performance', 'head_cleanup', 'rss_feeds' ), 'adjacentposts' => array( 'performance', 'head_cleanup', 'adjacent_posts' ), 'version' => array( 'performance', 'head_cleanup', 'version_strings' ), 'removecapitalpdangit' => array( 'performance', 'head_cleanup', 'capital_p_dangit' ), 'removehowdy' => array( 'admin', 'admin_experience', 'remove_howdy' ), 'removeitemsadminbar' => array( 'admin', 'admin_experience', 'remove_adminbar_items' ), 'removescreenoptions' => array( 'admin', 'admin_experience', 'screen_options' ), 'emptytrash' => array( 'performance', 'maintenance', 'empty_trash' ), 'pdfthumbnails' => array( 'performance', 'maintenance', 'pdf_thumbnails' ), 'remoteblockpatterns' => array( 'performance', 'maintenance', 'remote_block_patterns' ), 'xmlrpc' => array( 'security', 'security_access', 'xmlrpc' ), 'pluginandthemeeditor' => array( 'security', 'security_access', 'plugin_theme_editor' ), 'blockuserenumeration' => array( 'security', 'security_access', 'block_user_enumeration' ), 'applicationpasswords' => array( 'security', 'security_access', 'application_passwords' ), 'restapi' => array( 'security', 'security_access', 'rest_api' ), 'coreprivacytools' => array( 'security', 'security_access', 'core_privacy_tools' ), 'sitehealth' => array( 'security', 'security_access', 'site_health' ), 'disableauthorarchives' => array( 'security', 'author_pages', 'enabled' ), ); /** * Hook registration: run on `init` after the modules have booted. * * @return void */ public static function maybe_run(): void { if ( get_option( self::DONE_OPTION ) ) { return; } if ( self::is_v1_site() ) { self::migrate(); } update_option( self::DONE_OPTION, AIOU_VERSION, true ); } /** * Whether 1.x ever ran on this site. * * @return bool */ private static function is_v1_site(): bool { foreach ( array( 'aiowpu_db_version', 'aiowpu_disable_unnecessary_features_options', 'aiowpu_featured_image_id' ) as $option ) { if ( false !== get_option( $option ) ) { return true; } } foreach ( array( 'username_updater', 'user_multiple_roles', 'set_featured_image', 'disable_unnecessary_features' ) as $slug ) { if ( false !== get_option( 'aiowpu_enabled_' . $slug ) ) { return true; } } return false; } /** * Whether a 1.x module was on. Mirrors 1.x's aiowpu_module_enabled(): * the saved value, or the module's default when nothing was saved. * * @param string $slug 1.x module slug. * @param bool $default_on 1.x default. * @return bool */ private static function v1_module_on( string $slug, bool $default_on ): bool { $saved = get_option( 'aiowpu_enabled_' . $slug, null ); if ( null === $saved ) { return $default_on; } return self::truthy( $saved ); } /** * 1.x saved checkboxes as 1 (and sometimes '1', 'yes' or 'on'). * * @param mixed $value Saved value. * @return bool */ private static function truthy( mixed $value ): bool { return true === $value || 1 === $value || ( is_string( $value ) && in_array( strtolower( $value ), array( '1', 'yes', 'on', 'true' ), true ) ); } /** * Copy everything over. * * @return void */ private static function migrate(): void { $set = array(); // Username Updater and Multiple Roles: on by default in 1.x. if ( self::v1_module_on( 'username_updater', true ) ) { $set[] = array( 'users', 'username_updater', 'enabled', 1 ); } if ( self::v1_module_on( 'user_multiple_roles', true ) ) { $set[] = array( 'users', 'user_roles', 'enabled', 1 ); } // Default featured image. $image_id = (int) get_option( 'aiowpu_featured_image_id', 0 ); if ( $image_id > 0 && wp_attachment_is_image( $image_id ) ) { $set[] = array( 'media', 'default_featured_image', 'image_id', $image_id ); if ( self::v1_module_on( 'set_featured_image', false ) ) { $set[] = array( 'media', 'default_featured_image', 'enabled', 1 ); } } // Disable Unnecessary Features: only applied in 1.x when its module was on. $features = get_option( 'aiowpu_disable_unnecessary_features_options' ); if ( is_array( $features ) && self::v1_module_on( 'disable_unnecessary_features', false ) ) { foreach ( self::FEATURES as $old => $target ) { if ( self::truthy( $features[ 'aiowpu_duf_' . $old ] ?? null ) ) { $set[] = array( $target[0], $target[1], $target[2], 1 ); } } // "Clean Dashboard" removed every core widget; 2.0 chooses per widget. if ( self::truthy( $features['aiowpu_duf_cleandashboard'] ?? null ) && class_exists( \WordTenance\AllInOneUtilities\Modules\AdminCustomization\DashboardWidgets::class ) ) { $set[] = array( 'admin', 'dashboard', 'hide_widgets', array_keys( \WordTenance\AllInOneUtilities\Modules\AdminCustomization\DashboardWidgets::core() ) ); $set[] = array( 'admin', 'dashboard', 'hide_welcome_panel', 1 ); } } self::write( $set ); } /** * Write values into the 2.0 tab options, without replacing anything * already saved there. * * @param array $set Values to set. * @return void */ private static function write( array $set ): void { $tabs = array(); foreach ( $set as list( $tab, $section, $field, $value ) ) { if ( ! isset( $tabs[ $tab ] ) ) { $stored = get_option( 'aiou_settings_' . $tab, array() ); $tabs[ $tab ] = is_array( $stored ) ? $stored : array(); } if ( ! isset( $tabs[ $tab ][ $section ] ) || ! is_array( $tabs[ $tab ][ $section ] ) ) { $tabs[ $tab ][ $section ] = array(); } if ( ! array_key_exists( $field, $tabs[ $tab ][ $section ] ) ) { $tabs[ $tab ][ $section ][ $field ] = $value; } } foreach ( $tabs as $tab => $values ) { update_option( 'aiou_settings_' . $tab, $values ); } } }