widgets.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add Widget Options |
| 4 | * |
| 5 | * Process Managing of Widget Options. |
| 6 | * |
| 7 | * @copyright Copyright (c) 2016, Jeffrey Carandang |
| 8 | * @since 2.0 |
| 9 | */ |
| 10 | // Exit if accessed directly |
| 11 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 | |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Output widget search filter textfield before widget lists |
| 17 | * |
| 18 | * @since 3.3 |
| 19 | */ |
| 20 | if( !function_exists( 'widgetopts_add_search_input' ) ): |
| 21 | function widgetopts_add_search_input() { |
| 22 | global $widget_options; |
| 23 | if( isset( $widget_options['search'] ) && 'activate' == $widget_options['search'] ): ?> |
| 24 | <div id="widgetopts-widgets-filter"> |
| 25 | <label class="screen-reader-text" for="widgetopts-widgets-search"><?php _e( 'Search Widgets', 'widget-options' ); ?></label> |
| 26 | <input type="text" id="widgetopts-widgets-search" class="widgetopts-widgets-search" placeholder="<?php esc_attr_e( 'Search widgets…', 'widget-options' ) ?>" /> |
| 27 | <div class="widgetopts-search-icon" aria-hidden="true"></div> |
| 28 | <button type="button" class="widgetopts-clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results', 'widget-options' ); ?></span></button> |
| 29 | <p class="screen-reader-text" id="widgetopts-search-desc"><?php _e( 'The search results will be updated as you type.', 'widget-options' ); ?></p> |
| 30 | </div> |
| 31 | <?php |
| 32 | endif; |
| 33 | } |
| 34 | add_action( 'widgets_admin_page', 'widgetopts_add_search_input' ); |
| 35 | endif; |
| 36 | |
| 37 | /** |
| 38 | * Add Options on in_widget_form action |
| 39 | * |
| 40 | * @since 2.0 |
| 41 | * @return void |
| 42 | */ |
| 43 | |
| 44 | function widgetopts_in_widget_form( $widget, $return, $instance ){ |
| 45 | global $widget_options, $wp_registered_widget_controls; |
| 46 | $width = ( isset( $wp_registered_widget_controls[$widget->id]['width'] ) ) ? (int) $wp_registered_widget_controls[ $widget->id]['width' ] : 250; |
| 47 | $opts = ( isset( $instance[ 'extended_widget_opts-'. $widget->id ] ) ) ? $instance[ 'extended_widget_opts-'. $widget->id ] : array(); |
| 48 | $is_siteorigin = ( isset( $widget_options['siteorigin'] ) ) ? $widget_options['siteorigin'] : ''; |
| 49 | |
| 50 | /** change widget names for SO Pagebuilder support **/ |
| 51 | if( isset( $widget->id ) && 'temp' == $widget->id ){ |
| 52 | $namespace = 'widgets['. $widget->number .']'; |
| 53 | $optsname = 'widgets['. $widget->number .'][extended_widget_opts_name]'; |
| 54 | $opts = ( isset( $instance[ 'extended_widget_opts' ] ) ) ? $instance[ 'extended_widget_opts'] : array(); |
| 55 | $widget->id = $widget->number; |
| 56 | |
| 57 | //create siteorigin pagebuilder variable |
| 58 | echo '<input type="hidden" name="'. $namespace .'[siteorigin]" value="1" />'; |
| 59 | }else{ |
| 60 | $namespace = 'extended_widget_opts-'. $widget->id; |
| 61 | $optsname = 'extended_widget_opts_name'; |
| 62 | } |
| 63 | |
| 64 | $args = array( |
| 65 | 'width' => $width, |
| 66 | 'id' => $widget->id, |
| 67 | 'params' => $opts, |
| 68 | 'namespace' => $namespace |
| 69 | ); |
| 70 | $selected = 0; |
| 71 | if( isset( $opts['tabselect'] ) ){ |
| 72 | $selected = $opts['tabselect']; |
| 73 | } |
| 74 | |
| 75 | ?> |
| 76 | |
| 77 | <input type="hidden" name="extended_widget_opts_name" value="extended_widget_opts-<?php echo $widget->id;?>"> |
| 78 | <input type="hidden" name="<?php echo $args['namespace'];?>[extended_widget_opts][id_base]" value="<?php echo $widget->id;?>" /> |
| 79 | <div class="extended-widget-opts-form <?php if( $width > 480 ){ echo 'extended-widget-opts-form-large'; }else if( $width <= 480 ){ echo 'extended-widget-opts-form-small'; }?>"> |
| 80 | <div class="extended-widget-opts-tabs"> |
| 81 | <ul class="extended-widget-opts-tabnav-ul"> |
| 82 | <?php do_action( 'extended_widget_opts_tabs', $args );?> |
| 83 | <div class="extended-widget-opts-clearfix"></div> |
| 84 | </ul> |
| 85 | |
| 86 | <?php do_action( 'extended_widget_opts_tabcontent', $args );?> |
| 87 | <input type="hidden" id="extended-widget-opts-selectedtab" value="<?php echo $selected;?>" name="extended_widget_opts-<?php echo $args['id'];?>[extended_widget_opts][tabselect]" /> |
| 88 | <div class="extended-widget-opts-clearfix"></div> |
| 89 | </div><!-- end .extended-widget-opts-tabs --> |
| 90 | </div><!-- end .extended-widget-opts-form --> |
| 91 | |
| 92 | <?php if( 'activate' == $is_siteorigin ){?> |
| 93 | <script type="text/javascript"> |
| 94 | jQuery(document).ready(function($){ |
| 95 | if($('.so-content .extended-widget-opts-tabs').length > 0){ |
| 96 | $('.extended-widget-opts-tabs').tabs({ active: 1 }); |
| 97 | $('.extended-widget-opts-visibility-tabs').tabs({ active: 0 }); |
| 98 | $('.extended-widget-opts-settings-tabs').tabs({ active: 0 }); |
| 99 | } |
| 100 | }); |
| 101 | </script> |
| 102 | <?php } else{ ?> |
| 103 | <style type="text/css"> |
| 104 | .so-content.panel-dialog .extended-widget-opts-form{ display: none; } |
| 105 | </style> |
| 106 | <?php } ?> |
| 107 | |
| 108 | <?php |
| 109 | } |
| 110 | add_action( 'in_widget_form', 'widgetopts_in_widget_form', 10, 3 ); |
| 111 | |
| 112 | /* |
| 113 | * Update Options |
| 114 | */ |
| 115 | function widgetopts_ajax_update_callback( $instance, $new_instance, $this_widget){ |
| 116 | global $widget_options; |
| 117 | |
| 118 | if( isset($_POST['extended_widget_opts_name']) || |
| 119 | ( !isset( $_POST['extended_widget_opts_name'] ) && isset( $new_instance['siteorigin'] ) ) |
| 120 | ){ |
| 121 | //check if from SO pagebuilder |
| 122 | if( is_array( $new_instance ) && isset( $new_instance['extended_widget_opts'] ) && isset( $new_instance['siteorigin'] ) ){ |
| 123 | $name = 'extended_widget_opts'; |
| 124 | $options = widgetopts_sanitize_array( $new_instance ); |
| 125 | }else{ |
| 126 | $name = strip_tags( $_POST['extended_widget_opts_name'] ); |
| 127 | $options = $_POST[ $name ]; |
| 128 | } |
| 129 | if( isset( $options['extended_widget_opts'] ) ){ |
| 130 | // update_option( $name , $options['extended_widget_opts'] ); |
| 131 | if( isset( $options['extended_widget_opts']['class']['link'] ) && !empty( $options['extended_widget_opts']['class']['link'] ) ){ |
| 132 | $options['extended_widget_opts']['class']['link'] = widgetopts_addhttp( $options['extended_widget_opts']['class']['link'] ); |
| 133 | } |
| 134 | $instance[ $name ] = widgetopts_sanitize_array( $options['extended_widget_opts'] ); |
| 135 | |
| 136 | //remove cache |
| 137 | if( isset( $options['extended_widget_opts']['id_base'] ) && isset( $widget_options['cache'] ) && 'activate' == $widget_options['cache'] ){ |
| 138 | $transient_name = 'widgetopts-cache_'. $options['extended_widget_opts']['id_base']; |
| 139 | delete_transient( $transient_name ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return $instance; |
| 144 | } |
| 145 | add_filter( 'widget_update_callback', 'widgetopts_ajax_update_callback', 10, 3); |
| 146 | |
| 147 | ?> |
| 148 |