From 014bf251a04dd3abce4d12356af041baa06833be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Fri, 28 Oct 2022 12:50:33 +0200 Subject: [PATCH 1/6] Move gutenberg_get_global_settings to 6.2 compat folder --- .../get-global-styles-and-settings.php | 29 ------------------- .../get-global-styles-and-settings.php | 28 ++++++++++++++++++ 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/compat/wordpress-6.0/get-global-styles-and-settings.php b/lib/compat/wordpress-6.0/get-global-styles-and-settings.php index 9ed3e891182cb5..d4f1f9ef034924 100644 --- a/lib/compat/wordpress-6.0/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.0/get-global-styles-and-settings.php @@ -5,35 +5,6 @@ * @package gutenberg */ -/** - * Function to get the settings resulting of merging core, theme, and user data. - * - * @param array $path Path to the specific setting to retrieve. Optional. - * If empty, will return all settings. - * @param array $context { - * Metadata to know where to retrieve the $path from. Optional. - * - * @type string $block_name Which block to retrieve the settings from. - * If empty, it'll return the settings for the global context. - * @type string $origin Which origin to take data from. - * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). - * If empty or unknown, 'all' is used. - * } - * - * @return array The settings to retrieve. - */ -function gutenberg_get_global_settings( $path = array(), $context = array() ) { - if ( ! empty( $context['block_name'] ) ) { - $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); - } - $origin = 'custom'; - if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { - $origin = 'theme'; - } - $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); - return _wp_array_get( $settings, $path, $settings ); -} - /** * Function to get the styles resulting of merging core, theme, and user data. * diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 5d37d3182ccc85..b6663faae16492 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -189,3 +189,31 @@ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, } } +/* + * Function to get the settings resulting of merging core, theme, and user data. + * + * @param array $path Path to the specific setting to retrieve. Optional. + * If empty, will return all settings. + * @param array $context { + * Metadata to know where to retrieve the $path from. Optional. + * + * @type string $block_name Which block to retrieve the settings from. + * If empty, it'll return the settings for the global context. + * @type string $origin Which origin to take data from. + * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). + * If empty or unknown, 'all' is used. + * } + * + * @return array The settings to retrieve. + */ +function gutenberg_get_global_settings( $path = array(), $context = array() ) { + if ( ! empty( $context['block_name'] ) ) { + $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); + } + $origin = 'custom'; + if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { + $origin = 'theme'; + } + $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); + return _wp_array_get( $settings, $path, $settings ); +} From 841bcfc5f95270e39f24efa02d5520544305cb91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:45:26 +0100 Subject: [PATCH 2/6] Add new conditions to change origin --- .../wordpress-6.2/get-global-styles-and-settings.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index b6663faae16492..a341990d7e6deb 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -189,7 +189,7 @@ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, } } -/* +/** * Function to get the settings resulting of merging core, theme, and user data. * * @param array $path Path to the specific setting to retrieve. Optional. @@ -210,10 +210,17 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { if ( ! empty( $context['block_name'] ) ) { $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); } + $origin = 'custom'; - if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { + if ( ! wp_theme_has_theme_json() ) { + // For themes with no theme.json skip querying the database for user data (custom origin). $origin = 'theme'; + } elseif ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { + $origin = 'theme'; + } elseif ( isset( $context['origin'] ) && 'all' === $context['all'] ) { + $origin = 'custom'; } + $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); return _wp_array_get( $settings, $path, $settings ); } From b0a3cc8c7c7247e8a8e050c933cae6c2d3b43e18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 22 Nov 2022 17:42:24 +0100 Subject: [PATCH 3/6] Simplify logic to set origin --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index a341990d7e6deb..f775e3e2f4832a 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -211,14 +211,13 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); } + // This is the default value when no origin is provided or when it is 'all'. $origin = 'custom'; if ( ! wp_theme_has_theme_json() ) { // For themes with no theme.json skip querying the database for user data (custom origin). $origin = 'theme'; } elseif ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { $origin = 'theme'; - } elseif ( isset( $context['origin'] ) && 'all' === $context['all'] ) { - $origin = 'custom'; } $settings = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data( $origin )->get_settings(); From 1a5bbe8bd127051d0ea44ad42459e8c18101a073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 22 Nov 2022 17:45:11 +0100 Subject: [PATCH 4/6] Consolidate into a single path --- .../wordpress-6.2/get-global-styles-and-settings.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index f775e3e2f4832a..4b1918aacd52de 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -213,10 +213,10 @@ function gutenberg_get_global_settings( $path = array(), $context = array() ) { // This is the default value when no origin is provided or when it is 'all'. $origin = 'custom'; - if ( ! wp_theme_has_theme_json() ) { - // For themes with no theme.json skip querying the database for user data (custom origin). - $origin = 'theme'; - } elseif ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { + if ( + ! wp_theme_has_theme_json() || + ( isset( $context['origin'] ) && 'base' === $context['origin'] ) + ) { $origin = 'theme'; } From 178cf4e44f5eb515ca0f845cbf706592c075b8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 22 Nov 2022 17:52:14 +0100 Subject: [PATCH 5/6] Address feedback about micro-optimize array merges --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 4b1918aacd52de..3c2421642d560d 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -208,7 +208,11 @@ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, */ function gutenberg_get_global_settings( $path = array(), $context = array() ) { if ( ! empty( $context['block_name'] ) ) { - $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); + $new_path = array( 'blocks', $context['block_name'] ); + foreach( $path as $subpath ) { + $new_path[] = $subpath; + } + $path = $new_path; } // This is the default value when no origin is provided or when it is 'all'. From 6ce48992ac171103106f389917e70dc69239b9a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= <583546+oandregal@users.noreply.github.com> Date: Tue, 22 Nov 2022 17:54:21 +0100 Subject: [PATCH 6/6] Fix lint issue --- lib/compat/wordpress-6.2/get-global-styles-and-settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php index 3c2421642d560d..cee145acca5fc6 100644 --- a/lib/compat/wordpress-6.2/get-global-styles-and-settings.php +++ b/lib/compat/wordpress-6.2/get-global-styles-and-settings.php @@ -209,7 +209,7 @@ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading( $upgrader, function gutenberg_get_global_settings( $path = array(), $context = array() ) { if ( ! empty( $context['block_name'] ) ) { $new_path = array( 'blocks', $context['block_name'] ); - foreach( $path as $subpath ) { + foreach ( $path as $subpath ) { $new_path[] = $subpath; } $path = $new_path;