block-api
2 years ago
templates
2 years ago
class-gutenberg-controller.php
2 years ago
class-gutenberg-enhancements-controller.php
2 years ago
class-gutenberg-controller.php
323 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use SuperbAddons\Admin\Controllers\SettingsController; |
| 8 | use SuperbAddons\Data\Controllers\CompatibilitySettingsOptionKey; |
| 9 | use SuperbAddons\Data\Controllers\RestController; |
| 10 | use SuperbAddons\Gutenberg\BlocksAPI\Controllers\DynamicBlockAssets; |
| 11 | use SuperbAddons\Gutenberg\BlocksAPI\Controllers\RecentPostsController; |
| 12 | use SuperbAddons\Library\Controllers\LibraryController; |
| 13 | use SuperbAddons\Library\Controllers\LibraryRequestController; |
| 14 | |
| 15 | class GutenbergController |
| 16 | { |
| 17 | const MINIMUM_WORDPRESS_VERSION = '5.8'; |
| 18 | const MINIMUM_PHP_VERSION = '5.6'; |
| 19 | |
| 20 | const PATTERN_BLOCK_ARG = 'is_pattern_block'; |
| 21 | const BLOCKS = array( |
| 22 | ['path' => "animated-heading", "args" => ['render_callback' => array(DynamicBlockAssets::class, 'EnqueueAnimatedHeader')]], |
| 23 | ['path' => "author-box", "args" => []], |
| 24 | ['path' => "ratings", "args" => []], |
| 25 | ['path' => "table-of-contents", "args" => []], |
| 26 | ['path' => "recent-posts", "args" => ['render_callback' => array(RecentPostsController::class, 'DynamicRender')]], |
| 27 | ['path' => "cover-image", "args" => []], |
| 28 | ['path' => "google-maps", "args" => []], |
| 29 | ['path' => "reveal-buttons", "args" => []], |
| 30 | ['path' => "reveal-button", "args" => ['render_callback' => array(DynamicBlockAssets::class, 'EnqueueRevealButton')]] |
| 31 | ); |
| 32 | |
| 33 | public function __construct() |
| 34 | { |
| 35 | if (!self::is_compatible()) { |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | add_action('block_categories_all', array($this, 'RegisterBlockCategory'), defined('PHP_INT_MAX') ? PHP_INT_MAX : 999, 2); |
| 40 | add_action('init', array($this, 'RegisterBlocks'), 0); |
| 41 | add_action('enqueue_block_editor_assets', array($this, 'EnqueueBlockEditorAssets')); |
| 42 | |
| 43 | add_action("enqueue_block_assets", array($this, 'EnqueueEditorIframeAssets')); |
| 44 | add_action("wp_enqueue_scripts", array($this, 'EnqueuePatternAssets')); |
| 45 | GutenbergEnhancementsController::Initialize(); |
| 46 | } |
| 47 | |
| 48 | public static function is_compatible() |
| 49 | { |
| 50 | // Check for required Elementor version |
| 51 | if (version_compare(get_bloginfo('version'), self::MINIMUM_WORDPRESS_VERSION, '<')) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | // Check for required PHP version |
| 56 | if (version_compare(PHP_VERSION, self::MINIMUM_PHP_VERSION, '<')) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | public static function GetGutenbergLibraryMenuItems() |
| 64 | { |
| 65 | return array( |
| 66 | array( |
| 67 | "id" => "patterns", |
| 68 | "title" => esc_html__('Patterns', "superb-blocks"), |
| 69 | "routes" => array( |
| 70 | "list" => LibraryRequestController::GUTENBERG_LIST_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS, |
| 71 | "insert" => LibraryRequestController::GUTENBERG_INSERT_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PATTERNS |
| 72 | ), |
| 73 | "hidden" => false |
| 74 | ), |
| 75 | array( |
| 76 | "id" => "pages", |
| 77 | "title" => esc_html__('Pages', "superb-blocks"), |
| 78 | "routes" => array( |
| 79 | "list" => LibraryRequestController::GUTENBERG_LIST_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES, |
| 80 | "insert" => LibraryRequestController::GUTENBERG_INSERT_ROUTE . LibraryRequestController::GUTENBERG_ROUTE_TYPE_PAGES |
| 81 | ), |
| 82 | "hidden" => false |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | public function EnqueuePatternAssets() |
| 88 | { |
| 89 | wp_register_style( |
| 90 | 'superb-addons-patterns', |
| 91 | SUPERBADDONS_ASSETS_PATH . '/css/patterns.min.css', |
| 92 | array(), |
| 93 | SUPERBADDONS_VERSION |
| 94 | ); |
| 95 | wp_enqueue_style( |
| 96 | 'superb-addons-patterns' |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | public function EnqueueEditorIframeAssets() |
| 101 | { |
| 102 | global $pagenow; |
| 103 | // Enqueues as block assets - check the current page to make sure we only enqueue on the site editor page and not on the frontend |
| 104 | if ('site-editor.php' === $pagenow) { |
| 105 | wp_enqueue_style( |
| 106 | 'superb-gutenberg-layout-library', |
| 107 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css', |
| 108 | array(), |
| 109 | SUPERBADDONS_VERSION |
| 110 | ); |
| 111 | // Enhancements |
| 112 | wp_enqueue_style( |
| 113 | 'superb-addons-editor-enhancements', |
| 114 | SUPERBADDONS_ASSETS_PATH . '/css/editor-enhancements.min.css', |
| 115 | array(), |
| 116 | SUPERBADDONS_VERSION |
| 117 | ); |
| 118 | // Patterns |
| 119 | $this->EnqueuePatternAssets(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | public function EnqueueBlockEditorAssets() |
| 124 | { |
| 125 | self::AddonsLibrary(); |
| 126 | self::EditorEnhancements(); |
| 127 | $this->EnqueuePatternAssets(); |
| 128 | wp_enqueue_script( |
| 129 | 'superb-addons-gutenberg-library', |
| 130 | SUPERBADDONS_ASSETS_PATH . '/js/gutenberg/pattern-library.js', |
| 131 | array("wp-plugins", "wp-hooks", "wp-data", "wp-element", "wp-i18n", "wp-components", "wp-compose", "wp-blocks", "wp-editor"), |
| 132 | SUPERBADDONS_VERSION |
| 133 | ); |
| 134 | wp_localize_script('superb-addons-gutenberg-library', 'superblayoutlibrary_g', array( |
| 135 | "style_placeholder" => esc_html__('All themes', "superb-blocks"), |
| 136 | "category_placeholder" => esc_html__('All categories', "superb-blocks"), |
| 137 | "snacks" => array( |
| 138 | "settings_save_message" => esc_html__("Settings saved successfully.", "superb-blocks"), |
| 139 | "settings_save_error" => esc_html__("Something went wrong while attempting to save your settings. Please try again or contact support if the problem persists.", "superb-blocks"), |
| 140 | "insert_error" => esc_html__('Something went wrong while attempting to insert this element. Please try again or contact support if the problem persists.', "superb-blocks"), |
| 141 | "list_error" => esc_html__('Something went wrong while attempting to list elements. Please try again or contact support if the problem persists.', "superb-blocks") |
| 142 | ), |
| 143 | "menu_items" => self::GetGutenbergLibraryMenuItems(), |
| 144 | "rest" => array( |
| 145 | "base" => \get_rest_url(), |
| 146 | "namespace" => RestController::NAMESPACE, |
| 147 | "nonce" => wp_create_nonce("wp_rest") |
| 148 | ) |
| 149 | )); |
| 150 | wp_enqueue_style( |
| 151 | 'superb-addons-elements', |
| 152 | SUPERBADDONS_ASSETS_PATH . '/css/framework.min.css', |
| 153 | array(), |
| 154 | SUPERBADDONS_VERSION |
| 155 | ); |
| 156 | wp_enqueue_style( |
| 157 | 'superb-addons-font-manrope', |
| 158 | SUPERBADDONS_ASSETS_PATH . '/fonts/manrope/manrope.css', |
| 159 | array(), |
| 160 | SUPERBADDONS_VERSION |
| 161 | ); |
| 162 | wp_enqueue_style( |
| 163 | 'superb-gutenberg-editor-layout-library', |
| 164 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-editor.min.css', |
| 165 | array(), |
| 166 | SUPERBADDONS_VERSION |
| 167 | ); |
| 168 | wp_enqueue_style( |
| 169 | 'superb-gutenberg-layout-library', |
| 170 | SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css', |
| 171 | array(), |
| 172 | SUPERBADDONS_VERSION |
| 173 | ); |
| 174 | wp_enqueue_style( |
| 175 | 'superbaddons-js-snackbar', |
| 176 | SUPERBADDONS_ASSETS_PATH . '/lib/js-snackbar.min.css', |
| 177 | array(), |
| 178 | SUPERBADDONS_VERSION |
| 179 | ); |
| 180 | wp_enqueue_script('superb-addons-select2', SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.js', array('jquery'), SUPERBADDONS_VERSION, true); |
| 181 | wp_enqueue_style( |
| 182 | 'superbaddons-select2', |
| 183 | SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.css', |
| 184 | array(), |
| 185 | SUPERBADDONS_VERSION |
| 186 | ); |
| 187 | |
| 188 | wp_enqueue_script( |
| 189 | 'superbaddons-animated-heading', |
| 190 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/animated-heading.js', |
| 191 | [], |
| 192 | SUPERBADDONS_VERSION, |
| 193 | true |
| 194 | ); |
| 195 | |
| 196 | // Enhancements |
| 197 | wp_enqueue_style( |
| 198 | 'superb-addons-editor-enhancements', |
| 199 | SUPERBADDONS_ASSETS_PATH . '/css/editor-enhancements.min.css', |
| 200 | array(), |
| 201 | SUPERBADDONS_VERSION |
| 202 | ); |
| 203 | |
| 204 | /// Compatibility |
| 205 | |
| 206 | if (SettingsController::IsCompatibilitySettingRelevantAndEnabled(CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING)) { |
| 207 | wp_enqueue_script( |
| 208 | 'superb-addons-block-spacing-compatibility-fix', |
| 209 | SUPERBADDONS_ASSETS_PATH . '/js/compatibility/block-spacing.js', |
| 210 | array('jquery'), |
| 211 | SUPERBADDONS_VERSION, |
| 212 | true |
| 213 | ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public function RegisterBlockCategory($block_categories, $block_editor_context) |
| 218 | { |
| 219 | return array_merge( |
| 220 | array( |
| 221 | array( |
| 222 | 'slug' => 'superb-addons-blocks', |
| 223 | 'title' => __('Superb Addons', "superb-blocks"), |
| 224 | ), |
| 225 | ), |
| 226 | $block_categories |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | public function RegisterBlocks() |
| 231 | { |
| 232 | foreach (self::BLOCKS as $block) { |
| 233 | if (isset($block['args'][self::PATTERN_BLOCK_ARG])) { |
| 234 | $block['args']['category'] = 'superb-addons-blocks-patterns'; |
| 235 | unset($block['args'][self::PATTERN_BLOCK_ARG]); |
| 236 | } |
| 237 | register_block_type(SUPERBADDONS_PLUGIN_DIR . 'blocks/' . $block['path'], $block['args']); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | private static function EditorEnhancements() |
| 242 | { |
| 243 | add_action('admin_footer', function () { |
| 244 | ob_start(); |
| 245 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/block-quick-options.php'); |
| 246 | $template = ob_get_clean(); |
| 247 | echo '<script type="text/template" id="tmpl-gutenberg-superb-block-quick-options">' . $template . '</script>'; |
| 248 | }); |
| 249 | } |
| 250 | |
| 251 | public static function AddonsLibrary() |
| 252 | { |
| 253 | add_action('admin_footer', function () { |
| 254 | ///// Buttons |
| 255 | ob_start(); |
| 256 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/library-button.php'); |
| 257 | $template = ob_get_clean(); |
| 258 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button">' . $template . '</script>'; |
| 259 | // |
| 260 | ob_start(); |
| 261 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/pattern-tab-library-button.php'); |
| 262 | $template = ob_get_clean(); |
| 263 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button-patternstab">' . $template . '</script>'; |
| 264 | // |
| 265 | ob_start(); |
| 266 | include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/appender-button.php'); |
| 267 | $template = ob_get_clean(); |
| 268 | echo '<script type="text/template" id="tmpl-gutenberg-superb-library-appender-button">' . $template . '</script>'; |
| 269 | ////// Library |
| 270 | LibraryController::InsertTemplatesWithWrapper(); |
| 271 | }); |
| 272 | } |
| 273 | |
| 274 | public static function GutenbergDataImportAction($data) |
| 275 | { |
| 276 | if (!isset($data['content'])) { |
| 277 | return $data; |
| 278 | } |
| 279 | |
| 280 | $content = $data['content']; |
| 281 | $content = preg_replace_callback("/(http)?s?:?(\/\/[^\"']*\.(?:png|jpg|jpeg|gif|png|webp))/", function ($matches) { |
| 282 | // Get the URL |
| 283 | $url = $matches[0]; |
| 284 | $basename = pathinfo($url, PATHINFO_BASENAME); |
| 285 | $title = sanitize_title($basename); |
| 286 | |
| 287 | // Check if attachment exists based on the file slug |
| 288 | $posts = get_posts( |
| 289 | array( |
| 290 | 'post_type' => 'attachment', |
| 291 | 'title' => $title, |
| 292 | 'numberposts' => 1, |
| 293 | ) |
| 294 | ); |
| 295 | if (!empty($posts)) { |
| 296 | // Return existing attachment URL |
| 297 | $attachment = $posts[0]; |
| 298 | return wp_get_attachment_image_url($attachment->ID, 'full'); |
| 299 | } |
| 300 | |
| 301 | if (!function_exists('media_sideload_image')) { |
| 302 | require_once(ABSPATH . 'wp-admin/includes/image.php'); |
| 303 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
| 304 | require_once(ABSPATH . 'wp-admin/includes/media.php'); |
| 305 | } |
| 306 | |
| 307 | // Create new attachment |
| 308 | $attachment_id = \media_sideload_image($url, 0, $title, 'id'); |
| 309 | if (is_wp_error($attachment_id) || !is_numeric($attachment_id)) { |
| 310 | // Return original URL if any error occurred |
| 311 | return $url; |
| 312 | } |
| 313 | |
| 314 | // Return new attachment URL |
| 315 | return wp_get_attachment_image_url($attachment_id, 'full'); |
| 316 | }, $content); |
| 317 | |
| 318 | $data['content'] = $content; |
| 319 | |
| 320 | return $data; |
| 321 | } |
| 322 | } |
| 323 |