From b92aa3bf19c1458d8612fb618f54f4c282214f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 29 Jul 2020 21:33:08 +0200 Subject: [PATCH 1/3] Merge editor-features within global-styles --- lib/editor-features.php | 49 ----------------------------------------- lib/global-styles.php | 30 +++++++++++++++++++++++++ lib/load.php | 1 - 3 files changed, 30 insertions(+), 50 deletions(-) delete mode 100644 lib/editor-features.php diff --git a/lib/editor-features.php b/lib/editor-features.php deleted file mode 100644 index 4f43424e29e1a7..00000000000000 --- a/lib/editor-features.php +++ /dev/null @@ -1,49 +0,0 @@ - gutenberg_experimental_global_styles_get_stylesheet() ); + $editor_features = gutenberg_experimental_global_styles_get_editor_features(); + $settings['__experimentalFeatures'] = $editor_features; + return $settings; } diff --git a/lib/load.php b/lib/load.php index 3815faf3761417..cc8c54210033c3 100644 --- a/lib/load.php +++ b/lib/load.php @@ -101,5 +101,4 @@ function gutenberg_is_experiment_enabled( $name ) { require dirname( __FILE__ ) . '/customizer.php'; require dirname( __FILE__ ) . '/edit-site-page.php'; require dirname( __FILE__ ) . '/edit-site-export.php'; -require dirname( __FILE__ ) . '/editor-features.php'; require dirname( __FILE__ ) . '/global-styles.php'; From 2b080699ead12e405dd6f8b14d5d29399d7231c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Wed, 29 Jul 2020 21:46:44 +0200 Subject: [PATCH 2/3] Refactor to load files from filesystem only once --- lib/global-styles.php | 59 +++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index fbbc079cac90ca..f1fa562edb4b2b 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -535,25 +535,13 @@ function gutenberg_experimental_global_styles_normalize_schema( $tree ) { return $tree; } -/** - * Returns the stylesheet resulting of merging - * core's, theme's, and user's origins. - * - * @return string - */ -function gutenberg_experimental_global_styles_get_stylesheet() { - $gs_merged = array(); - $gs_core = gutenberg_experimental_global_styles_get_core(); - $gs_theme = gutenberg_experimental_global_styles_get_theme(); - $gs_user = gutenberg_experimental_global_styles_get_user(); - - $gs_merged = gutenberg_experimental_global_styles_merge_trees( $gs_core, $gs_theme, $gs_user ); +function gutenberg_experimental_global_styles_get_merged_origins() { + $core = gutenberg_experimental_global_styles_get_core(); + $theme = gutenberg_experimental_global_styles_get_theme(); + $user = gutenberg_experimental_global_styles_get_user(); + $merged = gutenberg_experimental_global_styles_merge_trees( $core, $theme, $user ); - $stylesheet = gutenberg_experimental_global_styles_resolver( $gs_merged ); - if ( empty( $stylesheet ) ) { - return; - } - return $stylesheet; + return $merged; } /** @@ -561,8 +549,11 @@ function gutenberg_experimental_global_styles_get_stylesheet() { * and enqueues the resulting stylesheet. */ function gutenberg_experimental_global_styles_enqueue_assets() { - - $stylesheet = gutenberg_experimental_global_styles_get_stylesheet(); + $merged = gutenberg_experimental_global_styles_get_merged_origins(); + $stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); + if ( empty( $stylesheet ) ) { + return; + } wp_register_style( 'global-styles', false, array(), true, true ); wp_add_inline_style( 'global-styles', $stylesheet ); @@ -573,27 +564,18 @@ function gutenberg_experimental_global_styles_enqueue_assets() { * Returns the default config for editor features, * or an empty array if none found. * + * @param array $config Config to extract values from. * @return array Default features config for the editor. */ -function gutenberg_experimental_global_styles_get_editor_features() { - $empty_config = array(); - $config_path = __DIR__ . '/experimental-default-theme.json'; - if ( ! file_exists( $config_path ) ) { - return $empty_config; - } - - $theme_config = json_decode( - @file_get_contents( $config_path ), - true - ); +function gutenberg_experimental_global_styles_get_editor_features( $config ) { if ( - empty( $theme_config['global']['features'] ) || - ! is_array( $theme_config['global']['features'] ) + empty( $config['global']['features'] ) || + ! is_array( $config['global']['features'] ) ) { - return $empty_config; + return array(); } - return $theme_config['global']['features']; + return $config['global']['features']; } /** @@ -618,10 +600,11 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // Add the styles for the editor via the settings // so they get processed as if they were added via add_editor_styles: // they will get the editor wrapper class. - $settings['styles'][] = array( 'css' => gutenberg_experimental_global_styles_get_stylesheet() ); + $merged = gutenberg_experimental_global_styles_get_merged_origins(); + $stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); + $settings['styles'][] = array( 'css' => $stylesheet ); - $editor_features = gutenberg_experimental_global_styles_get_editor_features(); - $settings['__experimentalFeatures'] = $editor_features; + $settings['__experimentalFeatures'] = gutenberg_experimental_global_styles_get_editor_features( $merged ); return $settings; } From 9aafb6e0f99ad1968e4740ce56326d4c2f4914cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= Date: Thu, 30 Jul 2020 17:07:45 +0200 Subject: [PATCH 3/3] Add phpdoc + fix linting issues --- lib/global-styles.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/global-styles.php b/lib/global-styles.php index f1fa562edb4b2b..60c2b15c036963 100644 --- a/lib/global-styles.php +++ b/lib/global-styles.php @@ -535,6 +535,12 @@ function gutenberg_experimental_global_styles_normalize_schema( $tree ) { return $tree; } +/** + * Takes data from the different origins (core, theme, and user) + * and returns the merged result. + * + * @return array Merged trees. + */ function gutenberg_experimental_global_styles_get_merged_origins() { $core = gutenberg_experimental_global_styles_get_core(); $theme = gutenberg_experimental_global_styles_get_theme(); @@ -600,8 +606,8 @@ function gutenberg_experimental_global_styles_settings( $settings ) { // Add the styles for the editor via the settings // so they get processed as if they were added via add_editor_styles: // they will get the editor wrapper class. - $merged = gutenberg_experimental_global_styles_get_merged_origins(); - $stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); + $merged = gutenberg_experimental_global_styles_get_merged_origins(); + $stylesheet = gutenberg_experimental_global_styles_resolver( $merged ); $settings['styles'][] = array( 'css' => $stylesheet ); $settings['__experimentalFeatures'] = gutenberg_experimental_global_styles_get_editor_features( $merged );