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

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

300 lines 7.3 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 [
186 'widget' => &$this,
187 'widget_id' => $this->get_name()
188 ],
189 $_origin_params
190 );
191
192 return wp_parse_args( $_view_params, $this->normalize_attributes( $this->attributes, $this->map_view_vars ) );
193 }
194
195 /**
196 * This method is a re-mapper or prettier for attributes variables.
197 *
198 * @param mixed $attributes
199 * @param mixed $mixed_settings
200 *
201 * @return mixed
202 */
203 final public function normalize_attributes( $attributes, $mixed_settings = [] ) {
204 $mixed_settings = array_merge(
205 [
206 'icon' => 'show_icon',
207 'nested_subcategory' => 'nested_subcategory',
208 'posts_per_grid' => 'posts_per_page',
209 'orderby' => 'post_orderby',
210 'order' => 'post_order',
211 'list_icon' => ''
212 ],
213 $mixed_settings
214 );
215
216 $_return_value = [];
217
218 foreach ( $mixed_settings as $old_key => $new_key ) {
219 if ( isset( $attributes[ $old_key ] ) && ! empty( $new_key ) ) {
220 $_return_value[ $new_key ] = $attributes[ $old_key ];
221 unset( $attributes[ $old_key ] );
222 }
223 }
224
225 return array_merge( $attributes, $_return_value );
226 }
227
228 /**
229 * Log error in debug.log if its enabled.
230 *
231 * @param string $key
232 * @param string $new_key
233 * @return void
234 */
235 protected function error( $key, $new_key ) {
236 if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
237 trigger_error(
238 sprintf(
239 '"<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>.',
240 esc_textarea( $key ), //to escape anything into strings
241 esc_html( $this->get_name() ),
242 esc_textarea( $new_key ), //to escape anything into strings
243 esc_html( $this->get_widget_type() )
244 ),
245 E_USER_NOTICE
246 );
247 }
248 }
249
250 /**
251 * Check if any attributes has passed.
252 *
253 * example: [shortcode_name attr1="value1"]
254 * this function will return true for $key attr1.
255 * before default value sets in attributes we had to check if user pass anything as attributes.
256 *
257 * @param mixed $key
258 * @return bool
259 */
260 public function has( $key ) {
261 return isset( $this->client_attributes[ $key ] );
262 }
263
264 /**
265 * Get attribute as property.
266 *
267 * @param string $key
268 * @return mixed
269 */
270 public function __get( $key ) {
271 if ( isset( $this->attributes[ $key ] ) ) {
272 return $this->attributes[ $key ];
273 }
274
275 if ( property_exists( $this, $key ) ) {
276 return $this->{$key};
277 }
278
279 return null;
280 }
281
282 /**
283 * Set property as attributes
284 * @param string $key
285 * @param mixed $value
286 */
287 public function __set( $key, $value ) {
288 $this->attributes[ $key ] = $value;
289 }
290
291 /**
292 * Check if a property is set in attributes list.
293 * @param mixed $key
294 * @return bool
295 */
296 public function __isset( $key ) {
297 return isset( $this->attributes[ $key ] );
298 }
299 }
300