Passed
Push — master ( 43702f...903e32 )
by Warwick
04:44 queued 10s
created

Workout::set_parent_only()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 9.6111
1
<?php
0 ignored issues
show
Coding Style introduced by
This file is missing a doc comment.
Loading history...
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
	 * Constructor
33
	 */
34
	public function __construct() {
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
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, 'workout_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_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 100 );
42
43
		// Template Redirects.
44
		add_action( 'pre_get_posts', array( $this, 'set_parent_only' ), 10, 1 );
45
		add_filter( 'lsx_health_plan_archive_template', array( $this, 'enable_post_type' ), 10, 1 );
46
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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 ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
58
			self::$instance = new self();
59
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
60
		return self::$instance;
61
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
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'  => esc_html__( 'Parent:', 'lsx-health-plan' ),
79
			'menu_name'          => esc_html__( 'Workouts', 'lsx-health-plan' ),
80
		);
81
		$args = array(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
108
109
	/**
110
	 * Register the Type taxonomy.
111
	 */
112
	public function workout_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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
140
141
	/**
142
	 * Adds the post type to the different arrays.
143
	 *
144
	 * @param array $post_types
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
145
	 * @return array
146
	 */
147
	public function enable_post_type( $post_types = array() ) {
148
		$post_types[] = $this->slug;
149
		return $post_types;
150
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
151
152
	/**
153
	 * Enables the Bi Directional relationships
154
	 *
155
	 * @param array $connections
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
156
	 * @return void
0 ignored issues
show
Coding Style introduced by
Function return type is void, but function contains return statement
Loading history...
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
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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' ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
178
			$title = __( 'Workouts', 'lsx-health-plan' );
179
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
180
		return $title;
181
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
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 workout' ),
201
				'id'         => $this->slug . '_featured_workout',
202
				'type'       => 'checkbox',
203
				'show_on_cb' => 'cmb2_hide_if_no_cats',
204
			)
205
		);
206
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
207
208
	/**
209
	 * Define the metabox and field configurations.
210
	 */
211
	public function details_metaboxes() {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
212
213
		$cmb = new_cmb2_box( array(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
214
			'id'           => $this->slug . '_details_metabox',
215
			'title'        => __( 'Workout Details', 'lsx-health-plan' ),
216
			'object_types' => array( $this->slug ), // Post type
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
217
			'context'      => 'normal',
218
			'priority'     => 'high',
219
			'show_names'   => true,
220
		) );
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
221
222
		$cmb->add_field( array(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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
		) );
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
228
229
		$workout_sections = apply_filters( 'lsx_health_plan_workout_sections_amount', 6 );
230
		if ( false !== $workout_sections && null !== $workout_sections ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
231
			$i = 1;
232
			while ( $i <= $workout_sections ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
Coding Style introduced by
Blank line found at start of control structure
Loading history...
233
234
				$cmb_group = new_cmb2_box( array(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
235
					'id'           => $this->slug . '_section_' . $i . '_metabox',
236
					'title'        => esc_html__( 'Exercise Group ', 'lsx-health-plan' ) . $i,
237
					'object_types' => array( $this->slug ),
238
				) );
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
239
240
				$cmb_group->add_field( array(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
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
				) );
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
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
							'closed'        => true, // true to have the groups closed by default
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
273
						),
274
					)
275
				);
276
277
				if ( false !== \lsx_health_plan\functions\get_option( 'exercise_enabled', false ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
278
					$cmb_group->add_group_field(
279
						$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

279
						/** @scrutinizer ignore-type */ $group_field_id,
Loading history...
280
						array(
281
							'name'       => __( 'Exercise related to this workout', 'lsx-health-plan' ),
282
							'id'         => 'connected_exercises',
283
							'type'       => 'post_search_ajax',
284
							// Optional :
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
285
							'limit'      => 1, // Limit selection to X items only (default 1)
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
286
							'sortable'   => true,  // Allow selected items to be sortable (default false)
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
287
							'query_args' => array(
288
								'post_type'      => array( 'exercise' ),
289
								'post_status'    => array( 'publish' ),
290
								'posts_per_page' => -1,
291
							),
292
						)
293
					);
294
				} else {
295
					$cmb_group->add_group_field(
296
						$group_field_id,
297
						array(
298
							'name'       => __( 'Video related to this workout', 'lsx-health-plan' ),
299
							'id'         => 'connected_videos',
300
							'type'       => 'post_search_ajax',
301
							// Optional :
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
302
							'limit'      => 1, // Limit selection to X items only (default 1)
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
303
							'sortable'   => true,  // Allow selected items to be sortable (default false)
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
304
							'query_args' => array(
305
								'post_type'      => array( 'video' ),
306
								'post_status'    => array( 'publish' ),
307
								'posts_per_page' => -1,
308
							),
309
						)
310
					);
311
					$cmb_group->add_group_field(
312
						$group_field_id,
313
						array(
314
							'name' => esc_html__( 'Workout Name', 'lsx-health-plan' ),
315
							'id'   => 'name',
316
							'type' => 'text',
317
							// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
318
						)
319
					);
320
321
					$cmb_group->add_group_field(
322
						$group_field_id,
323
						array(
324
							'name'    => __( 'Description', 'lsx-health-plan' ),
325
							'id'      => 'description',
326
							'type'    => 'wysiwyg',
327
							'options' => array(
328
								'textarea_rows' => 2,
329
							),
330
						)
331
					);
332
				}
333
334
				$cmb_group->add_group_field(
335
					$group_field_id,
336
					array(
337
						'name' => esc_html__( 'Exercise title (Optional)', 'lsx-health-plan' ),
338
						'id'   => 'alt_title',
339
						'type' => 'text',
340
					)
341
				);
342
				$cmb_group->add_group_field(
343
					$group_field_id,
344
					array(
345
						'name' => esc_html__( 'Exercise Description (Optional)', 'lsx-health-plan' ),
346
						'id'   => 'alt_description',
347
						'type' => 'textarea_small',
348
					)
349
				);
350
				$cmb_group->add_group_field(
351
					$group_field_id,
352
					array(
353
						'name' => esc_html__( 'Reps / Time / Distance', 'lsx-health-plan' ),
354
						'id'   => 'reps',
355
						'type' => 'text',
356
						// 'repeatable' => true, // Repeatable fields are supported w/in repeatable groups (for most types)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
357
					)
358
				);
359
				$cmb_group->add_group_field(
360
					$group_field_id,
361
					array(
362
						'name'         => __( 'Exercise Image (Optional)', 'lsx-health-plan' ),
363
						'id'           => 'exercise_alt_thumbnail',
364
						'type'         => 'file',
365
						'text'         => array(
366
							'add_upload_file_text' => __( 'Add File', 'lsx-health-plan' ),
367
						),
368
						'desc'         => __( 'Upload an image 300px x 300px in size.', 'lsx-health-plan' ),
369
						'query_args'   => array(
370
							'type' => array(
371
								'image/gif',
372
								'image/jpeg',
373
								'image/png',
374
							),
375
						),
376
						'preview_size' => 'thumbnail',
377
						'classes'      => 'lsx-field-col lsx-field-add-field  lsx-field-col-25',
378
					)
379
				);
380
381
				$i++;
382
			};
383
		}
384
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
385
	/**
386
	 * Set the post type archive to show the parent plans only.
387
	 *
388
	 * @param object $wp_query
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
389
	 * @return array
0 ignored issues
show
Coding Style introduced by
Function return type is not void, but function has no return statement
Loading history...
390
	 */
391
	public function set_parent_only( $wp_query ) {
392
		if ( ! is_admin() && $wp_query->is_main_query() && ( $wp_query->is_post_type_archive( 'workout' ) || $wp_query->is_tax( array( 'workout-type' ) ) ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces after opening bracket; 1 found
Loading history...
Coding Style introduced by
Expected 0 spaces before closing bracket; 1 found
Loading history...
393
			$wp_query->set( 'post_parent', '0' );
394
		}
395
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
396
}
397