From 8ce46a8d0a4b22ffe8339b437b3b70aca3b39379 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 11 Oct 2022 17:13:47 -0700 Subject: [PATCH 1/3] Internalize global_terms_enabled() --- php/utils-wp.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/php/utils-wp.php b/php/utils-wp.php index 47a12fe9ed..8cbc6c03b4 100644 --- a/php/utils-wp.php +++ b/php/utils-wp.php @@ -480,3 +480,39 @@ function strip_tags( $string ) { return trim( $string ); } + +/** + * Internalized version of global_terms_enabled() to get around a bug in WordPress Core. + * + * WP Core inadvertently removed the function instead of deprecating it during th 6.1 cycle. + * + * @see https://core.trac.wordpress.org/ticket/21734#comment:34 + */ +function global_terms_enabled() { + if ( ! is_multisite() ) { + return false; + } + + static $global_terms = null; + if ( is_null( $global_terms ) ) { + + /** + * Filters whether global terms are enabled. + * + * Returning a non-null value from the filter will effectively short-circuit the function + * and return the value of the 'global_terms_enabled' site option instead. + * + * @since 3.0.0 + * + * @param null $enabled Whether global terms are enabled. + */ + $filter = apply_filters( 'global_terms_enabled', null ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound + if ( ! is_null( $filter ) ) { + $global_terms = (bool) $filter; + } else { + $global_terms = (bool) get_site_option( 'global_terms_enabled', false ); + } + } + + return $global_terms; +} From f222821a7be929e0f6dcf09eac60c0fd75bd47e1 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 11 Oct 2022 17:37:00 -0700 Subject: [PATCH 2/3] Move site_categories table test into separate conditional scenario --- features/utils-wp.feature | 43 +++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/features/utils-wp.feature b/features/utils-wp.feature index 926b62d285..213ee29361 100644 --- a/features/utils-wp.feature +++ b/features/utils-wp.feature @@ -748,13 +748,48 @@ Feature: Utilities that depend on WordPress code wp_posts """ - Given an enable_sitecategories.php file: + @less-than-wp-6.1 + Scenario: Get WP table names for multisite install (site_categories only) + Given a WP multisite install + And I run `wp db query "CREATE TABLE xx_wp_posts ( id int );"` + And I run `wp db query "CREATE TABLE xx_wp_2_posts ( id int );"` + And I run `wp db query "CREATE TABLE wp_xx_posts ( id int );"` + And I run `wp db query "CREATE TABLE wp_2_xx_posts ( id int );"` + And I run `wp db query "CREATE TABLE wp_posts_xx ( id int );"` + And I run `wp db query "CREATE TABLE wp_2_posts_xx ( id int );"` + And I run `wp db query "CREATE TABLE wp_categories ( id int );"` + And I run `wp db query "CREATE TABLE wp_sitecategories ( id int );"` + And a table_names.php file: """ ...] + * : List tables based on wildcard search, e.g. 'wp_*_options' or 'wp_post?'. + * + * [--scope=] + * : Can be all, global, ms_global, blog, or old tables. Defaults to all. + * + * [--network] + * : List all the tables in a multisite installation. Overrides --scope=. + * + * [--all-tables-with-prefix] + * : List all tables that match the table prefix even if not registered on $wpdb. Overrides --network. + * + * [--all-tables] + * : List all tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --all-tables-with-prefix. + */ + function test_wp_get_table_names( $args, $assoc_args ) { + if ( $tables = WP_CLI\Utils\wp_get_table_names( $args, $assoc_args ) ) { + echo implode( PHP_EOL, $tables ) . PHP_EOL; + } + } + WP_CLI::add_command( 'get_table_names', 'test_wp_get_table_names' ); """ + When I run `wp --require=table_names.php --require=enable_sitecategories.php get_table_names` # Leave out wp_blog_versions as it was never used and is removed with WP 5.3+. # Leave out wp_blogmeta for old WP compat. From 27175c747fc9d74850f514fb8bcf010e4ba7fd85 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 11 Oct 2022 17:46:30 -0700 Subject: [PATCH 3/3] Add missing enable_sitecategories.php file to tests --- features/utils-wp.feature | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/features/utils-wp.feature b/features/utils-wp.feature index 213ee29361..e908007c7c 100644 --- a/features/utils-wp.feature +++ b/features/utils-wp.feature @@ -789,6 +789,13 @@ Feature: Utilities that depend on WordPress code } WP_CLI::add_command( 'get_table_names', 'test_wp_get_table_names' ); """ + And an enable_sitecategories.php file: + """ +