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