PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.5.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.5.2
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 / Traits / EditorHelper.php

EditorHelper.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.5.2, at includes/Traits/EditorHelper.php

294 lines 8.4 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\Traits;
4
5 use WPDeveloper\BetterDocs\Core\Query;
6 use WPDeveloper\BetterDocs\Utils\Helper;
7 use WPDeveloper\BetterDocs\Core\Settings;
8 use WPDeveloper\BetterDocs\Core\Shortcode;
9 use WPDeveloper\BetterDocs\Admin\Customizer\Defaults;
10 use WPDeveloper\BetterDocs\Editors\BlockEditor\Block;
11 use WPDeveloper\BetterDocs\Editors\Elementor\BaseWidget;
12
13 /**
14 * Description
15 *
16 * @since 1.0.0
17 * @package PackageName
18 *
19 * @method mixed betterdocs( $key = '' )
20 */
21 trait EditorHelper {
22
23 /**
24 * A list of deprecated attributes.
25 * <old attributes as key, new attributes as value>
26 * @var array<string, string>
27 */
28 protected $deprecated_attributes = [];
29
30 /**
31 * A list of paired attributes.
32 * @var array
33 */
34 public $attributes = [];
35
36 /**
37 * A list of attributes which is passed when a shortcode is used.
38 * @var array
39 */
40 protected $client_attributes = [];
41
42 /**
43 * A list of attributes which can be mapped as variables to the view.
44 * key as old variable, value to get as variable in views.
45 *
46 * @var array<string, string> An associative array containing string keys and string values.
47 */
48 protected $map_view_vars = [];
49
50 /**
51 * A list of attributes for markup.
52 * Which will converted to html attributes like string for use in html markup.
53 *
54 * @var array<string, mixed>
55 */
56 protected $html_attributes = ['wrapper_attr', 'inner_wrapper_attr'];
57
58 /**
59 * Summary of view_params
60 * @return array
61 */
62 protected function view_params() {
63 return [];
64 }
65
66 public function reset_attributes() {
67 }
68
69 public function betterdocs( $key = 'settings' ) {
70 switch ( $key ) {
71 case 'settings':
72 return betterdocs()->container->get( Settings::class );
73 case 'query':
74 return betterdocs()->container->get( Query::class );
75 case 'helper':
76 return betterdocs()->container->get( Helper::class );
77 case 'customizer':
78 return betterdocs()->container->get( Defaults::class );
79 }
80 }
81
82 public function merge( $a, $b ) {
83 return Helper::merge( $a, $b );
84 }
85
86 /**
87 * Define views for shortcode|elementor|blocks
88 * @param string $name
89 * @return void
90 */
91 final protected function views( $name ) {
92 /**
93 * Set widget_type property
94 */
95 $this->get_widget_type();
96
97 betterdocs()->views->get( $name, $this->get_view_params() );
98 }
99
100 /**
101 * If a shortcode has any deprecated attributes, we need to remap with new attributes
102 *
103 * @param mixed $atts
104 * @return mixed
105 */
106 private function remove_deprecated_attributes( &$atts, $trigger_error = true, $not_remove_old = true ) {
107 if ( empty( $this->deprecated_attributes ) || empty( $atts ) ) {
108 return $atts;
109 }
110
111 foreach ( $this->deprecated_attributes as $oldAttr => $newAttr ) {
112 if ( array_key_exists( $oldAttr, $atts ) ) {
113 if( $trigger_error ) {
114 $this->error( $oldAttr, $newAttr );
115 }
116
117 $atts[$newAttr] = $atts[$oldAttr];
118 if( $not_remove_old ) {
119 unset( $atts[$oldAttr] );
120 }
121 }
122 }
123
124 return $atts;
125 }
126
127 private function get_widget_type() {
128 $_is_shortcode = ( $this instanceof Shortcode );
129 $_is_elementor = ( $this instanceof BaseWidget );
130 $_is_block = ( $this instanceof Block );
131
132 if ( $_is_shortcode ) {
133 $this->widget_type = 'shortcode';
134 } elseif ( $_is_elementor ) {
135 $this->widget_type = 'elementor';
136 } elseif ( $_is_block ) {
137 $this->widget_type = 'blocks';
138 }
139
140 return $this->widget_type;
141 }
142
143 public function default_classnames() {
144 switch ( $this->get_widget_type() ) {
145 case 'blocks':
146 return 'betterdocs-blocks ' . ( isset( $this->attributes['blockId'] ) ? $this->attributes['blockId'] : '' );
147 case 'elementor':
148 return 'betterdocs-elementor';
149 case 'shortcode':
150 return 'betterdocs-shortcode';
151 }
152
153 return '';
154 }
155
156 private function get_view_params() {
157 $_origin_params = $this->view_params();
158
159 if ( isset( $_origin_params['wrapper_attr'] ) ) {
160 $_origin_params['wrapper_attr']['class'][] = $this->default_classnames();
161 } else {
162 $_origin_params['wrapper_attr'] = [
163 'class' => [$this->default_classnames()]
164 ];
165 }
166
167 if ( isset( $this->is_pro ) && $this->is_pro ) {
168 $_origin_params['wrapper_attr']['class'][] = 'betterdocs-pro';
169 }
170
171 if ( property_exists( $this, 'view_wrapper' ) ) {
172 $_origin_params['wrapper_attr']['class'][] = $this->view_wrapper;
173 }
174
175 if ( ! empty( $this->html_attributes ) ) {
176 foreach ( $this->html_attributes as $attr ) {
177 if ( array_key_exists( $attr, $_origin_params ) ) {
178 $_origin_params["{$attr}_array"] = $_origin_params[$attr];
179 $_origin_params[$attr] = betterdocs()->template_helper->get_html_attributes( $_origin_params[$attr] );
180 }
181 }
182 }
183
184 $_view_params = wp_parse_args( [
185 'widget' => &$this,
186 'widget_id' => $this->get_name()
187 ], $_origin_params );
188
189 return wp_parse_args( $_view_params, $this->normalize_attributes( $this->attributes, $this->map_view_vars ) );
190 }
191
192 /**
193 * This method is a re-mapper or prettier for attributes variables.
194 *
195 * @param mixed $attributes
196 * @param mixed $mixed_settings
197 *
198 * @return mixed
199 */
200 final public function normalize_attributes( $attributes, $mixed_settings = [] ) {
201 $mixed_settings = array_merge( [
202 'icon' => 'show_icon',
203 'nested_subcategory' => 'nested_subcategory',
204 'posts_per_grid' => 'posts_per_page',
205 'orderby' => 'post_orderby',
206 'order' => 'post_order',
207 'list_icon' => ''
208 ], $mixed_settings );
209
210 $_return_value = [];
211
212 foreach ( $mixed_settings as $old_key => $new_key ) {
213 if ( isset( $attributes[$old_key] ) && ! empty( $new_key ) ) {
214 $_return_value[$new_key] = $attributes[$old_key];
215 unset( $attributes[$old_key] );
216 }
217 }
218
219 return array_merge( $attributes, $_return_value );
220 }
221
222 /**
223 * Log error in debug.log if its enabled.
224 *
225 * @param string $key
226 * @param string $new_key
227 * @return void
228 */
229 protected function error( $key, $new_key ) {
230 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
231 trigger_error(
232 sprintf(
233 '"<strong>%1$s</strong>" attribute used in a %4$s called "%2$s" is no longer supported and will be removed in a future version. Instead, you should use the attribute <strong>%3$s</strong>.',
234 $key,
235 $this->get_name(),
236 $new_key,
237 $this->get_widget_type()
238 ),
239 E_USER_NOTICE
240 );
241 }
242 }
243
244 /**
245 * Check if any attributes has passed.
246 *
247 * example: [shortcode_name attr1="value1"]
248 * this function will return true for $key attr1.
249 * before default value sets in attributes we had to check if user pass anything as attributes.
250 *
251 * @param mixed $key
252 * @return bool
253 */
254 public function has( $key ) {
255 return isset( $this->client_attributes[$key] );
256 }
257
258 /**
259 * Get attribute as property.
260 *
261 * @param string $key
262 * @return mixed
263 */
264 public function __get( $key ) {
265 if ( isset( $this->attributes[$key] ) ) {
266 return $this->attributes[$key];
267 }
268
269 if ( property_exists( $this, $key ) ) {
270 return $this->{$key};
271 }
272
273 return null;
274 }
275
276 /**
277 * Set property as attributes
278 * @param string $key
279 * @param mixed $value
280 */
281 public function __set( $key, $value ) {
282 $this->attributes[$key] = $value;
283 }
284
285 /**
286 * Check if a property is set in attributes list.
287 * @param mixed $key
288 * @return bool
289 */
290 public function __isset( $key ) {
291 return isset( $this->attributes[$key] );
292 }
293 }
294