1: <?php
2: /**
3: * Define the Recent Recipes widget
4: *
5: * @since 1.1.0
6: *
7: * @package Simmer\Widgets
8: */
9:
10: // Die if this file is called directly.
11: if ( ! defined( 'WPINC' ) ) {
12: die;
13: }
14:
15: class Simmer_Recent_Recipes_Widget extends WP_Widget {
16:
17: /**
18: * Unique identifier for the widget.
19: *
20: * @since 1.1.0
21: *
22: * @var string
23: */
24: protected $widget_slug = 'simmer-recent-recipes';
25:
26: /**
27: * Construct the widget.
28: *
29: * @since 1.1.0
30: * @see WP_Widget
31: */
32: public function __construct() {
33:
34: parent::__construct(
35: $this->widget_slug,
36: __( 'Recent Recipes', Simmer()->domain ),
37: array(
38: 'classname' => $this->widget_slug . '-widget',
39: 'description' => __( "Your site's most recent recipes", Simmer()->domain ),
40: )
41: );
42:
43: }
44:
45: /**
46: * Display the widget on the front end.
47: *
48: * @since 1.1.0
49: *
50: * @param array $args The sidebar args for the instance.
51: * @param array $instance The instance and its settings.
52: */
53: public function widget( $args, $instance ) {
54:
55: if ( ! isset( $args['widget_id'] ) ) {
56: $widget_id = $this->id;
57: } else {
58: $widget_id = $args['widget_id'];
59: }
60:
61: $sidebar_id = $args['id'];
62:
63: // Output the wrapper.
64: echo $args['before_widget'];
65:
66: /**
67: * Filter the settings for the instance.
68: *
69: * @since 1.1.0
70: *
71: * @param array $instance The instance's settings.
72: * @param string $widget_id The instance's ID.
73: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
74: */
75: $instance = apply_filters( 'simmer_recent_recipes_widget_settings', $instance, $widget_id, $sidebar_id );
76:
77: /**
78: * Filter the title for the instance.
79: *
80: * @since 1.1.0
81: *
82: * @param string $title The instance's title.
83: * @param string $widget_id The instance's ID.
84: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
85: */
86: $title = apply_filters( 'simmer_recent_recipes_widget_title', $instance['title'], $widget_id, $sidebar_id );
87:
88: if ( $title ) {
89: echo $args['before_title'] . $title . $args['after_title'];
90: }
91:
92: // Set the recipe query args.
93: $query_args = array(
94: 'showposts' => (int) $instance['number'],
95: 'nopaging' => true,
96: 'no_found_rows' => true,
97: 'update_post_meta_cache' => false,
98: 'update_post_term_cache' => false,
99: );
100:
101: /**
102: * Filter the recipe query args for the instance.
103: *
104: * @since 1.1.0
105: *
106: * @param array $query_args The instance's recipe query args.
107: * @param string $widget_id The instance's ID.
108: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
109: */
110: $query_args = apply_filters( 'simmer_recent_recipes_widget_query_args', $query_args, $widget_id, $sidebar_id );
111:
112: // Override the above filter to ensure recipes are always queried.
113: $query_args['post_type'] = simmer_get_object_type();
114:
115: // Try to get the recipes.
116: $recent_recipes = new WP_Query( $query_args );
117:
118: /**
119: * Execute before displaying the widget.
120: *
121: * @since 1.1.0
122: *
123: * @param string $widget_id The instance's ID.
124: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
125: */
126: do_action( 'simmer_before_recent_recipes_widget', $widget_id, $sidebar_id );
127:
128: // Output the main markup.
129: include( plugin_dir_path( __FILE__ ) . 'html/recent-recipes-widget.php' );
130:
131: /**
132: * Execute after displaying the widget.
133: *
134: * @since 1.1.0
135: *
136: * @param string $widget_id The instance's ID.
137: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
138: */
139: do_action( 'simmer_after_recent_recipes_widget', $widget_id, $sidebar_id );
140:
141: // Close the wrapper.
142: echo $args['after_widget'];
143: }
144:
145: /**
146: * Set the new settings for the instance.
147: *
148: * @since 1.1.0
149: *
150: * @param array $new_instance The new settings.
151: * @param array $old_instance The old settings.
152: * @return array $instance The updated settings.
153: */
154: public function update( $new_instance, $old_instance ) {
155:
156: $instance = $old_instance;
157:
158: $instance['title'] = strip_tags( $new_instance['title'] );
159: $instance['number'] = (int) $new_instance['number'];
160:
161: $instance['show_dates'] = ! empty( $new_instance['show_dates'] ) ? true : false;
162:
163: return $instance;
164:
165: }
166:
167: /**
168: * Display the settings fields for the widget.
169: *
170: * @since 1.1.0
171: *
172: * @param array $instance The current instance's settings.
173: */
174: public function form( $instance ) {
175:
176: $defaults = array(
177: 'title' => '',
178: 'number' => 5,
179: 'show_dates' => false,
180: );
181:
182: // Check the settings (or lack thereof) against the defaults.
183: $instance = wp_parse_args( (array) $instance, $defaults );
184:
185: // Output the fields.
186: include( plugin_dir_path( __FILE__ ) . 'html/recent-recipes-widget-form.php' );
187:
188: }
189: }
190: