PluginProbe ʕ •ᴥ•ʔ
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More / 3.2.7
Superb Addons: Blocks, Patterns, Pre-built Pages, Sliders, Popups, Free Forms, Animations & More v3.2.7
4.2.0 4.1.0 4.0.9 4.0.8 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-controller.php
superb-blocks / src / gutenberg Last commit date
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
322 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 wp_enqueue_script(
128 'superb-addons-gutenberg-library',
129 SUPERBADDONS_ASSETS_PATH . '/js/gutenberg/pattern-library.js',
130 array("wp-plugins", "wp-hooks", "wp-data", "wp-element", "wp-i18n", "wp-components", "wp-compose", "wp-blocks", "wp-editor"),
131 SUPERBADDONS_VERSION
132 );
133 wp_localize_script('superb-addons-gutenberg-library', 'superblayoutlibrary_g', array(
134 "style_placeholder" => esc_html__('All themes', "superb-blocks"),
135 "category_placeholder" => esc_html__('All categories', "superb-blocks"),
136 "snacks" => array(
137 "settings_save_message" => esc_html__("Settings saved successfully.", "superb-blocks"),
138 "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"),
139 "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"),
140 "list_error" => esc_html__('Something went wrong while attempting to list elements. Please try again or contact support if the problem persists.', "superb-blocks")
141 ),
142 "menu_items" => self::GetGutenbergLibraryMenuItems(),
143 "rest" => array(
144 "base" => \get_rest_url(),
145 "namespace" => RestController::NAMESPACE,
146 "nonce" => wp_create_nonce("wp_rest")
147 )
148 ));
149 wp_enqueue_style(
150 'superb-addons-elements',
151 SUPERBADDONS_ASSETS_PATH . '/css/framework.min.css',
152 array(),
153 SUPERBADDONS_VERSION
154 );
155 wp_enqueue_style(
156 'superb-addons-font-manrope',
157 SUPERBADDONS_ASSETS_PATH . '/fonts/manrope/manrope.css',
158 array(),
159 SUPERBADDONS_VERSION
160 );
161 wp_enqueue_style(
162 'superb-gutenberg-editor-layout-library',
163 SUPERBADDONS_ASSETS_PATH . '/css/layout-library-editor.min.css',
164 array(),
165 SUPERBADDONS_VERSION
166 );
167 wp_enqueue_style(
168 'superb-gutenberg-layout-library',
169 SUPERBADDONS_ASSETS_PATH . '/css/layout-library-preview.min.css',
170 array(),
171 SUPERBADDONS_VERSION
172 );
173 wp_enqueue_style(
174 'superbaddons-js-snackbar',
175 SUPERBADDONS_ASSETS_PATH . '/lib/js-snackbar.min.css',
176 array(),
177 SUPERBADDONS_VERSION
178 );
179 wp_enqueue_script('superb-addons-select2', SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.js', array('jquery'), SUPERBADDONS_VERSION, true);
180 wp_enqueue_style(
181 'superbaddons-select2',
182 SUPERBADDONS_ASSETS_PATH . '/lib/select2.min.css',
183 array(),
184 SUPERBADDONS_VERSION
185 );
186
187 wp_enqueue_script(
188 'superbaddons-animated-heading',
189 SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/animated-heading.js',
190 [],
191 SUPERBADDONS_VERSION,
192 true
193 );
194
195 // Enhancements
196 wp_enqueue_style(
197 'superb-addons-editor-enhancements',
198 SUPERBADDONS_ASSETS_PATH . '/css/editor-enhancements.min.css',
199 array(),
200 SUPERBADDONS_VERSION
201 );
202
203 /// Compatibility
204
205 if (SettingsController::IsCompatibilitySettingRelevantAndEnabled(CompatibilitySettingsOptionKey::SPECTRA_BLOCK_SPACING)) {
206 wp_enqueue_script(
207 'superb-addons-block-spacing-compatibility-fix',
208 SUPERBADDONS_ASSETS_PATH . '/js/compatibility/block-spacing.js',
209 array('jquery'),
210 SUPERBADDONS_VERSION,
211 true
212 );
213 }
214 }
215
216 public function RegisterBlockCategory($block_categories, $block_editor_context)
217 {
218 return array_merge(
219 array(
220 array(
221 'slug' => 'superb-addons-blocks',
222 'title' => __('Superb Addons', "superb-blocks"),
223 ),
224 ),
225 $block_categories
226 );
227 }
228
229 public function RegisterBlocks()
230 {
231 foreach (self::BLOCKS as $block) {
232 if (isset($block['args'][self::PATTERN_BLOCK_ARG])) {
233 $block['args']['category'] = 'superb-addons-blocks-patterns';
234 unset($block['args'][self::PATTERN_BLOCK_ARG]);
235 }
236 register_block_type(SUPERBADDONS_PLUGIN_DIR . 'blocks/' . $block['path'], $block['args']);
237 }
238 }
239
240 private static function EditorEnhancements()
241 {
242 add_action('admin_footer', function () {
243 ob_start();
244 include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/block-quick-options.php');
245 $template = ob_get_clean();
246 echo '<script type="text/template" id="tmpl-gutenberg-superb-block-quick-options">' . $template . '</script>';
247 });
248 }
249
250 public static function AddonsLibrary()
251 {
252 add_action('admin_footer', function () {
253 ///// Buttons
254 ob_start();
255 include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/library-button.php');
256 $template = ob_get_clean();
257 echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button">' . $template . '</script>';
258 //
259 ob_start();
260 include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/pattern-tab-library-button.php');
261 $template = ob_get_clean();
262 echo '<script type="text/template" id="tmpl-gutenberg-superb-library-button-patternstab">' . $template . '</script>';
263 //
264 ob_start();
265 include(SUPERBADDONS_PLUGIN_DIR . 'src/gutenberg/templates/appender-button.php');
266 $template = ob_get_clean();
267 echo '<script type="text/template" id="tmpl-gutenberg-superb-library-appender-button">' . $template . '</script>';
268 ////// Library
269 LibraryController::InsertTemplatesWithWrapper();
270 });
271 }
272
273 public static function GutenbergDataImportAction($data)
274 {
275 if (!isset($data['content'])) {
276 return $data;
277 }
278
279 $content = $data['content'];
280 $content = preg_replace_callback("/(http)?s?:?(\/\/[^\"']*\.(?:png|jpg|jpeg|gif|png|webp))/", function ($matches) {
281 // Get the URL
282 $url = $matches[0];
283 $basename = pathinfo($url, PATHINFO_BASENAME);
284 $title = sanitize_title($basename);
285
286 // Check if attachment exists based on the file slug
287 $posts = get_posts(
288 array(
289 'post_type' => 'attachment',
290 'title' => $title,
291 'numberposts' => 1,
292 )
293 );
294 if (!empty($posts)) {
295 // Return existing attachment URL
296 $attachment = $posts[0];
297 return wp_get_attachment_image_url($attachment->ID, 'full');
298 }
299
300 if (!function_exists('media_sideload_image')) {
301 require_once(ABSPATH . 'wp-admin/includes/image.php');
302 require_once(ABSPATH . 'wp-admin/includes/file.php');
303 require_once(ABSPATH . 'wp-admin/includes/media.php');
304 }
305
306 // Create new attachment
307 $attachment_id = \media_sideload_image($url, 0, $title, 'id');
308 if (is_wp_error($attachment_id) || !is_numeric($attachment_id)) {
309 // Return original URL if any error occurred
310 return $url;
311 }
312
313 // Return new attachment URL
314 return wp_get_attachment_image_url($attachment_id, 'full');
315 }, $content);
316
317 $data['content'] = $content;
318
319 return $data;
320 }
321 }
322