| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Editors; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; // Exit if accessed directly |
| 7 |
} |
| 8 |
|
| 9 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\Patterns\BasePattern; |
| 10 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\StyleHandler; |
| 11 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\FontLoader; |
| 12 |
use WPDeveloper\BetterDocs\Editors\BlockEditor\TemplatesController; |
| 13 |
|
| 14 |
class BlockEditor extends BaseEditor { |
| 15 |
|
| 16 |
public function init() { |
| 17 |
betterdocs()->container->get( TemplatesController::class ); |
| 18 |
|
| 19 |
add_action( 'admin_init', [$this, 'enqueue'] ); |
| 20 |
add_action( 'admin_enqueue_scripts', [$this, 'enqueue'] ); |
| 21 |
|
| 22 |
$_blocks_category_hook = version_compare( get_bloginfo( 'version' ), '5.8', '>=' ) ? 'block_categories_all' : 'block_categories'; |
| 23 |
add_filter( $_blocks_category_hook, [$this, 'register_category'], 9, 1 ); |
| 24 |
|
| 25 |
StyleHandler::get_instance(); |
| 26 |
FontLoader::get_instance(); |
| 27 |
|
| 28 |
$this->register_blocks(); |
| 29 |
|
| 30 |
add_action( 'init', [$this, 'pattern_category'] ); |
| 31 |
|
| 32 |
$this->pattern_initialization(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Pattren category registration. |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public function pattern_category() { |
| 40 |
register_block_pattern_category( |
| 41 |
'batterdocs', |
| 42 |
array( 'label' => __( 'BetterDocs', 'betterdocs' ) ) |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Get all the Pattrens initialized. |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function pattern_initialization() { |
| 51 |
$_api_classes = scandir( __DIR__ . DIRECTORY_SEPARATOR . 'BlockEditor' . DIRECTORY_SEPARATOR . 'Patterns' ); |
| 52 |
|
| 53 |
if ( ! empty( $_api_classes ) && is_array( $_api_classes ) ) { |
| 54 |
foreach ( $_api_classes as $class ) { |
| 55 |
if ( $class == '.' || $class == '..' || $class == 'BasePattern.php' || strpos( $class, '.' ) === 0) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
$classname = basename( $class, '.php' ); |
| 60 |
$classname = '\\' . __NAMESPACE__ . "\\BlockEditor\\Patterns\\$classname"; |
| 61 |
$patterns_class = betterdocs()->container->get( $classname ); |
| 62 |
|
| 63 |
if ( $patterns_class instanceof BasePattern ) { |
| 64 |
$patterns_class->pattern_category(); |
| 65 |
$patterns_class->register(); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public function admin_init() { |
| 72 |
$blocks = $this->get_blocks(); |
| 73 |
|
| 74 |
if ( empty( $blocks ) || ! is_array( $blocks ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
foreach ( $blocks as $block_name => $block ) { |
| 79 |
if ( isset( $block['object'] ) ) { |
| 80 |
$block_object = betterdocs()->container->get( $block['object'] ); |
| 81 |
|
| 82 |
if ( ! $block_object->can_enable() ) { |
| 83 |
continue; |
| 84 |
} |
| 85 |
|
| 86 |
$block_object->register_scripts(); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Only for Admin Add/Edit Pages |
| 93 |
*/ |
| 94 |
public function enqueue( $hook ) { |
| 95 |
$editor = 'core/edit-post'; |
| 96 |
if ( $hook == 'site-editor.php' || ( $hook == 'themes.php' && isset( $_GET['page'] ) && $_GET['page'] == 'gutenberg-edit-site' ) ) { |
| 97 |
$editor = 'core/edit-site'; |
| 98 |
} |
| 99 |
|
| 100 |
$this->assets->register( 'betterdocs-blocks-editor-controls', 'blocks/controls.css' ); |
| 101 |
$this->assets->register( 'betterdocs-blocks-editor', 'blocks/style-editor.css', ['betterdocs-blocks-editor-controls'] ); |
| 102 |
$this->assets->register( 'betterdocs-blocks-editor', 'blocks/editor.js', ['betterdocs-blocks-style-handler'] ); |
| 103 |
$this->assets->enqueue( 'betterdocs-blocks-actions', 'blocks/actions.js', [] ); |
| 104 |
$this->assets->localize( 'betterdocs-blocks-editor', 'betterDocsBlocksHelper', [ |
| 105 |
'is_pro_active' => betterdocs()->is_pro_active(), |
| 106 |
'resturl' => get_rest_url(), |
| 107 |
'editorType' => $editor |
| 108 |
] ); |
| 109 |
|
| 110 |
if ( $hook == 'post-new.php' || $hook == 'post.php' || $hook == 'site-editor.php' ) { |
| 111 |
$this->assets->enqueue( 'fontpicker-default-theme', 'vendor/css/fonticonpicker.base-theme.react.css' ); |
| 112 |
$this->assets->enqueue( 'fontpicker-material-theme', 'vendor/css/fonticonpicker.material-theme.react.css' ); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Add a block category |
| 118 |
* |
| 119 |
* @param $block_categories array |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
public function register_category( array $block_categories ): array { |
| 124 |
$categories_slugs = wp_list_pluck( $block_categories, 'slug' ); |
| 125 |
|
| 126 |
return in_array( 'betterdocs', $categories_slugs, true ) ? $block_categories : array_merge( [[ |
| 127 |
'slug' => 'betterdocs', |
| 128 |
'title' => __( 'Betterdocs', 'betterdocs' ) |
| 129 |
]], $block_categories ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Get Blocks |
| 134 |
* |
| 135 |
* @since 2.5.0 |
| 136 |
* @return array<array> |
| 137 |
*/ |
| 138 |
public function get_blocks() { |
| 139 |
$config_array = require_once BETTERDOCS_ABSPATH . 'includes/blocks.php'; |
| 140 |
return apply_filters( 'betterdocs_blocks_config', $config_array ); |
| 141 |
} |
| 142 |
|
| 143 |
public function register_blocks( $enqueue = false ) { |
| 144 |
$blocks = $this->get_blocks(); |
| 145 |
|
| 146 |
if ( empty( $blocks ) || ! is_array( $blocks ) ) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
foreach ( $blocks as $block_name => $block ) { |
| 151 |
if ( isset( $block['object'] ) ) { |
| 152 |
$block_object = betterdocs()->container->get( $block['object'] ); |
| 153 |
|
| 154 |
if ( ! $block_object->can_enable() ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
if ( method_exists( $block_object, 'load_dependencies' ) ) { |
| 159 |
$block_object->load_dependencies(); |
| 160 |
} |
| 161 |
|
| 162 |
if ( $enqueue && method_exists( $block_object, 'enqueue' ) ) { |
| 163 |
$block_object->enqueue( $this->assets ); |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
if ( method_exists( $block_object, 'inner_blocks' ) ) { |
| 168 |
$_inner_blocks = $block_object->inner_blocks(); |
| 169 |
foreach ( $_inner_blocks as $block_name => $block ) { |
| 170 |
$block->register( $this->assets ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
$block_object->register( $this->assets ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|