Passed
Push — add/multiplan ( 87a4af...b0643e )
by Virginia
03:55
created

Workout::featured_metabox()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.7666
1
<?php
2
namespace lsx_health_plan\classes;
3
4
use function lsx_health_plan\functions\get_option;
5
6
/**
7
 * Contains the workout post type
8
 *
9
 * @package lsx-health-plan
10
 */
11
class Workout {
12
13
	/**
14
	 * Holds class instance
15
	 *
16
	 * @since 1.0.0
17
	 *
18
	 * @var      object \lsx_health_plan\classes\Workout()
19
	 */
20
	protected static $instance = null;
21
22
	/**
23
	 * Holds post_type slug used as an index
24
	 *
25
	 * @since 1.0.0
26
	 *
27
	 * @var      string
28
	 */
29
	public $slug = 'workout';
30
31
	/**
32
	 * Contructor
33
	 */
34
	public function __construct() {
35
		add_action( 'init', array( $this, 'register_post_type' ) );
36
		add_filter( 'lsx_health_plan_single_template', array( $this, 'enable_post_type' ), 10, 1 );
37
		add_action( 'init', array( $this, 'recipe_type_taxonomy_setup' ) );
38
		add_filter( 'lsx_health_plan_connections', array( $this, 'enable_connections' ), 10, 1 );
39
		add_action( 'cmb2_admin_init', array( $this, 'featured_metabox' ), 5 );
40
		add_action( 'cmb2_admin_init', array( $this, 'details_metaboxes' ) );
41
		add_action( 'lsx_hp_settings_page', array( $this, 'register_settings' ), 8, 1 );
42
		add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 100 );
43
44
		// Template Redirects.
45
		add_filter( 'lsx_health_plan_archive_template', array( $this, 'enable_post_type' ), 10, 1 );
46
	}
47
48
	/**
49
	 * Return an instance of this class.
50
	 *
51
	 * @since 1.0.0
52
	 *
53
	 * @return    object \lsx_health_plan\classes\Workout()    A single instance of this class.
54
	 */
55
	public static function get_instance() {
56
		// If the single instance hasn't been set, set it now.
57
		if ( null === self::$instance ) {
58
			self::$instance = new self();
59
		}
60
		return self::$instance;
61
	}
62
	/**
63
	 * Register the post type.
64
	 */
65
	public function register_post_type() {
66
		$labels = array(
67
			'name'               => esc_html__( 'Workouts', 'lsx-health-plan' ),
68
			'singular_name'      => esc_html__( 'Workout', 'lsx-health-plan' ),
69
			'add_new'            => esc_html_x( 'Add New', 'post type general name', 'lsx-health-plan' ),
70
			'add_new_item'       => esc_html__( 'Add New', 'lsx-health-plan' ),
71
			'edit_item'          => esc_html__( 'Edit', 'lsx-health-plan' ),
72
			'new_item'           => esc_html__( 'New', 'lsx-health-plan' ),
73
			'all_items'          => esc_html__( 'All Workouts', 'lsx-health-plan' ),
74
			'view_item'          => esc_html__( 'View', 'lsx-health-plan' ),
75
			'search_items'       => esc_html__( 'Search', 'lsx-health-plan' ),
76
			'not_found'          => esc_html__( 'None found', 'lsx-health-plan' ),
77
			'not_found_in_trash' => esc_html__( 'None found in Trash', 'lsx-health-plan' ),
78
			'parent_item_colon'  => '',
79
			'menu_name'          => esc_html__( 'Workouts', 'lsx-health-plan' ),
80
		);
81
		$args = array(
82
			'labels'             => $labels,
83
			'public'             => true,
84
			'publicly_queryable' => true,
85
			'show_ui'            => true,
86
			'show_in_menu'       => true,
87
			'show_in_rest'       => true,
88
			'menu_icon'          => 'dashicons-universal-access',
89
			'query_var'          => true,
90
			'rewrite'            => array(
91
				'slug' => \lsx_health_plan\functions\get_option( 'endpoint_workout', 'workout' ),
92
			),
93
			'capability_type'    => 'page',
94
			'has_archive'        => \lsx_health_plan\functions\get_option( 'endpoint_workout_archive', 'workouts' ),
95
			'hierarchical'       => true,
96
			'menu_position'      => null,
97
			'supports'           => array(
98
				'title',
99
				'thumbnail',
100
				'editor',
101
				'excerpt',
102
				'page-attributes',
103
				'custom-fields',
104
			),
105
		);
106
		register_post_type( 'workout', $args );
107
	}
108
109
	/**
110
	 * Register the Type taxonomy.
111
	 */
112
	public function recipe_type_taxonomy_setup() {
113
		$labels = array(
114
			'name'              => esc_html_x( 'Workout Type', 'taxonomy general name', 'lsx-health-plan' ),
115
			'singular_name'     => esc_html_x( 'Workout Type', 'taxonomy singular name', 'lsx-health-plan' ),
116
			'search_items'      => esc_html__( 'Search', 'lsx-health-plan' ),
117
			'all_items'         => esc_html__( 'All', 'lsx-health-plan' ),
118
			'parent_item'       => esc_html__( 'Parent', 'lsx-health-plan' ),
119
			'parent_item_colon' => esc_html__( 'Parent:', 'lsx-health-plan' ),
120
			'edit_item'         => esc_html__( 'Edit', 'lsx-health-plan' ),
121
			'update_item'       => esc_html__( 'Update', 'lsx-health-plan' ),
122
			'add_new_item'      => esc_html__( 'Add New', 'lsx-health-plan' ),
123
			'new_item_name'     => esc_html__( 'New Name', 'lsx-health-plan' ),
124
			'menu_name'         => esc_html__( 'Workout Types', 'lsx-health-plan' ),
125
		);
126
127
		$args = array(
128
			'hierarchical'      => true,
129
			'labels'            => $labels,
130
			'show_ui'           => true,
131
			'show_admin_column' => true,
132
			'query_var'         => true,
133
			'rewrite'           => array(
134
				'slug' => 'workout-type',
135
			),
136
		);
137
138
		register_taxonomy( 'workout-type', array( 'workout' ), $args );
139
	}
140
141
	/**
142
	 * Adds the post type to the different arrays.
143
	 *
144
	 * @param array $post_types
145
	 * @return array
146
	 */
147
	public function enable_post_type( $post_types = array() ) {
148
		$post_types[] = $this->slug;
149
		return $post_types;
150
	}
151
152
	/**
153
	 * Enables the Bi Directional relationships
154
	 *
155
	 * @param array $connections
156
	 * @return void
157
	 */
158
	public function enable_connections( $connections = array() ) {
159
		$connections['workout']['connected_plans'] = 'connected_workouts';
160
		$connections['plan']['connected_workouts'] = 'connected_plans';
161
162
		$connections['workout']['connected_videos'] = 'connected_workouts';
163
		$connections['video']['connected_workouts'] = 'connected_videos';
164
165
		$connections['workout']['connected_posts'] = 'connected_workouts';
166
		$connections['post']['connected_workouts'] = 'connected_posts';
167
		return $connections;
168
	}
169
170
	/**
171
	 * Remove the "Archives:" from the post type workouts.
172
	 *
173
	 * @param string $title the term title.
174
	 * @return string
175
	 */
176
	public function get_the_archive_title( $title ) {
177
		if ( is_post_type_archive( 'workout' ) ) {
178
			$title = __( 'Workouts', 'lsx-health-plan' );
179
		}
180
		return $title;
181
	}
182
183
	/**
184
	 * Define the metabox and field configurations.
185
	 */
186
	public function featured_metabox() {
187
		$cmb = new_cmb2_box(
188
			array(
189
				'id'           => $this->slug . '_featured_metabox_workout',
190
				'title'        => __( 'Featured Workout', 'lsx-health-plan' ),
191
				'object_types' => array( $this->slug ), // Post type
192
				'context'      => 'side',
193
				'priority'     => 'high',
194
				'show_names'   => true,
195
			)
196
		);
197
		$cmb->add_field(
198
			array(
199
				'name'       => __( 'Featured Workout', 'lsx-health-plan' ),
200
				'desc'       => __( 'Enable a featured plan' ),
201
				'id'         => $this->slug . '_featured_workout',
202
				'type'       => 'checkbox',
203
				'show_on_cb' => 'cmb2_hide_if_no_cats',
204
			)
205
		);
206
	}
207
208
	/**
209
	 * Define the metabox and field configurations.
210
	 */
211
	public function details_metaboxes() {
212
213
		$cmb = new_cmb2_box( array(
214
			'id'           => $this->slug . '_details_metabox',
215
			'title'        => __( 'Workout Details', 'lsx-health-plan' ),
216
			'object_types' => array( $this->slug ), // Post type
217
			'context'      => 'normal',
218
			'priority'     => 'high',
219
			'show_names'   => true,
220
		) );
221
222
		$cmb->add_field( array(
223
			'name' => __( 'Workout Short Description', 'lsx-health-plan' ),
224
			'id'   => $this->slug . '_short_description',
225
			'type' => 'textarea_small',
226
			'desc' => __( 'Add a small description for this workout (optional)', 'lsx-health-plan' ),
227
		) );
228
229
		$workout_sections = apply_filters( 'lsx_health_plan_workout_sections_amount', 6 );
230
		if ( false !== $workout_sections && null !== $workout_sections ) {
231
			$i = 1;
232
			while ( $i <= $workout_sections ) {
233
234
				$cmb_group = new_cmb2_box( array(
235
					'id'           => $this->slug . '_section_' . $i . '_metabox',
236
					'title'        => esc_html__( 'Exercise Group ', 'lsx-health-plan' ) . $i,
237
					'object_types' => array( $this->slug ),
238
				) );
239
240
				$cmb_group->add_field( array(
241
					'name'       => __( 'Title', 'lsx-health-plan' ),
242
					'id'         => $this->slug . '_section_' . $i . '_title',
243
					'type'       => 'text',
244
					'show_on_cb' => 'cmb2_hide_if_no_cats',
245
				) );
246
247
				$cmb_group->add_field(
248
					array(
249
						'name'       => __( 'Description', 'lsx-health-plan' ),
250
						'id'         => $this->slug . '_section_' . $i . '_description',
251
						'type'       => 'wysiwyg',
252
						'show_on_cb' => 'cmb2_hide_if_no_cats',
253
						'options'    => array(
254
							'textarea_rows' => 5,
255
						),
256
					)
257
				);
258
259
				/**
260
				 * Repeatable Field Groups
261
				 */
262
				// $group_field_id is the field id string, so in this case: $prefix . 'demo'
263
				$group_field_id = $cmb_group->add_field(
264
					array(
265
						'id'      => $this->slug . '_section_' . $i,
266
						'type'    => 'group',
267
						'options' => array(
268
							'group_title'   => esc_html__( 'Exercise {#}', 'lsx-health-plan' ), // {#} gets replaced by row number
269
							'add_button'    => esc_html__( 'Add New', 'lsx-health-plan' ),
270
							'remove_button' => esc_html__( 'Delete', 'lsx-health-plan' ),
271
							'sortable'      => true,
272
						),
273
					)
274
				);
275
276
				if ( false !== \lsx_health_plan\functions\get_option( 'exercise_enabled', false ) ) {
277
					$cmb_group->add_group_field(
278
						$group_field_id,
0 ignored issues
show
Bug introduced by
It seems like $group_field_id can also be of type false; however, parameter $parent_field_id of CMB2::add_group_field() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

278
						/** @scrutinizer ignore-type */ $group_field_id,
Loading history...
279
						array(
280
							'name'       => __( 'Exercise related to this workout', 'lsx-health-plan' ),
281
							'id'         => 'connected_exercises',
282
							'type'       => 'post_search_ajax',
283
							// Optional :
284
							'limit'      => 1, // Limit selection to X items only (default 1)
285
							'sortable'   => true,  // Allow selected items to be sortable (default false)
286
							'query_args' => array(
287
								'post_type'      => array( 'exercise' ),
288
								'post_status'    => array( 'publish' ),
289
								'posts_per_page' => -1,
290
							),
291
						)
292
					);
293
				} else {
294
					$cmb_group->add_group_field(
295
						$group_field_id,
296
						array(
297
							'name'       => __( 'Video related to this workout', 'lsx-health-plan' ),
298
							'id'         => 'connected_videos',
299
							'type'       => 'post_search_ajax',
300
							// Optional :
301
							'limit'      => 1, // Limit selection to X items only (default 1)
302
							'sortable'   => true,  // Allow selected items to be sortable (default false)
303
							'query_args' => array(
304
								'post_type'      => array( 'video' ),
305
								'post_status'    => array( 'publish' ),
306
								'posts_per_page' => -1,
307
							),
308
						)
309
					);
310
					$cmb_group->add_group_field(
311
						$group_field_id,
312
						array(
313
							'name' => esc_html__( 'Workout Name', 'lsx-health-plan' ),
314
							'id'   => 'name',
315
							'type' => 'text',
316
							// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
317
						)
318
					);
319
320
					$cmb_group->add_group_field(
321
						$group_field_id,
322
						array(
323
							'name'    => __( 'Description', 'lsx-health-plan' ),
324
							'id'      => 'description',
325
							'type'    => 'wysiwyg',
326
							'options' => array(
327
								'textarea_rows' => 2,
328
							),
329
						)
330
					);
331
				}
332
333
				$cmb_group->add_group_field(
334
					$group_field_id,
335
					array(
336
						'name' => esc_html__( 'Exercise title (Optional)', 'lsx-health-plan' ),
337
						'id'   => 'alt_title',
338
						'type' => 'text',
339
					)
340
				);
341
				$cmb_group->add_group_field(
342
					$group_field_id,
343
					array(
344
						'name' => esc_html__( 'Exercise Description (Optional)', 'lsx-health-plan' ),
345
						'id'   => 'alt_description',
346
						'type' => 'textarea_small',
347
					)
348
				);
349
				$cmb_group->add_group_field(
350
					$group_field_id,
351
					array(
352
						'name' => esc_html__( 'Reps / Time / Distance', 'lsx-health-plan' ),
353
						'id'   => 'reps',
354
						'type' => 'text',
355
						// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
356
					)
357
				);
358
				$cmb_group->add_group_field(
359
					$group_field_id,
360
					array(
361
						'name'         => __( 'Exercise Image (Optional)', 'lsx-health-plan' ),
362
						'id'           => 'exercise_alt_thumbnail',
363
						'type'         => 'file',
364
						'text'         => array(
365
							'add_upload_file_text' => __( 'Add File', 'lsx-health-plan' ),
366
						),
367
						'desc'         => __( 'Upload an image 300px x 300px in size.', 'lsx-health-plan' ),
368
						'query_args'   => array(
369
							'type' => array(
370
								'image/gif',
371
								'image/jpeg',
372
								'image/png',
373
							),
374
						),
375
						'preview_size' => 'thumbnail',
376
						'classes'      => 'lsx-field-col lsx-field-add-field  lsx-field-col-25',
377
					)
378
				);
379
380
				$i++;
381
			};
382
		}
383
	}
384
385
	/**
386
	 * Registers the lsx_search_settings
387
	 *
388
	 * @param object $cmb new_cmb2_box().
389
	 * @return void
390
	 */
391
	public function register_settings( $cmb ) {
392
		if ( false !== \lsx_health_plan\functions\get_option( 'exercise_enabled', false ) ) {
393
			$cmb->add_field(
394
				array(
395
					'id'          => 'workout_settings_title',
396
					'type'        => 'title',
397
					'name'        => __( 'Workout Settings', 'lsx-health-plan' ),
398
					'description' => __( 'Choose the layout, content and link settings for your exercises.', 'lsx-health-plan' ),
399
				)
400
			);
401
402
			$cmb->add_field(
403
				array(
404
					'id'          => 'workout_tab_layout',
405
					'type'        => 'select',
406
					'name'        => __( 'Workout Tab Layout', 'lsx-health-plan' ),
407
					'description' => __( 'Choose the layout for the workouts.', 'lsx-health-plan' ),
408
					'options'     => array(
409
						'table' => __( 'Table', 'lsx-health-plan' ),
410
						'list'  => __( 'List', 'lsx-health-plan' ),
411
						'grid'  => __( 'Grid', 'lsx-health-plan' ),
412
					),
413
				)
414
			);
415
			$cmb->add_field(
416
				array(
417
					'id'          => 'workout_tab_link',
418
					'type'        => 'select',
419
					'name'        => __( 'Workout Tab Link', 'lsx-health-plan' ),
420
					'description' => __( 'Choose to show the excerpt, full content or nothing.', 'lsx-health-plan' ),
421
					'options'     => array(
422
						''       => __( 'None', 'lsx-health-plan' ),
423
						'single' => __( 'Single', 'lsx-health-plan' ),
424
						'modal'  => __( 'Modal', 'lsx-health-plan' ),
425
					),
426
					'default' => 'modal',
427
				)
428
			);
429
			$cmb->add_field(
430
				array(
431
					'id'          => 'workout_tab_modal_content',
432
					'type'        => 'select',
433
					'name'        => __( 'Modal Content', 'lsx-health-plan' ),
434
					'description' => __( 'Choose to show the excerpt, full content or nothing. For the modal content only', 'lsx-health-plan' ),
435
					'options'     => array(
436
						''        => __( 'None', 'lsx-health-plan' ),
437
						'excerpt' => __( 'Excerpt', 'lsx-health-plan' ),
438
						'full'    => __( 'Full Content', 'lsx-health-plan' ),
439
					),
440
					'default' => '',
441
				)
442
			);
443
			$cmb->add_field(
444
				array(
445
					'id'          => 'workout_tab_columns',
446
					'type'        => 'select',
447
					'name'        => __( 'Grid Columns', 'lsx-health-plan' ),
448
					'description' => __( 'If you are displaying a grid, set the amount of columns you want to use.', 'lsx-health-plan' ),
449
					'options'     => array(
450
						'12' => __( '1', 'lsx-health-plan' ),
451
						'6'  => __( '2', 'lsx-health-plan' ),
452
						'4'  => __( '3', 'lsx-health-plan' ),
453
						'3'  => __( '4', 'lsx-health-plan' ),
454
						'2'  => __( '6', 'lsx-health-plan' ),
455
					),
456
					'default' => '4',
457
				)
458
			);
459
			$cmb->add_field(
460
				array(
461
					'id'          => 'workout_tab_content',
462
					'type'        => 'select',
463
					'name'        => __( 'Grid Content', 'lsx-health-plan' ),
464
					'description' => __( 'Choose to show the excerpt, full content or nothing. For the grid layout only', 'lsx-health-plan' ),
465
					'options'     => array(
466
						''        => __( 'None', 'lsx-health-plan' ),
467
						'excerpt' => __( 'Excerpt', 'lsx-health-plan' ),
468
						'full'    => __( 'Full Content', 'lsx-health-plan' ),
469
					),
470
					'default' => '',
471
				)
472
			);
473
474
			do_action( 'lsx_hp_workout_settings_page', $cmb );
475
476
			$cmb->add_field(
477
				array(
478
					'id'   => 'settings_workouts_closing',
479
					'type' => 'tab_closing',
480
				)
481
			);
482
		}
483
	}
484
}
485