Automattic\WooCommerce\Blocks\AIContent

UpdatePatterns::assign_selected_images_to_patterns()privateWC 1.0

Assign selected images to patterns.

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

Хуков нет.

Возвращает

Массив|WP_Error. The patterns with images.

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

// private - только в коде основоного (родительского) класса
$result = $this->assign_selected_images_to_patterns( $patterns_dictionary, $selected_images );
$patterns_dictionary(массив) (обязательный)
The array of patterns.
$selected_images(массив) (обязательный)
The array of images.

Код UpdatePatterns::assign_selected_images_to_patterns() WC 9.4.2

private function assign_selected_images_to_patterns( $patterns_dictionary, $selected_images ) {
	$patterns_with_images = array();

	foreach ( $patterns_dictionary as $pattern ) {
		if ( ! $this->pattern_has_images( $pattern ) ) {
			$patterns_with_images[] = $pattern;
			continue;
		}

		list( $images, $alts ) = $this->get_images_for_pattern( $pattern, $selected_images );
		if ( empty( $images ) ) {
			$patterns_with_images[] = $pattern;
			continue;
		}

		$pattern['images'] = $images;

		$string = wp_json_encode( $pattern );

		foreach ( $alts as $i => $alt ) {
			$alt    = empty( $alt ) ? 'the text should be related to the store description but generic enough to adapt to any image' : $alt;
			$string = str_replace( "{image.$i}", $alt, $string );
		}

		$pattern = json_decode( $string, true );

		$patterns_with_images[] = $pattern;
	}

	return $patterns_with_images;
}