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