Automattic\WooCommerce\Blocks\AIContent

UpdatePatterns::assign_selected_images_to_patterns()privateWC 1.0

Assign selected images to patterns.

Method of the class: UpdatePatterns{}

No Hooks.

Return

Array|WP_Error. The patterns with images.

Usage

// private - for code of main (parent) class only
$result = $this->assign_selected_images_to_patterns( $patterns_dictionary, $selected_images );
$patterns_dictionary(array) (required)
The array of patterns.
$selected_images(array) (required)
The array of images.

UpdatePatterns::assign_selected_images_to_patterns() code WC 9.8.1

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;
}