'chef_random_recipes', 'description' => __( "Display random recipes", "chef") ); parent::__construct('random-recipes', __( "Chef: Random recipes", "chef"), $widget_ops); $this->alt_option_name = 'widget_random_recipes'; 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_recipes', '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'] : ''; /** This filter is documented in wp-includes/default-widgets.php */ $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 6; if ( ! $number ) $number = 6; $course = isset( $instance['course'] ) ? esc_attr($instance['course']) : ''; $cuisine = isset( $instance['cuisine'] ) ? esc_attr($instance['cuisine']) : ''; $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'post_type' => 'recipe', 'course' => $course, 'cuisine' => $cuisine, 'orderby' => 'rand' ) ) ); if ($r->have_posts()) : ?> have_posts() ) : $r->the_post(); ?>
is_preview() ) { $cache[ $args['widget_id'] ] = ob_get_flush(); wp_cache_set( 'widget_recipes', $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['course'] = strip_tags($new_instance['course']); $instance['cuisine'] = strip_tags($new_instance['cuisine']); $this->flush_widget_cache(); $alloptions = wp_cache_get( 'alloptions', 'options' ); if ( isset($alloptions['chef_recipes']) ) delete_option('chef_recipes'); return $instance; } public function flush_widget_cache() { wp_cache_delete('widget_recipes', 'widget'); } public function form( $instance ) { $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 6; $course = isset( $instance['course'] ) ? esc_attr( $instance['course'] ) : ''; $cuisine = isset( $instance['cuisine'] ) ? esc_attr( $instance['cuisine'] ) : ''; ?>