Passed
Push — add/multiplan ( 24f151...309a59 )
by Virginia
03:35
created

Meal::taxonomy_setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 26
rs 9.568
c 1
b 0
f 0
1
<?php
2
namespace lsx_health_plan\classes;
3
4
/**
5
 * Contains the day post type
6
 *
7
 * @package lsx-health-plan
8
 */
9
class Meal {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx_health_plan\classes\Meal()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * Holds post_type slug used as an index
22
	 *
23
	 * @since 1.0.0
24
	 *
25
	 * @var      string
26
	 */
27
	public $slug = 'meal';
28
29
	/**
30
	 * Contructor
31
	 */
32
	public function __construct() {
33
		add_action( 'init', array( $this, 'register_post_type' ) );
34
		add_action( 'init', array( $this, 'taxonomy_setup' ) );
35
		add_filter( 'lsx_health_plan_single_template', array( $this, 'enable_post_type' ), 10, 1 );
36
		add_filter( 'lsx_health_plan_connections', array( $this, 'enable_connections' ), 10, 1 );
37
		add_action( 'cmb2_admin_init', array( $this, 'featured_metabox' ), 5 );
38
		add_action( 'cmb2_admin_init', array( $this, 'details_metaboxes' ) );
39
	}
40
41
	/**
42
	 * Return an instance of this class.
43
	 *
44
	 * @since 1.0.0
45
	 *
46
	 * @return    object \lsx_health_plan\classes\Day()    A single instance of this class.
47
	 */
48
	public static function get_instance() {
49
		// If the single instance hasn't been set, set it now.
50
		if ( null === self::$instance ) {
51
			self::$instance = new self();
52
		}
53
		return self::$instance;
54
	}
55
	/**
56
	 * Register the post type.
57
	 */
58
	public function register_post_type() {
59
		$labels = array(
60
			'name'               => esc_html__( 'Meals', 'lsx-health-plan' ),
61
			'singular_name'      => esc_html__( 'Meal', 'lsx-health-plan' ),
62
			'add_new'            => esc_html_x( 'Add New', 'post type general name', 'lsx-health-plan' ),
63
			'add_new_item'       => esc_html__( 'Add New', 'lsx-health-plan' ),
64
			'edit_item'          => esc_html__( 'Edit', 'lsx-health-plan' ),
65
			'new_item'           => esc_html__( 'New', 'lsx-health-plan' ),
66
			'all_items'          => esc_html__( 'All Meals', 'lsx-health-plan' ),
67
			'view_item'          => esc_html__( 'View', 'lsx-health-plan' ),
68
			'search_items'       => esc_html__( 'Search', 'lsx-health-plan' ),
69
			'not_found'          => esc_html__( 'None found', 'lsx-health-plan' ),
70
			'not_found_in_trash' => esc_html__( 'None found in Trash', 'lsx-health-plan' ),
71
			'parent_item_colon'  => '',
72
			'menu_name'          => esc_html__( 'Meals', 'lsx-health-plan' ),
73
		);
74
		$args   = array(
75
			'labels'             => $labels,
76
			'public'             => true,
77
			'publicly_queryable' => true,
78
			'show_ui'            => true,
79
			'show_in_menu'       => true,
80
			'show_in_rest'       => true,
81
			'menu_icon'          => 'dashicons-carrot',
82
			'query_var'          => true,
83
			'rewrite'            => array(
84
				'slug' => \lsx_health_plan\functions\get_option( 'meal_single_slug', 'meal' ),
85
			),
86
			'capability_type'    => 'post',
87
			'has_archive'        => \lsx_health_plan\functions\get_option( 'endpoint_meal_archive', 'meals' ),
88
			'hierarchical'       => true,
89
			'menu_position'      => null,
90
			'supports'           => array(
91
				'title',
92
				'editor',
93
				'thumbnail',
94
				'page-attributes',
95
				'custom-fields',
96
			),
97
		);
98
		register_post_type( 'meal', $args );
99
	}
100
101
	/**
102
	 * Register the Week taxonomy.
103
	 */
104
	public function taxonomy_setup() {
105
		$labels = array(
106
			'name'              => esc_html_x( 'Meal Type', 'taxonomy general name', 'lsx-health-plan' ),
107
			'singular_name'     => esc_html_x( 'Meal Types', 'taxonomy singular name', 'lsx-health-plan' ),
108
			'search_items'      => esc_html__( 'Search', 'lsx-health-plan' ),
109
			'all_items'         => esc_html__( 'All', 'lsx-health-plan' ),
110
			'parent_item'       => esc_html__( 'Parent', 'lsx-health-plan' ),
111
			'parent_item_colon' => esc_html__( 'Parent:', 'lsx-health-plan' ),
112
			'edit_item'         => esc_html__( 'Edit', 'lsx-health-plan' ),
113
			'update_item'       => esc_html__( 'Update', 'lsx-health-plan' ),
114
			'add_new_item'      => esc_html__( 'Add New', 'lsx-health-plan' ),
115
			'new_item_name'     => esc_html__( 'New Name', 'lsx-health-plan' ),
116
			'menu_name'         => esc_html__( 'Meal Types', 'lsx-health-plan' ),
117
		);
118
		$args   = array(
119
			'hierarchical'      => true,
120
			'labels'            => $labels,
121
			'show_ui'           => true,
122
			'show_in_menu'      => 'edit.php?post_type=meal',
123
			'show_admin_column' => true,
124
			'query_var'         => true,
125
			'rewrite'           => array(
126
				'slug' => 'meal-type',
127
			),
128
		);
129
		register_taxonomy( 'meal-type', array( $this->slug ), $args );
130
	}
131
132
	/**
133
	 * Adds the post type to the different arrays.
134
	 *
135
	 * @param array $post_types
136
	 * @return array
137
	 */
138
	public function enable_post_type( $post_types = array() ) {
139
		$post_types[] = $this->slug;
140
		return $post_types;
141
	}
142
143
	/**
144
	 * Enables the Bi Directional relationships
145
	 *
146
	 * @param array $connections
147
	 * @return void
148
	 */
149
	public function enable_connections( $connections = array() ) {
150
		$connections['meal']['connected_plans'] = 'connected_meals';
151
		$connections['plan']['connected_meals'] = 'connected_plans';
152
		return $connections;
153
	}
154
155
	/**
156
	 * Define the metabox and field configurations.
157
	 */
158
	public function featured_metabox() {
159
		$cmb = new_cmb2_box(
160
			array(
161
				'id'           => $this->slug . '_featured_metabox_meal',
162
				'title'        => __( 'Featured Meal', 'lsx-health-plan' ),
163
				'object_types' => array( $this->slug ), // Post type
164
				'context'      => 'side',
165
				'priority'     => 'high',
166
				'show_names'   => true,
167
			)
168
		);
169
		$cmb->add_field(
170
			array(
171
				'name'       => __( 'Featured Meal', 'lsx-health-plan' ),
172
				'desc'       => __( 'Enable a featured meal' ),
173
				'id'         => $this->slug . '_featured_meal',
174
				'type'       => 'checkbox',
175
				'show_on_cb' => 'cmb2_hide_if_no_cats',
176
			)
177
		);
178
	}
179
180
	/**
181
	 * Define the metabox and field configurations.
182
	 */
183
	public function details_metaboxes() {
184
		$cmb = new_cmb2_box( array(
185
			'id'           => $this->slug . '_shopping_list_metabox',
186
			'title'        => __( 'Shopping List', 'lsx-health-plan' ),
187
			'object_types' => array( $this->slug ), // Post type
188
			'context'      => 'normal',
189
			'priority'     => 'high',
190
			'show_names'   => true,
191
		) );
192
		$cmb->add_field( array(
193
			'name'       => __( 'Shopping List', 'lsx-health-plan' ),
194
			'desc'       => __( 'Connect the shopping list page that applies to this meal by entering the name of the page in the field provided.' ),
195
			'id'         => $this->slug . '_shopping_list',
196
			'type'       => 'post_search_ajax',
197
			// Optional :
198
			'limit'      => 1,  // Limit selection to X items only (default 1)
199
			'sortable'   => true, // Allow selected items to be sortable (default false)
200
			'query_args' => array(
201
				'post_type'      => array( 'page' ),
202
				'post_status'    => array( 'publish' ),
203
				'posts_per_page' => -1,
204
			),
205
		) );
206
		$cmb = new_cmb2_box( array(
207
			'id'           => $this->slug . '_details_metabox',
208
			'title'        => __( 'Meal Details', 'lsx-health-plan' ),
209
			'object_types' => array( $this->slug ), // Post type
210
			'context'      => 'normal',
211
			'priority'     => 'high',
212
			'show_names'   => true,
213
		) );
214
215
		$cmb->add_field( array(
216
			'name' => __( 'Meal Short Description', 'lsx-health-plan' ),
217
			'id'   => $this->slug . '_short_description',
218
			'type' => 'textarea_small',
219
			'desc' => __( 'Add a small description for this meal (optional)', 'lsx-health-plan' ),
220
		) );
221
222
		$cmb->add_field( array(
223
			'name'       => __( 'Pre Breakfast Snack', 'lsx-health-plan' ),
224
			'id'         => $this->slug . '_pre_breakfast_snack',
225
			'type'       => 'wysiwyg',
226
			'show_on_cb' => 'cmb2_hide_if_no_cats',
227
			'options'    => array(
228
				'textarea_rows' => 5,
229
			),
230
		) );
231
		$cmb->add_field( array(
232
			'name'       => __( 'Breakfast', 'lsx-health-plan' ),
233
			'id'         => $this->slug . '_breakfast',
234
			'type'       => 'wysiwyg',
235
			'show_on_cb' => 'cmb2_hide_if_no_cats',
236
			'options'    => array(
237
				'textarea_rows' => 5,
238
			),
239
		) );
240
241
		$cmb->add_field(
242
			array(
243
				'name'       => __( 'Post Breakfast Snack', 'lsx-health-plan' ),
244
				'id'         => $this->slug . '_breakfast_snack',
245
				'type'       => 'wysiwyg',
246
				'show_on_cb' => 'cmb2_hide_if_no_cats',
247
				'options'    => array(
248
					'textarea_rows' => 5,
249
				),
250
			)
251
		);
252
253
		if ( post_type_exists( 'recipe' ) ) {
254
			$cmb->add_field(
255
				array(
256
					'name'       => __( 'Breakfast Recipes', 'lsx-health-plan' ),
257
					'desc'       => __( 'Connect additional recipes options for breakfast.', 'lsx-health-plan' ),
258
					'id'         => 'breakfast_recipes',
259
					'type'       => 'post_search_ajax',
260
					// Optional :
261
					'limit'      => 15,  // Limit selection to X items only (default 1)
262
					'sortable'   => true, // Allow selected items to be sortable (default false)
263
					'query_args' => array(
264
						'post_type'      => array( 'recipe' ),
265
						'post_status'    => array( 'publish' ),
266
						'posts_per_page' => -1,
267
					),
268
				)
269
			);
270
		}
271
272
		$cmb->add_field(
273
			array(
274
				'name'       => __( 'Pre Lunch Snack', 'lsx-health-plan' ),
275
				'id'         => $this->slug . '_pre_lunch_snack',
276
				'type'       => 'wysiwyg',
277
				'show_on_cb' => 'cmb2_hide_if_no_cats',
278
				'options'    => array(
279
					'textarea_rows' => 5,
280
				),
281
			)
282
		);
283
		$cmb->add_field(
284
			array(
285
				'name'       => __( 'Lunch', 'lsx-health-plan' ),
286
				'id'         => $this->slug . '_lunch',
287
				'type'       => 'wysiwyg',
288
				'show_on_cb' => 'cmb2_hide_if_no_cats',
289
				'options'    => array(
290
					'textarea_rows' => 5,
291
				),
292
			)
293
		);
294
		$cmb->add_field(
295
			array(
296
				'name'       => __( 'Post Lunch Snack', 'lsx-health-plan' ),
297
				'id'         => $this->slug . '_lunch_snack',
298
				'type'       => 'wysiwyg',
299
				'show_on_cb' => 'cmb2_hide_if_no_cats',
300
				'options'    => array(
301
					'textarea_rows' => 5,
302
				),
303
			)
304
		);
305
306
		if ( post_type_exists( 'recipe' ) ) {
307
			$cmb->add_field(
308
				array(
309
					'name'       => __( 'Lunch Recipes', 'lsx-health-plan' ),
310
					'desc'       => __( 'Connect additional recipes options for lunch.', 'lsx-health-plan' ),
311
					'id'         => 'lunch_recipes',
312
					'type'       => 'post_search_ajax',
313
					// Optional :
314
					'limit'      => 15,  // Limit selection to X items only (default 1)
315
					'sortable'   => true, // Allow selected items to be sortable (default false)
316
					'query_args' => array(
317
						'post_type'      => array( 'recipe' ),
318
						'post_status'    => array( 'publish' ),
319
						'posts_per_page' => -1,
320
					),
321
				)
322
			);
323
		}
324
325
		$cmb->add_field(
326
			array(
327
				'name'       => __( 'Pre Dinner Snack', 'lsx-health-plan' ),
328
				'id'         => $this->slug . '_pre_dinner_snack',
329
				'type'       => 'wysiwyg',
330
				'show_on_cb' => 'cmb2_hide_if_no_cats',
331
				'options'    => array(
332
					'textarea_rows' => 5,
333
				),
334
			)
335
		);
336
		$cmb->add_field(
337
			array(
338
				'name'       => __( 'Dinner', 'lsx-health-plan' ),
339
				'id'         => $this->slug . '_dinner',
340
				'type'       => 'wysiwyg',
341
				'show_on_cb' => 'cmb2_hide_if_no_cats',
342
				'options'    => array(
343
					'textarea_rows' => 5,
344
				),
345
			)
346
		);
347
		$cmb->add_field(
348
			array(
349
				'name'       => __( 'Post Dinner Snack', 'lsx-health-plan' ),
350
				'id'         => $this->slug . '_dinner_snack',
351
				'type'       => 'wysiwyg',
352
				'show_on_cb' => 'cmb2_hide_if_no_cats',
353
				'options'    => array(
354
					'textarea_rows' => 5,
355
				),
356
			)
357
		);
358
359
		if ( post_type_exists( 'recipe' ) ) {
360
			$cmb->add_field(
361
				array(
362
					'name'       => __( 'Dinner Recipes', 'lsx-health-plan' ),
363
					'desc'       => __( 'Connect additional recipes options for dinner.', 'lsx-health-plan' ),
364
					'id'         => 'dinner_recipes',
365
					'type'       => 'post_search_ajax',
366
					// Optional :
367
					'limit'      => 15,  // Limit selection to X items only (default 1)
368
					'sortable'   => true, // Allow selected items to be sortable (default false)
369
					'query_args' => array(
370
						'post_type'      => array( 'recipe' ),
371
						'post_status'    => array( 'publish' ),
372
						'posts_per_page' => -1,
373
					),
374
				)
375
			);
376
		}
377
	}
378
}
379