Full Site Editing - Version 3.4073

Version Description

Download this release

Release Info

Developer ramonopoly
Plugin Icon wp plugin Full Site Editing
Version 3.4073
Comparing to
See all releases

Code changes from version 3.4070 to 3.4073

block-patterns/class-block-patterns-from-api.php CHANGED
@@ -7,6 +7,8 @@
7
 
8
  namespace A8C\FSE;
9
 
 
 
10
  /**
11
  * Class Block_Patterns_From_API
12
  */
@@ -29,37 +31,37 @@ class Block_Patterns_From_API {
29
  private $valid_patterns_sources = array( 'block_patterns', 'fse_block_patterns' );
30
 
31
  /**
32
- * Block_Patterns constructor.
33
  *
34
- * @param array $patterns_sources A array of strings, each of which matches a valid source for retrieving patterns.
35
  */
36
- private function __construct( $patterns_sources ) {
37
- $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources;
38
-
39
- // Tells the backend which patterns source site to default to.
40
- $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' );
41
-
42
- $this->register_patterns();
43
- }
44
 
45
  /**
46
- * Creates instance.
47
  *
48
- * @param array $patterns_sources A array of strings, each of which matches a valid source for retrieving patterns.
49
- * @return \A8C\FSE\Block_Patterns
50
  */
51
- public static function get_instance( $patterns_sources = array() ) {
52
- if ( null === self::$instance ) {
53
- self::$instance = new self( $patterns_sources );
54
- }
55
 
56
- return self::$instance;
 
 
 
 
 
 
 
 
 
57
  }
58
 
59
  /**
60
  * Register FSE block patterns and categories.
 
 
61
  */
62
- private function register_patterns() {
63
  if ( class_exists( 'WP_Block_Patterns_Registry' ) ) {
64
  // Remove core patterns.
65
  foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) {
@@ -69,18 +71,12 @@ class Block_Patterns_From_API {
69
  }
70
  }
71
 
 
 
 
72
  // For every pattern source site, fetch the patterns.
73
  foreach ( $this->patterns_sources as $patterns_source ) {
74
- $patterns_cache_key = sha1(
75
- implode(
76
- '_',
77
- array(
78
- $patterns_source,
79
- A8C_ETK_PLUGIN_VERSION,
80
- $this->get_block_patterns_locale(),
81
- )
82
- )
83
- );
84
 
85
  $pattern_categories = array();
86
  $block_patterns = $this->get_patterns( $patterns_cache_key, $patterns_source );
@@ -127,9 +123,10 @@ class Block_Patterns_From_API {
127
  // default width of 1280 and ensure a safe minimum width of 320.
128
  $viewport_width = isset( $pattern['pattern_meta']['viewport_width'] ) ? intval( $pattern['pattern_meta']['viewport_width'] ) : 1280;
129
  $viewport_width = $viewport_width < 320 ? 320 : $viewport_width;
 
130
 
131
- register_block_pattern(
132
- self::PATTERN_NAMESPACE . $pattern['name'],
133
  array(
134
  'title' => $pattern['title'],
135
  'description' => $pattern['description'],
@@ -144,6 +141,7 @@ class Block_Patterns_From_API {
144
  }
145
  }
146
  }
 
147
  }
148
 
149
  /**
@@ -167,25 +165,14 @@ class Block_Patterns_From_API {
167
  'tags' => 'pattern',
168
  'pattern_meta' => 'is_web',
169
  ),
170
- 'https://public-api.wordpress.com/rest/v1/ptk/patterns/' . $this->get_block_patterns_locale()
171
  )
172
  );
173
 
174
- $args = array( 'timeout' => 20 );
175
-
176
- if ( function_exists( 'wpcom_json_api_get' ) ) {
177
- $response = wpcom_json_api_get( $request_url, $args );
178
- } else {
179
- $response = wp_remote_get( $request_url, $args );
180
- }
181
-
182
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
183
- return array();
184
- }
185
- return json_decode( wp_remote_retrieve_body( $response ), true );
186
  }
187
 
188
- $block_patterns = wp_cache_get( $patterns_cache_key, 'ptk_patterns' );
189
 
190
  // Load fresh data if we don't have any patterns.
191
  if ( false === $block_patterns || ( defined( 'WP_DISABLE_PATTERN_CACHE' ) && WP_DISABLE_PATTERN_CACHE ) ) {
@@ -196,23 +183,12 @@ class Block_Patterns_From_API {
196
  'pattern_meta' => 'is_web',
197
  'patterns_source' => $patterns_source,
198
  ),
199
- 'https://public-api.wordpress.com/rest/v1/ptk/patterns/' . $this->get_block_patterns_locale()
200
  )
201
  );
202
 
203
- $args = array( 'timeout' => 20 );
204
-
205
- if ( function_exists( 'wpcom_json_api_get' ) ) {
206
- $response = wpcom_json_api_get( $request_url, $args );
207
- } else {
208
- $response = wp_remote_get( $request_url, $args );
209
- }
210
-
211
- if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
212
- return array();
213
- }
214
- $block_patterns = json_decode( wp_remote_retrieve_body( $response ), true );
215
- wp_cache_add( $patterns_cache_key, $block_patterns, 'ptk_patterns', DAY_IN_SECONDS );
216
  }
217
 
218
  return $block_patterns;
7
 
8
  namespace A8C\FSE;
9
 
10
+ require_once __DIR__ . '/class-block-patterns-utils.php';
11
+
12
  /**
13
  * Class Block_Patterns_From_API
14
  */
31
  private $valid_patterns_sources = array( 'block_patterns', 'fse_block_patterns' );
32
 
33
  /**
34
+ * Patterns source sites.
35
  *
36
+ * @var array
37
  */
38
+ private $patterns_sources;
 
 
 
 
 
 
 
39
 
40
  /**
41
+ * A collection of utility methods.
42
  *
43
+ * @var Block_Patterns_Utils
 
44
  */
45
+ private $utils;
 
 
 
46
 
47
+ /**
48
+ * Block_Patterns constructor.
49
+ *
50
+ * @param array $patterns_sources A array of strings, each of which matches a valid source for retrieving patterns.
51
+ * @param Block_Patterns_Utils $utils A class dependency containing utils methods.
52
+ */
53
+ public function __construct( $patterns_sources, Block_Patterns_Utils $utils = null ) {
54
+ $patterns_sources = empty( $patterns_sources ) ? array( 'block_patterns' ) : $patterns_sources;
55
+ $this->patterns_sources = empty( array_diff( $patterns_sources, $this->valid_patterns_sources ) ) ? $patterns_sources : array( 'block_patterns' );
56
+ $this->utils = empty( $utils ) ? new \A8C\FSE\Block_Patterns_Utils() : $utils;
57
  }
58
 
59
  /**
60
  * Register FSE block patterns and categories.
61
+ *
62
+ * @return array Results of pattern registration.
63
  */
64
+ public function register_patterns() {
65
  if ( class_exists( 'WP_Block_Patterns_Registry' ) ) {
66
  // Remove core patterns.
67
  foreach ( \WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $pattern ) {
71
  }
72
  }
73
 
74
+ // Used to track which patterns we successfully register.
75
+ $results = array();
76
+
77
  // For every pattern source site, fetch the patterns.
78
  foreach ( $this->patterns_sources as $patterns_source ) {
79
+ $patterns_cache_key = $this->utils->get_patterns_cache_key( 'block_patterns' );
 
 
 
 
 
 
 
 
 
80
 
81
  $pattern_categories = array();
82
  $block_patterns = $this->get_patterns( $patterns_cache_key, $patterns_source );
123
  // default width of 1280 and ensure a safe minimum width of 320.
124
  $viewport_width = isset( $pattern['pattern_meta']['viewport_width'] ) ? intval( $pattern['pattern_meta']['viewport_width'] ) : 1280;
125
  $viewport_width = $viewport_width < 320 ? 320 : $viewport_width;
126
+ $pattern_name = self::PATTERN_NAMESPACE . $pattern['name'];
127
 
128
+ $results[ $pattern_name ] = register_block_pattern(
129
+ $pattern_name,
130
  array(
131
  'title' => $pattern['title'],
132
  'description' => $pattern['description'],
141
  }
142
  }
143
  }
144
+ return $results;
145
  }
146
 
147
  /**
165
  'tags' => 'pattern',
166
  'pattern_meta' => 'is_web',
167
  ),
168
+ 'https://public-api.wordpress.com/rest/v1/ptk/patterns/' . $this->utils->get_block_patterns_locale()
169
  )
170
  );
171
 
172
+ return $this->utils->remote_get( $request_url );
 
 
 
 
 
 
 
 
 
 
 
173
  }
174
 
175
+ $block_patterns = $this->utils->cache_get( $patterns_cache_key, 'ptk_patterns' );
176
 
177
  // Load fresh data if we don't have any patterns.
178
  if ( false === $block_patterns || ( defined( 'WP_DISABLE_PATTERN_CACHE' ) && WP_DISABLE_PATTERN_CACHE ) ) {
183
  'pattern_meta' => 'is_web',
184
  'patterns_source' => $patterns_source,
185
  ),
186
+ 'https://public-api.wordpress.com/rest/v1/ptk/patterns/' . $this->utils->get_block_patterns_locale()
187
  )
188
  );
189
 
190
+ $block_patterns = $this->utils->remote_get( $request_url );
191
+ $this->utils->cache_add( $patterns_cache_key, $block_patterns, 'ptk_patterns', DAY_IN_SECONDS );
 
 
 
 
 
 
 
 
 
 
 
192
  }
193
 
194
  return $block_patterns;
block-patterns/test/class-block-patterns-from-api-test.php ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Coming Soon Tests File
4
+ * Run: yarn run test:php --testsuite block-patterns
5
+ *
6
+ * @package full-site-editing-plugin
7
+ */
8
+
9
+ namespace A8C\FSE;
10
+
11
+ use PHPUnit\Framework\TestCase;
12
+
13
+ require_once __DIR__ . '/../class-block-patterns-from-api.php';
14
+
15
+ /**
16
+ * Class Coming_Soon_Test
17
+ */
18
+ class Block_Patterns_From_Api_Test extends TestCase {
19
+ /**
20
+ * PHPUnit_Framework_MockObject_MockObject.
21
+ *
22
+ * @var string
23
+ */
24
+ protected $utils_mock;
25
+
26
+ /**
27
+ * Representation of a Pattern as returned by the API.
28
+ *
29
+ * @var string
30
+ */
31
+ protected $pattern_mock_object;
32
+
33
+ /**
34
+ * Pre-test setup.
35
+ */
36
+ public function setUp() {
37
+ parent::setUp();
38
+ $this->pattern_mock_object = array(
39
+ 'ID' => '1',
40
+ 'site_id' => '2',
41
+ 'title' => 'test title',
42
+ 'name' => 'test pattern name',
43
+ 'description' => 'test description',
44
+ 'html' => '<p>test</p>',
45
+ 'source_url' => 'http;//test',
46
+ 'modified_date' => 'dd:mm:YY',
47
+ 'categories' => array(
48
+ array(
49
+ 'title' => 'test-category',
50
+ ),
51
+ ),
52
+ );
53
+ }
54
+
55
+ /**
56
+ * Returns a mock of Block_Patterns_Utils.
57
+ *
58
+ * @param array $pattern_mock_response What we want Block_Patterns_Utils->remote_get() to return.
59
+ * @param bool|array $cache_get What we want Block_Patterns_Utils->cache_get() to return.
60
+ * @param bool $cache_add What we want Block_Patterns_Utils->cache_add() to return.
61
+ * @param string $get_patterns_cache_key What we want Block_Patterns_Utils->get_patterns_cache_key() to return.
62
+ * @param string $get_block_patterns_locale What we want Block_Patterns_Utils->get_block_patterns_locale() to return.
63
+ * @return obj PHP Unit mock object.
64
+ */
65
+ public function createBlockPatternsUtilsMock( $pattern_mock_response, $cache_get = false, $cache_add = true, $get_patterns_cache_key = 'key-largo', $get_block_patterns_locale = 'fr' ) {
66
+ $mock = $this->createMock( Block_Patterns_Utils::class );
67
+
68
+ $mock->method( 'remote_get' )
69
+ ->willReturn( $pattern_mock_response );
70
+
71
+ $mock->method( 'cache_get' )
72
+ ->willReturn( $cache_get );
73
+
74
+ $mock->method( 'cache_add' )
75
+ ->willReturn( $cache_add );
76
+
77
+ $mock->method( 'get_patterns_cache_key' )
78
+ ->willReturn( $get_patterns_cache_key );
79
+
80
+ $mock->method( 'get_block_patterns_locale' )
81
+ ->willReturn( $get_block_patterns_locale );
82
+
83
+ return $mock;
84
+ }
85
+
86
+ /**
87
+ * Tests that we're making a request where there are no cached patterns.
88
+ */
89
+ public function test_patterns_request_succeeds_with_empty_cache() {
90
+ $utils_mock = $this->createBlockPatternsUtilsMock( array( $this->pattern_mock_object ) );
91
+ $block_patterns_from_api = new Block_Patterns_From_API( array(), $utils_mock );
92
+
93
+ $utils_mock->expects( $this->once() )
94
+ ->method( 'cache_get' )
95
+ ->willReturn( false );
96
+
97
+ $utils_mock->expects( $this->once() )
98
+ ->method( 'remote_get' )
99
+ ->with( 'https://public-api.wordpress.com/rest/v1/ptk/patterns/fr?tags=pattern&pattern_meta=is_web&patterns_source=block_patterns' );
100
+
101
+ $utils_mock->expects( $this->once() )
102
+ ->method( 'cache_add' )
103
+ ->with( $this->stringContains( 'key-largo' ), array( $this->pattern_mock_object ), 'ptk_patterns', DAY_IN_SECONDS );
104
+
105
+ $this->assertEquals( array( 'a8c/' . $this->pattern_mock_object['name'] => true ), $block_patterns_from_api->register_patterns() );
106
+ }
107
+
108
+ /**
109
+ * Tests that we're NOT making a request where there ARE cached patterns.
110
+ */
111
+ public function test_patterns_request_succeeds_with_set_cache() {
112
+ $utils_mock = $this->createBlockPatternsUtilsMock( array( $this->pattern_mock_object ), array( $this->pattern_mock_object ) );
113
+ $block_patterns_from_api = new Block_Patterns_From_API( array(), $utils_mock );
114
+
115
+ $utils_mock->expects( $this->once() )
116
+ ->method( 'cache_get' )
117
+ ->with( $this->stringContains( 'key-largo' ), 'ptk_patterns' );
118
+
119
+ $utils_mock->expects( $this->never() )
120
+ ->method( 'remote_get' );
121
+
122
+ $utils_mock->expects( $this->never() )
123
+ ->method( 'cache_add' );
124
+
125
+ $this->assertEquals( array( 'a8c/' . $this->pattern_mock_object['name'] => true ), $block_patterns_from_api->register_patterns() );
126
+ }
127
+
128
+ /**
129
+ * Tests that we're making a request where we're overriding the source site.
130
+ */
131
+ public function test_patterns_request_succeeds_with_override_source_site() {
132
+ $example_site = function () {
133
+ return 'dotcom';
134
+ };
135
+
136
+ add_filter( 'a8c_override_patterns_source_site', $example_site );
137
+ $utils_mock = $this->createBlockPatternsUtilsMock( array( $this->pattern_mock_object ) );
138
+ $block_patterns_from_api = new Block_Patterns_From_API( array(), $utils_mock );
139
+
140
+ $utils_mock->expects( $this->never() )
141
+ ->method( 'cache_get' );
142
+
143
+ $utils_mock->expects( $this->never() )
144
+ ->method( 'cache_add' );
145
+
146
+ $utils_mock->expects( $this->once() )
147
+ ->method( 'remote_get' )
148
+ ->with( 'https://public-api.wordpress.com/rest/v1/ptk/patterns/fr?site=dotcom&tags=pattern&pattern_meta=is_web' );
149
+
150
+ $this->assertEquals( array( 'a8c/' . $this->pattern_mock_object['name'] => true ), $block_patterns_from_api->register_patterns() );
151
+
152
+ remove_filter( 'a8c_override_patterns_source_site', $example_site );
153
+ }
154
+ }
full-site-editing-plugin.php CHANGED
@@ -2,7 +2,7 @@
2
  /**
3
  * Plugin Name: WordPress.com Editing Toolkit
4
  * Description: Enhances your page creation workflow within the Block Editor.
5
- * Version: 3.4070
6
  * Author: Automattic
7
  * Author URI: https://automattic.com/wordpress-plugins/
8
  * License: GPLv2 or later
@@ -42,7 +42,7 @@ namespace A8C\FSE;
42
  *
43
  * @var string
44
  */
45
- define( 'A8C_ETK_PLUGIN_VERSION', '3.4070' );
46
 
47
  // Always include these helper files for dotcom FSE.
48
  require_once __DIR__ . '/dotcom-fse/helpers.php';
@@ -263,7 +263,8 @@ function load_block_patterns_from_api( $current_screen ) {
263
  }
264
 
265
  require_once __DIR__ . '/block-patterns/class-block-patterns-from-api.php';
266
- Block_Patterns_From_API::get_instance( $patterns_sources );
 
267
  }
268
  add_action( 'current_screen', __NAMESPACE__ . '\load_block_patterns_from_api' );
269
 
2
  /**
3
  * Plugin Name: WordPress.com Editing Toolkit
4
  * Description: Enhances your page creation workflow within the Block Editor.
5
+ * Version: 3.4073
6
  * Author: Automattic
7
  * Author URI: https://automattic.com/wordpress-plugins/
8
  * License: GPLv2 or later
42
  *
43
  * @var string
44
  */
45
+ define( 'A8C_ETK_PLUGIN_VERSION', '3.4073' );
46
 
47
  // Always include these helper files for dotcom FSE.
48
  require_once __DIR__ . '/dotcom-fse/helpers.php';
263
  }
264
 
265
  require_once __DIR__ . '/block-patterns/class-block-patterns-from-api.php';
266
+ $block_patterns_from_api = new Block_Patterns_From_API( $patterns_sources );
267
+ $block_patterns_from_api->register_patterns();
268
  }
269
  add_action( 'current_screen', __NAMESPACE__ . '\load_block_patterns_from_api' );
270
 
phpunit.xml.dist CHANGED
@@ -19,5 +19,8 @@
19
  <testsuite name="coming-soon">
20
  <directory suffix="-test.php">./coming-soon/test/</directory>
21
  </testsuite>
 
 
 
22
  </testsuites>
23
  </phpunit>
19
  <testsuite name="coming-soon">
20
  <directory suffix="-test.php">./coming-soon/test/</directory>
21
  </testsuite>
22
+ <testsuite name="block-patterns">
23
+ <directory suffix="-test.php">./block-patterns/test/</directory>
24
+ </testsuite>
25
  </testsuites>
26
  </phpunit>
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: automattic
3
  Tags: block, blocks, editor, gutenberg, page
4
  Requires at least: 5.5
5
  Tested up to: 5.6
6
- Stable tag: 3.4070
7
  Requires PHP: 5.6.20
8
  License: GPLv2 or later
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
3
  Tags: block, blocks, editor, gutenberg, page
4
  Requires at least: 5.5
5
  Tested up to: 5.6
6
+ Stable tag: 3.4073
7
  Requires PHP: 5.6.20
8
  License: GPLv2 or later
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html