Skip to content

Commit

Permalink
Revert "Editing Toolkit: Load patterns from the rest API endpoint (#4…
Browse files Browse the repository at this point in the history
…5926)"

This reverts commit 940db60.
  • Loading branch information
apeatling authored Oct 8, 2020
1 parent 4fabc36 commit d905726
Show file tree
Hide file tree
Showing 52 changed files with 2,995 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,11 @@ class Block_Patterns {
*/
private static $instance = null;

/**
* Cache key for patterns array.
*
* @var string
*/
private $patterns_cache_key;

/**
* Block_Patterns constructor.
*/
private function __construct() {
$this->patterns_cache_key = sha1(
implode(
'_',
array(
'block_patterns',
PLUGIN_VERSION,
$this->get_iso_639_locale(),
)
)
);

$this->register_patterns();
}

Expand All @@ -62,116 +45,112 @@ public static function get_instance() {
/**
* Register FSE block patterns and categories.
*/
private function register_patterns() {
// Remove core pattern categories, categories added later that match
// will then remain in alphabetical order.
public function register_patterns() {
// Register Block Pattern Categories.
if ( class_exists( 'WP_Block_Pattern_Categories_Registry' ) ) {
if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'buttons' ) ) {
unregister_block_pattern_category( 'buttons' );
}
register_block_pattern_category( 'blog', array( 'label' => _x( 'Blog', 'Block pattern category', 'full-site-editing' ) ) );
register_block_pattern_category( 'call-to-action', array( 'label' => _x( 'Call to Action', 'Block pattern category', 'full-site-editing' ) ) );
register_block_pattern_category( 'contact', array( 'label' => _x( 'Contact', 'Block pattern category', 'full-site-editing' ) ) );
register_block_pattern_category( 'images', array( 'label' => _x( 'Images', 'Block pattern category', 'full-site-editing' ) ) );
register_block_pattern_category( 'list', array( 'label' => _x( 'List', 'Block pattern category', 'full-site-editing' ) ) );

// The 'Two Columns of Text' pattern is in the 'columns' and 'text' categories.
// Removing 'columns' so it doesn't appear as a category with only a single item.
if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'columns' ) ) {
unregister_block_pattern_category( 'columns' );
}
if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'gallery' ) ) {
unregister_block_pattern_category( 'gallery' );
}
if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'header' ) ) {
unregister_block_pattern_category( 'header' );
}
if ( \WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( 'text' ) ) {
unregister_block_pattern_category( 'text' );
}
}

if ( class_exists( 'WP_Block_Patterns_Registry' ) ) {
// Remove core patterns.
// Remove core patterns except 'Two Columns of Text'.
// Unfortunately, \WP_Block_Patterns_Registry::get_instance()->get_all_registered() doesn't return the pattern names as keys.
foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) {
if ( 'core/' === substr( $pattern['name'], 0, 5 ) ) {
if ( 'core/' === substr( $pattern['name'], 0, 5 ) && 'core/text-two-columns' !== $pattern['name'] ) {
unregister_block_pattern( $pattern['name'] );
}
}
}

$pattern_categories = array();
$block_patterns = $this->get_patterns();

foreach ( (array) $this->get_patterns() as $pattern ) {
foreach ( (array) $pattern['categories'] as $slug => $category ) {
$pattern_categories[ $slug ] = $category['title'];
// Add our patterns.
foreach ( $this->get_patterns() as $name => $pattern ) {
register_block_pattern( $name, $pattern );
}
}

// Order categories alphabetically and register them.
ksort( $pattern_categories );
foreach ( (array) $pattern_categories as $slug => $label ) {
register_block_pattern_category( $slug, array( 'label' => $label ) );
}

foreach ( (array) $this->get_patterns() as $pattern ) {
register_block_pattern(
Block_Patterns::PATTERN_NAMESPACE . $pattern['name'],
array(
'title' => $pattern['title'],
'description' => $pattern['title'],
'content' => $pattern['html'],
'viewportWidth' => 1280,
'categories' => array_keys(
$pattern['categories']
),
)
);
}
}

/**
* Returns a list of patterns.
*
* @return array
*/
private function get_patterns() {
$block_patterns = get_transient( $this->patterns_cache_key );

// Load fresh data if we don't have any patterns.
if ( false === $block_patterns || ( defined( 'WP_DISABLE_PATTERN_CACHE' ) && WP_DISABLE_PATTERN_CACHE ) ) {
$request_url = add_query_arg(
array(
'language' => $this->get_iso_639_locale(),
'tags' => 'pattern',
),
'https://public-api.wordpress.com/rest/v1/ptk/patterns'
);

$response = wp_remote_get(
esc_url_raw( $request_url ),
array( 'timeout' => 20 )
);

if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return array();
public function get_patterns() {
$patterns_dir = __DIR__ . '/patterns/';
$patterns = array();

if ( ! is_dir( $patterns_dir ) ) {
return $patterns;
}

$directory_handle = opendir( $patterns_dir );
if ( ! $directory_handle ) {
return $patterns;
}

// Get all the pattern files.
$pattern = readdir( $directory_handle );
$pattern_files = array();
while ( false !== $pattern ) {
// Only allow files ending in .json or .php.
if ( substr( $pattern, -5 ) === '.json' || substr( $pattern, -4 ) === '.php' ) {
$pattern_files[] = $pattern;
}
$block_patterns = json_decode( wp_remote_retrieve_body( $response ), true );
set_transient( $this->patterns_cache_key, $block_patterns, DAY_IN_SECONDS );

$pattern = readdir( $directory_handle );
}

return $block_patterns;
}
closedir( $directory_handle );

// Manually curated list of patterns to go at the top of the list.
$featured_patterns = array(
'masonry-gallery.php',
'description-and-image.php',
'two-images-and-quote.php',
'image-and-description.php',
'three-images-side-by-side.php',
'image-and-text.php',
'three-columns-and-image.php',
'collage-gallery.php',
'headline.php',
'headline-02.php',
'recent-posts.php',
'recent-posts-02.php',
'two-images-side-by-side.php',
'numbers.php',
'contact.php',
'contact-02.php',
'contact-03.php',
'food-menu.php',
);

/**
* Returns ISO 639 conforming locale string.
*
* @return string ISO 639 locale string
*/
private function get_iso_639_locale() {
// Make sure to get blog locale, not user locale.
$language = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_locale();
$language = strtolower( $language );

if ( in_array( $language, array( 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ), true ) ) {
$language = str_replace( '_', '-', $language );
} else {
$language = preg_replace( '/([-_].*)$/i', '', $language );
// Add the featured patterns to the top of the patterns.
$pattern_files = array_merge( $featured_patterns, array_diff( $pattern_files, $featured_patterns ) );

// Get the pattern files contents.
foreach ( $pattern_files as $pattern_file ) {
if ( substr( $pattern_file, -5 ) === '.json' ) {
$pattern_name = self::PATTERN_NAMESPACE . substr( $pattern_file, 0, -5 );
$patterns[ $pattern_name ] = json_decode(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( $patterns_dir . $pattern_file ),
true
);
}

if ( substr( $pattern_file, -4 ) === '.php' ) {
$pattern_name = self::PATTERN_NAMESPACE . substr( $pattern_file, 0, -4 );
$patterns[ $pattern_name ] = include $patterns_dir . $pattern_file;
}
}

return $language;
return $patterns;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Podcast pattern.
*
* @package A8C\FSE
*/

$markup = '
<!-- wp:cover {"customOverlayColor":"#54426b","minHeight":250,"align":"full"} -->
<div class="wp-block-cover alignfull has-background-dim" style="background-color:#54426b;min-height:250px"><div class="wp-block-cover__inner-container"><!-- wp:heading {"align":"center","level":5,"style":{"color":{"text":"#ffffff"}}} -->
<h5 class="has-text-align-center has-text-color" style="color:#ffffff"><strong>%1$s</strong></h5>
<!-- /wp:heading -->
<!-- wp:audio {"id":1502} -->
<figure class="wp-block-audio"><audio controls src="https://dotcompatterns.files.wordpress.com/2020/06/komiku_-_02_-_chill_out_theme.mp3"></audio></figure>
<!-- /wp:audio --></div></div>
<!-- /wp:cover -->
';

return array(
'__file' => 'wp_block',
'title' => esc_html__( 'Audio Player', 'full-site-editing' ),
'categories' => array( 'about', 'media', 'podcast' ),
'content' => sprintf(
$markup,
esc_html__( 'First time here? Let me tell you why this show rocks!', 'full-site-editing' )
),
'viewportWidth' => 1280,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Call to Action pattern.
*
* @package A8C\FSE
*/

$markup = '
<!-- wp:cover {"url":"https://dotcompatterns.files.wordpress.com/2020/04/louis-hansel-shotsoflouis-d0ueoxdvj5i-unsplash.jpg","id":181,"dimRatio":20,"customOverlayColor":"#000000","focalPoint":{"x":0.5,"y":"0.70"},"minHeight":620,"align":"full"} -->
<div class="wp-block-cover alignfull has-background-dim-20 has-background-dim" style="background-image:url(https://dotcompatterns.files.wordpress.com/2020/04/louis-hansel-shotsoflouis-d0ueoxdvj5i-unsplash.jpg);background-color:#000000;background-position:50% 70%;min-height:620px">
<div class="wp-block-cover__inner-container">
<!-- wp:jetpack/layout-grid {"addGutterEnds":false,"column1DesktopSpan":10,"column1DesktopOffset":1,"column1TabletSpan":8,"column1MobileSpan":4,"column2DesktopOffset":1,"className":"column1-desktop-grid__span-10 column1-desktop-grid__start-2 column1-desktop-grid__row-1 column1-tablet-grid__span-8 column1-tablet-grid__row-1 column1-mobile-grid__span-4 column1-mobile-grid__row-1"} -->
<div class="wp-block-jetpack-layout-grid alignfull column1-desktop-grid__span-10 column1-desktop-grid__start-2 column1-desktop-grid__row-1 column1-tablet-grid__span-8 column1-tablet-grid__row-1 column1-mobile-grid__span-4 column1-mobile-grid__row-1 wp-block-jetpack-layout-gutter__nowrap">
<!-- wp:jetpack/layout-grid-column -->
<div class="wp-block-jetpack-layout-grid-column wp-block-jetpack-layout-grid__padding-none">
<!-- wp:paragraph {"align":"center","customTextColor":"#ffffff","style":{"typography":{"fontSize":80}},"className":"margin-bottom-none"} -->
<p style="color:#ffffff;font-size:80px" class="has-text-color has-text-align-center margin-bottom-none"><strong>' . esc_html__( 'Get it delivered', 'full-site-editing' ) . '</strong></p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"align":"center","customTextColor":"#ffffff","className":"margin-top-none"} -->
<p style="color:#ffffff" class="has-text-color has-text-align-center margin-top-none">' . esc_html__( 'If you can&#8217;t come to us, we&#8217;ll go to you.', 'full-site-editing' ) . '</p>
<!-- /wp:paragraph -->
<!-- wp:buttons {"align":"center"} -->
<div class="wp-block-buttons aligncenter"><!-- wp:button {"style":{"color":{"text":"#ffffff","background":"#a5150f"}}} -->
<a class="wp-block-button wp-block-button__link has-text-color has-background" style="background-color:#a5150f;color:#ffffff">' . esc_html__( 'Order now', 'full-site-editing' ) . '</a>
<!-- /wp:button -->
</div>
<!-- /wp:buttons -->
</div>
<!-- /wp:jetpack/layout-grid-column -->
</div>
<!-- /wp:jetpack/layout-grid -->
</div>
</div>
<!-- /wp:cover -->
';

return array(
'title' => esc_html__( 'Call to Action', 'full-site-editing' ),
'categories' => array( 'call-to-action' ),
'content' => $markup,
'viewportWidth' => 1280,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php
/**
* Call to Action pattern.
*
* @package A8C\FSE
*/

$markup = '
<!-- wp:jetpack/layout-grid {"gutterSize":"none","addGutterEnds":false,"column1DesktopSpan":6,"column1TabletSpan":4,"column1MobileSpan":4,"column2DesktopSpan":6,"column2TabletSpan":4,"column2MobileSpan":4,"className":"column1-desktop-grid__span-6 column1-desktop-grid__row-1 column2-desktop-grid__span-6 column2-desktop-grid__start-7 column2-desktop-grid__row-1 column1-tablet-grid__span-4 column1-tablet-grid__row-1 column2-tablet-grid__span-4 column2-tablet-grid__start-5 column2-tablet-grid__row-1 column1-mobile-grid__span-4 column1-mobile-grid__row-1 column2-mobile-grid__span-4 column2-mobile-grid__row-2"} -->
<div class="wp-block-jetpack-layout-grid alignfull column1-desktop-grid__span-6 column1-desktop-grid__row-1 column2-desktop-grid__span-6 column2-desktop-grid__start-7 column2-desktop-grid__row-1 column1-tablet-grid__span-4 column1-tablet-grid__row-1 column2-tablet-grid__span-4 column2-tablet-grid__start-5 column2-tablet-grid__row-1 column1-mobile-grid__span-4 column1-mobile-grid__row-1 column2-mobile-grid__span-4 column2-mobile-grid__row-2 wp-block-jetpack-layout-gutter__nowrap wp-block-jetpack-layout-gutter__none"><!-- wp:jetpack/layout-grid-column -->
<div class="wp-block-jetpack-layout-grid-column wp-block-jetpack-layout-grid__padding-none"><!-- wp:cover {"customOverlayColor":"#e40285"} -->
<div class="wp-block-cover has-background-dim" style="background-color:#e40285"><div class="wp-block-cover__inner-container"><!-- wp:group {"style":{"color":{"text":"#ffffff","background":"#e40285"}}} -->
<div class="wp-block-group has-text-color has-background" style="background-color:#e40285;color:#ffffff"><div class="wp-block-group__inner-container"><!-- wp:heading {"style":{"color":{"text":"#ffffff"}}} -->
<h2 class="has-text-color" style="color:#ffffff"><strong>%1$s</strong></h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"style":{"color":{"text":"#ffffff"}}} -->
<p class="has-text-color" style="color:#ffffff">%2$s</p>
<!-- /wp:paragraph -->
<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"style":{"color":{"text":"#ffffff","background":"#2e008b"}},"className":"is-style-fill"} -->
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link has-text-color has-background" style="background-color:#2e008b;color:#ffffff">%3$s</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div></div>
<!-- /wp:group --></div></div>
<!-- /wp:cover --></div>
<!-- /wp:jetpack/layout-grid-column -->
<!-- wp:jetpack/layout-grid-column -->
<div class="wp-block-jetpack-layout-grid-column wp-block-jetpack-layout-grid__padding-none"><!-- wp:cover {"customOverlayColor":"#2e008b"} -->
<div class="wp-block-cover has-background-dim" style="background-color:#2e008b"><div class="wp-block-cover__inner-container"><!-- wp:group {"style":{"color":{"text":"#ffffff","background":"#2e008b"}}} -->
<div class="wp-block-group has-text-color has-background" style="background-color:#2e008b;color:#ffffff"><div class="wp-block-group__inner-container"><!-- wp:heading {"style":{"color":{"text":"#ffffff"}}} -->
<h2 class="has-text-color" style="color:#ffffff"><strong>%4$s</strong></h2>
<!-- /wp:heading -->
<!-- wp:paragraph {"style":{"color":{"text":"#ffffff"}}} -->
<p class="has-text-color" style="color:#ffffff">%5$s</p>
<!-- /wp:paragraph -->
<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"style":{"color":{"text":"#ffffff","background":"#e40285"}},"className":"is-style-fill"} -->
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link has-text-color has-background" style="background-color:#e40285;color:#ffffff">%3$s</a></div>
<!-- /wp:button --></div>
<!-- /wp:buttons --></div></div>
<!-- /wp:group --></div></div>
<!-- /wp:cover --></div>
<!-- /wp:jetpack/layout-grid-column --></div>
<!-- /wp:jetpack/layout-grid -->
';

return array(
'__file' => 'wp_block',
'title' => esc_html__( 'Call to Action', 'full-site-editing' ),
'categories' => array( 'call-to-action' ),
'content' => sprintf(
$markup,
esc_html__( 'Want to volunteer?', 'full-site-editing' ),
esc_html__( 'We’ve had an incredible response so far, and are doing everything we can to respond to everyone who wants to volunteer in one of our community programmes.', 'full-site-editing' ),
esc_html__( 'Get involved', 'full-site-editing' ),
esc_html__( 'Are you a business?', 'full-site-editing' ),
esc_html__( 'We are uniting our resources around this challenge, and we are combining our resources and asks to make it easy for people to support their communities.', 'full-site-editing' )
),
'viewportWidth' => 1280,
);
Loading

0 comments on commit d905726

Please sign in to comment.