widget.php
80 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Compare Products widget |
| 4 | */ |
| 5 | class BeRocket_Compare_Products_Widget extends WP_Widget |
| 6 | { |
| 7 | public static $defaults = array( |
| 8 | 'fast_compare' => '', |
| 9 | 'title' => '', |
| 10 | 'type' => 'image', |
| 11 | 'toolbar' => '', |
| 12 | ); |
| 13 | public function __construct() { |
| 14 | parent::__construct("berocket_compare_products_widget", "WooCommerce Products Compare", |
| 15 | array("description" => "WooCommerce Products Compare List")); |
| 16 | } |
| 17 | /** |
| 18 | * WordPress widget for display Compare Products |
| 19 | */ |
| 20 | public function widget($args, $instance) |
| 21 | { |
| 22 | $BeRocket_Compare_Products = BeRocket_Compare_Products::getInstance(); |
| 23 | $instance = array_merge(self::$defaults, $instance); |
| 24 | $instance['title'] = apply_filters( 'widget_title', empty($instance['title']) ? '' : $instance['title'], $instance ); |
| 25 | $settings = array_merge( BeRocket_Compare_Products_Widget::$defaults, $instance ); |
| 26 | set_query_var( 'title', apply_filters( 'compare_products_widget_title', $settings['title'] ) ); |
| 27 | set_query_var( 'type', apply_filters( 'compare_products_widget_type', $settings['type'] ) ); |
| 28 | set_query_var( 'toolbar', apply_filters( 'compare_products_widget_toolbar', $settings['toolbar'] ) ); |
| 29 | set_query_var( 'fast_compare', apply_filters( 'compare_products_widget_fast_compare', $settings['fast_compare'] ) ); |
| 30 | echo $args['before_widget']; |
| 31 | $BeRocket_Compare_Products->br_get_template_part('selected_products'); |
| 32 | echo $args['after_widget']; |
| 33 | } |
| 34 | /** |
| 35 | * Update widget settings |
| 36 | */ |
| 37 | public function update( $new_instance, $old_instance ) { |
| 38 | $instance = $old_instance; |
| 39 | $instance['title'] = strip_tags( $new_instance['title'] ); |
| 40 | $instance['type'] = $new_instance['type']; |
| 41 | $instance['toolbar'] = ! empty($new_instance['toolbar']); |
| 42 | $instance['fast_compare'] = ! empty($new_instance['fast_compare']); |
| 43 | return $instance; |
| 44 | } |
| 45 | /** |
| 46 | * Widget settings form |
| 47 | */ |
| 48 | public function form($instance) |
| 49 | { |
| 50 | if( ! is_array($instance) ) { |
| 51 | $instance = array(); |
| 52 | } |
| 53 | $instance = array_merge(self::$defaults, $instance); |
| 54 | $title = strip_tags($instance['title']); |
| 55 | ?> |
| 56 | <p> |
| 57 | <label> |
| 58 | <input id="<?php echo $this->get_field_id('fast_compare'); ?>" name="<?php echo $this->get_field_name('fast_compare'); ?>" value="1" type="checkbox"<?php if ( ! empty($instance['fast_compare']) ) echo ' checked'; ?>> |
| 59 | <?php _e( 'Fast compare to load compare table via AJAX', 'products-compare-for-woocommerce' ) ?> |
| 60 | </label> |
| 61 | </p> |
| 62 | <p> |
| 63 | <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> |
| 64 | <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 65 | </p> |
| 66 | <p> |
| 67 | <select id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>"> |
| 68 | <option value="image" <?php if ( $instance['type'] == 'image' ) echo 'selected'; ?>><?php _e( 'Image', 'products-compare-for-woocommerce' ) ?></option> |
| 69 | <option value="text" <?php if ( $instance['type'] == 'text' ) echo 'selected'; ?>><?php _e( 'Text', 'products-compare-for-woocommerce' ) ?></option> |
| 70 | </select> |
| 71 | </p> |
| 72 | <p> |
| 73 | <input id="<?php echo $this->get_field_id('toolbar'); ?>" name="<?php echo $this->get_field_name('toolbar'); ?>" value="1" type="checkbox"<?php if ( ! empty($instance['toolbar']) ) echo ' checked'; ?>> |
| 74 | <label><?php _e( 'Is ToolBar', 'products-compare-for-woocommerce' ) ?></label> |
| 75 | </p> |
| 76 | <?php |
| 77 | } |
| 78 | } |
| 79 | ?> |
| 80 |