class-auxels-admin-assets.php
6 years ago
class-auxels-archive-menu-links.php
6 years ago
class-auxels-import-parser.php
8 years ago
class-auxels-import.php
6 years ago
class-auxels-search-post-type.php
6 years ago
class-auxin-admin-dashboard.php
6 years ago
class-auxin-demo-importer.php
6 years ago
class-auxin-dependency-sorting.php
8 years ago
class-auxin-import.php
6 years ago
class-auxin-install.php
6 years ago
class-auxin-master-nav-menu-admin.php
6 years ago
class-auxin-page-template.php
6 years ago
class-auxin-permalink.php
6 years ago
class-auxin-plugin-requirements.php
6 years ago
class-auxin-post-type-base.php
6 years ago
class-auxin-siteorigin-widget.php
6 years ago
class-auxin-svg-support-allowedattributes.php
7 years ago
class-auxin-svg-support-allowedtags.php
7 years ago
class-auxin-svg-support.php
7 years ago
class-auxin-walker-nav-menu-back.php
6 years ago
class-auxin-welcome-sections.php
6 years ago
class-auxin-welcome.php
6 years ago
class-auxin-whitelabel.php
6 years ago
class-auxin-widget-indie.php
6 years ago
class-auxin-widget-shortcode-map.php
6 years ago
class-auxin-widget.php
6 years ago
class-auxin-widget-shortcode-map.php
419 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adds Theme Widgets (elements), shortcodes and visual elements |
| 4 | * (visual composer and siteorigin page builder elements are the same) |
| 5 | * |
| 6 | * |
| 7 | * @package Auxin |
| 8 | * @license LICENSE.txt |
| 9 | * @author averta |
| 10 | * @link http://phlox.pro/ |
| 11 | * @copyright (c) 2010-2020 averta |
| 12 | */ |
| 13 | |
| 14 | // no direct access allowed |
| 15 | if ( ! defined('ABSPATH') ) exit; |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | class Auxin_Widget_Shortcode_Map { |
| 21 | |
| 22 | /** |
| 23 | * Instance of this class. |
| 24 | * |
| 25 | * @var object |
| 26 | */ |
| 27 | protected static $instance = null; |
| 28 | |
| 29 | /** |
| 30 | * The Master list of all shortcodes and widgets |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private $master_array = array(); |
| 35 | |
| 36 | /** |
| 37 | * The Master list of all shortcodes |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | public $master_shortcode_array = array(); |
| 42 | |
| 43 | |
| 44 | |
| 45 | function __construct(){ |
| 46 | add_action('auxin_loaded', array( $this, 'auxin_framework_loaded' ) ); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | public function auxin_framework_loaded(){ |
| 52 | |
| 53 | add_action( 'vc_before_init', array( $this, 'vc_mapper' ) ); |
| 54 | add_action( 'widgets_init' , array( $this, 'add_widgets' ) ); |
| 55 | |
| 56 | add_filter( 'siteorigin_panels_widgets' , array( $this,'add_siteorigin_widget' ) ); |
| 57 | add_filter( 'siteorigin_panels_widget_object', array( $this,'add_siteorigin_widget_object' ), 10,2 ); |
| 58 | |
| 59 | // map and add all shortcodes |
| 60 | $this->add_shortcodes(); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Collects and stores elements info |
| 67 | */ |
| 68 | public function get_master_array(){ |
| 69 | |
| 70 | if( empty( $this->master_array ) ){ |
| 71 | |
| 72 | $master_array = apply_filters( 'auxin_master_array_shortcodes', array() ); |
| 73 | |
| 74 | foreach ( $master_array as $element_id => $element_info ) { |
| 75 | // determines whether the widget should be generated or not |
| 76 | $element_info['is_widget'] = isset( $element_info['is_widget'] ) && $element_info['is_widget'] === false ? false : true; |
| 77 | // determines whether the shortcode should be added or not |
| 78 | $element_info['is_shortcode'] = isset( $element_info['is_shortcode'] ) && $element_info['is_shortcode'] === false ? false : true; |
| 79 | // determines whether the an element in siteorigin should be generated or not |
| 80 | $element_info['is_so'] = isset( $element_info['is_so'] ) && $element_info['is_so'] === false ? false : true; |
| 81 | // determines whether the an element in vc should be generated or not |
| 82 | $element_info['is_vc'] = isset( $element_info['is_vc'] ) && $element_info['is_vc'] === false ? false : true; |
| 83 | |
| 84 | // make sure icon is set |
| 85 | $element_info['icon'] = isset( $element_info['icon'] ) && ! empty( $element_info['icon'] ) ? $element_info['icon'] : 'dashicons dashicons-admin-generic'; |
| 86 | |
| 87 | // make sure key in master array is base name of elements, in this case we can find elements in this array faster |
| 88 | $this->master_array[ $element_info['base'] ] = $element_info; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $this->master_array; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Register all allowed widgets |
| 97 | */ |
| 98 | public function add_widgets() { |
| 99 | |
| 100 | $master_array = $this->get_master_array(); |
| 101 | |
| 102 | foreach ( $master_array as $element_id => $element_info ) { |
| 103 | |
| 104 | // Add a widget if it was allowed |
| 105 | if( $element_info['is_widget'] ) { |
| 106 | $widget_info = $this->generate_widget_array( $element_info ); |
| 107 | global $wp_widget_factory; |
| 108 | $wp_widget_factory->widgets[ $element_info['base'] ] = new Auxin_Widget( $widget_info ); |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Adds all collected shortcodes |
| 117 | */ |
| 118 | private function add_shortcodes() { |
| 119 | |
| 120 | $shortcode_array_list = $this->get_master_shortcode_array(); |
| 121 | |
| 122 | foreach ( $shortcode_array_list as $shortcode_index => $shortcode_array ) { |
| 123 | add_shortcode( $shortcode_array['base'], $shortcode_array['auxin_output_callback'] ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | protected function remove_empty_nodes_recuresively( $array ){ |
| 128 | $cleared_array = array(); |
| 129 | |
| 130 | foreach( $array as $key => $node ) { |
| 131 | if( is_array( $node ) ){ |
| 132 | $cleared_array[ $key ] = $this->remove_empty_nodes_recuresively( $node ); |
| 133 | } elseif ( ! empty( $node ) || $node === false ){ |
| 134 | $cleared_array[ $key ] = $array[ $key ]; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return $cleared_array; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * Get list of allowed shortcodes |
| 144 | */ |
| 145 | public function get_master_shortcode_array(){ |
| 146 | $this->get_master_array(); |
| 147 | |
| 148 | if( empty( $this->master_shortcode_array ) ){ |
| 149 | foreach ( $this->master_array as $element_id => $element_info ) { |
| 150 | // Collect the shortcode if it was allowed |
| 151 | if( $element_info['is_shortcode'] ){ |
| 152 | $this->master_shortcode_array[] = $this->generate_shortcode_array( $element_info ); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return $this->master_shortcode_array; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
| 162 | * Sanitize and proper |
| 163 | * |
| 164 | * @param [type] $element_info [description] |
| 165 | * @return [type] [description] |
| 166 | */ |
| 167 | protected function sanitize_element_info( $element_info ){ |
| 168 | |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Generates shortcode info |
| 174 | */ |
| 175 | protected function generate_shortcode_array( $element_info ){ |
| 176 | |
| 177 | $shortcode_array['base'] = $element_info['base']; |
| 178 | $shortcode_array['is_shortcode'] = $element_info['is_shortcode']; |
| 179 | |
| 180 | $shortcode_array['auxin_output_callback'] = $element_info['auxin_output_callback']; |
| 181 | |
| 182 | foreach ( $element_info['params'] as $param_id => $param ) { |
| 183 | if( isset( $param['def_value'] ) ){ |
| 184 | $shortcode_array['params'][ $param['param_name'] ] = $param['def_value']; |
| 185 | } else { |
| 186 | $shortcode_array['params'][ $param['param_name'] ] = ''; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return $shortcode_array; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Generates widget info |
| 196 | */ |
| 197 | protected function generate_widget_array( $element_info ){ |
| 198 | $widget_array = array(); |
| 199 | |
| 200 | $widget_array['base_ID'] = $element_info['base']; |
| 201 | $widget_array['name'] = $element_info['name']; |
| 202 | $widget_array['is_widget'] = $element_info['is_widget']; |
| 203 | $widget_array['is_so'] = $element_info['is_so']; |
| 204 | |
| 205 | /** |
| 206 | * This filter makes the others able to override element output function |
| 207 | */ |
| 208 | $widget_array['auxin_output_callback'] = apply_filters( 'auxin_element_output_callback', $element_info['auxin_output_callback'], 'widget' ); |
| 209 | $widget_array['args']['description'] = $element_info['description']; |
| 210 | $widget_array['args']['panels_groups'] = array('auxin'); |
| 211 | |
| 212 | if( ! empty( $element_info['icon'] ) ) { |
| 213 | $widget_array['args']['panels_icon'] = $element_info['icon']; |
| 214 | } |
| 215 | |
| 216 | foreach ( $element_info['params'] as $param_id => $param ) { |
| 217 | $widget_params = array(); |
| 218 | |
| 219 | $widget_params['name'] = $param['heading']; |
| 220 | $widget_params['id'] = $param['param_name']; |
| 221 | $widget_params['type'] = $param['type']; |
| 222 | |
| 223 | if( !empty( $param['std'] ) ) { |
| 224 | $widget_params['value'] = $param['std']; |
| 225 | } |
| 226 | elseif( !empty( $param['def_value'] ) ) { |
| 227 | $widget_params['value'] = $param['def_value']; |
| 228 | } |
| 229 | elseif( 1 ) { |
| 230 | $widget_params['value'] = isset( $param['value'] ) ? $param['value'] : ''; |
| 231 | } |
| 232 | if( $widget_params['type'] == 'aux_visual_select' ) { |
| 233 | $widget_params['choices'] = $param['choices']; |
| 234 | } |
| 235 | if( isset( $param['description'] ) ) { |
| 236 | $widget_params['description'] = $param['description']; |
| 237 | } |
| 238 | if( isset( $param['dependency'] ) ) { |
| 239 | $widget_params['dependency'] = $param['dependency']; |
| 240 | } |
| 241 | // special param for aux_taxonomy field type |
| 242 | if( isset( $param['taxonomy'] ) ) { |
| 243 | $widget_params['taxonomy'] = $param['taxonomy']; |
| 244 | } |
| 245 | // TODO: It shoould convert to array an array when dependency js writes |
| 246 | // $widget_params['dependency'] = array( $param['dependency'] ); |
| 247 | |
| 248 | if( in_array( $param['type'], array( 'select', 'dropdown', 'aux_select2_multiple', 'aux_select2_single' ) ) ) { |
| 249 | $widget_params['options'] = $param['value']; |
| 250 | } |
| 251 | |
| 252 | // lets set a key for each param, for improving search in array |
| 253 | $widget_array['params'][ $widget_params['id'] ] = $widget_params; |
| 254 | } |
| 255 | |
| 256 | return $widget_array; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * Adds all defined elements to vc |
| 262 | */ |
| 263 | public function vc_mapper(){ |
| 264 | $master_array = $this->get_master_array(); |
| 265 | |
| 266 | foreach ( $master_array as $array_node ) { |
| 267 | if( $array_node['is_vc'] !== false ) { |
| 268 | $array_node['params'] = $this->exchange_vc_fields( $array_node['params'] ); |
| 269 | vc_map( $this->make_vc_compatible( $array_node ) ); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Get the params array and flip the value array of its value key |
| 276 | */ |
| 277 | public function exchange_vc_fields( $params ){ |
| 278 | $result = array(); |
| 279 | |
| 280 | foreach ( $params as $params_cell => $params_value ) { |
| 281 | if( in_array( $params_value['type'], array( 'aux_visual_select', 'dropdown', 'aux_select2_multiple', 'aux_select2_single' ) ) ){ |
| 282 | $params_value['value'] = ! empty( $params_value['value'] ) ? $params_value['value'] : ''; |
| 283 | $params_value['std' ] = ! empty( $params_value['def_value'] ) ? $params_value['def_value'] : $params_value['value']; |
| 284 | |
| 285 | if( $params_value['type' ] == 'dropdown' ){ |
| 286 | $params_value['value'] = array_flip( $params_value['value'] ); |
| 287 | } |
| 288 | } |
| 289 | $result[] = $params_value; |
| 290 | } |
| 291 | |
| 292 | return $result; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * Removes all empty array keys and appends some special fields for visual composer |
| 298 | */ |
| 299 | protected function make_vc_compatible( $element_config ){ |
| 300 | |
| 301 | // Removes all empty array keys |
| 302 | $element_config = $this->remove_empty_nodes_recuresively( $element_config ); |
| 303 | |
| 304 | // Appending some special fields for visual composer |
| 305 | if( isset( $element_config['params'] ) ){ |
| 306 | $element_config['params'][] = array( |
| 307 | 'type' => 'css_editor', |
| 308 | 'heading' => __( 'CSS', 'auxin-elements' ), |
| 309 | 'param_name' => 'vc_css', |
| 310 | 'group' => __( 'Design options', 'auxin-elements' ) |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | return $element_config; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /** |
| 319 | * Register elements in siteorigin page builder |
| 320 | */ |
| 321 | function add_siteorigin_widget( $widgets ) { |
| 322 | |
| 323 | $master_array = $this->get_master_array(); |
| 324 | |
| 325 | foreach ( $master_array as $element_id => $element_info ) { |
| 326 | |
| 327 | // add a SO element if widget is not allowed |
| 328 | if( ! $element_info['is_widget'] && $element_info['is_so'] ) { |
| 329 | |
| 330 | $widgets[ $element_info['base'] ] = array( |
| 331 | 'class' => $element_info['base'], |
| 332 | 'title' => $element_info['name'], |
| 333 | 'panels_title'=> $element_info['name'], |
| 334 | 'description' => $element_info['description'], |
| 335 | 'installed' => true, |
| 336 | 'plugin' => array(), |
| 337 | 'groups' => array('auxin'), |
| 338 | 'icon' => $element_info['icon'] |
| 339 | ); |
| 340 | |
| 341 | |
| 342 | $screen = get_current_screen(); |
| 343 | |
| 344 | if ( class_exists( 'Auxin_SiteOrigin_Widget') && isset( $element_info['so_api'] ) && $element_info['so_api'] ) { |
| 345 | |
| 346 | // render widget to enqueue scripts |
| 347 | // this method is used in site origin panels too (siteorigin-panels.php:420) |
| 348 | if( $screen->base != 'widgets' ) { |
| 349 | //$original_post = isset($GLOBALS['post']) ? $GLOBALS['post'] : null; // Make sure widgets don't change the global post. |
| 350 | ob_start(); |
| 351 | $widget_obj = new Auxin_SiteOrigin_Widget( $element_info ); |
| 352 | $widget_obj->form( array() ); |
| 353 | ob_clean(); |
| 354 | |
| 355 | wp_reset_postdata(); |
| 356 | } |
| 357 | |
| 358 | } |
| 359 | |
| 360 | // remove the widget from SO if displaying widget in SO is not allowed |
| 361 | } elseif ( ! $element_info['is_so'] ) { |
| 362 | if( isset( $widgets[ $element_info['base'] ] ) ){ |
| 363 | unset( $widgets[ $element_info['base'] ] ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
| 369 | return $widgets; |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Define the instance of widget for SO |
| 374 | * |
| 375 | * @param object $object the instance of widget element |
| 376 | * @param string $widget The base name of widget |
| 377 | */ |
| 378 | function add_siteorigin_widget_object( $object, $widget ) { |
| 379 | |
| 380 | // Try to load the class again if the auxin-elements plugin was loaded prior to siteorigin |
| 381 | include AUXELS_INC_DIR . '/classes/class-auxin-siteorigin-widget.php'; |
| 382 | |
| 383 | $master_array = $this->get_master_array(); |
| 384 | |
| 385 | if ( ! isset( $master_array[ $widget ] ) ) { |
| 386 | return $object; |
| 387 | } else { |
| 388 | $element_info = $master_array[ $widget ]; |
| 389 | } |
| 390 | |
| 391 | if ( class_exists( 'Auxin_SiteOrigin_Widget') && isset( $element_info['so_api'] ) && $element_info['so_api'] ) { |
| 392 | return new Auxin_SiteOrigin_Widget( $element_info ); |
| 393 | } elseif ( ! empty( $object ) ) { |
| 394 | return $object; |
| 395 | } else { |
| 396 | return new Auxin_Widget( $this->generate_widget_array( $element_info ) ); |
| 397 | } |
| 398 | |
| 399 | } |
| 400 | |
| 401 | |
| 402 | /** |
| 403 | * Return an instance of this class. |
| 404 | * |
| 405 | * @return object A single instance of this class. |
| 406 | */ |
| 407 | public static function get_instance() { |
| 408 | |
| 409 | // If the single instance hasn't been set, set it now. |
| 410 | if ( null == self::$instance ) { |
| 411 | self::$instance = new self; |
| 412 | } |
| 413 | |
| 414 | return self::$instance; |
| 415 | } |
| 416 | |
| 417 | } |
| 418 | |
| 419 |