PluginProbe
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs / 5.0.1
BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs v5.0.1
5.0.1 4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 All 39 releases
blockspare / inc / init.php

init.php in BlockSpare – Gutenberg Blocks, AI Content Generator & Site Builder for News, Magazine & Blogs 5.0.1, at inc/init.php

546 lines 17.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * inc/init.php
5 * @package Blockspare
6 */
7
8 defined('ABSPATH') or die('No script kiddies please!');
9
10 class Blockspare_Init
11 {
12 public function __construct()
13 {
14
15 // 1. Hooks
16 add_action('init', [$this, 'blockspare_plugin_init']);
17 add_action('admin_enqueue_scripts', [$this, 'blockspare_enqueue_editor_assets']);
18 add_action('customize_controls_enqueue_scripts', [$this, 'blockspare_enqueue_customizer_assets']);
19 add_action('enqueue_block_assets', [$this, 'blockspare_fb_enqueue_frontend_assets']);
20
21 add_action('wp_enqueue_scripts', [$this, 'blockspare_enqueue_frontend_script']);
22 add_filter('block_categories_all', [$this, 'blockspare_register_custom_categories'], 10, 2);
23 add_action('plugins_loaded', [$this, 'blockspare_load_dynamic_blocks']);
24 add_action('rest_api_init', array($this, 'blockspare_blocks_register_api_endpoints'));
25 }
26
27 public function blockspare_plugin_init()
28 {
29 $this->blockspare_blocks_register_blocks();
30 //$this->blockspare_fb_enqueue_frontend_assets();
31 $this->blockspare_load_load_text_domain();
32 }
33 // ══════════════════════════════════════════════════════════════════════════
34 // Helper Functions
35 // ══════════════════════════════════════════════════════════════════════════
36 public static function get_post_types()
37 {
38 static $cache = null;
39 if ($cache !== null) return $cache;
40
41 $transient = get_transient('blockspare_post_types');
42 if ($transient !== false) {
43 $cache = $transient;
44 return $cache;
45 }
46
47
48 $args = ['public' => true, 'show_in_rest' => true];
49 $post_types = get_post_types($args, 'objects');
50 $excluded_post_types = [
51 'attachment',
52 'bs_templates',
53 ];
54 $output = [];
55
56
57 foreach ($post_types as $post_type) {
58
59 if (in_array($post_type->name, $excluded_post_types, true)) {
60 continue;
61 }
62
63 $output[] = [
64 'value' => $post_type->name,
65 'label' => $post_type->label,
66 ];
67 }
68
69 set_transient('blockspare_post_types', $output, HOUR_IN_SECONDS);
70 $cache = apply_filters('aft_blocks_post_types', $output);
71
72 return $cache;
73 }
74
75 public static function get_taxonomies()
76 {
77 static $cache = null;
78 if ($cache !== null) return $cache;
79
80 $transient = get_transient('blockspare_taxonomies');
81 if ($transient !== false) {
82 $cache = $transient;
83 return $cache;
84 }
85
86 $post_types = self::get_post_types();
87 $output = [];
88
89 foreach ($post_types as $post_type) {
90 $pt = $post_type['value'];
91 $taxonomies = get_object_taxonomies($pt, 'objects');
92
93 foreach ($taxonomies as $term_slug => $term) {
94 if (!$term->public || !$term->show_ui) continue;
95
96 $terms = get_terms([
97 'taxonomy' => $term_slug,
98 'hide_empty' => false,
99 ]);
100
101 if (!empty($terms) && !is_wp_error($terms)) {
102 foreach ($terms as $term_item) {
103 $output[$pt]['terms'][$term_slug][] = [
104 'value' => $term_item->term_id,
105 'label' => $term_item->name,
106 ];
107 }
108 }
109 }
110
111 $output[$pt]['taxonomy'] = $taxonomies;
112 }
113
114 set_transient('blockspare_taxonomies', $output, HOUR_IN_SECONDS);
115 $cache = apply_filters('aft_blocks_taxonomies', $output);
116
117 return $cache;
118 }
119
120 // ══════════════════════════════════════════════════════════════════════════
121 // 1. Register blocks
122 // ══════════════════════════════════════════════════════════════════════════
123 public function blockspare_blocks_register_blocks()
124 {
125 $blocks = glob(BLOCKSPARE_PLUGIN_DIR . 'build/block/*/block.json');
126
127 if ($blocks) {
128 foreach ($blocks as $block) {
129 register_block_type(dirname($block));
130 }
131 }
132
133 wp_enqueue_style(
134 'blockspare-fontawesome',
135 BLOCKSPARE_PLUGIN_URL . 'assets/fontawesome/css/all.css',
136 [],
137 BLOCKSPARE_VERSION
138 );
139 }
140
141 public function blockspare_enqueue_frontend_script()
142 {
143 wp_register_style(
144 'slick',
145 BLOCKSPARE_PLUGIN_URL . 'assets/slick/css/slick.min.css',
146 array(),
147 BLOCKSPARE_VERSION
148 );
149
150 wp_register_script(
151 'slick',
152 BLOCKSPARE_PLUGIN_URL . 'assets/slick/js/slick.min.js',
153 array('jquery'),
154 BLOCKSPARE_VERSION,
155 true
156 );
157 global $post;
158
159 if ($post && has_blocks($post->post_content)) {
160 $content = $post->post_content;
161
162 $hasCarousel = has_block('blockspare/blockspare-carousel', $content);
163 $hasSlider = has_block('blockspare/blockspare-slider', $content);
164 $hasMasonry = has_block('blockspare/blockspare-masonry', $content);
165
166 if ($hasCarousel || $hasSlider) {
167 wp_enqueue_style('slick');
168 wp_enqueue_script('slick');
169 }
170 if ($hasMasonry) {
171 wp_enqueue_script('jquery-masonry');
172 }
173 }
174
175
176
177
178 wp_enqueue_script(
179 'block_animation_script',
180 BLOCKSPARE_PLUGIN_URL . 'dist/block_animation_script.js',
181 ['jquery'],
182 BLOCKSPARE_VERSION,
183 true
184 );
185 wp_register_script(
186 'block_pagination_script',
187 BLOCKSPARE_PLUGIN_URL . 'dist/block_pagination_script.js',
188 ['jquery'],
189 BLOCKSPARE_VERSION,
190 true
191 );
192 wp_localize_script('block_pagination_script', 'blocsparePagination', array(
193 'ajaxurl' => admin_url('admin-ajax.php'),
194 'nonce' => wp_create_nonce('blockspare-load-more-nonce'),
195 ));
196 }
197
198 // ══════════════════════════════════════════════════════════════════════════
199 // 2. Editor-only assets
200 // ══════════════════════════════════════════════════════════════════════════
201 public function blockspare_enqueue_editor_assets()
202 {
203
204 global $pagenow;
205
206 // �
207 Prevent unnecessary loading
208 if (!in_array($pagenow, ['post.php', 'post-new.php', 'widgets.php'])) {
209 return;
210 }
211
212 wp_register_script(
213 'blockspare-category',
214 BLOCKSPARE_PLUGIN_URL . 'dist/block_category.js',
215 [
216 'wp-blocks',
217 'wp-i18n',
218 'wp-element',
219 'wp-components',
220 'wp-block-editor',
221 'wp-api-fetch',
222 ],
223 BLOCKSPARE_VERSION,
224 true
225 );
226
227 global $pagenow;
228 $loadscripton = ($pagenow === 'widgets.php' || is_customize_preview());
229
230 wp_add_inline_script(
231 'wp-blocks',
232 'var blockspare_globals = ' . wp_json_encode($this->blockspare_get_globals()) . ';',
233 'before'
234 );
235
236
237 wp_enqueue_script('blockspare-category');
238
239 wp_enqueue_style(
240 'blockspare-panel',
241 BLOCKSPARE_PLUGIN_URL . 'dist/panel.css',
242 ['wp-edit-blocks'],
243 BLOCKSPARE_VERSION
244 );
245 wp_enqueue_style(
246 'blockspare-animation-blocks',
247 BLOCKSPARE_PLUGIN_URL . 'dist/block_animation.css',
248 [],
249 BLOCKSPARE_VERSION
250 );
251 if (is_admin()):
252 wp_enqueue_script(
253 'blockspare-design-btn',
254 BLOCKSPARE_PLUGIN_URL . 'dist/block_design_btn.js',
255 array('blockspare-category'),
256 BLOCKSPARE_VERSION,
257 true
258 );
259 endif;
260
261
262 wp_localize_script('blockspare-category', 'cbvData', [
263 'isLoggedIn' => is_user_logged_in(),
264 ]);
265 }
266
267 public function blockspare_enqueue_customizer_assets()
268 {
269 global $pagenow;
270 if (!in_array($pagenow, ['customize.php'])) {
271 return;
272 }
273 // Register globals as a standalone no-file script
274 wp_register_script(
275 'blockspare-globals',
276 false, // ← no actual JS file
277 [],
278 null,
279 false
280 );
281
282 wp_add_inline_script(
283 'blockspare-globals',
284 'var blockspare_globals = ' . wp_json_encode($this->blockspare_get_globals()) . ';'
285 );
286
287 wp_enqueue_script('blockspare-globals');
288
289 // Also enqueue your category script for the widgets customizer
290 wp_enqueue_script(
291 'blockspare-category',
292 BLOCKSPARE_PLUGIN_URL . 'dist/block_category.js',
293 [
294 'blockspare-globals', // ← depends on globals, so globals loads first
295 'wp-blocks',
296 'wp-i18n',
297 'wp-element',
298 'wp-components',
299 'wp-block-editor',
300 'wp-api-fetch',
301 'wp-plugins',
302 'wp-edit-post',
303 ],
304 BLOCKSPARE_VERSION,
305 true
306 );
307
308 wp_enqueue_style(
309 'blockspare-panel',
310 BLOCKSPARE_PLUGIN_URL . 'dist/panel.css',
311 ['wp-edit-blocks'],
312 BLOCKSPARE_VERSION
313 );
314 }
315
316 private function blockspare_get_globals(): array
317 {
318 $blockspare_priview_img_url = BLOCKSPARE_PLUGIN_URL . 'assets/blockspare-placeholder-img.jpg';
319 $blockspare_food_img_url = BLOCKSPARE_PLUGIN_URL . 'assets/blockspare-placeholder-img-square.jpg';
320 $blockspare_promo_img = BLOCKSPARE_PLUGIN_URL . 'assets/banner-promo-1120-wu-1.png';
321 $date = wp_kses_post(date_i18n(get_option('date_format')));
322 $date_format = date_i18n(get_option('date_format'), current_time('timestamp'));
323 $open_design = isset($_GET['blockspare_show_intro']) && $_GET['blockspare_show_intro'] === 'true';
324 $blockData = '';
325
326 if (isset($_GET['blockspare_create_block'])) {
327
328 $blockName = sanitize_text_field($_GET['blockspare_create_block']);
329 $blockTitle = sanitize_text_field($_GET['post_title']);
330
331 // Get from template library filter
332 $filterdata = apply_filters('blockspare_template_library', []);
333
334 foreach ($filterdata as $block) {
335 if (isset($block['key']) && $block['key'] === $blockName) {
336 $blockData = $block['content'];
337 break;
338 }
339 }
340
341 // Get from bs_templates post type if not found
342 if (empty($blockData) && ! empty($blockTitle)) {
343
344 $templates = get_posts([
345 'post_type' => 'bs_templates',
346 'post_status' => 'publish',
347 'title' => $blockTitle,
348 'posts_per_page' => 1,
349 ]);
350
351 if (! empty($templates)) {
352 $blockData = $templates[0]->post_content;
353 }
354 }
355 }
356
357 $blockspare_current_wp_version = get_bloginfo('version');
358 $loadscripton = (isset($GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'widgets.php') || is_customize_preview();
359
360 return [
361 'srcUrl' => untrailingslashit(plugins_url('/', BLOCKSPARE_BASE_DIR . '/build/')),
362 'rest_url' => esc_url(rest_url()),
363 'nonce' => wp_create_nonce('wp_rest'),
364 'img' => $blockspare_priview_img_url,
365 'menu_img_url' => $blockspare_food_img_url,
366 'block_key' => '',
367 'homeUrl' => home_url(),
368 'blogUrl' => get_site_url(),
369 'postTypes' => self::get_post_types(),
370 'taxonomies' => self::get_taxonomies(),
371 'postQueryEndpoint' => 'aft/v1/post-query',
372 'config' => '',
373 'configuration' => '',
374 'settings' => '',
375 'bs_date' => $date,
376 'promo_img' => $blockspare_promo_img,
377 'imagePath' => 'https:raw.githubusercontent.com/afthemes/blockspare-demo-data/master/blocks/new/',
378 'open_design' => $open_design,
379 'date_format' => $date_format,
380 'blockData' => $blockData,
381 'template_link' => admin_url('edit.php?post_type=bs_templates'),
382 'newTemlateUrl' => admin_url('post-new.php?post_type=bs_templates&blockspare_create_block&blockspare_show_intro=true'),
383 'loadscripton' => $loadscripton,
384 'wpversion' => (version_compare($blockspare_current_wp_version, '6.6') >= 0) ? 'true' : 'false',
385 'inspector_imgs' => BLOCKSPARE_PLUGIN_URL . 'assets/images',
386 'upgradeUrl' => 'https://afthemes.com/blockspare-pro/',
387 'isPro' => false,
388 'allowedFonts' => $this->blockspare_allow_fonts()
389 ];
390 }
391 public function blockspare_allow_fonts()
392 {
393 return ['ABeeZee', 'Abel', 'Lato', 'Oswald'];
394 }
395
396 public function blockspare_fb_enqueue_frontend_assets()
397 {
398 wp_enqueue_style(
399 'blockspare-fontawesome',
400 BLOCKSPARE_PLUGIN_URL . 'assets/fontawesome/css/all.css',
401 [],
402 BLOCKSPARE_VERSION
403 );
404 wp_enqueue_style(
405 'blockspare-common-latest-post',
406 BLOCKSPARE_PLUGIN_URL . 'dist/lates_post_css.css',
407 [],
408 BLOCKSPARE_VERSION
409 );
410
411 wp_enqueue_style(
412 'blockspare-common-all-blocks',
413 BLOCKSPARE_PLUGIN_URL . 'dist/common_all_blocks.css',
414 [],
415 BLOCKSPARE_VERSION
416 );
417 wp_enqueue_style(
418 'blockspare-animation-blocks',
419 BLOCKSPARE_PLUGIN_URL . 'dist/block_animation.css',
420 [],
421 BLOCKSPARE_VERSION
422 );
423 }
424
425 // ══════════════════════════════════════════════════════════════════════════
426 // 3. Custom block categories
427 // ══════════════════════════════════════════════════════════════════════════
428 public function blockspare_register_custom_categories($categories)
429 {
430 $custom = [
431 [
432 'slug' => 'blockspare-post',
433 'title' => __('Editorial & Content', 'blockspare'),
434
435 ],
436 [
437 'slug' => 'blockspare',
438 'title' => __('Business & Layout', 'blockspare'),
439
440 ],
441
442 ];
443
444 return array_merge($custom, $categories);
445 }
446
447 // ══════════════════════════════════════════════════════════════════════════
448 // 4. Load server-side render files
449 // ══════════════════════════════════════════════════════════════════════════
450 public function blockspare_load_dynamic_blocks()
451 {
452 $this->load_layout_dependencies();
453 $this->load_block_includes();
454 }
455
456 private function load_layout_dependencies()
457 {
458 if (PHP_VERSION_ID < 50600) {
459 return;
460 }
461
462 $layout_files = [
463 'inc/layout/class-image-importer.php',
464 'inc/layout/functions.php',
465 'inc/layout/registry.php',
466 'inc/layout/components.php',
467 'inc/layout/endpoints.php',
468 'inc/layout/save-templates.php',
469 ];
470
471 $this->include_files($layout_files);
472 }
473
474 private function load_block_includes()
475 {
476 $block_files = [
477 'inc/rest-api-function.php',
478 'inc/latest-post-block/posts-grid/index.php',
479 'inc/latest-post-block/posts-carousel-grid/index.php',
480 'inc/latest-post-block/posts-slider/index.php',
481 'inc/latest-post-block/posts-list/index.php',
482 'inc/latest-post-block/posts-flash/index.php',
483 'inc/latest-post-block/posts-express-grid/index.php',
484 'inc/other-block/date-time/index.php',
485 'inc/latest-post-block/posts-popular-tags/index.php',
486 'inc/blockspare-functions.php',
487 'inc/block-posts-config/class-post-rest-api.php',
488 'inc/block-posts-config/class-multiauthor-backend.php',
489 'inc/block-posts-config/class-multiauthor-frontend.php',
490 'inc/block-posts-config/tax-category.php',
491 'inc/block-posts-config/class-bs-post.php',
492 'inc/block-posts-config/latest-post-tile-common.php',
493 'inc/other-block/social-sharing/index.php',
494 'inc/other-block/search/index.php',
495 'inc/block-banner/frontend-render/post-category-style.php',
496 'inc/block-banner/frontend-render/post-banner-config.php',
497 'inc/block-banner/frontend-render/post-banner-style.php',
498 'inc/block-posts-config/ajax-pagination.php',
499
500 'inc/post-banner/banner-1/index.php',
501 'inc/post-banner/banner-2/index.php',
502 'inc/post-banner/banner-9/index.php',
503 ];
504 $this->include_files($block_files);
505 }
506
507 /**
508 * Reusable file loader
509 */
510 private function include_files($files = [])
511 {
512
513 foreach ($files as $file) {
514 $filepath = BLOCKSPARE_PLUGIN_DIR . $file;
515
516 if (file_exists($filepath)) {
517 include_once $filepath;
518 }
519 }
520 }
521
522 public function blockspare_blocks_register_api_endpoints()
523 {
524
525 $posts_controller = new Aft_Post_Rest_Controller();
526 $posts_controller->register_routes();
527 }
528
529
530
531 // ══════════════════════════════════════════════════════════════════════════
532 // 5. Load text domain
533 // ══════════════════════════════════════════════════════════════════════════
534 public function blockspare_load_load_text_domain()
535 {
536 load_plugin_textdomain(
537 'blockspare',
538 false,
539 dirname(plugin_basename(BLOCKSPARE_BASE_FILE)) . '/languages'
540 );
541 }
542 }
543
544 // Initialize the class
545 new Blockspare_Init();
546