Passed
Push — add/multiplan ( 474996...879264 )
by Virginia
05:34
created

Settings::set_vars()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 64
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
c 0
b 0
f 0
nc 8
nop 0
dl 0
loc 64
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A Settings::general_settings() 0 40 3

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
		$cmb->add_field(
154
			array(
155
				'name'      =>  __( 'Login Slug', 'lsx-health-plan' ),
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
156
				'id'        => 'login_slug',
157
				'type'      => 'input',
158
				'value'     => '',
159
				'default'   => 'login',
160
				'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' ),
161
			)
162
		);
163
164
	}
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
165
166
	/**
167
	 * Enable Business Directory Search settings only if LSX Search plugin is enabled.
168
	 *
169
	 * @param object $cmb The CMB2() class.
170
	 * @param string $section either engine,archive or single.
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
171
	 * @return void
172
	 */
173
	public function generate_tabs( $cmb ) {
174
		$tabs = $this->get_settings_tabs();
175
176
		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...
177
			$cmb->add_field(
178
				array(
179
					'id'          => 'settings_' . $tab_key . '_title',
180
					'type'        => 'title',
181
					'name'        => $tab['title'],
182
					'default'     => $tab['title'],
183
					'description' => $tab['desc'],
184
				)
185
			);
186
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_top', $cmb );
187
188
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_middle', $cmb );
189
190
			do_action( 'lsx_hp_settings_page_' . $tab_key . '_bottom', $cmb );
191
192
			$cmb->add_field(
193
				array(
194
					'id'   => 'settings_' . $tab_key . '_closing',
195
					'type' => 'tab_closing',
196
				)
197
			);
198
		}
199
	}
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
200
201
202
	/**
203
	 * Returns the tabs and their descriptions.
204
	 *
205
	 * @return array
206
	 */
207
	public function get_settings_tabs() {
208
		$tabs = array(
209
			'general' => array(
210
				'title' => __( 'General', 'lsx-health-plan' ),
211
				'desc'  => __( 'Control the sitewide settings for the LSX HP site.', 'lsx-health-plan' ),
212
			),
213
		);
214
215
		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...
216
			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...
217
				default:
218
					//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...
219
						$temp_post_type = get_post_type_object( $post_type );
220
						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...
221
							$page_url    = get_post_type_archive_link( $temp_post_type->name );
222
							$description = sprintf(
223
								/* translators: %s: The subscription info */
224
								__( 'Control the settings for your <a target="_blank" href="%1$s">%2$s</a> archive.', 'lsx-search' ),
225
								$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

225
								/** @scrutinizer ignore-type */ $page_url,
Loading history...
226
								$temp_post_type->label
227
							);
228
229
							$tabs[ $post_type ] = array(
230
								'title' => $temp_post_type->label,
231
								'desc'  => $description,
232
							);
233
						}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
234
					//}
0 ignored issues
show
Coding Style introduced by
No space found before comment text; expected "// }" but found "//}"
Loading history...
235
					break;
236
			}
237
		}
0 ignored issues
show
Coding Style introduced by
No blank line found after control structure
Loading history...
238
		return $tabs;
239
	}
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...
240
}
241