get_col( "SHOW TABLES LIKE '%_options'" ); $found_prefixes = []; if ( count( $tables ) ) { foreach ( $tables as $table ) { $maybe_prefix = substr( $table, 0, - strlen( 'options' ) ); if ( $maybe_prefix !== $table_prefix ) { $found_prefixes[] = $maybe_prefix; } } } if ( count( $found_prefixes ) ) { sort( $found_prefixes ); $prefix_list = implode( ', ', $found_prefixes ); $install_label = count( $found_prefixes ) > 1 ? 'installations' : 'installation'; WP_CLI::error( "The site you have requested is not installed.\n" . "Your table prefix is '{$table_prefix}'. Found {$install_label} with table prefix: {$prefix_list}.\n" . 'Or, run `wp core install` to create database tables.' ); } else { WP_CLI::error( "The site you have requested is not installed.\n" . 'Run `wp core install` to create database tables.' ); } } } // phpcs:disable WordPress.PHP.IniSet -- Intentional & correct usage. /** * @return void */ function wp_debug_mode() { if ( WP_CLI::get_config( 'debug' ) ) { if ( ! defined( 'WP_DEBUG' ) ) { define( 'WP_DEBUG', true ); } error_reporting( E_ALL & ~E_DEPRECATED ); } else { if ( WP_DEBUG ) { error_reporting( E_ALL ); if ( WP_DEBUG_DISPLAY ) { ini_set( 'display_errors', 1 ); } elseif ( null !== WP_DEBUG_DISPLAY ) { ini_set( 'display_errors', 0 ); } if ( in_array( strtolower( (string) WP_DEBUG_LOG ), [ 'true', '1' ], true ) ) { $log_path = WP_CONTENT_DIR . '/debug.log'; } elseif ( is_string( WP_DEBUG_LOG ) ) { $log_path = WP_DEBUG_LOG; } else { $log_path = false; } if ( false !== $log_path ) { ini_set( 'log_errors', 1 ); ini_set( 'error_log', $log_path ); } } else { error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR ); } if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { ini_set( 'display_errors', 0 ); } } // XDebug already sends errors to STDERR. ini_set( 'display_errors', function_exists( 'xdebug_debug_zval' ) ? false : 'stderr' ); } // phpcs:enable /** * @return void */ function replace_wp_die_handler() { \remove_filter( 'wp_die_handler', '_default_wp_die_handler' ); \add_filter( 'wp_die_handler', function () { return __NAMESPACE__ . '\\wp_die_handler'; } ); } /** * @return never */ function wp_die_handler( $message ) { if ( $message instanceof \WP_Error ) { $text_message = $message->get_error_message(); $error_data = $message->get_error_data( 'internal_server_error' ); if ( ! empty( $error_data['error']['file'] ) && false !== stripos( $error_data['error']['file'], 'themes/functions.php' ) ) { $text_message = 'An unexpected functions.php file in the themes directory may have caused this internal server error.'; } } else { $text_message = $message; } $text_message = wp_clean_error_message( $text_message ); WP_CLI::error( $text_message ); } /** * Clean HTML error message so suitable for text display. * * @param string $message * @return string */ function wp_clean_error_message( $message ) { $original_message = trim( $message ); $message = $original_message; if ( preg_match( '|^\
(.+?)
|', $original_message, $matches ) ) { $message .= ' ' . $matches[1]; } $search_replace = [ '' => '`',
'' => '`',
];
$message = str_replace( array_keys( $search_replace ), array_values( $search_replace ), $message );
$message = namespace\strip_tags( $message );
$message = html_entity_decode( $message, ENT_COMPAT, 'UTF-8' );
return $message;
}
/**
* @param string $url
* @return string
*/
function wp_redirect_handler( $url ) {
WP_CLI::warning( 'Some code is trying to do a URL redirect. Backtrace:' );
ob_start();
debug_print_backtrace();
fwrite( STDERR, ob_get_clean() );
return $url;
}
/**
* @param string $since Version number.
* @param string $path File to include.
* @return void
*/
function maybe_require( $since, $path ) {
if ( wp_version_compare( $since, '>=' ) ) {
require $path;
}
}
/**
*
* @param class-string $class_name
* @param bool $insecure
*
* @return \WP_Upgrader Upgrader instance.
* @throws \ReflectionException
*/
function get_upgrader( $class_name, $insecure = false ) {
if ( ! class_exists( '\WP_Upgrader' ) ) {
if ( file_exists( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ) ) {
include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}
}
if ( ! class_exists( '\WP_Upgrader_Skin' ) ) {
if ( file_exists( ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php' ) ) {
include ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php';
}
}
$uses_insecure_flag = false;
$reflection = new ReflectionClass( $class_name );
$constructor = $reflection->getConstructor();
if ( $constructor ) {
$arguments = $constructor->getParameters();
/** @var ReflectionParameter $argument */
foreach ( $arguments as $argument ) {
if ( 'insecure' === $argument->name ) {
$uses_insecure_flag = true;
break;
}
}
}
if ( $uses_insecure_flag ) {
return new $class_name( new UpgraderSkin(), $insecure );
} else {
return new $class_name( new UpgraderSkin() );
}
}
/**
* Converts a plugin basename back into a friendly slug.
*
* @param string $basename
* @return string
*/
function get_plugin_name( $basename ) {
if ( false === strpos( $basename, '/' ) ) {
$name = basename( $basename, '.php' );
} else {
$name = dirname( $basename );
}
return $name;
}
/**
* Determine whether a plugin is skipped.
*
* @param string $file
* @return bool
*/
function is_plugin_skipped( $file ) {
$name = get_plugin_name( str_replace( WP_PLUGIN_DIR . '/', '', $file ) );
$skipped_plugins = WP_CLI::get_runner()->config['skip-plugins'];
if ( true === $skipped_plugins ) {
return true;
}
if ( ! is_array( $skipped_plugins ) ) {
$skipped_plugins = explode( ',', $skipped_plugins );
}
return in_array( $name, array_filter( $skipped_plugins ), true );
}
/**
* Get theme name from path.
*
* @param string $path
* @return string
*/
function get_theme_name( $path ) {
return basename( $path );
}
/**
* Determine whether a theme is skipped.
*
* @param string $path
* @return bool
*/
function is_theme_skipped( $path ) {
$name = get_theme_name( $path );
$skipped_themes = WP_CLI::get_runner()->config['skip-themes'];
if ( true === $skipped_themes ) {
return true;
}
if ( ! is_array( $skipped_themes ) ) {
$skipped_themes = explode( ',', $skipped_themes );
}
return in_array( $name, array_filter( $skipped_themes ), true );
}
/**
* Register the sidebar for unused widgets.
* Core does this in /wp-admin/widgets.php, which isn't helpful.
*
* @return void
*/
function wp_register_unused_sidebar() {
register_sidebar(
[
'name' => __( 'Inactive Widgets' ),
'id' => 'wp_inactive_widgets',
'class' => 'inactive-sidebar',
'description' => __( 'Drag widgets here to remove them from the sidebar but keep their settings.' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
]
);
}
/**
* Attempts to determine which object cache is being used.
*
* Note that the guesses made by this function are based on the WP_Object_Cache classes
* that define the 3rd party object cache extension. Changes to those classes could render
* problems with this function's ability to determine which object cache is being used.
*
* @return string
*/
function wp_get_cache_type() {
global $_wp_using_ext_object_cache, $wp_object_cache;
$message = 'Unknown';
if ( ! empty( $_wp_using_ext_object_cache ) ) {
// Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend)
if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) {
$message = 'Memcached';
// Test for Memcache PECL extension memcached object cache (https://wordpress.org/extend/plugins/memcached/)
} elseif ( isset( $wp_object_cache->mc ) ) {
$is_memcache = true;
foreach ( $wp_object_cache->mc as $bucket ) {
if ( ! $bucket instanceof \Memcache && ! $bucket instanceof \Memcached ) {
$is_memcache = false;
}
}
if ( $is_memcache ) {
$message = 'Memcache';
}
// Test for Xcache object cache (https://plugins.svn.wordpress.org/xcache/trunk/object-cache.php)
} elseif ( $wp_object_cache instanceof \XCache_Object_Cache ) {
$message = 'Xcache';
// Test for WinCache object cache (https://wordpress.org/extend/plugins/wincache-object-cache-backend/)
} elseif ( class_exists( 'WinCache_Object_Cache' ) ) {
$message = 'WinCache';
// Test for APC object cache (https://wordpress.org/extend/plugins/apc/)
} elseif ( class_exists( 'APC_Object_Cache' ) ) {
$message = 'APC';
// Test for WP Redis (https://wordpress.org/plugins/wp-redis/)
} elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof \Redis ) {
$message = 'Redis';
// Test for Redis Object Cache (https://wordpress.org/plugins/redis-cache/)
} elseif ( method_exists( $wp_object_cache, 'redis_instance' ) && method_exists( $wp_object_cache, 'redis_status' ) ) {
$message = 'Redis';
// Test for Object Cache Pro (https://objectcache.pro/)
} elseif ( method_exists( $wp_object_cache, 'config' ) && method_exists( $wp_object_cache, 'connection' ) ) {
$message = 'Redis';
// Test for WP LCache Object cache (https://github.com/lcache/wp-lcache)
} elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \LCache\Integrated ) {
$message = 'WP LCache';
} elseif ( function_exists( 'w3_instance' ) ) {
$config = w3_instance( 'W3_Config' );
if ( $config->get_boolean( 'objectcache.enabled' ) ) {
$message = 'W3TC ' . $config->get_string( 'objectcache.engine' );
}
}
} else {
$message = 'Default';
}
return $message;
}
/**
* Clear WordPress internal object caches.
*
* In long-running scripts, the internal caches on `$wp_object_cache` and `$wpdb`
* can grow to consume gigabytes of memory. Periodically calling this utility
* can help with memory management.
*
* @access public
* @category System
* @deprecated 1.5.0
*
* @return void
*/
function wp_clear_object_cache() {
global $wpdb, $wp_object_cache;
$wpdb->queries = [];
if ( function_exists( 'wp_cache_flush_runtime' ) && function_exists( 'wp_cache_supports' ) ) {
if ( wp_cache_supports( 'flush_runtime' ) ) {
wp_cache_flush_runtime();
return;
}
}
if ( ! is_object( $wp_object_cache ) ) {
return;
}
// The following are Memcached (Redux) plugin specific (see https://core.trac.wordpress.org/ticket/31463).
if ( isset( $wp_object_cache->group_ops ) ) {
$wp_object_cache->group_ops = [];
}
if ( isset( $wp_object_cache->stats ) ) {
$wp_object_cache->stats = [];
}
if ( isset( $wp_object_cache->memcache_debug ) ) {
$wp_object_cache->memcache_debug = [];
}
// Used by `WP_Object_Cache` also.
if ( isset( $wp_object_cache->cache ) ) {
$wp_object_cache->cache = [];
}
}
/**
* Get a set of tables in the database.
*
* Interprets common command-line options into a resolved set of table names.
*
* @param array