| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block Registration and Orchestration. |
| 4 |
* |
| 5 |
* This class handles Gutenberg block registration, editor assets, |
| 6 |
* and block category setup. Individual block render callbacks are |
| 7 |
* delegated to dedicated classes inside the Render/ directory. |
| 8 |
* |
| 9 |
* @package BBP_Core |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace admin\Blocks; |
| 13 |
|
| 14 |
// Load render classes. |
| 15 |
require_once __DIR__ . '/Render/forum-posts.php'; |
| 16 |
require_once __DIR__ . '/Render/forum-tab.php'; |
| 17 |
require_once __DIR__ . '/Render/forums.php'; |
| 18 |
require_once __DIR__ . '/Render/search.php'; |
| 19 |
require_once __DIR__ . '/Render/single-forum.php'; |
| 20 |
require_once __DIR__ . '/Render/forum-ajax.php'; |
| 21 |
require_once __DIR__ . '/Render/forum-overview.php'; |
| 22 |
|
| 23 |
|
| 24 |
use admin\Blocks\Render\Forum_Posts; |
| 25 |
use admin\Blocks\Render\Forum_Tab; |
| 26 |
use admin\Blocks\Render\Forums; |
| 27 |
use admin\Blocks\Render\Search; |
| 28 |
use admin\Blocks\Render\Single_Forum; |
| 29 |
use admin\Blocks\Render\Forum_Ajax; |
| 30 |
use admin\Blocks\Render\Forum_Overview; |
| 31 |
|
| 32 |
|
| 33 |
class Blocks_Register { |
| 34 |
|
| 35 |
/** |
| 36 |
* Render class instances. |
| 37 |
* |
| 38 |
* @var object[] |
| 39 |
*/ |
| 40 |
private $renderers = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor — hooks into WordPress. |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
add_action( 'init', array( $this, 'blocks_init' ) ); |
| 47 |
add_filter( 'block_categories_all', array( $this, 'register_block_category' ), 10, 2 ); |
| 48 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); |
| 49 |
|
| 50 |
// Instantiate render classes. |
| 51 |
$this->renderers['forum_posts'] = new Forum_Posts(); |
| 52 |
$this->renderers['forum_tab'] = new Forum_Tab(); |
| 53 |
$this->renderers['forums'] = new Forums(); |
| 54 |
$this->renderers['search'] = new Search(); |
| 55 |
$this->renderers['single_forum'] = new Single_Forum(); |
| 56 |
$this->renderers['forum_ajax'] = new Forum_Ajax(); |
| 57 |
$this->renderers['forum_overview'] = new Forum_Overview(); |
| 58 |
|
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Register the Forumax block category. |
| 64 |
* |
| 65 |
* @param array $categories Existing categories. |
| 66 |
* @param WP_Post $post Current post. |
| 67 |
* @return array Modified categories. |
| 68 |
*/ |
| 69 |
public function register_block_category( $categories, $post ) { |
| 70 |
return array_merge( |
| 71 |
$categories, |
| 72 |
array( |
| 73 |
array( |
| 74 |
'slug' => 'forumax', |
| 75 |
'title' => __( 'Forumax', 'forumax' ), |
| 76 |
), |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Enqueue assets for the Gutenberg block editor. |
| 83 |
* |
| 84 |
* This ensures the AJAX URL and upsell configuration |
| 85 |
* are available for block scripts. |
| 86 |
*/ |
| 87 |
public function enqueue_editor_assets() { |
| 88 |
$is_pro_active = class_exists( 'Forumax_Pro' ) && function_exists( 'bc_fs' ) && bc_fs()->can_use_premium_code(); |
| 89 |
|
| 90 |
wp_localize_script( 'wp-edit-post', 'bbpc_editor_config', array( |
| 91 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 92 |
'nonce' => wp_create_nonce( 'forumax-nonce' ), |
| 93 |
) ); |
| 94 |
|
| 95 |
wp_localize_script( 'wp-edit-post', 'bbpc_upsell_config', array( |
| 96 |
'is_pro_active' => $is_pro_active, |
| 97 |
'upsell_image_url' => defined( 'FORUMAX_IMG' ) ? FORUMAX_IMG . 'upsell-forum-ajax.png' : '', |
| 98 |
'upgrade_url' => admin_url( 'admin.php?page=forumax-pricing' ), |
| 99 |
) ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Register all Gutenberg blocks. |
| 104 |
* |
| 105 |
* Each block points to its compiled build directory and |
| 106 |
* delegates rendering to the appropriate Render class. |
| 107 |
*/ |
| 108 |
public function blocks_init() { |
| 109 |
|
| 110 |
$blocks = array( |
| 111 |
'forum-posts' => 'forum_posts', |
| 112 |
'forum-tab' => 'forum_tab', |
| 113 |
'forums' => 'forums', |
| 114 |
'search' => 'search', |
| 115 |
'single-forum' => 'single_forum', |
| 116 |
'forum-ajax' => 'forum_ajax', |
| 117 |
'forum-overview' => 'forum_overview', |
| 118 |
); |
| 119 |
|
| 120 |
foreach ( $blocks as $block_dir => $renderer_key ) { |
| 121 |
if ( 'forum-ajax' === $block_dir && \WP_Block_Type_Registry::get_instance()->is_registered( 'bbp-core/forum-ajax' ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
$block_path = __DIR__ . '/../../build/' . $block_dir; |
| 126 |
if ( file_exists( $block_path . '/block.json' ) ) { |
| 127 |
register_block_type( $block_path, array( |
| 128 |
'render_callback' => array( $this->renderers[ $renderer_key ], 'render_' . $renderer_key ), |
| 129 |
) ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
} |
| 135 |
} |
| 136 |
|