From 942aac142161418cc1617506750195e2769b8770 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Fri, 6 Nov 2020 08:53:09 +0100 Subject: [PATCH] Factorize template sync --- ...lass-wp-rest-template-parts-controller.php | 4 +- lib/edit-site-export.php | 4 +- lib/load.php | 1 + lib/template-loader.php | 2 +- lib/template-parts.php | 87 -------------- lib/templates-sync.php | 111 ++++++++++++++++++ lib/templates.php | 92 --------------- 7 files changed, 117 insertions(+), 184 deletions(-) create mode 100644 lib/templates-sync.php diff --git a/lib/class-wp-rest-template-parts-controller.php b/lib/class-wp-rest-template-parts-controller.php index 4d950835a473a5..8a48706a4e2f30 100644 --- a/lib/class-wp-rest-template-parts-controller.php +++ b/lib/class-wp-rest-template-parts-controller.php @@ -20,7 +20,7 @@ class WP_REST_Template_Parts_Controller extends WP_REST_Posts_Controller { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_items( $request ) { - synchronize_theme_template_parts(); + _gutenberg_synchronize_theme_templates( 'template-part' ); return parent::get_items( $request ); } @@ -32,7 +32,7 @@ public function get_items( $request ) { * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function get_item( $request ) { - synchronize_theme_template_parts(); + _gutenberg_synchronize_theme_templates( 'template-part' ); return parent::get_items( $request ); } diff --git a/lib/edit-site-export.php b/lib/edit-site-export.php index 1e810cbcdfb906..12751ed2b6796c 100644 --- a/lib/edit-site-export.php +++ b/lib/edit-site-export.php @@ -12,8 +12,8 @@ function gutenberg_edit_site_export() { // Theme templates and template parts need to be synchronized // before the export. - synchronize_theme_template_parts(); - synchronize_theme_templates(); + _gutenberg_synchronize_theme_templates( 'template-part' ); + _gutenberg_synchronize_theme_templates( 'template' ); // Create ZIP file and directories. $filename = tempnam( get_temp_dir(), 'edit-site-export' ); diff --git a/lib/load.php b/lib/load.php index 346e3a7826cc6b..078adbe261f5cf 100644 --- a/lib/load.php +++ b/lib/load.php @@ -107,6 +107,7 @@ function gutenberg_is_experiment_enabled( $name ) { require dirname( __FILE__ ) . '/utils.php'; require dirname( __FILE__ ) . '/full-site-editing.php'; +require dirname( __FILE__ ) . '/templates-sync.php'; require dirname( __FILE__ ) . '/templates.php'; require dirname( __FILE__ ) . '/template-parts.php'; require dirname( __FILE__ ) . '/template-loader.php'; diff --git a/lib/template-loader.php b/lib/template-loader.php index 7eb16c1ddad965..73851e561a8b82 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -141,7 +141,7 @@ function gutenberg_resolve_template( $template_type, $template_hierarchy = array } // Create auto-drafts for each theme template files. - synchronize_theme_templates(); + _gutenberg_synchronize_theme_templates( 'template' ); if ( empty( $template_hierarchy ) ) { if ( 'index' === $template_type ) { diff --git a/lib/template-parts.php b/lib/template-parts.php index 56eaa12236a137..3b11eb17e5f16e 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -183,90 +183,3 @@ function filter_rest_wp_template_part_query( $args, $request ) { return $args; } add_filter( 'rest_wp_template_part_query', 'filter_rest_wp_template_part_query', 99, 2 ); - -/** - * Creates a template part auto-draft if it doesn't exist yet. - * - * @access private - * - * @param string $slug Template part slug. - * @param string $theme Template part theme. - * @param string $content Template part content. - */ -function create_auto_draft_for_template_part( $slug, $theme, $content ) { - // We check if an auto-draft was already created, - // before running the REST API calls - // because the site editor needs an existing auto-draft - // for each theme template part to work properly. - $template_part_query = new WP_Query( - array( - 'post_type' => 'wp_template_part', - 'post_status' => array( 'publish', 'auto-draft' ), - 'title' => $slug, - 'meta_key' => 'theme', - 'meta_value' => $theme, - 'posts_per_page' => 1, - 'no_found_rows' => true, - ) - ); - $template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null; - if ( ! $template_part_post ) { - wp_insert_post( - array( - 'post_content' => $content, - 'post_title' => $slug, - 'post_status' => 'auto-draft', - 'post_type' => 'wp_template_part', - 'post_name' => $slug, - 'meta_input' => array( - 'theme' => $theme, - ), - ) - ); - } else { - // Potentially we could decide to update the content if different. - } -} - -/** - * Create the template parts auto-drafts for the current theme. - * - * @access private - */ -function synchronize_theme_template_parts() { - /** - * Finds all nested template part file paths in a theme's directory. - * - * @param string $base_directory The theme's file path. - * @return array $path_list A list of paths to all template part files. - */ - function get_template_part_paths( $base_directory ) { - $path_list = array(); - if ( file_exists( $base_directory . '/block-template-parts' ) ) { - $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/block-template-parts' ) ); - $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); - foreach ( $nested_html_files as $path => $file ) { - $path_list[] = $path; - } - } - return $path_list; - } - - // Get file paths for all theme supplied template parts. - $template_part_files = get_template_part_paths( get_stylesheet_directory() ); - if ( is_child_theme() ) { - $template_part_files = array_merge( $template_part_files, get_template_part_paths( get_template_directory() ) ); - } - // Build and save each template part. - foreach ( $template_part_files as $template_part_file ) { - $content = file_get_contents( $template_part_file ); - $slug = substr( - $template_part_file, - // Starting position of slug. - strpos( $template_part_file, 'block-template-parts/' ) + 21, - // Subtract ending '.html'. - -5 - ); - create_auto_draft_for_template_part( $slug, wp_get_theme()->get( 'TextDomain' ), $content ); - } -} diff --git a/lib/templates-sync.php b/lib/templates-sync.php new file mode 100644 index 00000000000000..ab909d7f2baaf0 --- /dev/null +++ b/lib/templates-sync.php @@ -0,0 +1,111 @@ + $post_type, + 'post_status' => array( 'publish', 'auto-draft' ), + 'title' => $slug, + 'meta_key' => 'theme', + 'meta_value' => $theme, + 'posts_per_page' => 1, + 'no_found_rows' => true, + ) + ); + $post = $template_query->have_posts() ? $template_query->next_post() : null; + if ( ! $post ) { + wp_insert_post( + array( + 'post_content' => $content, + 'post_title' => $slug, + 'post_status' => 'auto-draft', + 'post_type' => $post_type, + 'post_name' => $slug, + 'meta_input' => array( + 'theme' => $theme, + ), + ) + ); + } else { + // Potentially we could decide to update the content if different. + } +} + +/** + * Finds all nested template part file paths in a theme's directory. + * + * @access private + * + * @param string $base_directory The theme's file path. + * @return array $path_list A list of paths to all template part files. + */ +function _gutenberg_get_template_paths( $base_directory ) { + $path_list = array(); + if ( file_exists( $base_directory ) ) { + $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory ) ); + $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); + foreach ( $nested_html_files as $path => $file ) { + $path_list[] = $path; + } + } + return $path_list; +} + +/** + * Create the template parts auto-drafts for the current theme. + * + * @access private + * @internal + * + * @param string $template_type The template type (template or template-part). + */ +function _gutenberg_synchronize_theme_templates( $template_type ) { + $template_post_types = array( + 'template' => 'wp_template', + 'template-part' => 'wp_template_part', + ); + $template_base_paths = array( + 'template' => 'block-templates', + 'template-part' => 'block-template-parts', + ); + + // Get file paths for all theme supplied template. + $template_files = _gutenberg_get_template_paths( get_stylesheet_directory() . '/' . $template_base_paths[ $template_type ] ); + if ( is_child_theme() ) { + $template_files = array_merge( $template_files, _gutenberg_get_template_paths( get_template_directory() . '/' . $template_base_paths[ $template_type ] ) ); + } + + // Build and save each template part. + foreach ( $template_files as $template_file ) { + $content = file_get_contents( $template_file ); + $slug = substr( + $template_file, + // Starting position of slug. + strpos( $template_file, $template_base_paths[ $template_type ] . '/' ) + 1 + strlen( $template_base_paths[ $template_type ] ), + // Subtract ending '.html'. + -5 + ); + _gutenberg_create_auto_draft_for_template( $template_post_types[ $template_type ], $slug, wp_get_theme()->get( 'TextDomain' ), $content ); + } +} diff --git a/lib/templates.php b/lib/templates.php index dc770b228e7317..a062f67eb2687c 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -236,95 +236,3 @@ function filter_rest_wp_template_query( $args, $request ) { return $args; } add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 ); - -/** - * Creates a template auto-draft if it doesn't exist yet. - * - * @access private - * - * @param string $slug Template slug. - * @param string $theme Template theme. - * @param string $content Template content. - */ -function create_auto_draft_for_template( $slug, $theme, $content ) { - // We check if an auto-draft was already created, - // before running the REST API calls - // because the site editor needs an existing auto-draft - // for each theme template to work properly. - $template_query = new WP_Query( - array( - 'post_type' => 'wp_template', - 'post_status' => array( 'publish', 'auto-draft' ), - 'title' => $slug, - 'meta_key' => 'theme', - 'meta_value' => $theme, - 'posts_per_page' => 1, - 'no_found_rows' => true, - ) - ); - $template_post = $template_query->have_posts() ? $template_query->next_post() : null; - if ( ! $template_post ) { - wp_insert_post( - array( - 'post_content' => $content, - 'post_title' => $slug, - 'post_status' => 'auto-draft', - 'post_type' => 'wp_template', - 'post_name' => $slug, - 'meta_input' => array( - 'theme' => $theme, - ), - ) - ); - } else { - // Potentially we could decide to update the content if different. - } -} - -/** - * Create the template auto-drafts for the current theme. - * - * @access private - */ -function synchronize_theme_templates() { - /** - * Finds all nested template part file paths in a theme's directory. - * - * @param string $base_directory The theme's file path. - * @return array $path_list A list of paths to all template part files. - */ - function get_template_paths( $base_directory ) { - $path_list = array(); - if ( file_exists( $base_directory . '/block-templates' ) ) { - $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/block-templates' ) ); - $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); - foreach ( $nested_html_files as $path => $file ) { - $path_list[] = $path; - } - } - return $path_list; - } - - // Get file paths for all theme supplied template parts. - $template_files = get_template_paths( get_stylesheet_directory() ); - if ( is_child_theme() ) { - $template_files = array_merge( $template_files, get_template_paths( get_template_directory() ) ); - } - // Build and save each template part. - foreach ( $template_files as $template_file ) { - $content = file_get_contents( $template_file ); - $slug = substr( - $template_file, - // Starting position of slug. - strpos( $template_file, 'block-templates/' ) + 16, - // Subtract ending '.html'. - -5 - ); - create_auto_draft_for_template( $slug, wp_get_theme()->get( 'TextDomain' ), $content ); - } - - // If we haven't found any block-template by default, create a fallback one. - if ( count( $template_files ) === 0 ) { - create_auto_draft_for_template( 'index', wp_get_theme()->get( 'TextDomain' ), '' ); - } -}