$this->widget_cssclass, 'description' => $this->widget_description ); 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 */ 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. * * @param array $args * @param string $content * @return string the content that was cached */ public function cache_widget( $args, $content ) { wp_cache_set( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), array( $args['widget_id'] => $content ), 'widget' ); return $content; } /** * Flush the cache. */ public function flush_widget_cache() { wp_cache_delete( apply_filters( 'woocommerce_cached_widget_id', $this->widget_id ), 'widget' ); } /** * Output the html at the start of a widget. * * @param array $args * @return string */ public function widget_start( $args, $instance ) { echo $args['before_widget']; if ( $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ) ) { echo $args['before_title'] . $title . $args['after_title']; } } /** * Output the html at the end of a widget. * * @param array $args * @return string */ public function widget_end( $args ) { echo $args['after_widget']; } /** * 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; 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 ] = is_null( $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 * @param array $instance */ public function form( $instance ) { if ( empty( $this->settings ) ) { return; } foreach ( $this->settings as $key => $setting ) { $class = isset( $setting['class'] ) ? $setting['class'] : ''; $value = isset( $instance[ $key ] ) ? $instance[ $key ] : $setting['std']; switch ( $setting['type'] ) { case 'text' : ?>

/>