1: <?php
2: /**
3: * Define the Recipe Categories 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_Categories_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-recipe-categories';
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: __( 'Recipe Categories', Simmer()->domain ),
37: array(
38: 'classname' => $this->widget_slug . '-widget',
39: 'description' => __( 'A list of recipe categories', 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_categories_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_categories_widget_title', $instance['title'], $widget_id, $sidebar_id );
87:
88: if ( $title ) {
89: echo $args['before_title'] . $title . $args['after_title'];
90: }
91:
92: // Define the wp_list_categories args based on this widget instance's settings.
93: $list_args = array(
94: 'show_count' => $instance['show_count'],
95: 'hierarchical' => $instance['hierarchical'],
96: 'title_li' => false,
97: );
98:
99: /**
100: * Filter the wp_list_categories args for the widget.
101: *
102: * @since 1.1.0
103: *
104: * @param array $list_args The arguments.
105: * @param string $widget_id The instance's ID.
106: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
107: */
108: $list_args = apply_filters( 'simmer_categories_widget_list_args', $list_args, $widget_id, $sidebar_id );
109:
110: // Override the above filter to always set the taxonomy to display recipe categories.
111: $list_args['taxonomy'] = simmer_get_category_taxonomy();
112:
113: /**
114: * Execute before displaying the widget.
115: *
116: * @since 1.1.0
117: *
118: * @param string $widget_id The instance's ID.
119: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
120: */
121: do_action( 'simmer_before_categories_widget', $widget_id, $sidebar_id );
122:
123: // Output the main markup.
124: include( plugin_dir_path( __FILE__ ) . 'html/categories-widget.php' );
125:
126: /**
127: * Execute after displaying the widget.
128: *
129: * @since 1.1.0
130: *
131: * @param string $widget_id The instance's ID.
132: * @param string $sidebar_id The ID of the sidebar in which the instance is located.
133: */
134: do_action( 'simmer_after_categories_widget', $widget_id, $sidebar_id );
135:
136: // Close the wrapper.
137: echo $args['after_widget'];
138: }
139:
140: /**
141: * Set the new settings for the instance.
142: *
143: * @since 1.1.0
144: *
145: * @param array $new_instance The new settings.
146: * @param array $old_instance The old settings.
147: * @return array $instance The updated settings.
148: */
149: public function update( $new_instance, $old_instance ) {
150:
151: $instance = $old_instance;
152:
153: $instance['title'] = strip_tags( $new_instance['title'] );
154:
155: $instance['show_count'] = ! empty( $new_instance['show_count'] ) ? true : false;
156: $instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? true : false;
157:
158: return $instance;
159:
160: }
161:
162: /**
163: * Display the settings fields for the widget.
164: *
165: * @since 1.1.0
166: *
167: * @param array $instance The instance's settings.
168: */
169: public function form( $instance ) {
170:
171: $defaults = array(
172: 'title' => '',
173: 'show_count' => false,
174: 'hierarchical' => false,
175: );
176:
177: // Check the settings (or lack thereof) against the defaults.
178: $instance = wp_parse_args( (array) $instance, $defaults );
179:
180: // Output the fields.
181: include( plugin_dir_path( __FILE__ ) . 'html/categories-widget-form.php' );
182:
183: }
184: }
185: