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

212 lines 5.2 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 public function string_to_array( $data = [], $key = 'value' ) {
99 $_return_val = [];
100
101 if ( is_string( $data ) && ! empty( $data ) ) {
102 $_return_val = json_decode( $data, true );
103 $_return_val = array_map(
104 function ( $item ) use ( &$key ) {
105 return $item[ $key ];
106 },
107 $_return_val == null || empty( $_return_val ) ? [] : $_return_val
108 );
109 }
110
111 return $_return_val;
112 }
113
114 public function register( $assets_manager ) {
115 $this->assets_manager = $assets_manager;
116
117 $_args = [];
118
119 if ( method_exists( $this, 'register_scripts' ) ) {
120 $this->register_scripts();
121 }
122
123 if ( method_exists( $this, 'render_callback' ) ) {
124 $_args['render_callback'] = function ( $attributes, $content ) {
125 if ( ! is_admin() ) {
126 $this->load_scripts();
127 }
128 return $this->render_callback( $attributes, $content );
129 };
130 }
131
132 if ( ( ! empty( $this->frontend_scripts ) || ! empty( $this->frontend_styles ) ) && ! method_exists( $this, 'render_callback' ) ) {
133 $_args['render_callback'] = function ( $attributes, $content ) {
134 if ( ! is_admin() ) {
135 $this->load_scripts();
136 }
137 return $content;
138 };
139 }
140
141 $_args['editor_script'] = $this->editor_scripts;
142 $_args['editor_script_handles'] = $this->editor_scripts;
143
144 $_args['editor_style'] = $this->editor_styles;
145 $_args['editor_style_handles'] = $this->editor_styles;
146
147 if ( property_exists( $this, 'attributes' ) ) {
148 $_args['attributes'] = $this->attributes;
149 }
150
151 return $this->register_block_type( $this->get_name(), $_args );
152 }
153
154 public function get_default_attributes() {
155 return [];
156 }
157
158 abstract public function render( $attributes, $content );
159
160 public function render_callback( $attributes, $content ) {
161 $this->attributes = wp_parse_args( $attributes, $this->get_default_attributes() );
162 $this->attributes = $this->remove_deprecated_attributes( $this->attributes, false, false );
163
164 do_action_ref_array( 'betterdocs_before_render', [ & $this, 'blocks' ] );
165
166 ob_start();
167 $this->render( $this->attributes, $content );
168 $content = ob_get_clean();
169
170 do_action_ref_array( 'betterdocs_after_render', [ & $this, 'blocks' ] );
171
172 return $content;
173 }
174
175 public function normalize_attributes( $attributes, $mixed_settings = [] ) {
176 $mixed_settings = wp_parse_args(
177 [
178 'prefix' => 'count_prefix',
179 'suffix' => 'count_suffix',
180 'suffixSingular' => 'count_suffix_singular',
181 'showIcon' => 'show_icon',
182 'showTitle' => 'show_title',
183 'titleTag' => 'title_tag',
184 'showCount' => 'show_count',
185 'showButton' => 'show_button',
186 'showButtonIcon' => 'show_button_icon',
187 'buttonIconPosition' => 'button_icon_position',
188 'buttonText' => 'button_text',
189 'buttonIcon' => 'button_icon',
190 'showList' => 'show_list',
191 'showHeader' => 'show_header',
192 'postsPerPage' => 'post_per_page',
193 'postsOrderBy' => 'post_orderby',
194 'postsOrder' => 'post_order',
195 'enableNestedSubcategory' => 'nested_subcategory'
196 ],
197 $mixed_settings
198 );
199
200 $_return_value = [];
201
202 foreach ( $mixed_settings as $key => $old_key ) {
203 if ( isset( $attributes[ $key ] ) ) {
204 $_return_value[ $old_key ] = $attributes[ $key ];
205 unset( $attributes[ $key ] );
206 }
207 }
208
209 return array_merge( $attributes, $_return_value );
210 }
211 }
212