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