Passed
Push — add/multiplan ( 879264...3cd95a )
by Virginia
05:11
created

Settings::general_settings()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 80
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 56
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 80
rs 8.3377

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains the settings class for LSX
4
 *
5
 * @package lsx-health-plan
6
 */
7
8
namespace lsx_health_plan\classes\admin;
9
10
/**
11
 * Contains the settings for each post type \lsx_health_plan\classes\admin\Settings().
12
 */
13
class Settings {
14
15
	/**
16
	 * Holds class instance
17
	 *
18
	 * @since 1.0.0
19
	 *
20
	 * @var      object \lsx_health_plan\classes\admin\Settings()
21
	 */
22
	protected static $instance = null;
23
24
	/**
25
	 * Option key, and option page slug
26
	 *
27
	 * @var string
28
	 */
29
	protected $screen_id = 'lsx_health_plan_settings';
30
31
	/**
32
	 * An array of the post types for the Global Downloads field.
33
	 *
34
	 * @var array
35
	 */
36
	public $download_types = array();
37
38
	/**
39
	 * An array of the post types for the Global Defaults field.
40
	 *
41
	 * @var array
42
	 */
43
	//public $default_types = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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 "// public $default_types = array();" but found "//public $default_types = array();"
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
44
45
	/**
46
	 * An array of the endpoints for the Endpoint Translation field.
47
	 *
48
	 * @var array
49
	 */
50
	public $endpoints = array();
51
52
	/**
53
	 * Constructor
54
	 */
55
	public function __construct() {
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
56
		$this->load_classes();
57
		add_action( 'cmb2_admin_init', array( $this, 'register_settings_page' ) );
58
		add_action( 'lsx_hp_settings_page', array( $this, 'generate_tabs' ), 1, 1 );
59
60
		add_action( 'lsx_hp_settings_page_general_top', array( $this, 'general_settings' ), 1, 1 );
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; 1 found
Loading history...
62
63
	/**
64
	 * Return an instance of this class.
65
	 *
66
	 * @since 1.0.0
67
	 *
68
	 * @return    object \lsx_health_plan\classes\admin\Settings()    A single instance of this class.
69
	 */
70
	public static function get_instance() {
71
		// If the single instance hasn't been set, set it now.
72
		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...
73
			self::$instance = new self();
74
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
75
		return self::$instance;
76
	}
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...
77
78
	/**
79
	 * Loads the variable classes and the static classes.
80
	 */
81
	private function load_classes() {
0 ignored issues
show
Coding Style introduced by
Expected 0 blank lines after opening function brace; 1 found
Loading history...
82
83
		$this->post_types = array(
0 ignored issues
show
Bug Best Practice introduced by
The property post_types does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
			'plan',
85
			'workout',
86
			'exercise',
87
			'meal',
88
			'recipe',
89
			//'tip',
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// 'tip'," but found "//'tip',"
Loading history...
90
		);
91
92
		foreach ( $this->post_types as $post_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...
93
			$this->$post_type = require_once LSX_HEALTH_PLAN_PATH . 'classes/admin/settings/class-' . $post_type . '.php';
94
		}
95
96
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
97
98
	/**
99
	 * Hook in and register a submenu options page for the Page post-type menu.
100
	 */
101
	public function register_settings_page() {
102
		$cmb = new_cmb2_box(
103
			array(
104
				'id'           => $this->screen_id,
105
				'title'        => esc_html__( 'Settings', 'lsx-health-plan' ),
106
				'object_types' => array( 'options-page' ),
107
				'option_key'   => 'lsx_health_plan_options', // The option key and admin menu page slug.
108
				'parent_slug'  => 'edit.php?post_type=plan', // Make options page a submenu item of the themes menu.
109
				'capability'   => 'manage_options', // Cap required to view options-page.
110
			)
111
		);
112
		do_action( 'lsx_hp_settings_page', $cmb );
113
	}
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...
114
115
	/**
116
	 * Registers the general settings.
117
	 *
118
	 * @param object $cmb new_cmb2_box().
119
	 * @return void
120
	 */
121
	public function general_settings( $cmb ) {
122
		if ( function_exists( 'wc_memberships_is_post_content_restricted' ) ) {
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...
123
			$cmb->add_field(
124
				array(
125
					'name'       => __( 'Membership Product', 'lsx-health-plan' ),
126
					'id'         => 'membership_product',
127
					'type'       => 'post_search_ajax',
128
					'limit'      => 1,
129
					'sortable'   => false,
130
					'query_args' => array(
131
						'post_type'      => array( 'product' ),
132
						'post_status'    => array( 'publish' ),
133
						'posts_per_page' => -1,
134
					),
135
				)
136
			);
137
		}
138
139
		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...
140
			$cmb->add_field(
141
				array(
142
					'name'        => __( 'Exercises', 'lsx-health-plan' ),
143
					'id'          => 'exercise_enabled',
144
					'type'        => 'checkbox',
145
					'value'       => 1,
146
					'default'     => 0,
147
					'description' => __( 'Enabling the exercise post type will automatically replace the Video post type.', 'lsx-health-plan' ),
148
				)
149
			);
150
		}
151
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
152
153
		if ( \lsx_health_plan\functions\get_option( 'workout_disabled' ) ) {
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...
154
			$cmb->add_field(
155
				array(
156
					'name'        => __( 'Disable Workouts', 'lsx-health-plan' ),
157
					'id'          => 'workout_disabled',
158
					'type'        => 'checkbox',
159
					'value'       => 1,
160
					'default'     => 0,
161
					'description' => __( 'Disable workout post type if you are wanting a minimal site.', 'lsx-health-plan' ),
162
				)
163
			);
164
		}
165
166
		if ( \lsx_health_plan\functions\get_option( 'recipe_disabled' ) ) {
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...
167
			$cmb->add_field(
168
				array(
169
					'name'        => __( 'Disable Recipes', 'lsx-health-plan' ),
170
					'id'          => 'recipe_disabled',
171
					'type'        => 'checkbox',
172
					'value'       => 1,
173
					'default'     => 0,
174
					'description' => __( 'Disable recipe post type if you are wanting a minimal site.', 'lsx-health-plan' ),
175
				)
176
			);
177
		}
178
179
		if ( \lsx_health_plan\functions\get_option( 'meal_disabled' ) ) {
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...
180
			$cmb->add_field(
181
				array(
182
					'name'        => __( 'Disable Meals', 'lsx-health-plan' ),
183
					'id'          => 'meal_disabled',
184
					'type'        => 'checkbox',
185
					'value'       => 1,
186
					'default'     => 0,
187
					'description' => __( 'Disable meal post type if you are wanting a minimal site.', 'lsx-health-plan' ),
188
				)
189
			);
190
		}
191
		
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
192
193
		$cmb->add_field(
194
			array(
195
				'name'      =>  __( 'Login Slug', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
196
				'id'        => 'login_slug',
197
				'type'      => 'input',
198
				'value'     => '',
199
				'default'   => 'login',
200
				'after_row' => __( '<p style="font-style: italic;">If you have changed any URL slugs, please remember re-save your permalinks in Settings > Permalinks.</p>', 'lsx-health-plan' ),
201
			)
202
		);
203
204
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
205
206
	/**
207
	 * Enable Business Directory Search settings only if LSX Search plugin is enabled.
208
	 *
209
	 * @param object $cmb The CMB2() class.
210
	 * @param string $section either engine,archive or single.
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
211
	 * @return void
212
	 */
213
	public function generate_tabs( $cmb ) {
214
		$tabs = $this->get_settings_tabs();
215
216
		foreach ( $tabs as $tab_key => $tab ) {
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...
217
			$cmb->add_field(
218
				array(
219
					'id'          => 'settings_' . $tab_key . '_title',
220
					'type'        => 'title',
221
					'name'        => $tab['title'],
222
					'default'     => $tab['title'],
223
					'description' => $tab['desc'],
224
				)
225
			);
226
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_top', $cmb );
227
228
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_middle', $cmb );
229
230
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_bottom', $cmb );
231
232
			$cmb->add_field(
233
				array(
234
					'id'   => 'settings_' . $tab_key . '_closing',
235
					'type' => 'tab_closing',
236
				)
237
			);
238
		}
239
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
240
241
242
	/**
243
	 * Returns the tabs and their descriptions.
244
	 *
245
	 * @return array
246
	 */
247
	public function get_settings_tabs() {
248
		$tabs = array(
249
			'general' => array(
250
				'title' => __( 'General', 'lsx-health-plan' ),
251
				'desc'  => __( 'Control the sitewide settings for the LSX HP site.', 'lsx-health-plan' ),
252
			),
253
		);
254
255
		foreach ( $this->post_types as $post_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...
256
			switch ( $post_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...
257
				default:
258
					//if ( ! in_array( $post_type, \lsx\search\includes\get_restricted_post_types() ) ) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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 "// if ( ! in_array( $post_type, \lsx\search\includes\get_restricted_post_types() ) ) {" but found "//if ( ! in_array( $post_type, \lsx\search\includes\get_restricted_post_types() ) ) {"
Loading history...
Coding Style introduced by
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
259
						$temp_post_type = get_post_type_object( $post_type );
260
						if ( ! is_wp_error( $temp_post_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...
261
							$page_url    = get_post_type_archive_link( $temp_post_type->name );
262
							$description = sprintf(
263
								/* translators: %s: The subscription info */
264
								__( 'Control the settings for your <a target="_blank" href="%1$s">%2$s</a> archive.', 'lsx-search' ),
265
								$page_url,
0 ignored issues
show
Bug introduced by
It seems like $page_url can also be of type false; however, parameter $args of sprintf() 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

265
								/** @scrutinizer ignore-type */ $page_url,
Loading history...
266
								$temp_post_type->label
267
							);
268
269
							$tabs[ $post_type ] = array(
270
								'title' => $temp_post_type->label,
271
								'desc'  => $description,
272
							);
273
						}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
274
					//}
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// }" but found "//}"
Loading history...
275
					break;
276
			}
277
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
278
		return $tabs;
279
	}
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...
280
}
281