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