gutenberg
1 month ago
option-tabs
2 months ago
display.php
1 month ago
extras.php
8 months ago
widgets.php
3 months ago
extras.php
177 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Extra Functions |
| 4 | * |
| 5 | * Collections of extra functions to avoid repeatition |
| 6 | * |
| 7 | * @copyright Copyright (c) 2016, Jeffrey Carandang |
| 8 | * @since 4.0 |
| 9 | */ |
| 10 | |
| 11 | //create separate function returning classes for reuse |
| 12 | if( !function_exists( 'widgetopts_classes_generator' ) ){ |
| 13 | function widgetopts_classes_generator( $opts, $tabs, $settings, $so = false ){ |
| 14 | if( !empty( $opts ) && is_array( $opts ) ){ |
| 15 | $classes = array(); |
| 16 | $devices = isset( $opts['devices'] ) ? $opts['devices'] : ''; |
| 17 | $alignment = isset( $opts['alignment'] ) ? $opts['alignment'] : ''; |
| 18 | $columns = isset( $opts['column'] ) ? $opts['column'] : ''; |
| 19 | $clearfix = isset( $opts['clearfix'] ) ? $opts['clearfix'] : ''; |
| 20 | $custom_class = isset( $opts['class'] ) ? $opts['class'] : ''; |
| 21 | $abbr = array( |
| 22 | 'mobile' => 'xs', |
| 23 | 'tablet' => 'sm', |
| 24 | 'desktop' => 'md', |
| 25 | ); |
| 26 | if( isset( $devices['options'] ) ){ |
| 27 | unset( $devices['options'] ); |
| 28 | } |
| 29 | |
| 30 | if( 'activate' == $tabs['devices'] ){ |
| 31 | //devices visibility |
| 32 | if( !empty( $devices ) ){ |
| 33 | $device_opts = ( isset( $opts['devices']['options'] ) ) ? $opts['devices']['options'] : 'hide'; |
| 34 | $classes[] = sanitize_html_class('extendedwopts-' . $device_opts); |
| 35 | |
| 36 | foreach ($devices as $key => $value) { |
| 37 | $classes[] = sanitize_html_class('extendedwopts-' . $key); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if( 'activate' == $tabs['alignment'] ){ |
| 43 | //alignment |
| 44 | if( !empty( $alignment ) ){ |
| 45 | foreach ($alignment as $k => $v) { |
| 46 | if( 'default' != $v ){ |
| 47 | $classes[] = sanitize_html_class('extendedwopts-' . $abbr[ $k ] . '-'. $v); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if( 'activate' == $tabs['classes'] && isset( $settings['classes'] ) ){ |
| 54 | //classes & ID |
| 55 | // $options = get_option('extwopts_class_settings'); |
| 56 | $predefined = array(); |
| 57 | if( isset( $settings['classes'] ) && isset( $settings['classes']['classlists'] ) && !empty( $settings['classes']['classlists'] ) ){ |
| 58 | $predefined = $settings['classes']['classlists']; |
| 59 | } |
| 60 | |
| 61 | //don't add any classes when settings is set to predefined or hide |
| 62 | if( !isset( $settings['classes']['type'] ) || |
| 63 | ( isset( $settings['classes']['type'] ) && !in_array( $settings['classes']['type'] , array( 'hide', 'predefined' ) ) ) ){ |
| 64 | if( is_array( $custom_class ) && isset( $custom_class['classes'] ) && !empty( $custom_class['classes'] ) ){ |
| 65 | $tmpClasses = explode(" ", $custom_class['classes']); |
| 66 | if(!empty($tmpClasses)){ |
| 67 | foreach($tmpClasses as $c) { |
| 68 | $classes[] = sanitize_html_class($c); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | //don't add any classes when settings is set to text or hide |
| 75 | if( !isset( $settings['classes']['type'] ) || |
| 76 | ( isset( $settings['classes']['type'] ) && !in_array( $settings['classes']['type'] , array( 'hide', 'text' ) ) ) ){ |
| 77 | if( is_array( $predefined ) && !empty( $predefined ) ){ |
| 78 | $predefined = array_unique( $predefined ); |
| 79 | if( isset( $custom_class['predefined'] ) && is_array( $custom_class['predefined'] ) ){ |
| 80 | $filtered = array_intersect( $predefined, $custom_class['predefined'] ); |
| 81 | if( !empty( $filtered ) ){ |
| 82 | $classes = array_merge( $classes, $filtered ); |
| 83 | // $classes[] = implode( ' ', $filtered ); |
| 84 | // $classes[] = ' '; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if( $so && 'activate' == $tabs['hide_title'] ){ |
| 92 | //add fixed class to widget |
| 93 | if( isset( $custom_class['title'] ) && !empty( $custom_class['title'] ) ){ |
| 94 | $classes[] = 'widgetopts-hide_title'; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return apply_filters( 'widgetopts_get_classes', $classes ); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //add is_active_sidebar support |
| 104 | if( !function_exists( 'widgetopts_sidebars_widgets' ) ){ |
| 105 | add_action( 'wp_loaded', 'widgetopts_sidebars_widgets_action' ); |
| 106 | function widgetopts_sidebars_widgets_action() { |
| 107 | if( apply_filters( 'widgetopts_is_active_sidebar_support', true ) ){ |
| 108 | add_filter( 'sidebars_widgets', 'widgetopts_sidebars_widgets' ); |
| 109 | } |
| 110 | } |
| 111 | function widgetopts_sidebars_widgets( $sidebars ) { |
| 112 | if ( is_admin() ) { |
| 113 | return $sidebars; |
| 114 | } |
| 115 | |
| 116 | global $wp_registered_widgets; |
| 117 | $checked = array(); |
| 118 | |
| 119 | foreach ( $sidebars as $s => $sidebar ) { |
| 120 | if ( $s == 'wp_inactive_widgets' || strpos( $s, 'orphaned_widgets' ) === 0 || empty( $sidebar ) ) { |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | foreach ( $sidebar as $w => $widget ) { |
| 125 | // $widget is the id of the widget |
| 126 | if ( ! isset( $wp_registered_widgets[ $widget ] ) ) { |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | if ( isset( $checked[ $widget ] ) ) { |
| 131 | $show = $checked[ $widget ]; |
| 132 | } else { |
| 133 | $opts = $wp_registered_widgets[ $widget ]; |
| 134 | $id_base = is_array( $opts['callback'] ) || $opts['callback'] instanceof ArrayAccess ? $opts['callback'][0]->id_base : $opts['callback']; |
| 135 | |
| 136 | if ( ! is_string( $id_base ) ) { |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | $instance = get_option( 'widget_' . $id_base ); |
| 141 | |
| 142 | if ( ! $instance || ! is_array( $instance ) ) { |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if ( isset( $instance['_multiwidget'] ) && $instance['_multiwidget'] ) { |
| 147 | $number = $opts['params'][0]['number']; |
| 148 | if ( ! isset( $instance[ $number ] ) ) { |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | $instance = $instance[ $number ]; |
| 153 | unset( $number ); |
| 154 | } |
| 155 | |
| 156 | unset( $opts ); |
| 157 | |
| 158 | $show = widgetopts_display_callback( $instance, (object) array( 'id' => $widget ), '' ); |
| 159 | |
| 160 | $checked[ $widget ] = $show ? true : false; |
| 161 | } |
| 162 | |
| 163 | if ( ! $show ) { |
| 164 | unset( $sidebars[ $s ][ $w ] ); |
| 165 | } |
| 166 | |
| 167 | unset( $widget ); |
| 168 | } |
| 169 | unset( $sidebar ); |
| 170 | } |
| 171 | |
| 172 | return $sidebars; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | ?> |
| 177 |