2013-05-24 15:51:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Abstract Widget Class
|
|
|
|
*
|
2014-08-31 05:49:58 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @version 2.1.0
|
|
|
|
* @extends WP_Widget
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
abstract class WC_Widget extends WP_Widget {
|
|
|
|
|
|
|
|
public $widget_cssclass;
|
|
|
|
public $widget_description;
|
|
|
|
public $widget_id;
|
|
|
|
public $widget_name;
|
|
|
|
public $settings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$widget_ops = array(
|
|
|
|
'classname' => $this->widget_cssclass,
|
|
|
|
'description' => $this->widget_description
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->WP_Widget( $this->widget_id, $this->widget_name, $widget_ops );
|
|
|
|
|
|
|
|
add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
|
|
|
|
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
|
|
|
|
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_cached_widget function.
|
|
|
|
*/
|
2014-10-29 11:07:02 +00:00
|
|
|
public function get_cached_widget( $args ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2014-06-06 16:34:12 +00:00
|
|
|
$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
if ( ! is_array( $cache ) ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
$cache = array();
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
if ( isset( $cache[ $args['widget_id'] ] ) ) {
|
|
|
|
echo $cache[ $args['widget_id'] ];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache the widget
|
2014-10-29 11:07:02 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param string $content
|
|
|
|
* @return string the content that was cached
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
public function cache_widget( $args, $content ) {
|
2014-10-29 11:07:02 +00:00
|
|
|
wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
return $content;
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flush the cache
|
2014-09-02 19:50:19 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
public function flush_widget_cache() {
|
2014-06-06 16:34:12 +00:00
|
|
|
wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 11:07:02 +00:00
|
|
|
/**
|
|
|
|
* Output the html at the start of a widget
|
|
|
|
* @param array $args
|
|
|
|
*/
|
|
|
|
public function widget_start( $args, $instance ) {
|
|
|
|
extract( $args );
|
|
|
|
|
|
|
|
if ( $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) ) {
|
|
|
|
echo $before_widget . $before_title . $title . $after_title;
|
|
|
|
} else {
|
|
|
|
echo $before_widget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the html at the end of a widget
|
|
|
|
* @param array $args
|
|
|
|
*/
|
|
|
|
public function widget_end( $args ) {
|
|
|
|
extract( $args );
|
|
|
|
|
|
|
|
echo $after_widget;
|
|
|
|
}
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
/**
|
|
|
|
* update function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget->update
|
|
|
|
* @param array $new_instance
|
|
|
|
* @param array $old_instance
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function update( $new_instance, $old_instance ) {
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$instance = $old_instance;
|
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
if ( ! $this->settings ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
return $instance;
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
foreach ( $this->settings as $key => $setting ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2014-02-12 09:29:47 +00:00
|
|
|
if ( isset( $new_instance[ $key ] ) ) {
|
2013-11-14 02:03:46 +00:00
|
|
|
$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
|
2014-02-12 09:29:47 +00:00
|
|
|
} elseif ( 'checkbox' === $setting['type'] ) {
|
|
|
|
$instance[ $key ] = 0;
|
2013-11-14 19:31:08 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->flush_widget_cache();
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* form function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget->form
|
|
|
|
* @param array $instance
|
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function form( $instance ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
if ( ! $this->settings ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
return;
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
foreach ( $this->settings as $key => $setting ) {
|
|
|
|
|
|
|
|
$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
|
|
|
|
|
|
|
|
switch ( $setting['type'] ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
case "text" :
|
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="text" value="<?php echo esc_attr( $value ); ?>" />
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
case "number" :
|
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" type="number" step="<?php echo esc_attr( $setting['step'] ); ?>" min="<?php echo esc_attr( $setting['min'] ); ?>" max="<?php echo esc_attr( $setting['max'] ); ?>" value="<?php echo esc_attr( $value ); ?>" />
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
case "select" :
|
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>">
|
|
|
|
<?php foreach ( $setting['options'] as $option_key => $option_value ) : ?>
|
|
|
|
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $option_key, $value ); ?>><?php echo esc_html( $option_value ); ?></option>
|
|
|
|
<?php endforeach; ?>
|
|
|
|
</select>
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
case "checkbox" :
|
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<input id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $key ) ); ?>" type="checkbox" value="1" <?php checked( $value, 1 ); ?> />
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 19:50:19 +00:00
|
|
|
}
|