wppaste
WordPress

block_editor_rest_api_preload( (string|string[])[] $preload_paths, WP_Block_Editor_Context $block_editor_context )

Since
5.8.0
Source
wp-includes/block-editor.php:680
Preloads common data used with the block editor by specifying an array of REST API paths that will be preloaded for a given block editor context.

Compatibility

WordPress
since 5.8.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.

Parameters

$preload_paths(string|string[])[]
List of paths to preload.
$block_editor_contextWP_Block_Editor_Context
The current block editor context.

Performance profile

How much work a call to block_editor_rest_api_preload() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–66

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 90.

Plugin surface
2 hooks

Third-party callbacks on 'block_editor_rest_api_preload_paths', 'block_editor_preload_paths' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost block_editor_rest_api_preload() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($preload_paths)16–26apply_filters()
!empty($preload_paths)52–66apply_filters(), array_reduce(), wp_json_encode(), wp_add_inline_script()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9016–6612
8.59016–6612
8.49016–66127 fewer instructions than PHP 8.3
8.39716–6712
8.29716–6712
8.19716–67124 fewer instructions than PHP 7.4
7.410116–7012

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.

Hooks and filters fired · 2

2 hooks fire while block_editor_rest_api_preload() runs, in this order:

  1. apply_filters( block_editor_rest_api_preload_paths )filterline 691 (+11 into the body)

    Filters the array of REST API paths that will be used to preloaded common data for the block editor.

  2. do_action( block_editor_preload_paths )filter_deprecatedline 707 (+27 into the body)

    Filters the array of paths that will be preloaded.

Uses · 5

Source code

function block_editor_rest_api_preload( array $preload_paths, $block_editor_context ) {	global $post, $wp_scripts, $wp_styles; 	/**	 * Filters the array of REST API paths that will be used to preloaded common data for the block editor.	 *	 * @since 5.8.0	 *	 * @param (string|string[])[]     $preload_paths        Array of paths to preload.	 * @param WP_Block_Editor_Context $block_editor_context The current block editor context.	 */	$preload_paths = apply_filters( 'block_editor_rest_api_preload_paths', $preload_paths, $block_editor_context ); 	if ( ! empty( $block_editor_context->post ) ) {		$selected_post = $block_editor_context->post; 		/**		 * Filters the array of paths that will be preloaded.		 *		 * Preload common data by specifying an array of REST API paths that will be preloaded.		 *		 * @since 5.0.0		 * @deprecated 5.8.0 Use the {@see 'block_editor_rest_api_preload_paths'} filter instead.		 *		 * @param (string|string[])[] $preload_paths Array of paths to preload.		 * @param WP_Post             $selected_post Post being edited.		 */		$preload_paths = apply_filters_deprecated( 'block_editor_preload_paths', array( $preload_paths, $selected_post ), '5.8.0', 'block_editor_rest_api_preload_paths' );	} 	if ( empty( $preload_paths ) ) {		return;	} 	/*	 * Ensure the global $post, $wp_scripts, and $wp_styles remain the same after	 * API data is preloaded.	 * Because API preloading can call the_content and other filters, plugins	 * can unexpectedly modify the global $post or enqueue assets which are not	 * intended for the block editor.	 */	$backup_global_post = ! empty( $post ) ? clone $post : $post;	$backup_wp_scripts  = ! empty( $wp_scripts ) ? clone $wp_scripts : $wp_scripts;	$backup_wp_styles   = ! empty( $wp_styles ) ? clone $wp_styles : $wp_styles; 	foreach ( $preload_paths as &$path ) {		if ( is_string( $path ) && ! str_starts_with( $path, '/' ) ) {			$path = '/' . $path;			continue;		} 		if ( is_array( $path ) && is_string( $path[0] ) && ! str_starts_with( $path[0], '/' ) ) {			$path[0] = '/' . $path[0];		}	} 	unset( $path ); 	$preload_data = array_reduce(		$preload_paths,		'rest_preload_api_request',		array()	); 	// Restore the global $post, $wp_scripts, and $wp_styles as they were before API preloading.	$post       = $backup_global_post;	$wp_scripts = $backup_wp_scripts;	$wp_styles  = $backup_wp_styles; 	wp_add_inline_script(		'wp-api-fetch',		sprintf(			'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );',			wp_json_encode( $preload_data, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )		),		'after'	);}

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/block-editor.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.