WP_CLI\Utils
wp_get_table_names()
Get a set of tables in the database.
Interprets common command-line options into a resolved set of table names.
Хуков нет.
Возвращает
Массив<Строку>.
Использование
wp_get_table_names( $args, $assoc_args );
- $args(array
) (обязательный) - Provided table names, or tables with wildcards.
- $assoc_args
- .
По умолчанию:[]
Код wp_get_table_names() wp get table names WP-CLI 2.13.0-alpha
function wp_get_table_names( $args, $assoc_args = [] ) {
global $wpdb;
// Abort if incompatible args supplied.
if ( get_flag_value( $assoc_args, 'base-tables-only' ) && get_flag_value( $assoc_args, 'views-only' ) ) {
WP_CLI::error( 'You cannot supply --base-tables-only and --views-only at the same time.' );
}
// Pre-load tables SQL query with Views restriction if needed.
if ( get_flag_value( $assoc_args, 'base-tables-only' ) ) {
$tables_sql = 'SHOW FULL TABLES WHERE Table_Type = "BASE TABLE"';
} elseif ( get_flag_value( $assoc_args, 'views-only' ) ) {
$tables_sql = 'SHOW FULL TABLES WHERE Table_Type = "VIEW"';
}
if ( get_flag_value( $assoc_args, 'all-tables' ) ) {
if ( empty( $tables_sql ) ) {
$tables_sql = 'SHOW TABLES';
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is safe, see above.
$tables = $wpdb->get_col( $tables_sql, 0 );
} elseif ( get_flag_value( $assoc_args, 'all-tables-with-prefix' ) ) {
if ( empty( $tables_sql ) ) {
$tables_sql = $wpdb->prepare( 'SHOW TABLES LIKE %s', esc_like( $wpdb->get_blog_prefix() ) . '%' );
} else {
$tables_sql .= sprintf( " AND %s LIKE '%s'", esc_sql_ident( 'Tables_in_' . $wpdb->dbname ), esc_like( $wpdb->get_blog_prefix() ) . '%' );
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared, see above.
$tables = $wpdb->get_col( $tables_sql, 0 );
} else {
$scope = get_flag_value( $assoc_args, 'scope', 'all' );
// Note: BC change 1.5.0, taking scope into consideration for network also.
if ( get_flag_value( $assoc_args, 'network' ) && is_multisite() ) {
$network_global_scope = in_array( $scope, [ 'all', 'global', 'ms_global' ], true ) ? ( 'all' === $scope ? 'global' : $scope ) : '';
$wp_tables = array_values( $wpdb->tables( $network_global_scope ) );
if ( in_array( $scope, [ 'all', 'blog' ], true ) ) {
// Do directly for compat with old WP versions. Note: private, deleted, archived sites are not excluded.
$blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs WHERE site_id = $wpdb->siteid" );
foreach ( $blog_ids as $blog_id ) {
$wp_tables = array_merge( $wp_tables, array_values( $wpdb->tables( 'blog', true /*prefix*/, $blog_id ) ) );
}
}
} else {
$wp_tables = array_values( $wpdb->tables( $scope ) );
}
// The global_terms_enabled() function has been deprecated with WP 6.1+.
if ( wp_version_compare( '6.1', '>=' ) || ! global_terms_enabled() ) { // phpcs:ignore WordPress.WP.DeprecatedFunctions.global_terms_enabledFound
// Only include sitecategories when it's actually enabled.
$wp_tables = array_values( array_diff( $wp_tables, [ $wpdb->sitecategories ] ) );
}
// Note: BC change 1.5.0, tables are sorted (via TABLES view).
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- uses esc_sql_ident() and $wpdb->_escape().
$tables = $wpdb->get_col( sprintf( "SHOW TABLES WHERE %s IN ('%s')", esc_sql_ident( 'Tables_in_' . $wpdb->dbname ), implode( "', '", $wpdb->_escape( $wp_tables ) ) ) );
if ( get_flag_value( $assoc_args, 'base-tables-only' ) || get_flag_value( $assoc_args, 'views-only' ) ) {
// Apply Views restriction args if needed.
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Query is prepared, see above.
$views_query_tables = $wpdb->get_col( $tables_sql, 0 );
$tables = array_intersect( $tables, $views_query_tables );
}
}
// Filter by `$args`.
if ( $args ) {
$args_tables = [];
foreach ( $args as $arg ) {
if ( false !== strpos( $arg, '*' ) || false !== strpos( $arg, '?' ) ) {
$args_tables = array_merge(
$args_tables,
array_filter(
$tables,
function ( $v ) use ( $arg ) {
return fnmatch( $arg, $v );
}
)
);
} else {
$args_tables[] = $arg;
}
}
$args_tables = array_values( array_unique( $args_tables ) );
$tables = array_values( array_intersect( $tables, $args_tables ) );
if ( empty( $tables ) ) {
WP_CLI::error( sprintf( "Couldn't find any tables matching: %s", implode( ' ', $args ) ) );
}
}
return $tables;
}