2013-05-24 15:51:58 +00:00
|
|
|
<?php
|
2017-05-10 18:03:19 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Abstract Widget Class
|
2013-05-24 15:51:58 +00:00
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Abstracts
|
2015-11-30 06:26:29 +00:00
|
|
|
* @version 2.5.0
|
2014-11-15 01:12:59 +00:00
|
|
|
* @extends WP_Widget
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
abstract class WC_Widget extends WP_Widget {
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* CSS class.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $widget_cssclass;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Widget description.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $widget_description;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Widget ID.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $widget_id;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Widget name.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $widget_name;
|
2014-11-15 01:12:59 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Settings.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public $settings;
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Constructor.
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$widget_ops = array(
|
|
|
|
'classname' => $this->widget_cssclass,
|
2016-05-21 13:22:03 +00:00
|
|
|
'description' => $this->widget_description,
|
2016-08-27 01:46:45 +00:00
|
|
|
'customize_selective_refresh' => true,
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
|
|
|
|
2015-06-29 18:46:03 +00:00
|
|
|
parent::__construct( $this->widget_id, $this->widget_name, $widget_ops );
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
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' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-04 14:37:12 +00:00
|
|
|
* Get cached widget.
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return bool true if the widget is cached otherwise false
|
2013-05-24 15:51:58 +00:00
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Cache the widget.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Flush the cache.
|
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
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output the html at the start of a widget.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2014-10-29 11:07:02 +00:00
|
|
|
* @param array $args
|
2017-05-15 11:50:52 +00:00
|
|
|
* @param array $instance
|
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @return string
|
2014-10-29 11:07:02 +00:00
|
|
|
*/
|
|
|
|
public function widget_start( $args, $instance ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
echo $args['before_widget'];
|
2014-10-29 11:07:02 +00:00
|
|
|
|
2015-01-02 13:05:06 +00:00
|
|
|
if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
echo $args['before_title'] . $title . $args['after_title'];
|
2014-10-29 11:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 12:28:01 +00:00
|
|
|
* Output the html at the end of a widget.
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2014-10-29 11:07:02 +00:00
|
|
|
* @param array $args
|
2014-11-15 01:12:59 +00:00
|
|
|
* @return string
|
2014-10-29 11:07:02 +00:00
|
|
|
*/
|
|
|
|
public function widget_end( $args ) {
|
2014-11-15 01:12:59 +00:00
|
|
|
echo $args['after_widget'];
|
2014-10-29 11:07:02 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
/**
|
2016-01-04 14:37:12 +00:00
|
|
|
* Updates a particular instance of a widget.
|
2013-05-24 15:51:58 +00:00
|
|
|
*
|
2015-11-30 06:26:29 +00:00
|
|
|
* @see WP_Widget->update
|
|
|
|
* @param array $new_instance
|
|
|
|
* @param array $old_instance
|
2013-05-24 15:51:58 +00:00
|
|
|
* @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;
|
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
if ( empty( $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
|
|
|
|
2015-12-04 23:40:51 +00:00
|
|
|
// Loop settings and get values to save.
|
2013-05-24 15:51:58 +00:00
|
|
|
foreach ( $this->settings as $key => $setting ) {
|
2015-12-04 23:40:51 +00:00
|
|
|
if ( ! isset( $setting['type'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the value based on settings type.
|
|
|
|
switch ( $setting['type'] ) {
|
|
|
|
case 'number' :
|
|
|
|
$instance[ $key ] = absint( $new_instance[ $key ] );
|
|
|
|
|
|
|
|
if ( isset( $setting['min'] ) && '' !== $setting['min'] ) {
|
|
|
|
$instance[ $key ] = max( $instance[ $key ], $setting['min'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $setting['max'] ) && '' !== $setting['max'] ) {
|
|
|
|
$instance[ $key ] = min( $instance[ $key ], $setting['max'] );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'textarea' :
|
|
|
|
$instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) );
|
|
|
|
break;
|
|
|
|
case 'checkbox' :
|
2016-05-19 10:47:05 +00:00
|
|
|
$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
|
2015-12-04 23:40:51 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
|
|
|
|
break;
|
2013-11-14 19:31:08 +00:00
|
|
|
}
|
2015-12-08 10:34:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize the value of a setting.
|
|
|
|
*/
|
|
|
|
$instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->flush_widget_cache();
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-04 14:37:12 +00:00
|
|
|
* Outputs the settings update form.
|
2013-05-24 15:51:58 +00:00
|
|
|
*
|
2015-11-30 06:26:29 +00:00
|
|
|
* @see WP_Widget->form
|
2017-05-12 08:48:46 +00:00
|
|
|
*
|
2013-05-24 15:51:58 +00:00
|
|
|
* @param array $instance
|
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function form( $instance ) {
|
2013-05-24 15:51:58 +00:00
|
|
|
|
2015-03-27 15:15:40 +00:00
|
|
|
if ( empty( $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 ) {
|
|
|
|
|
2015-12-02 10:02:57 +00:00
|
|
|
$class = isset( $setting['class'] ) ? $setting['class'] : '';
|
2014-11-15 01:12:59 +00:00
|
|
|
$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
|
2013-05-24 15:51:58 +00:00
|
|
|
|
|
|
|
switch ( $setting['type'] ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
case 'text' :
|
2013-05-24 15:51:58 +00:00
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
2015-11-30 06:26:29 +00:00
|
|
|
<input class="widefat <?php echo esc_attr( $class ); ?>" 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 ); ?>" />
|
2013-05-24 15:51:58 +00:00
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
case 'number' :
|
2013-05-24 15:51:58 +00:00
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
2015-11-30 06:26:29 +00:00
|
|
|
<input class="widefat <?php echo esc_attr( $class ); ?>" 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 ); ?>" />
|
2013-05-24 15:51:58 +00:00
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
case 'select' :
|
2013-05-24 15:51:58 +00:00
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
2015-11-30 06:26:29 +00:00
|
|
|
<select class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>">
|
2013-05-24 15:51:58 +00:00
|
|
|
<?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
|
|
|
|
2015-12-04 15:38:19 +00:00
|
|
|
case 'textarea' :
|
|
|
|
?>
|
|
|
|
<p>
|
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
<textarea class="widefat <?php echo esc_attr( $class ); ?>" id="<?php echo esc_attr( $this->get_field_id( $key ) ); ?>" name="<?php echo $this->get_field_name( $key ); ?>" cols="20" rows="3"><?php echo esc_textarea( $value ); ?></textarea>
|
|
|
|
<?php if ( isset( $setting['desc'] ) ) : ?>
|
|
|
|
<small><?php echo esc_html( $setting['desc'] ); ?></small>
|
|
|
|
<?php endif; ?>
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
case 'checkbox' :
|
2013-05-24 15:51:58 +00:00
|
|
|
?>
|
|
|
|
<p>
|
2015-12-04 23:41:28 +00:00
|
|
|
<input class="checkbox <?php echo esc_attr( $class ); ?>" 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 ); ?> />
|
2013-05-24 15:51:58 +00:00
|
|
|
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
|
|
|
|
</p>
|
|
|
|
<?php
|
|
|
|
break;
|
2015-12-08 10:34:49 +00:00
|
|
|
|
|
|
|
// Default: run an action
|
2015-12-14 12:48:03 +00:00
|
|
|
default :
|
|
|
|
do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
|
2015-12-08 10:34:49 +00:00
|
|
|
break;
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-09-02 19:50:19 +00:00
|
|
|
}
|