PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.1
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 4.9.1, at includes/Editors/BlockEditor/Block.php

255 lines 6.6 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 use WPDeveloper\BetterDocs\Utils\Enqueue;
12
13 /**
14 * Description
15 *
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 public $is_pro = false;
25
26 /**
27 * Enqueue
28 * @var Enqueue
29 */
30 protected $assets_manager = null;
31
32 protected $dir = '';
33
34 protected $editor_scripts = [ 'betterdocs-blocks-editor' ];
35
36 protected $editor_styles = [ 'betterdocs-blocks-editor' ];
37
38 protected $frontend_styles = [];
39 protected $frontend_scripts = [];
40
41 /**
42 * unique name of block
43 * @return string
44 */
45 abstract public function get_name();
46
47 /**
48 * Block can be enabled or not.
49 *
50 * Override if needed.
51 *
52 * @return bool
53 */
54 public function can_enable() {
55 return true;
56 }
57
58 public function path( $name = '' ) {
59 if ( empty( $name ) ) {
60 $name = $this->get_name();
61 }
62
63 return BETTERDOCS_BLOCKS_DIRECTORY . $name;
64 }
65
66 public function register_block_type( $name, ...$args ) {
67 return register_block_type(
68 apply_filters_ref_array( 'betterdocs.blocks.path', [ $this->path( $name ), $name, &$this ] ),
69 ...$args
70 );
71 }
72
73 public function load_frontend_styles() {
74 if ( empty( $this->frontend_styles ) ) {
75 return;
76 }
77
78 foreach ( $this->frontend_styles as $handle ) {
79 wp_enqueue_style( $handle );
80 }
81 }
82
83 public function load_frontend_scripts() {
84 if ( empty( $this->frontend_scripts ) ) {
85 return;
86 }
87
88 foreach ( $this->frontend_scripts as $handle ) {
89 wp_enqueue_script( $handle );
90 }
91 }
92
93 public function load_scripts() {
94 $this->load_frontend_styles();
95 $this->load_frontend_scripts();
96 }
97
98 /**
99 * Flatten a decoded selection attribute into a plain list of values.
100 *
101 * Selection attributes are saved by the editor as `[{ value, label }]`
102 * objects, but a bare list of values — `[5, 7]` — is what REST clients and
103 * other programmatic writers naturally produce. Both shapes are accepted:
104 * an array element contributes its `$key`, a scalar element contributes
105 * itself, and elements that carry no value are dropped.
106 *
107 * @since 4.9.0
108 *
109 * @param mixed $decoded Decoded attribute value. Anything that is not an array yields an empty list.
110 * @param string $key Key holding the value on object elements. Default 'value'.
111 *
112 * @return array Re-indexed list of values.
113 */
114 public static function normalize_id_list( $decoded, $key = 'value' ) {
115 if ( ! is_array( $decoded ) ) {
116 return [];
117 }
118
119 $_values = array_map(
120 function ( $item ) use ( $key ) {
121 return is_array( $item ) ? ( isset( $item[ $key ] ) ? $item[ $key ] : null ) : $item;
122 },
123 $decoded
124 );
125
126 return array_values(
127 array_filter(
128 $_values,
129 function ( $value ) {
130 return null !== $value;
131 }
132 )
133 );
134 }
135
136 /**
137 * Decode a JSON attribute string into a list of values.
138 *
139 * @since 4.9.0 Bare-value lists (`"[5]"`) are accepted alongside the
140 * editor's `[{ value, label }]` form, and no longer warn.
141 *
142 * @param mixed $data JSON encoded attribute value; non-strings yield an empty list.
143 * @param string $key Key holding the value on object elements. Default 'value'.
144 *
145 * @return array
146 */
147 public function string_to_array( $data = [], $key = 'value' ) {
148 $_return_val = [];
149
150 if ( is_string( $data ) && ! empty( $data ) ) {
151 $_return_val = self::normalize_id_list( json_decode( $data, true ), $key );
152 }
153
154 return $_return_val;
155 }
156
157 public function register( $assets_manager ) {
158 $this->assets_manager = $assets_manager;
159
160 $_args = [];
161
162 if ( method_exists( $this, 'register_scripts' ) ) {
163 $this->register_scripts();
164 }
165
166 if ( method_exists( $this, 'render_callback' ) ) {
167 $_args['render_callback'] = function ( $attributes, $content ) {
168 if ( ! is_admin() ) {
169 $this->load_scripts();
170 }
171 return $this->render_callback( $attributes, $content );
172 };
173 }
174
175 if ( ( ! empty( $this->frontend_scripts ) || ! empty( $this->frontend_styles ) ) && ! method_exists( $this, 'render_callback' ) ) {
176 $_args['render_callback'] = function ( $attributes, $content ) {
177 if ( ! is_admin() ) {
178 $this->load_scripts();
179 }
180 return $content;
181 };
182 }
183
184 $_args['editor_script'] = $this->editor_scripts;
185 $_args['editor_script_handles'] = $this->editor_scripts;
186
187 $_args['editor_style'] = $this->editor_styles;
188 $_args['editor_style_handles'] = $this->editor_styles;
189
190 if ( property_exists( $this, 'attributes' ) ) {
191 $_args['attributes'] = $this->attributes;
192 }
193
194 return $this->register_block_type( $this->get_name(), $_args );
195 }
196
197 public function get_default_attributes() {
198 return [];
199 }
200
201 abstract public function render( $attributes, $content );
202
203 public function render_callback( $attributes, $content ) {
204 $this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() );
205 $this->attributes = $this->remove_deprecated_attributes( $this->attributes, false, false );
206
207 do_action_ref_array( 'betterdocs_before_render', [ & $this, 'blocks' ] );
208
209 ob_start();
210 $this->render( $this->attributes, $content );
211 $content = ob_get_clean();
212
213 do_action_ref_array( 'betterdocs_after_render', [ & $this, 'blocks' ] );
214
215 return $content;
216 }
217
218 public function normalize_attributes( $attributes, $mixed_settings = [] ) {
219 $mixed_settings = wp_parse_args(
220 [
221 'prefix' => 'count_prefix',
222 'suffix' => 'count_suffix',
223 'suffixSingular' => 'count_suffix_singular',
224 'showIcon' => 'show_icon',
225 'showTitle' => 'show_title',
226 'titleTag' => 'title_tag',
227 'showCount' => 'show_count',
228 'showButton' => 'show_button',
229 'showButtonIcon' => 'show_button_icon',
230 'buttonIconPosition' => 'button_icon_position',
231 'buttonText' => 'button_text',
232 'buttonIcon' => 'button_icon',
233 'showList' => 'show_list',
234 'showHeader' => 'show_header',
235 'postsPerPage' => 'post_per_page',
236 'postsOrderBy' => 'post_orderby',
237 'postsOrder' => 'post_order',
238 'enableNestedSubcategory' => 'nested_subcategory'
239 ],
240 $mixed_settings
241 );
242
243 $_return_value = [];
244
245 foreach ( $mixed_settings as $key => $old_key ) {
246 if ( isset( $attributes[ $key ] ) ) {
247 $_return_value[ $old_key ] = $attributes[ $key ];
248 unset( $attributes[ $key ] );
249 }
250 }
251
252 return array_merge( $attributes, $_return_value );
253 }
254 }
255