PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.4.2
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.4.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 trunk 1.0.0 2.0.0 2.0.1 2.0.2 2.0.3 3.0 3.0.1 3.0.2 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1
superb-blocks / src / gutenberg / class-gutenberg-enhancements-controller.php
superb-blocks / src / gutenberg Last commit date
block-api 1 year ago templates 1 year ago class-gutenberg-controller.php 1 year ago class-gutenberg-enhancements-controller.php 1 year ago
class-gutenberg-enhancements-controller.php
237 lines
1 <?php
2
3 namespace SuperbAddons\Gutenberg\Controllers;
4
5 defined('ABSPATH') || exit();
6
7 use Exception;
8 use SuperbAddons\Config\Capabilities;
9 use SuperbAddons\Data\Controllers\LogController;
10 use SuperbAddons\Data\Controllers\RestController;
11
12 use WP_Block_Type_Registry;
13 use WP_Error;
14 use WP_REST_Server;
15 use WP_HTML_Tag_Processor;
16
17 class GutenbergEnhancementsController
18 {
19 const ENHANCEMENTS_OPTION = 'superb-blocks-enhancements';
20
21 const HIGHLIGHTS_KEY = 'superb-blocks-highlights';
22 const HIGHLIGHTS_QUICKOPTIONS_KEY = 'superb-blocks-highlights-qo';
23 const HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY = 'superb-blocks-highlights-qo-b';
24 const HIDERS_KEY = 'superb-blocks-hiders';
25
26 public static function Initialize()
27 {
28 self::InitializeEnhancementEndpoints();
29 self::InitializeEditorEnhancements();
30 }
31
32 private static function InitializeEnhancementEndpoints()
33 {
34 RestController::AddRoute('/options/enhancements', array(
35 'methods' => WP_REST_Server::READABLE,
36 'permission_callback' => array(self::class, 'OptionsCallbackPermissionCheck'),
37 'callback' => array(self::class, 'OptionsCallback'),
38 ));
39 RestController::AddRoute('/options/enhancements', array(
40 'methods' => WP_REST_Server::EDITABLE,
41 'permission_callback' => array(self::class, 'OptionsCallbackPermissionCheck'),
42 'callback' => array(self::class, 'OptionsSaveCallback'),
43 ));
44 }
45
46 private static function InitializeEditorEnhancements()
47 {
48 add_filter('render_block', [__CLASS__, 'FilterEnhancementsRender'], 10, 2);
49 add_filter('wp_enqueue_scripts', [__CLASS__, 'EnhancementsEnqueue']);
50
51 add_filter('rest_pre_dispatch', array(__CLASS__, 'rest_pre_dispatch'), 10, 3);
52 }
53
54 /*
55 * This function is used to remove the attributes that are not allowed for the block during the server side rendering.
56 * Solves the issue of editor server side rendering not working if blocks have custom attributes registered during runtime.
57 */
58 public static function rest_pre_dispatch($result, $server, $request)
59 {
60 if (strpos($request->get_route(), '/wp/v2/block-renderer') === false || !isset($request['context']) || $request['context'] !== 'edit') {
61 return $result;
62 }
63
64 if (!isset($request['attributes']) || !class_exists('WP_Block_Type_Registry')) {
65 return $result;
66 }
67
68 $block_type = str_replace('/wp/v2/block-renderer/', '', $request->get_route());
69 $registry = WP_Block_Type_Registry::get_instance();
70 $block = $registry->get_registered($block_type);
71 if (!$block) {
72 return $result;
73 }
74
75 $allowed_attributes = $block->get_attributes();
76 $attributes = $request['attributes'];
77
78 foreach ($attributes as $key => $value) {
79 if (!isset($allowed_attributes[$key])) {
80 unset($attributes[$key]);
81 }
82 }
83
84 $request['attributes'] = $attributes;
85
86 return $result;
87 }
88
89 public static function FilterEnhancementsRender($block_content, $block)
90 {
91 $block_content = self::MaybeAddBlockTagModifications($block_content, $block, array(
92 'spbaddHideOnMobile' => array(
93 'class' => 'superb-addons-hide-on-mobile',
94 ),
95 'spbaddHideOnTablet' => array(
96 'class' => 'superb-addons-hide-on-tablet',
97 ),
98 ));
99
100 return $block_content;
101 }
102
103 public static function EnhancementsEnqueue()
104 {
105 wp_enqueue_style('superb-addons-enhancements', SUPERBADDONS_ASSETS_PATH . '/css/enhancements.min.css', array(), SUPERBADDONS_VERSION);
106 }
107
108 public static function MaybeAddBlockTagModifications($block_content, $block, $classes)
109 {
110 if (!is_array($classes) || empty($classes)) {
111 return $block_content;
112 }
113
114 $added_html_classes = array();
115 $added_html_styles = array();
116 $added_html_attributes = array();
117 foreach ($classes as $required_attribute => $modification) {
118 if (!isset($block['attrs'][$required_attribute]) || !$block['attrs'][$required_attribute]) {
119 continue;
120 }
121 foreach ($modification as $modification_key => $value) {
122 if ($modification_key === 'required-values') {
123 foreach ($value as $required_value => $conditional_modifications) {
124 if ($block['attrs'][$required_attribute] !== $required_value) {
125 continue;
126 }
127 foreach ($conditional_modifications as $conditional_modification_key => $conditional_value) {
128 self::AppendModificationArrays($conditional_modification_key, $conditional_value, $added_html_classes, $added_html_styles, $added_html_attributes);
129 }
130 }
131 continue;
132 }
133
134 self::AppendModificationArrays($modification_key, $value, $added_html_classes, $added_html_styles, $added_html_attributes);
135 }
136 }
137
138 if (empty($added_html_classes) && empty($added_html_styles) && empty($added_html_attributes)) {
139 return $block_content;
140 }
141
142 $block_content = new WP_HTML_Tag_Processor($block_content);
143 $block_content->next_tag();
144 if (!empty($added_html_attributes)) {
145 foreach ($added_html_attributes as $attribute => $value) {
146 $block_content->set_attribute($attribute, $value);
147 }
148 }
149 if (!empty($added_html_styles)) {
150 $block_content->set_attribute("style", join(" ", $added_html_styles));
151 }
152 if (!empty($added_html_classes)) {
153 $block_content->add_class(join(" ", $added_html_classes));
154 }
155 return $block_content->get_updated_html();
156 }
157
158 private static function AppendModificationArrays($modification_key, $value, &$added_html_classes, &$added_html_styles, &$added_html_attributes)
159 {
160 switch ($modification_key) {
161 case 'class':
162 $added_html_classes[] = $value;
163 break;
164 case 'style':
165 $added_html_styles[] = $value;
166 break;
167 default:
168 $added_html_attributes[$modification_key] = $value;
169 break;
170 }
171 }
172
173 public static function OptionsCallbackPermissionCheck()
174 {
175 // Restrict endpoint to only users who have the proper capability.
176 if (!current_user_can(Capabilities::CONTRIBUTOR)) {
177 return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401));
178 }
179
180 return true;
181 }
182
183 public static function OptionsCallback()
184 {
185 try {
186 return rest_ensure_response(self::GetEnhancementsOptions(get_current_user_id()));
187 } catch (Exception $ex) {
188 LogController::HandleException($ex);
189 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
190 }
191 }
192
193 public static function OptionsSaveCallback($request)
194 {
195 try {
196 $key = sanitize_title($request['action']);
197 switch ($key) {
198 case self::HIGHLIGHTS_KEY:
199 case self::HIGHLIGHTS_QUICKOPTIONS_KEY:
200 case self::HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY:
201 case self::HIDERS_KEY:
202 // Allowed
203 break;
204 default:
205 return new \WP_Error('invalid_request', 'Invalid Request', array('status' => 400));
206 }
207
208 $userOptions = self::GetEnhancementsOptions(get_current_user_id());
209 $userOptions[$key] = !$userOptions[$key];
210 if (update_user_meta(get_current_user_id(), self::ENHANCEMENTS_OPTION, $userOptions) !== true) {
211 throw new Exception('Failed to update user meta');
212 }
213
214 return rest_ensure_response(['success' => true]);
215 } catch (Exception $ex) {
216 LogController::HandleException($ex);
217 return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500));
218 }
219 }
220
221 public static function GetEnhancementsOptions($user_id)
222 {
223 $defaults = array(
224 self::HIGHLIGHTS_KEY => true,
225 self::HIGHLIGHTS_QUICKOPTIONS_KEY => false,
226 self::HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY => false,
227 self::HIDERS_KEY => true,
228 );
229 $enhancements = get_user_meta($user_id, self::ENHANCEMENTS_OPTION, true);
230 if (!$enhancements) {
231 return $defaults;
232 }
233
234 return wp_parse_args($enhancements, $defaults);
235 }
236 }
237