Plan   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 591
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 342
dl 0
loc 591
rs 9.0399
c 8
b 0
f 0
wmc 42

15 Methods

Rating   Name   Duplication   Size   Complexity  
A get_the_archive_title() 0 5 2
A week_taxonomy_setup() 0 28 1
A details_metaboxes() 0 33 2
A featured_metabox() 0 18 1
A plan_connections() 0 23 1
A plan_type_taxonomy_setup() 0 27 1
A register_post_type() 0 41 1
A get_instance() 0 6 2
A set_parent_only() 0 3 5
B save_meta() 0 21 8
B add_thumbnail_form_field() 0 24 7
B plan_breadcrumb_filter() 0 59 5
B sections_metabox_loop() 0 132 4
A __construct() 0 35 1
A enable_post_type() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Plan often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Plan, and based on these observations, apply Extract Interface, too.

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 meal_plan post type
8
 *
9
 * @package lsx-health-plan
10
 */
11
class Plan {
12
13
	/**
14
	 * Holds class instance
15
	 *
16
	 * @since 1.0.0
17
	 *
18
	 * @var      object \lsx_health_plan\classes\Plan()
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 = 'plan';
30
31
	/**
32
	 * Constructor
33
	 */
34
	public function __construct() {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
35
36
		add_action( 'init', array( $this, 'register_post_type' ) );
37
		add_action( 'init', array( $this, 'plan_type_taxonomy_setup' ) );
38
		add_action( 'init', array( $this, 'week_taxonomy_setup' ) );
39
40
		// Icons for the plan types.
41
		add_action( 'create_term', array( $this, 'save_meta' ), 10, 2 );
42
		add_action( 'edit_term', array( $this, 'save_meta' ), 10, 2 );
43
		$prefix_taxonomy = 'plan-type';
44
		add_action( sprintf( '%s_edit_form_fields', $prefix_taxonomy ), array( $this, 'add_thumbnail_form_field' ), 3, 1 );
45
46
		// Register the Metaboxes.
47
		add_action( 'cmb2_admin_init', array( $this, 'featured_metabox' ), 5 );
48
		add_action( 'cmb2_admin_init', array( $this, 'details_metaboxes' ), 5 );
49
		add_action( 'cmb2_admin_init', array( $this, 'plan_connections' ), 5 );
50
		add_action( 'cmb2_admin_init', array( $this, 'sections_metabox_loop' ), 1 );
51
52
		add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 100 );
53
		//add_filter( 'lsx_global_header_title', array( $this, 'hp_recipe_header_title' ), 200, 1 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
Coding Style introduced by
No space found before comment text; expected "// add_filter( 'lsx_global_header_title', array( $this, 'hp_recipe_header_title' ), 200, 1 );" but found "//add_filter( 'lsx_global_header_title', array( $this, 'hp_recipe_header_title' ), 200, 1 );"
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
54
55
		// Template Redirects.
56
		add_filter( 'lsx_health_plan_archive_template', array( $this, 'enable_post_type' ), 10, 1 );
57
		add_filter( 'lsx_health_plan_single_template', array( $this, 'enable_post_type' ), 10, 1 );
58
59
		// Plan Archive Actions.
60
		add_action( 'pre_get_posts', array( $this, 'set_parent_only' ), 10, 1 );
61
		add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 100 );
62
		add_action( 'lsx_content_top', 'lsx_hp_plan_archive_filters', 10, 1 );
63
		add_filter( 'lsx_hp_disable_plan_archive_filters', '\lsx_health_plan\functions\plan\is_search_enabled', 10, 1 );
64
		add_filter( 'lsx_hp_disable_plan_archive_filters', '\lsx_health_plan\functions\plan\is_filters_disabled', 10, 1 );
65
66
		//Breadcrumbs
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// Breadcrumbs" but found "//Breadcrumbs"
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
67
		add_filter( 'wpseo_breadcrumb_links', array( $this, 'plan_breadcrumb_filter' ), 30, 1 );
68
		add_filter( 'woocommerce_get_breadcrumb', array( $this, 'plan_breadcrumb_filter' ), 30, 1 );
69
		
70
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
71
72
	/**
73
	 * Return an instance of this class.
74
	 *
75
	 * @since 1.0.0
76
	 *
77
	 * @return    object \lsx_health_plan\classes\Meal_Plan()    A single instance of this class.
78
	 */
79
	public static function get_instance() {
80
		// If the single instance hasn't been set, set it now.
81
		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...
82
			self::$instance = new self();
83
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
84
		return self::$instance;
85
	}
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...
86
	/**
87
	 * Register the post type.
88
	 */
89
	public function register_post_type() {
90
		$labels = array(
91
			'name'               => esc_html__( 'Plans', 'lsx-health-plan' ),
92
			'singular_name'      => esc_html__( 'Plan', 'lsx-health-plan' ),
93
			'add_new'            => esc_html_x( 'Add New', 'post type general name', 'lsx-health-plan' ),
94
			'add_new_item'       => esc_html__( 'Add New', 'lsx-health-plan' ),
95
			'edit_item'          => esc_html__( 'Edit', 'lsx-health-plan' ),
96
			'new_item'           => esc_html__( 'New', 'lsx-health-plan' ),
97
			'all_items'          => esc_html__( 'All Plans', 'lsx-health-plan' ),
98
			'view_item'          => esc_html__( 'View', 'lsx-health-plan' ),
99
			'search_items'       => esc_html__( 'Search', 'lsx-health-plan' ),
100
			'not_found'          => esc_html__( 'None found', 'lsx-health-plan' ),
101
			'not_found_in_trash' => esc_html__( 'None found in Trash', 'lsx-health-plan' ),
102
			'parent_item_colon'  => esc_html__( 'Parent:', 'lsx-health-plan' ),
103
			'menu_name'          => esc_html__( 'Plans', 'lsx-health-plan' ),
104
		);
105
		$args   = array(
106
			'labels'             => $labels,
107
			'public'             => true,
108
			'publicly_queryable' => true,
109
			'show_ui'            => true,
110
			'show_in_menu'       => true,
111
			'show_in_rest'       => true,
112
			'menu_icon'          => 'dashicons-welcome-write-blog',
113
			'query_var'          => true,
114
			'rewrite'            => array(
115
				'slug' => \lsx_health_plan\functions\get_option( 'plan_single_slug', 'plan' ),
116
			),
117
			'capability_type'    => 'page',
118
			'has_archive'        => \lsx_health_plan\functions\get_option( 'endpoint_plan_archive', 'plans' ),
119
			'hierarchical'       => false,
120
			'menu_position'      => null,
121
			'supports'           => array(
122
				'title',
123
				'editor',
124
				'thumbnail',
125
				'page-attributes',
126
				'custom-fields',
127
			),
128
		);
129
		register_post_type( 'plan', $args );
130
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
131
132
	/**
133
	 * Register the Type taxonomy.
134
	 */
135
	public function plan_type_taxonomy_setup() {
136
		$labels = array(
137
			'name'              => esc_html_x( 'Plan Type', 'taxonomy general name', 'lsx-health-plan' ),
138
			'singular_name'     => esc_html_x( 'Plan Type', 'taxonomy singular name', 'lsx-health-plan' ),
139
			'search_items'      => esc_html__( 'Search', 'lsx-health-plan' ),
140
			'all_items'         => esc_html__( 'All', 'lsx-health-plan' ),
141
			'parent_item'       => esc_html__( 'Parent', 'lsx-health-plan' ),
142
			'parent_item_colon' => esc_html__( 'Parent:', 'lsx-health-plan' ),
143
			'edit_item'         => esc_html__( 'Edit', 'lsx-health-plan' ),
144
			'update_item'       => esc_html__( 'Update', 'lsx-health-plan' ),
145
			'add_new_item'      => esc_html__( 'Add New', 'lsx-health-plan' ),
146
			'new_item_name'     => esc_html__( 'New Name', 'lsx-health-plan' ),
147
			'menu_name'         => esc_html__( 'Plan Types', 'lsx-health-plan' ),
148
		);
149
150
		$args = array(
151
			'hierarchical'      => true,
152
			'labels'            => $labels,
153
			'show_ui'           => true,
154
			'show_admin_column' => true,
155
			'query_var'         => true,
156
			'rewrite'           => array(
157
				'slug' => 'plan-type',
158
			),
159
		);
160
161
		register_taxonomy( 'plan-type', array( 'plan' ), $args );
162
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
163
164
	/**
165
	 * Register the Week taxonomy.
166
	 */
167
	public function week_taxonomy_setup() {
168
		$labels = array(
169
			'name'              => esc_html_x( 'Week', 'taxonomy general name', 'lsx-health-plan' ),
170
			'singular_name'     => esc_html_x( 'Week', 'taxonomy singular name', 'lsx-health-plan' ),
171
			'search_items'      => esc_html__( 'Search', 'lsx-health-plan' ),
172
			'all_items'         => esc_html__( 'All', 'lsx-health-plan' ),
173
			'parent_item'       => esc_html__( 'Parent', 'lsx-health-plan' ),
174
			'parent_item_colon' => esc_html__( 'Parent:', 'lsx-health-plan' ),
175
			'edit_item'         => esc_html__( 'Edit', 'lsx-health-plan' ),
176
			'update_item'       => esc_html__( 'Update', 'lsx-health-plan' ),
177
			'add_new_item'      => esc_html__( 'Add New', 'lsx-health-plan' ),
178
			'new_item_name'     => esc_html__( 'New Name', 'lsx-health-plan' ),
179
			'menu_name'         => esc_html__( 'Weeks', 'lsx-health-plan' ),
180
		);
181
182
		$args = array(
183
			'hierarchical'      => true,
184
			'labels'            => $labels,
185
			'show_ui'           => true,
186
			'show_admin_column' => true,
187
			'query_var'         => true,
188
			'show_in_rest'      => true,
189
			'rewrite'           => array(
190
				'slug' => 'week',
191
			),
192
		);
193
194
		register_taxonomy( 'week', array( 'plan' ), $args );
195
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
196
197
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$term" missing
Loading history...
198
	 * Output the form field for this metadata when adding a new term
199
	 *
200
	 * @since 0.1.0
201
	 */
202
	public function add_thumbnail_form_field( $term = false ) {
203
		if ( is_object( $term ) ) {
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...
204
			$value         = get_term_meta( $term->term_id, 'thumbnail', true );
205
			$image_preview = wp_get_attachment_image_src( $value, 'thumbnail' );
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false and string; however, parameter $attachment_id of wp_get_attachment_image_src() does only seem to accept integer, 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

205
			$image_preview = wp_get_attachment_image_src( /** @scrutinizer ignore-type */ $value, 'thumbnail' );
Loading history...
206
207
			if ( is_array( $image_preview ) ) {
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...
208
				$image_preview = '<img style="height: 50px; width: 50px;" src="' . esc_url( $image_preview[0] ) . '" width="' . $image_preview[1] . '" height="' . $image_preview[2] . '" class="alignnone size-thumbnail d wp-image-' . $value . '" />';
209
			}
210
		} else {
211
			$image_preview = false;
212
			$value         = false;
213
		}
214
		?>
215
		<tr class="form-field form-required term-thumbnail-wrap">
216
			<th scope="row"><label for="thumbnail"><?php esc_html_e( 'Icon Image', 'lsx-health-plan' ); ?></label></th>
217
			<td>
218
				<input class="input_image_id" type="hidden" name="thumbnail" value="<?php echo wp_kses_post( $value ); ?>">
219
				<div class="thumbnail-preview">
220
					<?php echo wp_kses_post( $image_preview ); ?>
0 ignored issues
show
Bug introduced by
It seems like $image_preview can also be of type false; however, parameter $data of wp_kses_post() 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

220
					<?php echo wp_kses_post( /** @scrutinizer ignore-type */ $image_preview ); ?>
Loading history...
221
				</div>
222
				<a style="<?php if ( '' !== $value && false !== $value ) { ?>display:none;<?php } ?>" class="button-secondary lsx-thumbnail-image-add"><?php esc_html_e( 'Choose Image', 'lsx-health-plan' ); ?></a>
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...
223
				<a style="<?php if ( '' === $value || false === $value ) { ?>display:none;<?php } ?>" class="button-secondary lsx-thumbnail-image-remove"><?php esc_html_e( 'Remove Image', 'lsx-health-plan' ); ?></a>
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...
224
				<?php wp_nonce_field( 'lsx_hp_term_thumbnail_nonce', 'lsx_hp_term_thumbnail_nonce' ); ?>
225
			</td>
226
		</tr>
227
		<?php
228
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
229
230
	/**
231
	 * Saves the Taxonomy term icon image
232
	 *
233
	 * @since 0.1.0
234
	 *
235
	 * @param  int    $term_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
236
	 * @param  string $taxonomy
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
237
	 */
238
	public function save_meta( $term_id = 0, $taxonomy = '' ) {
239
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
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...
240
			return;
241
		}
242
243
		if ( ! isset( $_POST['thumbnail'] ) ) {
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...
244
			return;
245
		}
246
247
		if ( check_admin_referer( 'lsx_hp_term_thumbnail_nonce', 'lsx_hp_term_thumbnail_nonce' ) ) {
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...
248
			if ( ! isset( $_POST['thumbnail'] ) ) {
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...
249
				return;
250
			}
251
252
			$thumbnail_meta = sanitize_text_field( $_POST['thumbnail'] );
0 ignored issues
show
introduced by
$_POST data not unslashed before sanitization. Use wp_unslash() or similar
Loading history...
253
			$thumbnail_meta = ! empty( $thumbnail_meta ) ? $thumbnail_meta : '';
254
255
			if ( empty( $thumbnail_meta ) ) {
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...
256
				delete_term_meta( $term_id, 'thumbnail' );
257
			} else {
258
				update_term_meta( $term_id, 'thumbnail', $thumbnail_meta );
259
			}
260
		}
261
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
262
263
	/**
264
	 * Define the metabox and field configurations.
265
	 */
266
	public function details_metaboxes() {
267
		$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...
268
			'id'           => $this->slug . '_details_metabox',
269
			'title'        => __( 'Details', 'lsx-health-plan' ),
270
			'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...
271
			'context'      => 'normal',
272
			'priority'     => 'high',
273
			'show_names'   => true,
274
		) );
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...
275
276
		$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...
277
			'name' => __( 'Plan Short Description', 'lsx-health-plan' ),
278
			'id'   => $this->slug . '_short_description',
279
			'type' => 'textarea_small',
280
			'desc' => __( 'Add a small description for this plan (optional)', 'lsx-health-plan' ),
281
		) );
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...
282
283
		$warmup_type = 'page';
284
		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...
285
			$warmup_type = array( 'page', 'workout' );
286
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
287
		$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...
288
			'name'       => __( 'Warmup', 'lsx-health-plan' ),
289
			'desc'       => __( 'Connect the warm up page that applies to this day plan using the field provided.', 'lsx-health-plan' ),
290
			'id'         => $this->slug . '_warmup',
291
			'type'       => 'post_search_ajax',
292
			// Optional :
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
293
			'limit'      => 3,  // 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...
294
			'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...
295
			'query_args' => array(
296
				'post_type'      => $warmup_type,
297
				'post_status'    => array( 'publish' ),
298
				'posts_per_page' => -1,
299
			),
300
		) );
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...
301
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
302
303
	/**
304
	 * Adds the post type to the different arrays.
305
	 *
306
	 * @param array $post_types
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
307
	 * @return array
308
	 */
309
	public function enable_post_type( $post_types = array() ) {
310
		$post_types[] = $this->slug;
311
		return $post_types;
312
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
313
314
	/**
315
	 * Registers the workout connections on the plan post type.
316
	 *
317
	 * @return void
318
	 */
319
	public function plan_connections() {
320
		$cmb = new_cmb2_box(
321
			array(
322
				'id'           => $this->slug . '_connections_metabox',
323
				'title'        => __( 'Plans', 'lsx-health-plan' ),
324
				'object_types' => array( 'workout', 'meal', 'tip', 'recipe' ),
325
				'context'      => 'normal',
326
				'priority'     => 'high',
327
				'show_names'   => true,
328
			)
329
		);
330
		$cmb->add_field(
331
			array(
332
				'name'       => __( 'Plan', 'lsx-health-plan' ),
333
				'id'         => 'connected_plans',
334
				'desc'       => __( 'Connect this to the day plan it applies to, using the field provided.', 'lsx-health-plan' ),
335
				'type'       => 'post_search_ajax',
336
				'limit'      => 15,
337
				'sortable'   => true,
338
				'query_args' => array(
339
					'post_type'      => array( 'plan' ),
340
					'post_status'    => array( 'publish' ),
341
					'posts_per_page' => -1,
342
				),
343
			)
344
		);
345
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
346
347
	/**
348
	 * Remove the "Archives:" from the post type.
349
	 *
350
	 * @param string $title the term title.
351
	 * @return string
352
	 */
353
	public function get_the_archive_title( $title ) {
354
		if ( is_post_type_archive( 'plan' ) ) {
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...
355
			$title = __( 'Our health plans', 'lsx-health-plan' );
356
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
357
		return $title;
358
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
359
360
	/**
361
	 * Set the post type archive to show the parent plans only.
362
	 *
363
	 * @param object $wp_query
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
364
	 * @return array
0 ignored issues
show
Coding Style introduced by
Function return type is not void, but function has no return statement
Loading history...
365
	 */
366
	public function set_parent_only( $wp_query ) {
367
		if ( ! is_admin() && $wp_query->is_main_query() && ( $wp_query->is_post_type_archive( 'plan' ) || $wp_query->is_tax( 'plan-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...
368
			$wp_query->set( 'post_parent', '0' );
369
		}
370
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
371
372
	/**
373
	 * Define the metabox and field configurations.
374
	 */
375
	public function featured_metabox() {
376
		$cmb = new_cmb2_box(
377
			array(
378
				'id'           => $this->slug . '_featured_metabox_plan',
379
				'title'        => __( 'Featured Plan', 'lsx-health-plan' ),
380
				'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...
381
				'context'      => 'side',
382
				'priority'     => 'high',
383
				'show_names'   => true,
384
			)
385
		);
386
		$cmb->add_field(
387
			array(
388
				'name'       => __( 'Featured Plan', 'lsx-health-plan' ),
389
				'desc'       => __( 'Enable a featured plan' ),
390
				'id'         => $this->slug . '_featured_plan',
391
				'type'       => 'checkbox',
392
				'show_on_cb' => 'cmb2_hide_if_no_cats',
393
			)
394
		);
395
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
396
397
	/**
398
	 * Define the metabox and field configurations.
399
	 */
400
	public function sections_metabox_loop() {
401
		$cmb = new_cmb2_box(
402
			array(
403
				'id'           => $this->slug . '_sections_metabox',
404
				'title'        => __( 'Sections', 'lsx-health-plan' ),
405
				'object_types' => array( $this->slug ), // Post type.
406
				'context'      => 'normal',
407
				'priority'     => 'low',
408
				'show_names'   => true,
409
			)
410
		);
411
412
		/*
413
		This is where the repeatable group is defined, each field has the same ID as the legacy field.
414
		There is a function which runs and adds to looped fields to individual fields for WP Query compatability.
415
		*/
416
		$group = $cmb->add_field(
417
			array(
418
				'id'      => $this->slug . '_sections',
419
				'type'    => 'group',
420
				'options' => array(
421
					'group_title'   => __( 'Section', 'lsx-health-plan' ) . ' {#}', // {#} gets replaced by row number
422
					'add_button'    => __( 'Add section', 'lsx-health-plan' ),
423
					'remove_button' => __( 'Remove section', 'lsx-health-plan' ),
424
					'sortable'      => true,
425
					'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...
426
				),
427
				'classes' => 'lsx-admin-row',
428
				
429
			)
430
		);
431
432
		$cmb->add_group_field(
433
			$group,
0 ignored issues
show
Bug introduced by
It seems like $group 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

433
			/** @scrutinizer ignore-type */ $group,
Loading history...
434
			array(
435
				'name'       => __( 'Title', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 7.
Loading history...
436
				'id'         => 'title',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 6 space(s) between "'id'" and double arrow, but found 9.
Loading history...
437
				'type'       => 'text',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'type'" and double arrow, but found 7.
Loading history...
438
				'desc'       => __( 'e.g Day 1 / Week 1', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'desc'" and double arrow, but found 7.
Loading history...
439
				'classes'    => 'lsx-field-col  lsx-field-col-50',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 1 space(s) between "'classes'" and double arrow, but found 4.
Loading history...
440
			)
441
		);
442
443
		$cmb->add_group_field(
444
			$group,
445
			array(
446
				'name'       => __( 'Group', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 7.
Loading history...
447
				'id'         => 'group',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 6 space(s) between "'id'" and double arrow, but found 9.
Loading history...
448
				'type'       => 'text',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'type'" and double arrow, but found 7.
Loading history...
449
				'desc'       => __( 'e.g Week 1 / January', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 4 space(s) between "'desc'" and double arrow, but found 7.
Loading history...
450
				'classes'    => 'lsx-field-col  lsx-field-col-50',
0 ignored issues
show
introduced by
Array double arrow not aligned correctly; expected 1 space(s) between "'classes'" and double arrow, but found 4.
Loading history...
451
			)
452
		);
453
454
		$cmb->add_group_field(
455
			$group,
456
			array(
457
				'name' => __( 'Overview', 'lsx-health-plan' ),
458
				'id'   => 'description',
459
				'type' => 'wysiwyg',
460
			)
461
		);
462
463
		if ( post_type_exists( '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...
464
			$cmb->add_group_field(
465
				$group,
466
				array(
467
					'name'       => __( 'Workouts', 'lsx-health-plan' ),
468
					'id'         => 'connected_workouts',
469
					'desc'       => __( 'Connect the workout(s) that apply to this section.', 'lsx-health-plan' ),
470
					'type'       => 'post_search_ajax',
471
					'limit'      => 15,
472
					'sortable'   => true,
473
					'query_args' => array(
474
						'post_type'      => array( 'workout' ),
475
						'post_status'    => array( 'publish' ),
476
						'posts_per_page' => -1,
477
					),
478
					'classes'    => 'lsx-field-col lsx-field-add-field lsx-field-col-50',
479
				)
480
			);
481
			$cmb->add_group_field(
482
				$group,
483
				array(
484
					'name'        => __( 'Rest day', 'lsx-health-plan' ),
485
					'id'          => 'rest_day_enabled',
486
					'type'        => 'checkbox',
487
					'value'       => 1,
488
					'default'     => 0,
489
					'description' => __( 'Enabling the rest day will add an item called "Rest" with no links.', 'lsx-health-plan' ),
490
					'classes'     => 'lsx-field-col lsx-field-add-field lsx-field-col-50',
491
				)
492
			);
493
		}
494
495
		if ( post_type_exists( 'meal' ) ) {
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...
496
			$cmb->add_group_field(
497
				$group,
498
				array(
499
					'name'       => __( 'Meals', 'lsx-health-plan' ),
500
					'desc'       => __( 'Connect the meal(s) that apply to this section.', 'lsx-health-plan' ),
501
					'id'         => 'connected_meals',
502
					'type'       => 'post_search_ajax',
503
					// Optional :
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
504
					'limit'      => 15, // 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...
505
					'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...
506
					'query_args' => array(
507
						'post_type'      => array( 'meal' ),
508
						'post_status'    => array( 'publish' ),
509
						'posts_per_page' => -1,
510
					),
511
					'classes'    => 'lsx-field-col lsx-field-add-field  lsx-field-col-50',
512
				)
513
			);
514
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
515
		if ( post_type_exists( 'tip' ) ) {
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...
516
			$cmb->add_group_field(
517
				$group,
518
				array(
519
					'name'       => __( 'Tips', 'lsx-health-plan' ),
520
					'id'         => 'connected_tips',
521
					'desc'       => __( 'Connect the tip(s) that apply to this section.', 'lsx-health-plan' ),
522
					'type'       => 'post_search_ajax',
523
					// Optional :
0 ignored issues
show
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
524
					'limit'      => 15,  // 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...
525
					'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...
526
					'query_args' => array(
527
						'post_type'      => array( 'tip' ),
528
						'post_status'    => array( 'publish' ),
529
						'posts_per_page' => -1,
530
					),
531
					'classes'    => 'lsx-field-col lsx-field-add-field  lsx-field-col-50',
532
				)
533
			);
534
		}
535
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
536
537
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$crumbs" missing
Loading history...
538
	 * Holds the array for the single plan breadcrumbs.
539
	 *
540
	 * @var array $crumbs
541
	 * @return array
542
	 */
543
	public function plan_breadcrumb_filter( $crumbs ) {
544
		$plan  = \lsx_health_plan\functions\get_option( 'endpoint_plan', 'plan' );
545
		$plans = \lsx_health_plan\functions\get_option( 'endpoint_plan_archive', 'plan' );
546
547
		if ( is_singular( 'plan' ) ) {	
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...
introduced by
Spaces must be used for mid-line alignment; tabs are not allowed
Loading history...
548
			$plan_name     = get_the_title();
549
			$url           = get_post_type_archive_link( $plan );
0 ignored issues
show
Bug introduced by
It seems like $plan can also be of type array; however, parameter $post_type of get_post_type_archive_link() 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

549
			$url           = get_post_type_archive_link( /** @scrutinizer ignore-type */ $plan );
Loading history...
550
			$term_obj_list = get_the_terms( get_the_ID(), 'plan-type' );
0 ignored issues
show
Bug introduced by
It seems like get_the_ID() can also be of type false; however, parameter $post of get_the_terms() does only seem to accept WP_Post|integer, 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

550
			$term_obj_list = get_the_terms( /** @scrutinizer ignore-type */ get_the_ID(), 'plan-type' );
Loading history...
551
			$plan_type     = $term_obj_list[0]->name;
552
			$plan_type_url = get_term_link( $term_obj_list[0]->term_id );
553
554
			$new_crumbs    = array();
555
			$new_crumbs[0] = $crumbs[0];
556
557
			if ( function_exists( 'woocommerce_breadcrumb' ) ) {
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...
558
				$new_crumbs[1] = array(
559
					0 => $plans,
560
					1 => $url,
561
				);
562
				$new_crumbs[2] = array(
563
					0 => $plan_type,
564
					1 => $plan_type_url,
565
				);
566
				$new_crumbs[3] = array(
567
					0 => $plan_name,
568
				);
569
			} else {
570
				$new_crumbs[1] = array(
571
					'text' => $plans,
572
					'url'  => $url,
573
				);
574
				$new_crumbs[2] = array(
575
					'text' => $plan_type,
576
					'url'  => $plan_type_url,
577
				);
578
				$new_crumbs[3] = array(
579
					'text' => $plan_name,
580
				);
581
			}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
582
			$crumbs = $new_crumbs;
583
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
584
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
585
		if ( is_post_type_archive( 'plan' ) ) {
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...
586
587
			$new_crumbs    = array();
588
			$new_crumbs[0] = $crumbs[0];
589
590
			if ( function_exists( 'woocommerce_breadcrumb' ) ) {
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...
591
				$new_crumbs[1] = array(
592
					0 => $plans,
593
				);
594
			} else {
595
				$new_crumbs[1] = array(
596
					'text' => $plans,
597
				);
598
			}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
599
			$crumbs = $new_crumbs;
600
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
601
		return $crumbs;
602
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
603
}
604