WP_Customize_Selective_Refresh::handle_render_partials_request()publicWP 4.5.0

Handles the Ajax request to return the rendered partials for the requested placements.

Метод класса: WP_Customize_Selective_Refresh{}

Возвращает

null. Ничего (null).

Использование

$WP_Customize_Selective_Refresh = new WP_Customize_Selective_Refresh();
$WP_Customize_Selective_Refresh->handle_render_partials_request();

Список изменений

С версии 4.5.0 Введена.

Код WP_Customize_Selective_Refresh::handle_render_partials_request() WP 6.4.3

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
	 * via the {@see 'customize_render_partials_response'} filter.
	 *
	 * @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_after', $this, $partials );

	$response = array(
		'contents' => $contents,
	);

	if ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) {
		$response['errors'] = $this->triggered_errors;
	}

	$setting_validities             = $this->manager->validate_setting_values( $this->manager->unsanitized_post_values() );
	$exported_setting_validities    = array_map( array( $this->manager, 'prepare_setting_validity_for_js' ), $setting_validities );
	$response['setting_validities'] = $exported_setting_validities;

	/**
	 * Filters the response from rendering the partials.
	 *
	 * Plugins may use this filter to inject `$scripts` and `$styles`, which are dependencies
	 * for the partials being rendered. The response data will be available to the client via
	 * the `render-partials-response` JS event, so the client can then inject the scripts and
	 * styles into the DOM if they have not already been enqueued there.
	 *
	 * If plugins do this, they'll need to take care for any scripts that do `document.write()`
	 * and make sure that these are not injected, or else to override the function to no-op,
	 * or else the page will be destroyed.
	 *
	 * Plugins should be aware that `$scripts` and `$styles` may eventually be included by
	 * default in the response.
	 *
	 * @since 4.5.0
	 *
	 * @param array $response {
	 *     Response.
	 *
	 *     @type array $contents Associative array mapping a partial ID its corresponding array of contents
	 *                           for the containers requested.
	 *     @type array $errors   List of errors triggered during rendering of partials, if `WP_DEBUG_DISPLAY`
	 *                           is enabled.
	 * }
	 * @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.
	 */
	$response = apply_filters( 'customize_render_partials_response', $response, $this, $partials );

	wp_send_json_success( $response );
}