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.
No Hooks.
Return
Array
.
Usage
wp_get_table_names( $args, $assoc_args );
- $args(array) (required)
- Provided table names, or tables with wildcards.
- $assoc_args(array)
- Optional flags for groups of tables (e.g. --network)
Default: []
wp_get_table_names() wp get table names code WP-CLI 2.8.0-alpha
function wp_get_table_names( $args, $assoc_args = [] ) { global $wpdb; $tables = []; // 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 ) ); } if ( ! global_terms_enabled() ) { // 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; }