| 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 |
|
| 320 |
$blockName = sanitize_text_field($_GET['blockspare_create_block']); |
| 321 |
$blockTitle = sanitize_text_field($_GET['post_title']); |
| 322 |
|
| 323 |
// Get from template library filter |
| 324 |
$filterdata = apply_filters('blockspare_template_library', []); |
| 325 |
|
| 326 |
foreach ($filterdata as $block) { |
| 327 |
if (isset($block['key']) && $block['key'] === $blockName) { |
| 328 |
$blockData = $block['content']; |
| 329 |
break; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
// Get from bs_templates post type if not found |
| 334 |
if (empty($blockData) && ! empty($blockTitle)) { |
| 335 |
|
| 336 |
$templates = get_posts([ |
| 337 |
'post_type' => 'bs_templates', |
| 338 |
'post_status' => 'publish', |
| 339 |
'title' => $blockTitle, |
| 340 |
'posts_per_page' => 1, |
| 341 |
]); |
| 342 |
|
| 343 |
if (! empty($templates)) { |
| 344 |
$blockData = $templates[0]->post_content; |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
$blockspare_current_wp_version = get_bloginfo('version'); |
| 350 |
$loadscripton = (isset($GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'widgets.php') || is_customize_preview(); |
| 351 |
|
| 352 |
return [ |
| 353 |
'srcUrl' => untrailingslashit(plugins_url('/', BLOCKSPARE_BASE_DIR . '/build/')), |
| 354 |
'rest_url' => esc_url(rest_url()), |
| 355 |
'nonce' => wp_create_nonce('wp_rest'), |
| 356 |
'img' => $blockspare_priview_img_url, |
| 357 |
'menu_img_url' => $blockspare_food_img_url, |
| 358 |
'block_key' => '', |
| 359 |
'homeUrl' => home_url(), |
| 360 |
'blogUrl' => get_site_url(), |
| 361 |
'postTypes' => self::get_post_types(), |
| 362 |
'taxonomies' => self::get_taxonomies(), |
| 363 |
'postQueryEndpoint' => 'aft/v1/post-query', |
| 364 |
'config' => '', |
| 365 |
'configuration' => '', |
| 366 |
'settings' => '', |
| 367 |
'bs_date' => $date, |
| 368 |
'promo_img' => $blockspare_promo_img, |
| 369 |
'imagePath' => 'https:raw.githubusercontent.com/afthemes/blockspare-demo-data/master/blocks/new/', |
| 370 |
'open_design' => $open_design, |
| 371 |
'date_format' => $date_format, |
| 372 |
'blockData' => $blockData, |
| 373 |
'template_link' => admin_url('edit.php?post_type=bs_templates'), |
| 374 |
'newTemlateUrl' => admin_url('post-new.php?post_type=bs_templates&blockspare_create_block&blockspare_show_intro=true'), |
| 375 |
'loadscripton' => $loadscripton, |
| 376 |
'wpversion' => (version_compare($blockspare_current_wp_version, '6.6') >= 0) ? 'true' : 'false', |
| 377 |
'inspector_imgs' => BLOCKSPARE_PLUGIN_URL . 'assets/images', |
| 378 |
'upgradeUrl' => 'https://afthemes.com/blockspare-pro/', |
| 379 |
'isPro' => false, |
| 380 |
'allowedFonts' => $this->blockspare_allow_fonts() |
| 381 |
]; |
| 382 |
} |
| 383 |
public function blockspare_allow_fonts() |
| 384 |
{ |
| 385 |
return ['ABeeZee', 'Abel', 'Lato', 'Oswald']; |
| 386 |
} |
| 387 |
|
| 388 |
public function blockspare_fb_enqueue_frontend_assets() |
| 389 |
{ |
| 390 |
wp_enqueue_style( |
| 391 |
'blockspare-fontawesome', |
| 392 |
BLOCKSPARE_PLUGIN_URL . 'assets/fontawesome/css/all.css', |
| 393 |
[], |
| 394 |
BLOCKSPARE_VERSION |
| 395 |
); |
| 396 |
wp_enqueue_style( |
| 397 |
'blockspare-common-latest-post', |
| 398 |
BLOCKSPARE_PLUGIN_URL . 'dist/lates_post_css.css', |
| 399 |
[], |
| 400 |
BLOCKSPARE_VERSION |
| 401 |
); |
| 402 |
|
| 403 |
wp_enqueue_style( |
| 404 |
'blockspare-common-all-blocks', |
| 405 |
BLOCKSPARE_PLUGIN_URL . 'dist/common_all_blocks.css', |
| 406 |
[], |
| 407 |
BLOCKSPARE_VERSION |
| 408 |
); |
| 409 |
wp_enqueue_style( |
| 410 |
'blockspare-animation-blocks', |
| 411 |
BLOCKSPARE_PLUGIN_URL . 'dist/block_animation.css', |
| 412 |
[], |
| 413 |
BLOCKSPARE_VERSION |
| 414 |
); |
| 415 |
} |
| 416 |
|
| 417 |
// ══════════════════════════════════════════════════════════════════════════ |
| 418 |
// 3. Custom block categories |
| 419 |
// ══════════════════════════════════════════════════════════════════════════ |
| 420 |
public function blockspare_register_custom_categories($categories) |
| 421 |
{ |
| 422 |
$custom = [ |
| 423 |
[ |
| 424 |
'slug' => 'blockspare-post', |
| 425 |
'title' => __('Editorial & Content', 'blockspare'), |
| 426 |
|
| 427 |
], |
| 428 |
[ |
| 429 |
'slug' => 'blockspare', |
| 430 |
'title' => __('Business & Layout', 'blockspare'), |
| 431 |
|
| 432 |
], |
| 433 |
|
| 434 |
]; |
| 435 |
|
| 436 |
return array_merge($custom, $categories); |
| 437 |
} |
| 438 |
|
| 439 |
// ══════════════════════════════════════════════════════════════════════════ |
| 440 |
// 4. Load server-side render files |
| 441 |
// ══════════════════════════════════════════════════════════════════════════ |
| 442 |
public function blockspare_load_dynamic_blocks() |
| 443 |
{ |
| 444 |
$this->load_layout_dependencies(); |
| 445 |
$this->load_block_includes(); |
| 446 |
} |
| 447 |
|
| 448 |
private function load_layout_dependencies() |
| 449 |
{ |
| 450 |
if (PHP_VERSION_ID < 50600) { |
| 451 |
return; |
| 452 |
} |
| 453 |
|
| 454 |
$layout_files = [ |
| 455 |
'inc/layout/class-image-importer.php', |
| 456 |
'inc/layout/functions.php', |
| 457 |
'inc/layout/registry.php', |
| 458 |
'inc/layout/components.php', |
| 459 |
'inc/layout/endpoints.php', |
| 460 |
'inc/layout/save-templates.php', |
| 461 |
]; |
| 462 |
|
| 463 |
$this->include_files($layout_files); |
| 464 |
} |
| 465 |
|
| 466 |
private function load_block_includes() |
| 467 |
{ |
| 468 |
$block_files = [ |
| 469 |
'inc/rest-api-function.php', |
| 470 |
'inc/latest-post-block/posts-grid/index.php', |
| 471 |
'inc/latest-post-block/posts-carousel-grid/index.php', |
| 472 |
'inc/latest-post-block/posts-slider/index.php', |
| 473 |
'inc/latest-post-block/posts-list/index.php', |
| 474 |
'inc/latest-post-block/posts-flash/index.php', |
| 475 |
'inc/latest-post-block/posts-express-grid/index.php', |
| 476 |
'inc/other-block/date-time/index.php', |
| 477 |
'inc/latest-post-block/posts-popular-tags/index.php', |
| 478 |
'inc/blockspare-functions.php', |
| 479 |
'inc/block-posts-config/class-post-rest-api.php', |
| 480 |
'inc/block-posts-config/class-multiauthor-backend.php', |
| 481 |
'inc/block-posts-config/class-multiauthor-frontend.php', |
| 482 |
'inc/block-posts-config/tax-category.php', |
| 483 |
'inc/block-posts-config/class-bs-post.php', |
| 484 |
'inc/block-posts-config/latest-post-tile-common.php', |
| 485 |
'inc/other-block/social-sharing/index.php', |
| 486 |
'inc/other-block/search/index.php', |
| 487 |
'inc/block-banner/frontend-render/post-category-style.php', |
| 488 |
'inc/block-banner/frontend-render/post-banner-config.php', |
| 489 |
'inc/block-banner/frontend-render/post-banner-style.php', |
| 490 |
'inc/block-posts-config/ajax-pagination.php', |
| 491 |
|
| 492 |
'inc/post-banner/banner-1/index.php', |
| 493 |
'inc/post-banner/banner-2/index.php', |
| 494 |
'inc/post-banner/banner-9/index.php', |
| 495 |
]; |
| 496 |
$this->include_files($block_files); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Reusable file loader |
| 501 |
*/ |
| 502 |
private function include_files($files = []) |
| 503 |
{ |
| 504 |
|
| 505 |
foreach ($files as $file) { |
| 506 |
$filepath = BLOCKSPARE_PLUGIN_DIR . $file; |
| 507 |
|
| 508 |
if (file_exists($filepath)) { |
| 509 |
include_once $filepath; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
public function blockspare_blocks_register_api_endpoints() |
| 515 |
{ |
| 516 |
|
| 517 |
$posts_controller = new Aft_Post_Rest_Controller(); |
| 518 |
$posts_controller->register_routes(); |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
// ══════════════════════════════════════════════════════════════════════════ |
| 524 |
// 5. Load text domain |
| 525 |
// ══════════════════════════════════════════════════════════════════════════ |
| 526 |
public function blockspare_load_load_text_domain() |
| 527 |
{ |
| 528 |
load_plugin_textdomain( |
| 529 |
'blockspare', |
| 530 |
false, |
| 531 |
dirname(plugin_basename(BLOCKSPARE_BASE_FILE)) . '/languages' |
| 532 |
); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
// Initialize the class |
| 537 |
new Blockspare_Init(); |
| 538 |
|