From 2101d9967884a0a52b194dbeb6282a76523eee87 Mon Sep 17 00:00:00 2001 From: Deepak Kumar Date: Fri, 26 Apr 2024 23:36:06 +0530 Subject: [PATCH] Add polyfill for get_sudirectory_reserved_names --- src/Site_Command.php | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/src/Site_Command.php b/src/Site_Command.php index fe569fcd..de5451a5 100644 --- a/src/Site_Command.php +++ b/src/Site_Command.php @@ -424,8 +424,7 @@ public function create( $args, $assoc_args ) { // If not a subdomain install, make sure the domain isn't a reserved word if ( ! is_subdomain_install() ) { - // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling WordPress native hook. - $subdirectory_reserved_names = apply_filters( 'subdirectory_reserved_names', [ 'page', 'comments', 'blog', 'files', 'feed' ] ); + $subdirectory_reserved_names = $this->get_subdirectory_reserved_names(); if ( in_array( $base, $subdirectory_reserved_names, true ) ) { WP_CLI::error( 'The following words are reserved and cannot be used as blog names: ' . implode( ', ', $subdirectory_reserved_names ) ); } @@ -552,7 +551,7 @@ public function generate( $args, $assoc_args ) { $is_subdomain_install = is_subdomain_install(); // If not a subdomain install, make sure the domain isn't a reserved word if ( ! $is_subdomain_install ) { - $subdirectory_reserved_names = get_subdirectory_reserved_names(); + $subdirectory_reserved_names = $this->get_subdirectory_reserved_names(); if ( in_array( $base, $subdirectory_reserved_names, true ) ) { WP_CLI::error( 'The following words are reserved and cannot be used as blog names: ' . implode( ', ', $subdirectory_reserved_names ) ); } @@ -646,6 +645,35 @@ public function generate( $args, $assoc_args ) { } } + /** + * Retrieves a list of reserved site on a sub-directory Multisite installation. + * + * Works on older WordPress versions where get_subdirectory_reserved_names() does not exist. + * + * @return string[] Array of reserved names. + */ + private function get_subdirectory_reserved_names() { + if ( function_exists( 'get_subdirectory_reserved_names' ) ) { + return get_subdirectory_reserved_names(); + } + + $names = array( + 'page', + 'comments', + 'blog', + 'files', + 'feed', + 'wp-admin', + 'wp-content', + 'wp-includes', + 'wp-json', + 'embed', + ); + + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling WordPress native hook. + return apply_filters( 'subdirectory_reserved_names', $names ); + } + /** * Gets network data for a given id. *