PluginProbe
Gutenberg / 9.7.3
Gutenberg v9.7.3
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / edit-site-export.php

edit-site-export.php in Gutenberg 9.7.3, at lib/edit-site-export.php

128 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * REST endpoint for exporting the contents of the Edit Site Page editor.
4 *
5 * @package gutenberg
6 */
7
8 /**
9 * Output a ZIP file with an export of the current templates
10 * and template parts from the site editor, and close the connection.
11 */
12 function gutenberg_edit_site_export() {
13 // Theme templates and template parts need to be synchronized
14 // before the export.
15 _gutenberg_synchronize_theme_templates( 'template-part' );
16 _gutenberg_synchronize_theme_templates( 'template' );
17
18 // Create ZIP file and directories.
19 $filename = tempnam( get_temp_dir(), 'edit-site-export' );
20 $zip = new ZipArchive();
21 $zip->open( $filename, ZipArchive::OVERWRITE );
22 $zip->addEmptyDir( 'theme' );
23 $zip->addEmptyDir( 'theme/block-templates' );
24 $zip->addEmptyDir( 'theme/block-template-parts' );
25
26 $theme = wp_get_theme()->get_stylesheet();
27
28 // Load templates into the zip file.
29 $template_query = new WP_Query(
30 array(
31 'post_type' => 'wp_template',
32 'post_status' => array( 'publish', 'auto-draft' ),
33 'tax_query' => array(
34 array(
35 'taxonomy' => 'wp_theme',
36 'field' => 'slug',
37 'terms' => $theme,
38 ),
39 ),
40 'posts_per_page' => -1,
41 'no_found_rows' => true,
42 )
43 );
44 while ( $template_query->have_posts() ) {
45 $template = $template_query->next_post();
46 $zip->addFromString(
47 'theme/block-templates/' . $template->post_name . '.html',
48 gutenberg_strip_post_ids_from_template_part_blocks( $template->post_content )
49 );
50 }
51
52 // Load template parts into the zip file.
53 $template_part_query = new WP_Query(
54 array(
55 'post_type' => 'wp_template_part',
56 'post_status' => array( 'publish', 'auto-draft' ),
57 'tax_query' => array(
58 array(
59 'taxonomy' => 'wp_theme',
60 'field' => 'slug',
61 'terms' => $theme,
62 ),
63 ),
64 'posts_per_page' => -1,
65 'no_found_rows' => true,
66 )
67 );
68 while ( $template_part_query->have_posts() ) {
69 $template_part = $template_part_query->next_post();
70 $zip->addFromString(
71 'theme/block-template-parts/' . $template_part->post_name . '.html',
72 gutenberg_strip_post_ids_from_template_part_blocks( $template_part->post_content )
73 );
74 }
75
76 // Send back the ZIP file.
77 $zip->close();
78 header( 'Content-Type: application/zip' );
79 header( 'Content-Disposition: attachment; filename=edit-site-export.zip' );
80 header( 'Content-Length: ' . filesize( $filename ) );
81 flush();
82 echo readfile( $filename );
83 die();
84 }
85
86 add_action(
87 'rest_api_init',
88 function () {
89 register_rest_route(
90 '__experimental/edit-site/v1',
91 '/export',
92 array(
93 'methods' => 'GET',
94 'callback' => 'gutenberg_edit_site_export',
95 'permission_callback' => function () {
96 return current_user_can( 'edit_theme_options' );
97 },
98 )
99 );
100 }
101 );
102
103 /**
104 * Remove post id attributes from template part blocks.
105 *
106 * This is needed so that Gutenberg loads the HTML file of the template, instead of looking for a template part post.
107 *
108 * @param string $template_content Template content to modify.
109 *
110 * @return string Potentially modified template content.
111 */
112 function gutenberg_strip_post_ids_from_template_part_blocks( $template_content ) {
113 $blocks = parse_blocks( $template_content );
114
115 array_walk(
116 $blocks,
117 function( &$block ) {
118 if ( 'core/template-part' !== $block['blockName'] ) {
119 return;
120 }
121
122 unset( $block['attrs']['postId'] );
123 }
124 );
125
126 return serialize_blocks( $blocks );
127 }
128