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