-
Notifications
You must be signed in to change notification settings - Fork 2k
/
class-block-patterns-utils.php
118 lines (105 loc) · 3.36 KB
/
class-block-patterns-utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Block Patterns Utils.
*
* @package A8C\FSE
*/
namespace A8C\FSE;
/**
* Class Block_Patterns_Utils
*/
class Block_Patterns_Utils {
/**
* Make remote get requests.
*
* @param string $request_url The request URL.
* @return array The response.
*/
public function remote_get( $request_url ) {
$args = array( 'timeout' => 20 );
if ( function_exists( 'wpcom_json_api_get' ) ) {
$response = wpcom_json_api_get( $request_url, $args );
} else {
$response = wp_remote_get( $request_url, $args );
}
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return array();
}
return json_decode( wp_remote_retrieve_body( $response ), true );
}
/**
* A wrapper for wp_cache_add.
*
* @param int|string $key The cache key to use for retrieval later.
* @param mixed $data The data to add to the cache.
* @param string $group The group to add the cache to. Enables the same key to be used across groups. Default empty.
* @param int $expire When the cache data should expire, in seconds.
* Default 0 (no expiration).
* @return bool True on success, false if cache key and group already exist.
*/
public function cache_add( $key, $data, $group, $expire ) {
return wp_cache_add( $key, $data, $group, $expire );
}
/**
* A wrapper for wp_cache_get.
*
* @param int|string $key The key under which the cache contents are stored.
* @param string $group Where the cache contents are grouped. Default empty.
* @return mixed|false The cache contents on success, false on failure to retrieve contents.
*/
public function cache_get( $key, $group ) {
return wp_cache_get( $key, $group );
}
/**
* Returns the sha1 hash of a concatenated string to use as a cache key.
*
* @param string $patterns_slug A slug for a patterns source site, e.g., `block_patterns`.
* @return string locale slug
*/
public function get_patterns_cache_key( $patterns_slug ) {
return sha1(
implode(
'_',
array(
$patterns_slug,
A8C_ETK_PLUGIN_VERSION,
$this->get_block_patterns_locale(),
)
)
);
}
/**
* Get the locale to be used for fetching block patterns
*
* @return string locale slug
*/
public function get_block_patterns_locale() {
// Block patterns display in the user locale.
$language = get_user_locale();
return \A8C\FSE\Common\get_iso_639_locale( $language );
}
/**
* Check for block type values in the pattern_meta tag.
* When tags have a prefix of `block_type_`, we expect the remaining suffix to be a blockType value.
* We'll add these values to the `(array) blockType` options property when registering the pattern
* via `register_block_pattern`.
*
* @param array $pattern A pattern with a 'pattern_meta' array.
*
* @return array An array of block types defined in pattern meta.
*/
public function maybe_get_pattern_block_types_from_pattern_meta( $pattern ) {
$block_types = array();
if ( ! isset( $pattern['pattern_meta'] ) || empty( $pattern['pattern_meta'] ) ) {
return $block_types;
}
foreach ( $pattern['pattern_meta'] as $pattern_meta => $value ) {
// Match against tags starting with `block_type_`.
$split_slug = preg_split( '/^block_type_/', $pattern_meta );
if ( isset( $split_slug[1] ) ) {
$block_types[] = $split_slug[1];
}
}
return $block_types;
}
}