Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Ensure the pattern content exists within the wc_blocks_patterns_conte…
Browse files Browse the repository at this point in the history
…nt option: if it doesn't exist, fetch the default content from the default dictionary instead.
  • Loading branch information
nefeline committed Sep 24, 2023
1 parent 5555c09 commit 6676e32
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions src/Patterns/PatternsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,7 @@ class PatternsHelper {
* @return array The pattern content.
*/
public static function get_pattern_content( string $pattern_slug ) {
$dictionary = get_option( PatternUpdater::WC_BLOCKS_PATTERNS_CONTENT );
if ( empty( $dictionary ) ) {
$dictionary = self::get_patterns_dictionary();
}

$pattern = null;
foreach ( $dictionary as $item ) {
if ( $item['slug'] === $pattern_slug ) {
$pattern = $item;
break;
}
}
$pattern = self::get_patterns_dictionary( $pattern_slug );

if ( empty( $pattern ) ) {
return array();
Expand All @@ -48,18 +37,7 @@ public static function get_pattern_content( string $pattern_slug ) {
* @return array The pattern images.
*/
public static function get_pattern_images( string $pattern_slug ): array {
$dictionary = get_option( PatternUpdater::WC_BLOCKS_PATTERNS_CONTENT );
if ( empty( $dictionary ) ) {
return array();
}

$pattern = null;
foreach ( $dictionary as $item ) {
if ( $item['slug'] === $pattern_slug ) {
$pattern = $item;
break;
}
}
$pattern = self::get_patterns_dictionary( $pattern_slug );

if ( empty( $pattern ) ) {
return array();
Expand Down Expand Up @@ -114,15 +92,41 @@ private static function get_random_images( array $images, int $images_total ): a
/**
* Get the Patterns Dictionary.
*
* @param string|null $pattern_slug The pattern slug.
*
* @return mixed|WP_Error|null
*/
private static function get_patterns_dictionary() {
$patterns_dictionary = plugin_dir_path( __FILE__ ) . 'dictionary.json';
private static function get_patterns_dictionary( $pattern_slug = null ) {
$patterns_dictionary = get_option( PatternUpdater::WC_BLOCKS_PATTERNS_CONTENT );

if ( ! file_exists( $patterns_dictionary ) ) {
if ( ! empty( $patterns_dictionary ) ) {
if ( empty( $pattern_slug ) ) {
return $patterns_dictionary;
}

foreach ( $patterns_dictionary as $pattern_dictionary ) {
if ( $pattern_dictionary['slug'] === $pattern_slug ) {
return $pattern_dictionary;
}
}
}

$patterns_dictionary_file = plugin_dir_path( __FILE__ ) . 'dictionary.json';

if ( ! file_exists( $patterns_dictionary_file ) ) {
return new WP_Error( 'missing_patterns_dictionary', __( 'The patterns dictionary is missing.', 'woo-gutenberg-products-block' ) );
}

return wp_json_file_decode( $patterns_dictionary, array( 'associative' => true ) );
$patterns_dictionary = wp_json_file_decode( $patterns_dictionary_file, array( 'associative' => true ) );

if ( ! empty( $pattern_slug ) ) {
foreach ( $patterns_dictionary as $pattern_dictionary ) {
if ( $pattern_dictionary['slug'] === $pattern_slug ) {
return $pattern_dictionary;
}
}
}

return $patterns_dictionary;
}
}

0 comments on commit 6676e32

Please sign in to comment.