wppaste
WordPress

WP_Customize_Selective_Refresh::handle_render_partials_request()

Since
4.5.0
Source
wp-includes/customize/class-wp-customize-selective-refresh.php:304
Handles the Ajax request to return the rendered partials for the requested placements.

Compatibility

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

Performance profile

How much work a call to WP_Customize_Selective_Refresh::handle_render_partials_request() 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 how much data it finds.

Instructions
4–105

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

Plugin surface
3 hooks

Third-party callbacks on 'customize_render_partials_before', 'customize_render_partials_after', 'customize_render_partials_response' 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 callbacksdo_action()called directly
  • serializeserialisationjson_decode()called directly

Further down the call graph this can also reach option, cache, 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 · 5 distinct outcomes

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

WhenInstructionsCalls it makes
!->is_render_partials_request()4->is_render_partials_request()
->is_render_partials_request() && is_customize_preview() && isset($value) && is_array($partials)92–98->is_render_partials_request(), is_customize_preview(), status_header(), wp_unslash(), json_decode(), array_keys(), ->add_dynamic_partials(), do_action(), error_reporting(), restore_error_handler(), do_action(), ->unsanitized_post_values(), ->validate_setting_values(), apply_filters(), wp_send_json_success()
->is_render_partials_request() && is_array($partials)94–102->is_render_partials_request(), is_customize_preview(), wp_send_json_error(), status_header(), wp_unslash(), json_decode(), array_keys(), ->add_dynamic_partials(), do_action(), error_reporting(), restore_error_handler(), do_action(), ->unsanitized_post_values(), ->validate_setting_values(), apply_filters(), wp_send_json_success()
->is_render_partials_request() && is_customize_preview() && isset($value) && !is_array($partials)95–101->is_render_partials_request(), is_customize_preview(), status_header(), wp_unslash(), json_decode(), wp_send_json_error(), array_keys(), ->add_dynamic_partials(), do_action(), error_reporting(), restore_error_handler(), do_action(), ->unsanitized_post_values(), ->validate_setting_values(), apply_filters(), wp_send_json_success()
->is_render_partials_request() && !is_array($partials)97–105->is_render_partials_request(), is_customize_preview(), wp_send_json_error(), status_header(), wp_unslash(), json_decode(), wp_send_json_error(), array_keys(), ->add_dynamic_partials(), do_action(), error_reporting(), restore_error_handler(), do_action(), ->unsanitized_post_values(), ->validate_setting_values(), apply_filters(), wp_send_json_success()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 152 instructions, 4–105 executed per call, 14 branches. The work does not change between versions.

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 · 3

3 hooks fire while WP_Customize_Selective_Refresh::handle_render_partials_request() runs, in this order:

  1. do_action( customize_render_partials_before )actionline 344 (+40 into the body)

    Fires immediately before partials are rendered.

  2. do_action( customize_render_partials_after )actionline 393 (+89 into the body)

    Fires immediately after partials are rendered.

  3. apply_filters( customize_render_partials_response )filterline 437 (+133 into the body)

    Filters the response from rendering the partials.

Uses · 10

Source code

	public function handle_render_partials_request() {		if ( ! $this->is_render_partials_request() ) {			return;		} 		/*		 * Note that is_customize_preview() returning true will entail that the		 * user passed the 'customize' capability check and the nonce check, since		 * WP_Customize_Manager::setup_theme() is where the previewing flag is set.		 */		if ( ! is_customize_preview() ) {			wp_send_json_error( 'expected_customize_preview', 403 );		} elseif ( ! isset( $_POST['partials'] ) ) {			wp_send_json_error( 'missing_partials', 400 );		} 		// Ensure that doing selective refresh on 404 template doesn't result in fallback rendering behavior (full refreshes).		status_header( 200 ); 		$partials = json_decode( wp_unslash( $_POST['partials'] ), true ); 		if ( ! is_array( $partials ) ) {			wp_send_json_error( 'malformed_partials' );		} 		$this->add_dynamic_partials( array_keys( $partials ) ); 		/**		 * Fires immediately before partials are rendered.		 *		 * Plugins may do things like call wp_enqueue_scripts() and gather a list of the scripts		 * and styles which may get enqueued in the response.		 *		 * @since 4.5.0		 *		 * @param WP_Customize_Selective_Refresh $refresh  Selective refresh component.		 * @param array                          $partials Placements' context data for the partials rendered in the request.		 *                                                 The array is keyed by partial ID, with each item being an array of		 *                                                 the placements' context data.		 */		do_action( 'customize_render_partials_before', $this, $partials ); 		set_error_handler( array( $this, 'handle_error' ), error_reporting() ); 		$contents = array(); 		foreach ( $partials as $partial_id => $container_contexts ) {			$this->current_partial_id = $partial_id; 			if ( ! is_array( $container_contexts ) ) {				wp_send_json_error( 'malformed_container_contexts' );			} 			$partial = $this->get_partial( $partial_id ); 			if ( ! $partial || ! $partial->check_capabilities() ) {				$contents[ $partial_id ] = null;				continue;			} 			$contents[ $partial_id ] = array(); 			// @todo The array should include not only the contents, but also whether the container is included?			if ( empty( $container_contexts ) ) {				// Since there are no container contexts, render just once.				$contents[ $partial_id ][] = $partial->render( null );			} else {				foreach ( $container_contexts as $container_context ) {					$contents[ $partial_id ][] = $partial->render( $container_context );				}			}		}		$this->current_partial_id = null; 		restore_error_handler(); 		/**		 * Fires immediately after partials are rendered.		 *		 * Plugins may do things like call wp_footer() to scrape scripts output and return them

Changelog

Introduced in 4.5.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.0.4 tag, from src/wp-includes/customize/class-wp-customize-selective-refresh.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.