wp_generate_block_templates_export_file(): WP_Error|string
- Since
- 5.9.0, 6.0.0
- Source
wp-includes/block-template-utils.php:1483
Compatibility
- WordPress
- since 6.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
WP_Error|string- Path of the ZIP file or error on failure.
Performance profile
How much work a call to wp_generate_block_templates_export_file() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 13–111
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 178.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_generate_block_templates_export_file() - optionoption read or write
get_option()one call below wp_generate_block_templates_export_file()
Further down the call graph this can also reach transient, cache, serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_generate_block_templates_export_file() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13 | wp_get_wp_version(), __() |
| always | 42 | wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), __() |
!$theme_json_raw | 97–100 | wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), ->addEmptyDir(), ->addEmptyDir(), get_stylesheet_directory(), wp_normalize_path(), get_block_templates(), get_block_templates(), ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->merge(), ->get_data(), wp_json_encode(), ->addFromString(), ->close() |
$theme_json_raw | 108–111 | wp_get_wp_version(), wp_generate_password(), get_stylesheet(), basename(), get_temp_dir(), ->open(), ->addEmptyDir(), ->addEmptyDir(), get_stylesheet_directory(), wp_normalize_path(), get_block_templates(), get_block_templates(), ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->merge(), ->get_data(), array_merge(), wp_json_encode(), ->addFromString(), ->close() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 178 | 13–111 | 11 | |
| 8.5 | 178 | 13–111 | 11 | |
| 8.4 | 178 | 13–111 | 11 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 190 | 15–120 | 11 | |
| 8.2 | 190 | 15–120 | 11 | |
| 8.1 | 190 | 15–120 | 11 | |
| 7.4 | 190 | 15–120 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 18
- wp_get_wp_version()Returns the current WordPress version.
- __()Retrieves the translation of $text.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- get_stylesheet()Retrieves name of the current stylesheet.
- get_temp_dir()Determines a writable directory for temporary files.
- wp_normalize_path()Normalizes a filesystem path.
- get_stylesheet_directory()Retrieves stylesheet directory path for the active theme.
- wp_is_theme_directory_ignored()Determines whether a theme directory should be ignored during export.
- get_block_templates()Retrieves a list of unified template objects based on a query.
- traverse_and_serialize_blocks()Given an array of parsed block trees, applies callbacks before and after serializing them and returns their concatenated output.
- parse_blocks()Parses blocks out of a content string.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
Show all 18
- WP_Error::__construct()Initializes the error.
- ZipArchive::__construct()
- RecursiveIteratorIterator::__construct()
- RecursiveDirectoryIterator::__construct()
- WP_Theme_JSON_Resolver::get_theme_data()Returns the theme's data.
- WP_Theme_JSON_Resolver::get_user_data()Returns the user's origin config.
Used by · 1
- WP_REST_Edit_Site_Export_Controller::export()Output a ZIP file with an export of the current templates and template parts from the site editor, and close the connection.
Source code
function wp_generate_block_templates_export_file() { $wp_version = wp_get_wp_version(); if ( ! class_exists( 'ZipArchive' ) ) { return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) ); } $obscura = wp_generate_password( 12, false, false ); $theme_name = basename( get_stylesheet() ); $filename = get_temp_dir() . $theme_name . $obscura . '.zip'; $zip = new ZipArchive(); if ( true !== $zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE ) ) { return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) ); } $zip->addEmptyDir( 'templates' ); $zip->addEmptyDir( 'parts' ); // Get path of the theme. $theme_path = wp_normalize_path( get_stylesheet_directory() ); // Create recursive directory iterator. $theme_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $theme_path ), RecursiveIteratorIterator::LEAVES_ONLY ); // Make a copy of the current theme. foreach ( $theme_files as $file ) { // Skip directories as they are added automatically. if ( ! $file->isDir() ) { // Get real and relative path for current file. $file_path = wp_normalize_path( $file ); $relative_path = substr( $file_path, strlen( $theme_path ) + 1 ); if ( ! wp_is_theme_directory_ignored( $relative_path ) ) { $zip->addFile( $file_path, $relative_path ); } } } // Load templates into the zip file. $templates = get_block_templates(); foreach ( $templates as $template ) { $template->content = traverse_and_serialize_blocks( parse_blocks( $template->content ), '_remove_theme_attribute_from_template_part_block' ); $zip->addFromString( 'templates/' . $template->slug . '.html', $template->content ); } // Load template parts into the zip file. $template_parts = get_block_templates( array(), 'wp_template_part' ); foreach ( $template_parts as $template_part ) { $zip->addFromString( 'parts/' . $template_part->slug . '.html', $template_part->content ); } // Load theme.json into the zip file. $tree = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) ); // Merge with user data. $tree->merge( WP_Theme_JSON_Resolver::get_user_data() ); $theme_json_raw = $tree->get_data(); // If a version is defined, add a schema. if ( $theme_json_raw['version'] ) { $theme_json_version = 'wp/' . substr( $wp_version, 0, 3 ); $schema = array( '$schema' => 'https://schemas.wp.org/' . $theme_json_version . '/theme.json' ); $theme_json_raw = array_merge( $schema, $theme_json_raw ); } // Convert to a string. $theme_json_encoded = wp_json_encode( $theme_json_raw, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );Changelog
Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/block-template-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.