PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.5.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.5.5
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Editors / BlockEditor / Block.php

Block.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.5.5, at includes/Editors/BlockEditor/Block.php

203 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Editors\BlockEditor;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 use WPDeveloper\BetterDocs\Utils\Base;
10 use WPDeveloper\BetterDocs\Traits\EditorHelper;
11
12 /**
13 * Description
14 *
15 * @method string render_callback($attributes, $content)
16 * @method void register_scripts()
17 * @property-read mixed $attributes
18 *
19 * @since 2.5.0
20 */
21 abstract class Block extends Base {
22 use EditorHelper;
23 /**
24 * Enqueue
25 * @var \WPDeveloper\BetterDocs\Utils\Enqueue
26 */
27 protected $assets_manager = null;
28
29 protected $dir = '';
30
31 protected $editor_scripts = ['betterdocs-blocks-editor'];
32
33 protected $editor_styles = ['betterdocs-blocks-editor'];
34
35 protected $frontend_styles = [];
36 protected $frontend_scripts = [];
37
38 /**
39 * unique name of block
40 * @return string
41 */
42 abstract public function get_name();
43
44 /**
45 * Block can be enabled or not.
46 *
47 * Override if needed.
48 *
49 * @return bool
50 */
51 public function can_enable() {
52 return true;
53 }
54
55 public function path( $name = '' ) {
56 if ( empty( $name ) ) {
57 $name = $this->get_name();
58 }
59
60 return BETTERDOCS_BLOCKS_DIRECTORY . $name;
61 }
62
63 public function register_block_type( $name, ...$args ) {
64 return register_block_type(
65 $this->path( $name ),
66 ...$args
67 );
68 }
69
70 public function load_frontend_styles() {
71 if ( empty( $this->frontend_styles ) ) {
72 return;
73 }
74
75 foreach ( $this->frontend_styles as $handle ) {
76 wp_enqueue_style( $handle );
77 }
78 }
79
80 public function load_frontend_scripts() {
81 if ( empty( $this->frontend_scripts ) ) {
82 return;
83 }
84
85 foreach ( $this->frontend_scripts as $handle ) {
86 wp_enqueue_script( $handle );
87 }
88 }
89
90 public function load_scripts() {
91 $this->load_frontend_styles();
92 $this->load_frontend_scripts();
93 }
94
95 public function string_to_array( $data = [], $key = 'value' ) {
96 $_return_val = [];
97
98 if ( is_string( $data ) && ! empty( $data ) ) {
99 $_return_val = json_decode( $data, true );
100 $_return_val = array_map( function ( $item ) use ( &$key ) {
101 return $item[$key];
102 }, $_return_val );
103 }
104
105 return $_return_val;
106 }
107
108 public function register( $assets_manager ) {
109 $this->assets_manager = $assets_manager;
110
111 $_args = [];
112
113 if ( method_exists( $this, 'register_scripts' ) ) {
114 $this->register_scripts();
115 }
116
117 if ( method_exists( $this, 'render_callback' ) ) {
118 $_args['render_callback'] = function ( $attributes, $content ) {
119 if ( ! is_admin() ) {
120 $this->load_scripts();
121 }
122 return $this->render_callback( $attributes, $content );
123 };
124 }
125
126 if ( ( ! empty( $this->frontend_scripts ) || ! empty( $this->frontend_styles ) ) && ! method_exists( $this, 'render_callback' ) ) {
127 $_args['render_callback'] = function ( $attributes, $content ) {
128 if ( ! is_admin() ) {
129 $this->load_scripts();
130 }
131 return $content;
132 };
133 }
134
135 $_args['editor_script'] = $this->editor_scripts;
136 $_args['editor_script_handles'] = $this->editor_scripts;
137
138 $_args['editor_style'] = $this->editor_styles;
139 $_args['editor_style_handles'] = $this->editor_styles;
140
141 if ( property_exists( $this, 'attributes' ) ) {
142 $_args['attributes'] = $this->attributes;
143 }
144
145 return $this->register_block_type( $this->get_name(), $_args );
146 }
147
148 public function get_default_attributes() {
149 return [];
150 }
151
152 abstract public function render( $attributes, $content );
153
154 public function render_callback( $attributes, $content ) {
155 $this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() );
156 $this->attributes = $this->remove_deprecated_attributes( $this->attributes, false, false );
157
158 do_action_ref_array( 'betterdocs_before_render', [ &$this, 'blocks' ] );
159
160 ob_start();
161 $this->render( $this->attributes, $content );
162 $content = ob_get_clean();
163
164 do_action_ref_array( 'betterdocs_after_render', [ &$this, 'blocks' ] );
165
166 return $content;
167 }
168
169 public function normalize_attributes( $attributes, $mixed_settings = [] ) {
170 $mixed_settings = wp_parse_args( [
171 'prefix' => 'count_prefix',
172 'suffix' => 'count_suffix',
173 'suffixSingular' => 'count_suffix_singular',
174 'showIcon' => 'show_icon',
175 'showTitle' => 'show_title',
176 'titleTag' => 'title_tag',
177 'showCount' => 'show_count',
178 'showButton' => 'show_button',
179 'showButtonIcon' => 'show_button_icon',
180 'buttonIconPosition' => 'button_icon_position',
181 'buttonText' => 'button_text',
182 'buttonIcon' => 'button_icon',
183 'showList' => 'show_list',
184 'showHeader' => 'show_header',
185 'postsPerPage' => 'post_per_page',
186 'postsOrderBy' => 'post_orderby',
187 'postsOrder' => 'post_order',
188 'enableNestedSubcategory' => 'nested_subcategory'
189 ], $mixed_settings );
190
191 $_return_value = [];
192
193 foreach ( $mixed_settings as $key => $old_key ) {
194 if ( isset( $attributes[$key] ) ) {
195 $_return_value[$old_key] = $attributes[$key];
196 unset( $attributes[$key] );
197 }
198 }
199
200 return array_merge( $attributes, $_return_value );
201 }
202 }
203