woocommerce/includes/abstracts/abstract-wc-widget.php

356 lines
9.8 KiB
PHP
Raw Normal View History

<?php
2017-05-10 18:03:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
2015-11-03 13:53:50 +00:00
* Abstract Widget Class
*
* @author WooThemes
* @category Widgets
* @package WooCommerce/Abstracts
* @version 2.5.0
* @extends WP_Widget
*/
abstract class WC_Widget extends WP_Widget {
/**
* CSS class.
*
* @var string
*/
public $widget_cssclass;
/**
* Widget description.
*
* @var string
*/
public $widget_description;
/**
* Widget ID.
*
* @var string
*/
public $widget_id;
/**
* Widget name.
*
* @var string
*/
public $widget_name;
/**
* Settings.
*
* @var array
*/
public $settings;
/**
* Constructor.
*/
public function __construct() {
$widget_ops = array(
'classname' => $this->widget_cssclass,
'description' => $this->widget_description,
'customize_selective_refresh' => true,
);
parent::__construct( $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.
*
* @param array $args
* @return bool true if the widget is cached otherwise false
*/
2014-10-29 11:07:02 +00:00
public function get_cached_widget( $args ) {
$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
if ( ! is_array( $cache ) ) {
$cache = array();
}
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
*/
public function cache_widget( $args, $content ) {
2017-06-02 12:34:09 +00:00
$cache = wp_cache_get( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
if ( ! is_array( $cache ) ) {
$cache = array();
}
$cache[ $args['widget_id'] ] = $content;
wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), $cache, 'widget' );
2014-10-29 11:07:02 +00:00
return $content;
}
/**
* Flush the cache.
*/
public function flush_widget_cache() {
wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' );
}
2014-10-29 11:07:02 +00:00
/**
* Output the html at the start of a widget.
*
* @param array $args
* @param array $instance
2014-10-29 11:07:02 +00:00
*/
public function widget_start( $args, $instance ) {
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 ) ) {
echo $args['before_title'] . $title . $args['after_title'];
2014-10-29 11:07:02 +00:00
}
}
/**
* Output the html at the end of a widget.
*
2014-10-29 11:07:02 +00:00
* @param array $args
*/
public function widget_end( $args ) {
echo $args['after_widget'];
2014-10-29 11:07:02 +00:00
}
/**
* Updates a particular instance of a widget.
*
* @see WP_Widget->update
* @param array $new_instance
* @param array $old_instance
* @return array
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
2015-03-27 15:15:40 +00:00
if ( empty( $this->settings ) ) {
return $instance;
}
// Loop settings and get values to save.
foreach ( $this->settings as $key => $setting ) {
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' :
$instance[ $key ] = empty( $new_instance[ $key ] ) ? 0 : 1;
break;
default:
$instance[ $key ] = sanitize_text_field( $new_instance[ $key ] );
break;
}
/**
* Sanitize the value of a setting.
*/
$instance[ $key ] = apply_filters( 'woocommerce_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting );
}
$this->flush_widget_cache();
return $instance;
}
/**
* Outputs the settings update form.
*
* @see WP_Widget->form
2017-05-12 08:48:46 +00:00
*
* @param array $instance
*/
public function form( $instance ) {
2015-03-27 15:15:40 +00:00
if ( empty( $this->settings ) ) {
return;
}
foreach ( $this->settings as $key => $setting ) {
2015-12-02 10:02:57 +00:00
$class = isset( $setting['class'] ) ? $setting['class'] : '';
$value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std'];
switch ( $setting['type'] ) {
case 'text' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<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 ); ?>" />
</p>
<?php
break;
case 'number' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<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 ); ?>" />
</p>
<?php
break;
case 'select' :
?>
<p>
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
<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 ); ?>">
<?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;
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;
case 'checkbox' :
?>
<p>
<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 ); ?> />
<label for="<?php echo $this->get_field_id( $key ); ?>"><?php echo $setting['label']; ?></label>
</p>
<?php
break;
// Default: run an action
2015-12-14 12:48:03 +00:00
default :
do_action( 'woocommerce_widget_field_' . $setting['type'], $key, $value, $setting, $instance );
break;
}
}
}
2017-11-09 11:16:47 +00:00
/**
* Get current page URL with various filtering props supported by WC.
*
2017-12-14 09:23:51 +00:00
* @param string $taxonomy Taxonomy.
2017-11-09 11:16:47 +00:00
* @return string
*/
2017-12-14 09:23:51 +00:00
protected function get_page_base_url( $taxonomy ) {
wc_deprecated_function( 'WC_Widget::get_page_base_url', '3.3.0', 'WC_Widget::get_page_url' );
return $this->get_page_url();
}
2017-12-14 09:23:51 +00:00
/**
* Get current page URL with various filtering props supported by WC.
*
* @return string
*/
protected function get_page_url() {
2017-11-09 11:16:47 +00:00
if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
$link = home_url();
} elseif ( is_shop() ) {
$link = get_permalink( wc_get_page_id( 'shop' ) );
} elseif ( is_product_category() ) {
$link = get_term_link( get_query_var( 'product_cat' ), 'product_cat' );
} elseif ( is_product_tag() ) {
$link = get_term_link( get_query_var( 'product_tag' ), 'product_tag' );
} else {
$queried_object = get_queried_object();
$link = get_term_link( $queried_object->slug, $queried_object->taxonomy );
}
// Min/Max.
if ( isset( $_GET['min_price'] ) ) {
$link = add_query_arg( 'min_price', wc_clean( wp_unslash( $_GET['min_price'] ) ), $link );
}
if ( isset( $_GET['max_price'] ) ) {
$link = add_query_arg( 'max_price', wc_clean( wp_unslash( $_GET['max_price'] ) ), $link );
}
// Order by.
if ( isset( $_GET['orderby'] ) ) {
$link = add_query_arg( 'orderby', wc_clean( wp_unslash( $_GET['orderby'] ) ), $link );
}
/**
* Search Arg.
* To support quote characters, first they are decoded from &quot; entities, then URL encoded.
*/
if ( get_search_query() ) {
$link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link );
}
// Post Type Arg.
if ( isset( $_GET['post_type'] ) ) {
$link = add_query_arg( 'post_type', wc_clean( wp_unslash( $_GET['post_type'] ) ), $link );
}
// Min Rating Arg.
if ( isset( $_GET['rating_filter'] ) ) {
$link = add_query_arg( 'rating_filter', wc_clean( wp_unslash( $_GET['rating_filter'] ) ), $link );
}
// All current filters.
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
foreach ( $_chosen_attributes as $name => $data ) {
$filter_name = sanitize_title( str_replace( 'pa_', '', $name ) );
if ( ! empty( $data['terms'] ) ) {
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
}
if ( 'or' == $data['query_type'] ) {
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
}
}
}
return $link;
}
2014-09-02 19:50:19 +00:00
}