WP_Customize_Widgets::filter_dynamic_sidebar_params()publicWP 4.5.0

Inject selective refresh data attributes into widget container elements.

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

Хуков нет.

Возвращает

Массив. Params.

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

$WP_Customize_Widgets = new WP_Customize_Widgets();
$WP_Customize_Widgets->filter_dynamic_sidebar_params( $params );
$params(массив) (обязательный)

Dynamic sidebar params.

  • args(массив)
    Sidebar args.

  • widget_args(массив)
    Widget args.

Заметки

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

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

Код WP_Customize_Widgets::filter_dynamic_sidebar_params() WP 6.4.3

public function filter_dynamic_sidebar_params( $params ) {
	$sidebar_args = array_merge(
		array(
			'before_widget' => '',
			'after_widget'  => '',
		),
		$params[0]
	);

	// Skip widgets not in a registered sidebar or ones which lack a proper wrapper element to attach the data-* attributes to.
	$matches  = array();
	$is_valid = (
		isset( $sidebar_args['id'] )
		&&
		is_registered_sidebar( $sidebar_args['id'] )
		&&
		( isset( $this->current_dynamic_sidebar_id_stack[0] ) && $this->current_dynamic_sidebar_id_stack[0] === $sidebar_args['id'] )
		&&
		preg_match( '#^<(?P<tag_name>\w+)#', $sidebar_args['before_widget'], $matches )
	);
	if ( ! $is_valid ) {
		return $params;
	}
	$this->before_widget_tags_seen[ $matches['tag_name'] ] = true;

	$context = array(
		'sidebar_id' => $sidebar_args['id'],
	);
	if ( isset( $this->context_sidebar_instance_number ) ) {
		$context['sidebar_instance_number'] = $this->context_sidebar_instance_number;
	} elseif ( isset( $sidebar_args['id'] ) && isset( $this->sidebar_instance_count[ $sidebar_args['id'] ] ) ) {
		$context['sidebar_instance_number'] = $this->sidebar_instance_count[ $sidebar_args['id'] ];
	}

	$attributes                    = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'widget[' . $sidebar_args['widget_id'] . ']' ) );
	$attributes                   .= ' data-customize-partial-type="widget"';
	$attributes                   .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $context ) ) );
	$attributes                   .= sprintf( ' data-customize-widget-id="%s"', esc_attr( $sidebar_args['widget_id'] ) );
	$sidebar_args['before_widget'] = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $sidebar_args['before_widget'] );

	$params[0] = $sidebar_args;
	return $params;
}