PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 4.1.0
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v4.1.0
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 2.6.1 All 38 releases
blockspare / inc / init.php

init.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 4.1.0, at inc/init.php

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