| @@ -19,283 +19,275 @@ | ||
| 19 | 19 | * @method mixed betterdocs( $key = '' ) |
| 20 | 20 | */ |
| 21 | 21 | trait EditorHelper { |
| 22 | 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 = []; | |
| 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 | 29 | |
| 30 | - /** | |
| 31 | - * A list of paired attributes. | |
| 32 | - * @var array | |
| 33 | - */ | |
| 34 | - public $attributes = []; | |
| 30 | + /** | |
| 31 | + * A list of paired attributes. | |
| 32 | + * @var array | |
| 33 | + */ | |
| 34 | + public $attributes = []; | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * A list of attributes which is passed when a shortcode is used. | |
| 38 | - * @var array | |
| 39 | - */ | |
| 40 | - protected $client_attributes = []; | |
| 36 | + /** | |
| 37 | + * A list of attributes which is passed when a shortcode is used. | |
| 38 | + * @var array | |
| 39 | + */ | |
| 40 | + protected $client_attributes = []; | |
| 41 | 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 = []; | |
| 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 | 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' ]; | |
| 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 | 57 | |
| 58 | - /** | |
| 59 | - * Summary of view_params | |
| 60 | - * @return array | |
| 61 | - */ | |
| 62 | - protected function view_params() { | |
| 63 | - return []; | |
| 64 | - } | |
| 58 | + /** | |
| 59 | + * Summary of view_params | |
| 60 | + * @return array | |
| 61 | + */ | |
| 62 | + protected function view_params() { | |
| 63 | + return []; | |
| 64 | + } | |
| 65 | 65 | |
| 66 | - public function reset_attributes() { | |
| 67 | - } | |
| 66 | +public function reset_attributes() { | |
| 67 | +} | |
| 68 | 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 | - } | |
| 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 | 81 | |
| 82 | - public function merge( $a, $b ) { | |
| 83 | - return Helper::merge( $a, $b ); | |
| 84 | - } | |
| 82 | + public function merge( $a, $b ) { | |
| 83 | + return Helper::merge( $a, $b ); | |
| 84 | + } | |
| 85 | 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(); | |
| 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 | 96 | |
| 97 | - betterdocs()->views->get( $name, $this->get_view_params() ); | |
| 98 | - } | |
| 97 | + betterdocs()->views->get( $name, $this->get_view_params() ); | |
| 98 | + } | |
| 99 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 116 | |
| 117 | - $atts[ $newAttr ] = $atts[ $oldAttr ]; | |
| 118 | - if ( $not_remove_old ) { | |
| 119 | - unset( $atts[ $oldAttr ] ); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - } | |
| 117 | + $atts[$newAttr] = $atts[$oldAttr]; | |
| 118 | + if( $not_remove_old ) { | |
| 119 | + unset( $atts[$oldAttr] ); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + } | |
| 123 | 123 | |
| 124 | - return $atts; | |
| 125 | - } | |
| 124 | + return $atts; | |
| 125 | + } | |
| 126 | 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 ); | |
| 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 | 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 | - } | |
| 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 | 139 | |
| 140 | - return $this->widget_type; | |
| 141 | - } | |
| 140 | + return $this->widget_type; | |
| 141 | + } | |
| 142 | 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 | - } | |
| 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 | 152 | |
| 153 | - return ''; | |
| 154 | - } | |
| 153 | + return ''; | |
| 154 | + } | |
| 155 | 155 | |
| 156 | - private function get_view_params() { | |
| 157 | - $_origin_params = $this->view_params(); | |
| 156 | + private function get_view_params() { | |
| 157 | + $_origin_params = $this->view_params(); | |
| 158 | 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 | - } | |
| 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 | 166 | |
| 167 | - if ( isset( $this->is_pro ) && $this->is_pro ) { | |
| 168 | - $_origin_params['wrapper_attr']['class'][] = 'betterdocs-pro'; | |
| 169 | - } | |
| 167 | + if ( isset( $this->is_pro ) && $this->is_pro ) { | |
| 168 | + $_origin_params['wrapper_attr']['class'][] = 'betterdocs-pro'; | |
| 169 | + } | |
| 170 | 170 | |
| 171 | - if ( property_exists( $this, 'view_wrapper' ) ) { | |
| 172 | - $_origin_params['wrapper_attr']['class'][] = $this->view_wrapper; | |
| 173 | - } | |
| 171 | + if ( property_exists( $this, 'view_wrapper' ) ) { | |
| 172 | + $_origin_params['wrapper_attr']['class'][] = $this->view_wrapper; | |
| 173 | + } | |
| 174 | 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 | - } | |
| 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 | 183 | |
| 184 | - $_view_params = wp_parse_args( | |
| 185 | - [ | |
| 186 | - 'widget' => &$this, | |
| 187 | - 'widget_id' => $this->get_name() | |
| 188 | - ], | |
| 189 | - $_origin_params | |
| 190 | - ); | |
| 184 | + $_view_params = wp_parse_args( [ | |
| 185 | + 'widget' => &$this, | |
| 186 | + 'widget_id' => $this->get_name() | |
| 187 | + ], $_origin_params ); | |
| 191 | 188 | |
| 192 | - return wp_parse_args( $_view_params, $this->normalize_attributes( $this->attributes, $this->map_view_vars ) ); | |
| 193 | - } | |
| 189 | + return wp_parse_args( $_view_params, $this->normalize_attributes( $this->attributes, $this->map_view_vars ) ); | |
| 190 | + } | |
| 194 | 191 | |
| 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 | - ); | |
| 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 ); | |
| 215 | 209 | |
| 216 | - $_return_value = []; | |
| 210 | + $_return_value = []; | |
| 217 | 211 | |
| 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 | - } | |
| 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 | + } | |
| 224 | 218 | |
| 225 | - return array_merge( $attributes, $_return_value ); | |
| 226 | - } | |
| 219 | + return array_merge( $attributes, $_return_value ); | |
| 220 | + } | |
| 227 | 221 | |
| 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 | - // Developer-only deprecation notice; gated by WP_DEBUG so production never reaches it. | |
| 238 | - // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error | |
| 239 | - trigger_error( | |
| 240 | - sprintf( | |
| 241 | - '"<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>.', | |
| 242 | - esc_textarea( $key ), //to escape anything into strings | |
| 243 | - esc_html( $this->get_name() ), | |
| 244 | - esc_textarea( $new_key ), //to escape anything into strings | |
| 245 | - esc_html( $this->get_widget_type() ) | |
| 246 | - ), | |
| 247 | - E_USER_NOTICE | |
| 248 | - ); | |
| 249 | - } | |
| 250 | - } | |
| 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 | + } | |
| 251 | 243 | |
| 252 | - /** | |
| 253 | - * Check if any attributes has passed. | |
| 254 | - * | |
| 255 | - * example: [shortcode_name attr1="value1"] | |
| 256 | - * this function will return true for $key attr1. | |
| 257 | - * before default value sets in attributes we had to check if user pass anything as attributes. | |
| 258 | - * | |
| 259 | - * @param mixed $key | |
| 260 | - * @return bool | |
| 261 | - */ | |
| 262 | - public function has( $key ) { | |
| 263 | - return isset( $this->client_attributes[ $key ] ); | |
| 264 | - } | |
| 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 | + } | |
| 265 | 257 | |
| 266 | - /** | |
| 267 | - * Get attribute as property. | |
| 268 | - * | |
| 269 | - * @param string $key | |
| 270 | - * @return mixed | |
| 271 | - */ | |
| 272 | - public function __get( $key ) { | |
| 273 | - if ( isset( $this->attributes[ $key ] ) ) { | |
| 274 | - return $this->attributes[ $key ]; | |
| 275 | - } | |
| 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 | + } | |
| 276 | 268 | |
| 277 | - if ( property_exists( $this, $key ) ) { | |
| 278 | - return $this->{$key}; | |
| 279 | - } | |
| 269 | + if ( property_exists( $this, $key ) ) { | |
| 270 | + return $this->{$key}; | |
| 271 | + } | |
| 280 | 272 | |
| 281 | - return null; | |
| 282 | - } | |
| 273 | + return null; | |
| 274 | + } | |
| 283 | 275 | |
| 284 | - /** | |
| 285 | - * Set property as attributes | |
| 286 | - * @param string $key | |
| 287 | - * @param mixed $value | |
| 288 | - */ | |
| 289 | - public function __set( $key, $value ) { | |
| 290 | - $this->attributes[ $key ] = $value; | |
| 291 | - } | |
| 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 | + } | |
| 292 | 284 | |
| 293 | - /** | |
| 294 | - * Check if a property is set in attributes list. | |
| 295 | - * @param mixed $key | |
| 296 | - * @return bool | |
| 297 | - */ | |
| 298 | - public function __isset( $key ) { | |
| 299 | - return isset( $this->attributes[ $key ] ); | |
| 300 | - } | |
| 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 | + } | |
| 301 | 293 | } |