Issues (1223)

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

1
<?php
0 ignored issues
show
This file is missing a doc comment.
Loading history...
2
3
namespace lsx\team\classes\admin;
4
5
/**
6
 * Houses the functions for the CMB2 Settings page.
7
 *
8
 * @package lsx-search
9
 */
10
class Settings_Theme {
11
12
	/**
13
	 * Holds class instance
14
	 *
15
	 * @since 1.0.0
16
	 *
17
	 * @var      object \lsx_team\classes\admin\Settings_Theme()
18
	 */
19
	protected static $instance = null;
20
21
	/**
22
	 * Will return true if this is the LSX Search settings page.
23
	 *
24
	 * @var array
25
	 */
26
	public $is_options_page = false;
27
28
	/**
29
	 * Holds the id and labels for the navigation.
30
	 *
31
	 * @var array
32
	 */
33
	public $navigation = array();
34
35
	/**
36
	 * Contructor
37
	 */
38
	public function __construct() {
0 ignored issues
show
Expected 2 blank lines before function; 1 found
Loading history...
39
		add_filter( 'cmb2_enqueue_css', array( $this, 'disable_cmb2_styles' ), 1, 1 );
40
		add_action( 'cmb2_before_form', array( $this, 'generate_navigation' ), 10, 4 );
41
		add_action( 'cmb2_before_title_field_row', array( $this, 'output_tab_open_div' ), 10, 1 );
42
		add_action( 'cmb2_after_tab_closing_field_row', array( $this, 'output_tab_closing_div' ), 10, 1 );
43
		add_action( 'cmb2_render_tab_closing', array( $this, 'cmb2_render_callback_for_tab_closing' ), 10, 5 );
44
		add_filter( 'cmb2_sanitize_tab_closing', array( $this, 'cmb2_sanitize_tab_closing_callback' ), 10, 2 );
45
		add_action( 'cmb2_after_form', array( $this, 'navigation_js' ), 10, 4 );
46
		add_filter( 'cmb2_options_page_redirect_url', array( $this, 'add_tab_argument' ), 10, 1 );
47
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
48
49
	/**
50
	 * Return an instance of this class.
51
	 *
52
	 * @since 1.0.0
53
	 *
54
	 * @return    object \lsx_team\classes\admin\Settings_Theme()    A single instance of this class.
55
	 */
56
	public static function get_instance() {
57
		// If the single instance hasn't been set, set it now.
58
		if ( null == self::$instance ) {
0 ignored issues
show
Found: ==. Use strict comparisons (=== or !==).
Loading history...
Expected 0 spaces after opening bracket; 1 found
Loading history...
Expected 0 spaces before closing bracket; 1 found
Loading history...
59
			self::$instance = new self();
60
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
61
		return self::$instance;
62
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
63
64
	/**
0 ignored issues
show
Coding Style Documentation introduced by
Doc comment for parameter "$enabled" missing
Loading history...
65
	 * Disable CMB2 styles on front end forms.
66
	 *
67
	 * @return bool $enabled Whether to enable (enqueue) styles.
68
	 */
69
	public function disable_cmb2_styles( $enabled ) {
70
		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...
71
			$current_screen = get_current_screen();
72
			if ( is_object( $current_screen ) && 'team_page_lsx_team_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...
73
				$enabled = false;
74
			}
75
		}
0 ignored issues
show
No blank line found after control structure
Loading history...
76
		return $enabled;
77
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
78
79
	/**
80
	 * Generates the tabbed navigation for the settings page.
81
	 *
82
	 * @param string $cmb_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
83
	 * @param string $object_id
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
84
	 * @param string $object_type
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
85
	 * @param object $cmb2_obj
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
86
	 * @return void
87
	 */
88
	public function generate_navigation( $cmb_id, $object_id, $object_type, $cmb2_obj ) {
89
		if ( 'lsx_team_settings' === $cmb_id && 'lsx_team_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...
90
			$this->navigation      = array();
91
			$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...
92
			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...
93
				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...
94
					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...
95
						$this->navigation[ $field_index ] = $field['name'];
96
					}
97
				}
98
			}
0 ignored issues
show
No blank line found after control structure
Loading history...
99
			$this->output_navigation();
100
		}
101
	}
0 ignored issues
show
Expected 2 blank lines after function; 1 found
Loading history...
Expected 1 blank line before closing function brace; 0 found
Loading history...
102
103
	/**
104
	 * Outputs the WP style navigation for the Settings page.
105
	 *
106
	 * @return void
107
	 */
108
	public function output_navigation() {
109
		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...
110
			?>
111
			<div class="wp-filter hide-if-no-js">
112
				<ul class="filter-links">
113
					<?php
114
					$first_tab    = true;
115
					$total        = count( $this->navigation );
116
					$count        = 0;
117
					$separator    = ' |';
118
					$selected_tab = '';
119
					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...
120
						$selected_tab  = sanitize_text_field( wp_unslash( $_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...
It seems like wp_unslash($_GET['cmb_tab']) can also be of type array; however, parameter $str of sanitize_text_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

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