'bistro_menu_a', 'description' => __( "Show menu items from a particular tag", "bistro") ); parent::__construct('menu-type-a', __( "Bistro: Menu Type A", "bistro"), $widget_ops); $this->alt_option_name = 'widget_menu_a'; 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') ); } public function widget($args, $instance) { $cache = array(); if ( ! $this->is_preview() ) { $cache = wp_cache_get( 'widget_menu_tags', 'widget' ); } if ( ! is_array( $cache ) ) { $cache = array(); } if ( ! isset( $args['widget_id'] ) ) { $args['widget_id'] = $this->id; } if ( isset( $cache[ $args['widget_id'] ] ) ) { echo $cache[ $args['widget_id'] ]; return; } ob_start(); $title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : ''; $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 4; if ( ! $number ) $number = 4; $tag = isset( $instance['tag'] ) ? esc_attr($instance['tag']) : ''; $price = isset( $instance['price'] ) ? esc_attr($instance['price']) : ''; $r = new WP_Query( array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post_type' => 'restaurant_item', 'restaurant_tag' => $tag, ) ); if ($r->have_posts()) : ?> have_posts() ) : $r->the_post(); ?>
is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'widget_menu_tags', $cache, 'widget' ); } else { ob_end_flush(); } } public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; $instance['tag'] = strip_tags($new_instance['tag']); $instance['price'] = isset( $new_instance['price'] ) ? (bool) $new_instance['price'] : false; $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['bistro_menu_tags']) ) delete_option('bistro_menu_tags'); return $instance; } public function flush_widget_cache() { wp_cache_delete('widget_menu_tags', 'widget'); } public function form( $instance ) { $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 4; $tag = isset( $instance['tag'] ) ? esc_attr( $instance['tag'] ) : ''; $price = isset( $instance['price'] ) ? esc_attr( $instance['price'] ) : ''; ?>
id="get_field_id( 'price' ); ?>" name="get_field_name( 'price' ); ?>" />