PluginProbe
Tableberg – Simple Gutenberg Table Block / trunk
Tableberg – Simple Gutenberg Table Block vtrunk
1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 0.5.8 All 41 releases
tableberg / includes / Patterns / RegisterPatterns.php

RegisterPatterns.php in Tableberg – Simple Gutenberg Table Block trunk, at includes/Patterns/RegisterPatterns.php

172 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Tableberg\Patterns;
4
5 use WP_Block_Pattern_Categories_Registry;
6 use WP_Block_Patterns_Registry;
7
8 use function register_rest_field;
9 use function trailingslashit;
10
11 class RegisterPatterns {
12 static $image_path_segment = 'includes/Patterns/images';
13
14 public static function from_dir($dir) {
15 if (! file_exists($dir)) {
16 return;
17 }
18 $files = scandir($dir);
19 foreach ($files as $file) {
20 if ($file == '.' || $file == '..') {
21 continue;
22 }
23 $content = json_decode(file_get_contents($dir . '/' . $file), true);
24 register_block_pattern('tableberg/' . str_replace('.json', '', $file), $content);
25 }
26 }
27
28 public static function upsells() {
29 $dir = __DIR__ . '/upsells';
30 if (! file_exists($dir)) {
31 return;
32 }
33 $files = scandir($dir);
34 foreach ($files as $file) {
35 if ($file == '.' || $file == '..') {
36 continue;
37 }
38 $content = json_decode(file_get_contents($dir . '/' . $file), true);
39 $content['content'] = str_replace('::PLUGIN_URL::', TABLEBERG_URL, $content['content']);
40 register_block_pattern('tableberg/' . str_replace('.json', '', $file), $content);
41 }
42 }
43
44 public static function categories() {
45 register_block_pattern_category('tableberg', array( 'label' => __('Tableberg', 'tableberg') ));
46
47 register_block_pattern_category('comparison-table', array( 'label' => __('Comparison Table', 'tableberg') ));
48 register_block_pattern_category('featured-box', array( 'label' => __('Featured Box', 'tableberg') ));
49 register_block_pattern_category('pricing-table', array( 'label' => __('Pricing Table', 'tableberg') ));
50 register_block_pattern_category('pros-cons', array( 'label' => __('Pros & Cons', 'tableberg') ));
51
52 register_block_pattern_category('other', array( 'label' => __('Other', 'tableberg') ));
53 }
54
55 /**
56 * Generate the tableberg pattern name from the pattern id.
57 *
58 * Tableberg patterns are registered with the format of tableberg/{upsell-}?pattern-{pattern_name}.
59 *
60 * @param string $pattern_id ID of the pattern as it is registered to patterns api.
61 *
62 * @return false | string The tableberg pattern name or false if id of pattern is not from tableberg.
63 */
64 public static function generate_tableberg_pattern_name($pattern_id) {
65 $generated_name = false;
66 $tableberg_prefix_matches = array();
67 $match_status = preg_match('/^tableberg\/(.*)/', $pattern_id, $tableberg_prefix_matches);
68
69 if ($match_status) {
70 $generated_name = $tableberg_prefix_matches[1];
71
72 // Check if the pattern is an upsell pattern.
73 $upsell_name_matches = array();
74 $is_upsell = preg_match('/^upsell\-(.*)/', $generated_name, $upsell_name_matches);
75 if ($is_upsell) {
76 $generated_name = $upsell_name_matches[1];
77 }
78 }
79
80 return $generated_name;
81 }
82
83 /**
84 * Register custom rest fields for tableberg patterns.
85 */
86 public static function register_pattern_custom_rest_fields() {
87 $image_path_segment = static::$image_path_segment;
88 register_rest_field(
89 'block-pattern',
90 'tablebergPatternScreenshot',
91 array(
92 'get_callback' => function ($pattern_obj) use ($image_path_segment) {
93 return self::generate_screenshot_path($pattern_obj, $image_path_segment);
94 },
95 'schema' => array(
96 'description' => __('The screenshot url of the tableberg pattern.', 'tableberg'),
97 'type' => 'string',
98 'oneOf' => array(
99 array(
100 'type' => 'string',
101 'format' => 'uri',
102 ),
103 array(
104 'type' => 'boolean',
105 'enum' => false,
106 ),
107 ),
108 'context' => array( 'view' ),
109 ),
110 )
111 );
112 }
113
114 /**
115 * Generate the screenshot url for the tableberg pattern.
116 *
117 * @param array $pattern_obj The pattern object.
118 * @param string $image_path_segment The path segment to the images directory relative to the plugin root.
119 *
120 * @return string|false The screenshot url or false if the pattern is not from tableberg.
121 */
122 public static function generate_screenshot_path($pattern_obj, $image_path_segment) {
123 $screenshot_url = false;
124 $pattern_name = $pattern_obj['name'];
125
126 $pattern_name = self::generate_tableberg_pattern_name($pattern_name);
127
128 // Only add rest field value if the pattern is from tableberg.
129 if ($pattern_name) {
130 $path_segment = sprintf('%s/%s.png', $image_path_segment, $pattern_name);
131 $screenshot_url = trailingslashit(TABLEBERG_URL) . $path_segment;
132 }
133 return $screenshot_url;
134 }
135
136 /**
137 * Get all registered tableberg patterns.
138 *
139 * @return array The list of all registered tableberg patterns.
140 */
141 public static function get_all_registered_tableberg_patterns() {
142 $available_block_patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
143
144 return array_values(
145 array_map(
146 function ($pattern_obj) {
147 $pattern_screenshot_url = self::generate_screenshot_path($pattern_obj, static::$image_path_segment);
148 $pattern_obj['tablebergPatternScreenshot'] = $pattern_screenshot_url;
149
150 return $pattern_obj;
151 },
152 array_filter(
153 $available_block_patterns,
154 function ($pattern_obj) {
155 $pattern_name = $pattern_obj['name'];
156 return self::generate_tableberg_pattern_name($pattern_name);
157 }
158 )
159 )
160 );
161 }
162
163 /**
164 * Get all registered block pattern categories.
165 *
166 * @return array The list of all available block pattern categories.
167 */
168 public static function get_all_registered_tableberg_pattern_categories() {
169 return WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
170 }
171 }
172