block-api
1 year ago
templates
1 year ago
class-gutenberg-block-styles.php
1 year ago
class-gutenberg-controller.php
1 year ago
class-gutenberg-enhancements-controller.php
1 year ago
class-gutenberg-enhancements-controller.php
267 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 | const ANIMATIONS_KEY = 'superb-blocks-animations'; |
| 26 | |
| 27 | public static function Initialize() |
| 28 | { |
| 29 | self::InitializeEnhancementEndpoints(); |
| 30 | self::InitializeEditorEnhancements(); |
| 31 | GutenbergBlockStyles::Initialize(); |
| 32 | } |
| 33 | |
| 34 | private static function InitializeEnhancementEndpoints() |
| 35 | { |
| 36 | RestController::AddRoute('/options/enhancements', array( |
| 37 | 'methods' => WP_REST_Server::READABLE, |
| 38 | 'permission_callback' => array(self::class, 'OptionsCallbackPermissionCheck'), |
| 39 | 'callback' => array(self::class, 'OptionsCallback'), |
| 40 | )); |
| 41 | RestController::AddRoute('/options/enhancements', array( |
| 42 | 'methods' => WP_REST_Server::EDITABLE, |
| 43 | 'permission_callback' => array(self::class, 'OptionsCallbackPermissionCheck'), |
| 44 | 'callback' => array(self::class, 'OptionsSaveCallback'), |
| 45 | )); |
| 46 | } |
| 47 | |
| 48 | private static function InitializeEditorEnhancements() |
| 49 | { |
| 50 | add_filter('render_block', [__CLASS__, 'FilterEnhancementsRender'], 10, 2); |
| 51 | add_filter('wp_enqueue_scripts', [__CLASS__, 'EnhancementsEnqueue']); |
| 52 | |
| 53 | add_filter('rest_pre_dispatch', array(__CLASS__, 'rest_pre_dispatch'), 10, 3); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * This function is used to remove the attributes that are not allowed for the block during the server side rendering. |
| 58 | * Solves the issue of editor server side rendering not working if blocks have custom attributes registered during runtime. |
| 59 | */ |
| 60 | public static function rest_pre_dispatch($result, $server, $request) |
| 61 | { |
| 62 | if (strpos($request->get_route(), '/wp/v2/block-renderer') === false || !isset($request['context']) || $request['context'] !== 'edit') { |
| 63 | return $result; |
| 64 | } |
| 65 | |
| 66 | if (!isset($request['attributes']) || !class_exists('WP_Block_Type_Registry')) { |
| 67 | return $result; |
| 68 | } |
| 69 | |
| 70 | $block_type = str_replace('/wp/v2/block-renderer/', '', $request->get_route()); |
| 71 | $registry = WP_Block_Type_Registry::get_instance(); |
| 72 | $block = $registry->get_registered($block_type); |
| 73 | if (!$block) { |
| 74 | return $result; |
| 75 | } |
| 76 | |
| 77 | $allowed_attributes = $block->get_attributes(); |
| 78 | $attributes = $request['attributes']; |
| 79 | |
| 80 | foreach ($attributes as $key => $value) { |
| 81 | if (!isset($allowed_attributes[$key])) { |
| 82 | unset($attributes[$key]); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $request['attributes'] = $attributes; |
| 87 | |
| 88 | return $result; |
| 89 | } |
| 90 | |
| 91 | public static function FilterEnhancementsRender($block_content, $block) |
| 92 | { |
| 93 | $block_content = self::MaybeAddBlockTagModifications($block_content, $block, array( |
| 94 | 'spbaddHideOnMobile' => array( |
| 95 | 'class' => 'superb-addons-hide-on-mobile', |
| 96 | ), |
| 97 | 'spbaddHideOnTablet' => array( |
| 98 | 'class' => 'superb-addons-hide-on-tablet', |
| 99 | ) |
| 100 | )); |
| 101 | |
| 102 | if (isset($block['attrs']['spbaddAnimationsEnabled']) && $block['attrs']['spbaddAnimationsEnabled']) { |
| 103 | wp_enqueue_script('superb-addons-animations', SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/block-animations.js', array(), SUPERBADDONS_VERSION, true); |
| 104 | $block_content = self::MaybeAddBlockTagModifications($block_content, $block, array( |
| 105 | 'spbaddAnimationId' => array( |
| 106 | 'superb-addons-animation' => array("attribute" => "spbaddAnimationId"), |
| 107 | 'style' => 'opacity: 0;' |
| 108 | ) |
| 109 | )); |
| 110 | } |
| 111 | |
| 112 | return $block_content; |
| 113 | } |
| 114 | |
| 115 | public static function EnhancementsEnqueue() |
| 116 | { |
| 117 | wp_enqueue_style('superb-addons-enhancements', SUPERBADDONS_ASSETS_PATH . '/css/enhancements.min.css', array(), SUPERBADDONS_VERSION); |
| 118 | } |
| 119 | |
| 120 | public static function MaybeAddBlockTagModifications($block_content, $block, $classes) |
| 121 | { |
| 122 | if (!is_array($classes) || empty($classes)) { |
| 123 | return $block_content; |
| 124 | } |
| 125 | |
| 126 | $added_html_classes = array(); |
| 127 | $added_html_styles = array(); |
| 128 | $added_html_attributes = array(); |
| 129 | foreach ($classes as $required_attribute => $modification) { |
| 130 | if (!isset($block['attrs'][$required_attribute]) || !$block['attrs'][$required_attribute]) { |
| 131 | continue; |
| 132 | } |
| 133 | foreach ($modification as $modification_key => $value) { |
| 134 | if ($modification_key === 'required-values') { |
| 135 | foreach ($value as $required_value => $conditional_modifications) { |
| 136 | if ($block['attrs'][$required_attribute] != $required_value) { |
| 137 | continue; |
| 138 | } |
| 139 | foreach ($conditional_modifications as $conditional_modification_key => $conditional_value) { |
| 140 | self::AppendModificationArrays($block, $conditional_modification_key, $conditional_value, $added_html_classes, $added_html_styles, $added_html_attributes); |
| 141 | } |
| 142 | } |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | self::AppendModificationArrays($block, $modification_key, $value, $added_html_classes, $added_html_styles, $added_html_attributes); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (empty($added_html_classes) && empty($added_html_styles) && empty($added_html_attributes)) { |
| 151 | return $block_content; |
| 152 | } |
| 153 | |
| 154 | $block_content = new WP_HTML_Tag_Processor($block_content); |
| 155 | $block_content->next_tag(); |
| 156 | if (!empty($added_html_attributes)) { |
| 157 | foreach ($added_html_attributes as $attribute => $value) { |
| 158 | $block_content->set_attribute($attribute, $value); |
| 159 | } |
| 160 | } |
| 161 | if (!empty($added_html_styles)) { |
| 162 | $styles = $block_content->get_attribute("style") ?? ""; |
| 163 | if (!empty($styles) && substr($styles, -1) !== ";") { |
| 164 | $styles .= ";"; |
| 165 | } |
| 166 | $block_content->set_attribute("style", $styles . join(" ", $added_html_styles)); |
| 167 | } |
| 168 | if (!empty($added_html_classes)) { |
| 169 | $block_content->add_class(join(" ", $added_html_classes)); |
| 170 | } |
| 171 | return $block_content->get_updated_html(); |
| 172 | } |
| 173 | |
| 174 | private static function AppendModificationArrays($block, $modification_key, $value, &$added_html_classes, &$added_html_styles, &$added_html_attributes) |
| 175 | { |
| 176 | if (is_array($value)) { |
| 177 | $key = key($value); |
| 178 | $dynamic_value = $value[$key]; |
| 179 | switch ($key) { |
| 180 | case 'attribute': |
| 181 | $value = isset($block['attrs'][$dynamic_value]) ? $block['attrs'][$dynamic_value] : (isset($value['default']) ? $value['default'] : ''); |
| 182 | break; |
| 183 | default: |
| 184 | $value = ""; |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | switch ($modification_key) { |
| 189 | case 'class': |
| 190 | $added_html_classes[] = $value; |
| 191 | break; |
| 192 | case 'style': |
| 193 | $added_html_styles[] = $value; |
| 194 | break; |
| 195 | default: |
| 196 | $added_html_attributes[$modification_key] = $value; |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | public static function OptionsCallbackPermissionCheck() |
| 202 | { |
| 203 | // Restrict endpoint to only users who have the proper capability. |
| 204 | if (!current_user_can(Capabilities::CONTRIBUTOR)) { |
| 205 | return new WP_Error('rest_forbidden', esc_html__('Unauthorized. Please check user permissions.', "superb-blocks"), array('status' => 401)); |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | public static function OptionsCallback() |
| 212 | { |
| 213 | try { |
| 214 | return rest_ensure_response(self::GetEnhancementsOptions(get_current_user_id())); |
| 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 OptionsSaveCallback($request) |
| 222 | { |
| 223 | try { |
| 224 | $key = sanitize_title($request['action']); |
| 225 | switch ($key) { |
| 226 | case self::HIGHLIGHTS_KEY: |
| 227 | case self::HIGHLIGHTS_QUICKOPTIONS_KEY: |
| 228 | case self::HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY: |
| 229 | case self::HIDERS_KEY: |
| 230 | case self::ANIMATIONS_KEY: |
| 231 | // Allowed |
| 232 | break; |
| 233 | default: |
| 234 | return new \WP_Error('invalid_request', 'Invalid Request', array('status' => 400)); |
| 235 | } |
| 236 | |
| 237 | $userOptions = self::GetEnhancementsOptions(get_current_user_id()); |
| 238 | $userOptions[$key] = !$userOptions[$key]; |
| 239 | if (update_user_meta(get_current_user_id(), self::ENHANCEMENTS_OPTION, $userOptions) !== true) { |
| 240 | throw new Exception('Failed to update user meta'); |
| 241 | } |
| 242 | |
| 243 | return rest_ensure_response(['success' => true]); |
| 244 | } catch (Exception $ex) { |
| 245 | LogController::HandleException($ex); |
| 246 | return new \WP_Error('internal_error_plugin', 'Internal Plugin Error', array('status' => 500)); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public static function GetEnhancementsOptions($user_id) |
| 251 | { |
| 252 | $defaults = array( |
| 253 | self::HIGHLIGHTS_KEY => true, |
| 254 | self::HIGHLIGHTS_QUICKOPTIONS_KEY => false, |
| 255 | self::HIGHLIGHTS_QUICKOPTIONS_BOTTOM_KEY => false, |
| 256 | self::HIDERS_KEY => true, |
| 257 | self::ANIMATIONS_KEY => true, |
| 258 | ); |
| 259 | $enhancements = get_user_meta($user_id, self::ENHANCEMENTS_OPTION, true); |
| 260 | if (!$enhancements) { |
| 261 | return $defaults; |
| 262 | } |
| 263 | |
| 264 | return wp_parse_args($enhancements, $defaults); |
| 265 | } |
| 266 | } |
| 267 |