Issues (1066)

classes/admin/class-settings-theme.php (90 issues)

1
<?php
0 ignored issues
show
This file is missing a doc comment.
Loading history...
2
3
namespace lsx\projects\classes\admin;
4
5
//use CMB_Tab_Field;
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...
No space found before comment text; expected "// use CMB_Tab_Field;" but found "//use CMB_Tab_Field;"
Loading history...
Inline comments must end in full-stops, exclamation marks, or question marks
Loading history...
6
7
/**
8
 * Houses the functions for the CMB2 Settings page.
9
 *
10
 * @package lsx-projects
11
 */
12
class Settings_Theme {
13
14
	/**
15
	 * Holds class instance
16
	 *
17
	 * @since 1.0.0
18
	 *
19
	 * @var      object \lsx\projects\classes\admin\Settings_Theme()
20
	 */
21
	protected static $instance = null;
22
23
	/**
24
	 * Will return true if this is the LSX projects settings page.
25
	 *
26
	 * @var array
27
	 */
28
	public $is_options_page = false;
29
30
	/**
31
	 * Holds the id and labels for the navigation.
32
	 *
33
	 * @var array
34
	 */
35
	public $navigation = array();
36
37
	/**
38
	 * Contructor
39
	 */
40
	public function __construct() {
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
41
		add_filter( 'cmb2_enqueue_css', array( $this, 'disable_cmb2_styles' ), 1, 1 );
42
		add_action( 'cmb2_before_form', array( $this, 'generate_navigation' ), 10, 4 );
43
		add_action( 'cmb2_before_title_field_row', array( $this, 'output_tab_open_div' ), 10, 1 );
44
		add_action( 'cmb2_after_tab_closing_field_row', array( $this, 'output_tab_closing_div' ), 10, 1 );
45
		add_action( 'cmb2_render_tab_closing', array( $this, 'cmb2_render_callback_for_tab_closing' ), 10, 5 );
46
		add_filter( 'cmb2_sanitize_tab_closing', array( $this, 'cmb2_sanitize_tab_closing_callback' ), 10, 2 );
47
		add_action( 'cmb2_after_form', array( $this, 'navigation_js' ), 10, 4 );
48
		add_filter( 'cmb2_options_page_redirect_url', array( $this, 'add_tab_argument' ), 10, 1 );
49
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
50
51
	/**
52
	 * Return an instance of this class.
53
	 *
54
	 * @since 1.0.0
55
	 *
56
	 * @return    object \lsx\projects\classes\admin\Settings_Theme()    A single instance of this class.
57
	 */
58
	public static function get_instance() {
59
		// If the single instance hasn't been set, set it now.
60
		if ( null == self::$instance ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Found: ==. Use strict comparisons (=== or !==).
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
61
			self::$instance = new self();
62
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
63
		return self::$instance;
64
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
65
66
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$enabled" missing
Loading history...
67
	 * Disable CMB2 styles on front end forms.
68
	 *
69
	 * @return bool $enabled Whether to enable (enqueue) styles.
70
	 */
71
	public function disable_cmb2_styles( $enabled ) {
72
		if ( is_admin() ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
73
			$current_screen = get_current_screen();
74
			if ( is_object( $current_screen ) && 'project_page_lsx_projects_options' === $current_screen->id ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
75
				$enabled = false;
76
			}
77
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
78
		return $enabled;
79
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
80
81
	/**
82
	 * Generates the tabbed navigation for the settings page.
83
	 *
84
	 * @param string $cmb_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
85
	 * @param string $object_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
86
	 * @param string $object_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
87
	 * @param object $cmb2_obj
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
88
	 * @return void
89
	 */
90
	public function generate_navigation( $cmb_id, $object_id, $object_type, $cmb2_obj ) {
91
		if ( 'lsx_projects_settings' === $cmb_id && 'lsx_projects_options' === $object_id && 'options-page' === $object_type ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
92
			$this->navigation      = array();
93
			$this->is_options_page = true;
0 ignored issues
show
Documentation Bug introduced by
It seems like true of type true is incompatible with the declared type array of property $is_options_page.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
94
			if ( isset( $cmb2_obj->meta_box['fields'] ) && ! empty( $cmb2_obj->meta_box['fields'] ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
95
				foreach ( $cmb2_obj->meta_box['fields'] as $field_index => $field ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
96
					if ( 'title' === $field['type'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
97
						$this->navigation[ $field_index ] = $field['name'];
98
					}
99
				}
100
			}
0 ignored issues
show
No blank line found after control structure
Loading history...
101
			$this->output_navigation();
102
		}
103
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
104
105
	/**
106
	 * Outputs the WP style navigation for the Settings page.
107
	 *
108
	 * @return void
109
	 */
110
	public function output_navigation() {
111
		if ( ! empty( $this->navigation ) ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
112
			?>
113
			<div class="wp-filter hide-if-no-js">
114
				<ul class="filter-links">
115
					<?php
116
					$first_tab    = true;
117
					$total        = count( $this->navigation );
118
					$count        = 0;
119
					$separator    = ' |';
120
					$selected_tab = '';
121
					if ( isset( $_GET['cmb_tab'] ) && '' !== $_GET['cmb_tab'] ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Processing form data without nonce verification.
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
122
						$selected_tab  = sanitize_text_field( $_GET['cmb_tab'] );
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces

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...
Processing form data without nonce verification.
Loading history...
$_GET data not unslashed before sanitization. Use wp_unslash() or similar
Loading history...
123
						$selected_tab  = 'settings_' . $selected_tab;
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces

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...
124
					}
0 ignored issues
show
No blank line found after control structure
Loading history...
125
					foreach ( $this->navigation as $key => $label ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
126
						$count++;
127
						$current_css = '';
128
						if ( ( true === $first_tab && '' === $selected_tab ) || $key === $selected_tab ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
129
							$first_tab   = false;
130
							$current_css = 'current';
131
						}
0 ignored issues
show
No blank line found after control structure
Loading history...
132
						if ( $count === $total ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
133
							$separator = '';
134
						}
135
						?>
136
							<li><a href="#" class="<?php echo esc_attr( $current_css ); ?>" data-sort="<?php echo esc_attr( $key ); ?>_tab"><?php echo esc_attr( $label ); ?></a><?php echo esc_attr( $separator ); ?></li>
137
						<?php
138
					}
139
					?>
140
				</ul>
141
			</div>
142
			<?php
143
		}
144
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
145
146
	/**
147
	 * Outputs the opening tab div.
148
	 *
149
	 * @param object $field CMB2_Field();
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
150
	 * @return void
151
	 */
152
	public function output_tab_open_div( $field ) {
153
		if ( true === $this->is_options_page && isset( $field->args['type'] ) && 'title' === $field->args['type'] ) {
0 ignored issues
show
The condition true === $this->is_options_page is always false.
Loading history...
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
154
			?>
155
			<div id="<?php echo esc_attr( $field->args['id'] ); ?>_tab" class="tab tab-nav hidden">
156
			<?php
157
		}
158
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
159
160
	/**
161
	 * Outputs the opening closing div.
162
	 *
163
	 * @param object $field CMB2_Field();
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
164
	 * @return void
165
	 */
166
	public function output_tab_closing_div( $field ) {
167
		if ( true === $this->is_options_page && isset( $field->args['type'] ) && 'tab_closing' === $field->args['type'] ) {
0 ignored issues
show
The condition true === $this->is_options_page is always false.
Loading history...
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
168
			?>
169
			</div>
170
			<?php
171
		}
172
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
173
174
	public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function cmb2_render_callback_for_tab_closing()
Loading history...
175
		return;
0 ignored issues
show
Empty return statement not required here
Loading history...
176
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
177
178
	public function cmb2_sanitize_tab_closing_callback( $override_value, $value ) {
0 ignored issues
show
Coding Style Documentation introduced by
Missing doc comment for function cmb2_sanitize_tab_closing_callback()
Loading history...
179
		return '';
180
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
181
182
	/**
183
	 * Outputs the Script for the tabbed navigation.
184
	 *
185
	 * @param string $cmb_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
186
	 * @param string $object_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
187
	 * @param string $object_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
188
	 * @param object $cmb2_obj
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
189
	 * @return void
190
	 */
191
	public function navigation_js( $cmb_id, $object_id, $object_type, $cmb2_obj ) {
192
		if ( 'lsx_projects_settings' === $cmb_id && 'lsx_projects_options' === $object_id && 'options-page' === $object_type ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
193
			?>
194
			<script>
195
				var LSX_PROJECTS_CMB2 = Object.create( null );
196
197
				;( function( $, window, document, undefined ) {
198
199
					'use strict';
200
201
					LSX_PROJECTS_CMB2.document = $(document);
202
203
					/**
204
					 * Start the JS Class
205
					 */
206
					LSX_PROJECTS_CMB2.init = function() {
207
						var tab = LSX_PROJECTS_CMB2.urlParam( 'cmb_tab' );
208
						if ( 0 === tab || '0' === tab ) {
209
							tab = '';
210
						}
211
						LSX_PROJECTS_CMB2.addTabInput( tab );
212
						LSX_PROJECTS_CMB2.prepNavigation( tab );
213
						LSX_PROJECTS_CMB2.watchNavigation();
214
					};
215
216
					LSX_PROJECTS_CMB2.addTabInput = function( tab = '' ) {
217
						var counter = 1;
218
						$( "form.cmb-form" ).append('<input type="hidden" name="cmb_tab" value="' + tab + '" />');
219
					}
220
221
					LSX_PROJECTS_CMB2.prepNavigation = function( tab = '' ) {
222
						var counter = 1;
223
						$( ".tab.tab-nav" ).each(function(){
224
							console.log( tab );
225
							if ( ( 1 !== counter && '' === tab ) || ( '' !== tab && 'settings_' + tab + '_tab' !== $( this ).attr('id') ) ) {
226
								$( this ).hide().removeClass('hidden');
227
							} else {
228
								$( this ).addClass( 'current' ).removeClass('hidden');
229
							}
230
							counter++;
231
						});
232
					}
233
234
					LSX_PROJECTS_CMB2.watchNavigation = function() {
235
						$( ".wp-filter li a" ).on( 'click', function(event){
236
							event.preventDefault();
237
							// Change the current Tab heading.
238
							$( ".wp-filter li a" ).removeClass('current');
239
							$( this ).addClass('current');
240
241
							// Change the current tab div.
242
							var target = $( this ).attr('data-sort');
243
							$( ".tab.tab-nav.current" ).hide().removeClass('current');
244
							$( "#"+target ).show().addClass('current');
245
							$( 'input[name="cmb_tab"]').val(target);
246
						});
247
					};
248
249
					LSX_PROJECTS_CMB2.urlParam = function(name){
250
						var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
251
						if ( results == null ){
252
							return 0;
253
						} else {
254
							return results[1] || 0;
255
						}
256
					}
257
258
					LSX_PROJECTS_CMB2.document.ready( function() {
259
						LSX_PROJECTS_CMB2.init();
260
					} );
261
262
				} )( jQuery, window, document );
263
			</script>
264
			<?php
265
		}
266
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 1 found
Loading history...
267
268
	/**
269
	 * This will add the tab selection to the url.
270
	 *
271
	 * @param string $url
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
272
	 * @return void
0 ignored issues
show
Function return type is void, but function contains return statement
Loading history...
273
	 */
274
	public function add_tab_argument( $url ) {
275
		if ( isset( $_POST['cmb_tab'] ) && '' !== $_POST['cmb_tab'] ) { // @codingStandardsIgnoreLine
276
			$tab_selection = sanitize_text_field( $_POST['cmb_tab'] ); // @codingStandardsIgnoreLine
277
			$tab_selection = str_replace( array( 'settings_', '_tab' ), '', $tab_selection ); // @codingStandardsIgnoreLine
278
			if ( 'single' !== $tab_selection ) {
0 ignored issues
show
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
279
				$url = add_query_arg( 'cmb_tab', $tab_selection, $url );
280
			} else {
281
				$url = remove_query_arg( 'cmb_tab', $url );
282
			}
283
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
284
		return $url;
285
	}
0 ignored issues
show
Expected 1 blank line before closing function brace; 0 found
Loading history...
Expected 2 blank lines after function; 0 found
Loading history...
286
}
287