| @@ -80,26 +80,46 @@ | ||
| 80 | 80 | * |
| 81 | 81 | * @param array $block The root block to start traversing from. |
| 82 | 82 | */ |
| 83 | 83 | function create_auto_draft_for_template_part_block( $block ) { |
| 84 | - if ( 'core/template-part' === $block['blockName'] && ! isset( $block['attrs']['id'] ) ) { | |
| 85 | - $template_part_file_path = | |
| 86 | - get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html'; | |
| 87 | - if ( ! file_exists( $template_part_file_path ) ) { | |
| 88 | - return; | |
| 84 | + global $_wp_current_template_part_ids; | |
| 85 | + | |
| 86 | + if ( 'core/template-part' === $block['blockName'] ) { | |
| 87 | + if ( ! isset( $block['attrs']['postId'] ) ) { | |
| 88 | + $template_part_file_path = | |
| 89 | + get_stylesheet_directory() . '/block-template-parts/' . $block['attrs']['slug'] . '.html'; | |
| 90 | + if ( ! file_exists( $template_part_file_path ) ) { | |
| 91 | + if ( gutenberg_is_experiment_enabled( 'gutenberg-full-site-editing-demo' ) ) { | |
| 92 | + $template_part_file_path = | |
| 93 | + dirname( __FILE__ ) . '/demo-block-template-parts/' . $block['attrs']['slug'] . '.html'; | |
| 94 | + if ( ! file_exists( $template_part_file_path ) ) { | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + } else { | |
| 98 | + return; | |
| 99 | + } | |
| 100 | + } | |
| 101 | + $template_part_id = wp_insert_post( | |
| 102 | + array( | |
| 103 | + 'post_content' => file_get_contents( $template_part_file_path ), | |
| 104 | + 'post_title' => $block['attrs']['slug'], | |
| 105 | + 'post_status' => 'auto-draft', | |
| 106 | + 'post_type' => 'wp_template_part', | |
| 107 | + 'post_name' => $block['attrs']['slug'], | |
| 108 | + 'meta_input' => array( | |
| 109 | + 'theme' => $block['attrs']['theme'], | |
| 110 | + ), | |
| 111 | + ) | |
| 112 | + ); | |
| 113 | + } else { | |
| 114 | + $template_part_id = $block['attrs']['postId']; | |
| 89 | 115 | } |
| 90 | - wp_insert_post( | |
| 91 | - array( | |
| 92 | - 'post_content' => file_get_contents( $template_part_file_path ), | |
| 93 | - 'post_title' => ucfirst( $block['attrs']['slug'] ), | |
| 94 | - 'post_status' => 'auto-draft', | |
| 95 | - 'post_type' => 'wp_template_part', | |
| 96 | - 'post_name' => $block['attrs']['slug'], | |
| 97 | - 'meta_input' => array( | |
| 98 | - 'theme' => $block['attrs']['theme'], | |
| 99 | - ), | |
| 100 | - ) | |
| 101 | - ); | |
| 116 | + | |
| 117 | + if ( isset( $_wp_current_template_part_ids ) ) { | |
| 118 | + $_wp_current_template_part_ids[ $block['attrs']['slug'] ] = $template_part_id; | |
| 119 | + } else { | |
| 120 | + $_wp_current_template_part_ids = array( $block['attrs']['slug'] => $template_part_id ); | |
| 121 | + } | |
| 102 | 122 | } |
| 103 | 123 | |
| 104 | 124 | foreach ( $block['innerBlocks'] as $inner_block ) { |
| 105 | 125 | create_auto_draft_for_template_part_block( $inner_block ); |
| @@ -113,9 +133,9 @@ | ||
| 113 | 133 | * @param string $template_file Original template file. Will be overridden. |
| 114 | 134 | * @return string Path to the canvas file to include. |
| 115 | 135 | */ |
| 116 | 136 | function gutenberg_find_template( $template_file ) { |
| 117 | - global $_wp_current_template_id, $_wp_current_template_content, $_wp_current_template_hierarchy; | |
| 137 | + global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy; | |
| 118 | 138 | |
| 119 | 139 | // Bail if no relevant template hierarchy was determined, or if the template file |
| 120 | 140 | // was overridden another way. |
| 121 | 141 | if ( ! $_wp_current_template_hierarchy || $template_file ) { |
| @@ -172,9 +192,9 @@ | ||
| 172 | 192 | if ( isset( $higher_priority_block_template_path ) ) { |
| 173 | 193 | $post_name = basename( $higher_priority_block_template_path, '.html' ); |
| 174 | 194 | $current_template_post = array( |
| 175 | 195 | 'post_content' => file_get_contents( $higher_priority_block_template_path ), |
| 176 | - 'post_title' => ucfirst( $post_name ), | |
| 196 | + 'post_title' => $post_name, | |
| 177 | 197 | 'post_status' => 'auto-draft', |
| 178 | 198 | 'post_type' => 'wp_template', |
| 179 | 199 | 'post_name' => $post_name, |
| 180 | 200 | ); |
| @@ -191,8 +211,16 @@ | ||
| 191 | 211 | ); |
| 192 | 212 | } |
| 193 | 213 | } |
| 194 | 214 | |
| 215 | + if ( isset( $_GET['_wp-find-template'] ) ) { | |
| 216 | + if ( $current_template_post ) { | |
| 217 | + wp_send_json_success( $current_template_post ); | |
| 218 | + } else { | |
| 219 | + wp_send_json_error( array( 'message' => __( 'No matching template found.', 'gutenberg' ) ) ); | |
| 220 | + } | |
| 221 | + } | |
| 222 | + | |
| 195 | 223 | if ( $current_template_post ) { |
| 196 | 224 | if ( is_admin() ) { |
| 197 | 225 | foreach ( parse_blocks( $current_template_post->post_content ) as $block ) { |
| 198 | 226 | create_auto_draft_for_template_part_block( $block ); |
| @@ -198,9 +226,10 @@ | ||
| 198 | 226 | create_auto_draft_for_template_part_block( $block ); |
| 199 | 227 | } |
| 200 | 228 | } |
| 201 | 229 | $_wp_current_template_id = $current_template_post->ID; |
| 202 | - $_wp_current_template_content = $current_template_post->post_content; | |
| 230 | + $_wp_current_template_name = $current_template_post->post_name; | |
| 231 | + $_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content; | |
| 203 | 232 | } |
| 204 | 233 | |
| 205 | 234 | // Add extra hooks for template canvas. |
| 206 | 235 | add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 ); |