class-auxels-admin-assets.php
1 year ago
class-auxels-archive-menu-links.php
1 year ago
class-auxels-envato-elements.php
1 year ago
class-auxels-import-parser.php
3 years ago
class-auxels-import.php
3 years ago
class-auxels-search-post-type.php
6 months ago
class-auxels-wc-attribute-nav-menu.php
1 year ago
class-auxin-admin-dashboard.php
1 year ago
class-auxin-demo-importer.php
6 months ago
class-auxin-dependency-sorting.php
3 years ago
class-auxin-import.php
1 year ago
class-auxin-install.php
1 year ago
class-auxin-master-nav-menu-admin.php
1 year ago
class-auxin-page-template.php
1 year ago
class-auxin-permalink.php
1 year ago
class-auxin-plugin-requirements.php
3 years ago
class-auxin-post-type-base.php
1 year ago
class-auxin-svg-support-allowedattributes.php
7 years ago
class-auxin-svg-support-allowedtags.php
7 years ago
class-auxin-svg-support.php
1 year ago
class-auxin-walker-nav-menu-back.php
1 year ago
class-auxin-welcome-sections.php
1 year ago
class-auxin-welcome.php
6 months ago
class-auxin-whitelabel.php
3 years ago
class-auxin-widget-indie.php
1 year ago
class-auxin-widget-shortcode-map.php
11 months ago
class-auxin-widget.php
1 year ago
class-auxin-widget-shortcode-map.php
272 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Adds Theme Widgets (elements), shortcodes and visual elements |
| 4 | * |
| 5 | * |
| 6 | * @package Auxin |
| 7 | * @license LICENSE.txt |
| 8 | * @author averta |
| 9 | * @link http://phlox.pro/ |
| 10 | * @copyright (c) 2010-2025 averta |
| 11 | */ |
| 12 | |
| 13 | // no direct access allowed |
| 14 | if ( ! defined('ABSPATH') ) exit; |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | class Auxin_Widget_Shortcode_Map { |
| 20 | |
| 21 | /** |
| 22 | * Instance of this class. |
| 23 | * |
| 24 | * @var object |
| 25 | */ |
| 26 | protected static $instance = null; |
| 27 | |
| 28 | /** |
| 29 | * The Master list of all shortcodes and widgets |
| 30 | * |
| 31 | * @var array |
| 32 | */ |
| 33 | private $master_array = array(); |
| 34 | |
| 35 | /** |
| 36 | * The Master list of all shortcodes |
| 37 | * |
| 38 | * @var array |
| 39 | */ |
| 40 | public $master_shortcode_array = array(); |
| 41 | |
| 42 | |
| 43 | |
| 44 | public function __construct(){ |
| 45 | add_action('init', array( $this, 'auxin_framework_loaded' ) ); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | public function auxin_framework_loaded(){ |
| 51 | |
| 52 | add_action( 'widgets_init' , array( $this, 'add_widgets' ) ); |
| 53 | |
| 54 | // map and add all shortcodes |
| 55 | $this->add_shortcodes(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Collects and stores elements info |
| 62 | */ |
| 63 | public function get_master_array(){ |
| 64 | |
| 65 | if( empty( $this->master_array ) ){ |
| 66 | |
| 67 | $master_array = apply_filters( 'auxin_master_array_shortcodes', array() ); |
| 68 | |
| 69 | foreach ( $master_array as $element_id => $element_info ) { |
| 70 | // determines whether the widget should be generated or not |
| 71 | $element_info['is_widget'] = isset( $element_info['is_widget'] ) && $element_info['is_widget'] === false ? false : true; |
| 72 | // determines whether the shortcode should be added or not |
| 73 | $element_info['is_shortcode'] = isset( $element_info['is_shortcode'] ) && $element_info['is_shortcode'] === false ? false : true; |
| 74 | |
| 75 | // make sure icon is set |
| 76 | $element_info['icon'] = isset( $element_info['icon'] ) && ! empty( $element_info['icon'] ) ? $element_info['icon'] : 'dashicons dashicons-admin-generic'; |
| 77 | |
| 78 | // make sure key in master array is base name of elements, in this case we can find elements in this array faster |
| 79 | $this->master_array[ $element_info['base'] ] = $element_info; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $this->master_array; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Register all allowed widgets |
| 88 | */ |
| 89 | public function add_widgets() { |
| 90 | |
| 91 | $master_array = $this->get_master_array(); |
| 92 | |
| 93 | foreach ( $master_array as $element_id => $element_info ) { |
| 94 | |
| 95 | // Add a widget if it was allowed |
| 96 | if( $element_info['is_widget'] ) { |
| 97 | $widget_info = $this->generate_widget_array( $element_info ); |
| 98 | global $wp_widget_factory; |
| 99 | $wp_widget_factory->widgets[ $element_info['base'] ] = new Auxin_Widget( $widget_info ); |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Adds all collected shortcodes |
| 108 | */ |
| 109 | private function add_shortcodes() { |
| 110 | |
| 111 | if ( defined('WP_CLI') && WP_CLI ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | $shortcode_array_list = $this->get_master_shortcode_array(); |
| 117 | |
| 118 | foreach ( $shortcode_array_list as $shortcode_index => $shortcode_array ) { |
| 119 | add_shortcode( $shortcode_array['base'], $shortcode_array['auxin_output_callback'] ); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | protected function remove_empty_nodes_recuresively( $array ){ |
| 124 | $cleared_array = array(); |
| 125 | |
| 126 | foreach( $array as $key => $node ) { |
| 127 | if( is_array( $node ) ){ |
| 128 | $cleared_array[ $key ] = $this->remove_empty_nodes_recuresively( $node ); |
| 129 | } elseif ( ! empty( $node ) || $node === false ){ |
| 130 | $cleared_array[ $key ] = $array[ $key ]; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return $cleared_array; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Get list of allowed shortcodes |
| 140 | */ |
| 141 | public function get_master_shortcode_array(){ |
| 142 | $this->get_master_array(); |
| 143 | |
| 144 | if( empty( $this->master_shortcode_array ) ){ |
| 145 | foreach ( $this->master_array as $element_id => $element_info ) { |
| 146 | // Collect the shortcode if it was allowed |
| 147 | if( $element_info['is_shortcode'] ){ |
| 148 | $this->master_shortcode_array[] = $this->generate_shortcode_array( $element_info ); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return $this->master_shortcode_array; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Sanitize and proper |
| 159 | * |
| 160 | * @param [type] $element_info [description] |
| 161 | * @return [type] [description] |
| 162 | */ |
| 163 | protected function sanitize_element_info( $element_info ){ |
| 164 | |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /** |
| 169 | * Generates shortcode info |
| 170 | */ |
| 171 | protected function generate_shortcode_array( $element_info ){ |
| 172 | |
| 173 | $shortcode_array['base'] = $element_info['base']; |
| 174 | $shortcode_array['is_shortcode'] = $element_info['is_shortcode']; |
| 175 | |
| 176 | $shortcode_array['auxin_output_callback'] = $element_info['auxin_output_callback']; |
| 177 | |
| 178 | foreach ( $element_info['params'] as $param_id => $param ) { |
| 179 | if( isset( $param['def_value'] ) ){ |
| 180 | $shortcode_array['params'][ $param['param_name'] ] = $param['def_value']; |
| 181 | } else { |
| 182 | $shortcode_array['params'][ $param['param_name'] ] = ''; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return $shortcode_array; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Generates widget info |
| 192 | */ |
| 193 | protected function generate_widget_array( $element_info ){ |
| 194 | $widget_array = array(); |
| 195 | |
| 196 | $widget_array['base_ID'] = $element_info['base']; |
| 197 | $widget_array['name'] = $element_info['name']; |
| 198 | $widget_array['is_widget'] = $element_info['is_widget']; |
| 199 | |
| 200 | /** |
| 201 | * This filter makes the others able to override element output function |
| 202 | */ |
| 203 | $widget_array['auxin_output_callback'] = apply_filters( 'auxin_element_output_callback', $element_info['auxin_output_callback'], 'widget' ); |
| 204 | $widget_array['args']['description'] = $element_info['description']; |
| 205 | $widget_array['args']['panels_groups'] = array('auxin'); |
| 206 | |
| 207 | if( ! empty( $element_info['icon'] ) ) { |
| 208 | $widget_array['args']['panels_icon'] = $element_info['icon']; |
| 209 | } |
| 210 | |
| 211 | foreach ( $element_info['params'] as $param_id => $param ) { |
| 212 | $widget_params = array(); |
| 213 | |
| 214 | $widget_params['name'] = $param['heading']; |
| 215 | $widget_params['id'] = $param['param_name']; |
| 216 | $widget_params['type'] = $param['type']; |
| 217 | |
| 218 | if( !empty( $param['std'] ) ) { |
| 219 | $widget_params['value'] = $param['std']; |
| 220 | } |
| 221 | elseif( !empty( $param['def_value'] ) ) { |
| 222 | $widget_params['value'] = $param['def_value']; |
| 223 | } |
| 224 | elseif( 1 ) { |
| 225 | $widget_params['value'] = isset( $param['value'] ) ? $param['value'] : ''; |
| 226 | } |
| 227 | if( $widget_params['type'] == 'aux_visual_select' ) { |
| 228 | $widget_params['choices'] = $param['choices']; |
| 229 | } |
| 230 | if( isset( $param['description'] ) ) { |
| 231 | $widget_params['description'] = $param['description']; |
| 232 | } |
| 233 | if( isset( $param['dependency'] ) ) { |
| 234 | $widget_params['dependency'] = $param['dependency']; |
| 235 | } |
| 236 | // special param for aux_taxonomy field type |
| 237 | if( isset( $param['taxonomy'] ) ) { |
| 238 | $widget_params['taxonomy'] = $param['taxonomy']; |
| 239 | } |
| 240 | // TODO: It shoould convert to array an array when dependency js writes |
| 241 | // $widget_params['dependency'] = array( $param['dependency'] ); |
| 242 | |
| 243 | if( in_array( $param['type'], array( 'select', 'dropdown', 'aux_select2_multiple', 'aux_select2_single' ) ) ) { |
| 244 | $widget_params['options'] = $param['value']; |
| 245 | } |
| 246 | |
| 247 | // lets set a key for each param, for improving search in array |
| 248 | $widget_array['params'][ $widget_params['id'] ] = $widget_params; |
| 249 | } |
| 250 | |
| 251 | return $widget_array; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * Return an instance of this class. |
| 257 | * |
| 258 | * @return object A single instance of this class. |
| 259 | */ |
| 260 | public static function get_instance() { |
| 261 | |
| 262 | // If the single instance hasn't been set, set it now. |
| 263 | if ( null == self::$instance ) { |
| 264 | self::$instance = new self; |
| 265 | } |
| 266 | |
| 267 | return self::$instance; |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 |