Passed
Pull Request — master (#4)
by Warwick
02:31
created

Settings_Theme::generate_navigation()   B

Complexity

Conditions 8
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 8
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 12
rs 8.4444
1
<?php
2
namespace lsx\sharing\classes\admin;
3
4
use CMB_Tab_Field;
0 ignored issues
show
Bug introduced by
The type CMB_Tab_Field was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
6
/**
7
 * Houses the functions for the CMB2 Settings page.
8
 *
9
 * @package lsx-sharing
10
 */
11
class Settings_Theme {
12
13
	/**
14
	 * Holds class instance
15
	 *
16
	 * @since 1.0.0
17
	 *
18
	 * @var      object \lsx\sharing\classes\admin\Settings_Theme()
19
	 */
20
	protected static $instance = null;
21
22
	/**
23
	 * Will return true if this is the LSX Sharing settings page.
24
	 *
25
	 * @var array
26
	 */
27
	public $is_options_page = false;
28
29
	/**
30
	 * Holds the id and labels for the navigation.
31
	 *
32
	 * @var array
33
	 */
34
	public $navigation = array();
35
36
	/**
37
	 * Contructor
38
	 */
39
	public function __construct() {
40
		add_filter( 'cmb2_enqueue_css', array( $this, 'disable_cmb2_styles' ), 1, 1 );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
		/** @scrutinizer ignore-call */ 
41
  add_filter( 'cmb2_enqueue_css', array( $this, 'disable_cmb2_styles' ), 1, 1 );
Loading history...
41
		add_action( 'cmb2_before_form', array( $this, 'generate_navigation' ), 10, 4 );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		/** @scrutinizer ignore-call */ 
42
  add_action( 'cmb2_before_form', array( $this, 'generate_navigation' ), 10, 4 );
Loading history...
42
		add_action( 'cmb2_before_title_field_row', array( $this, 'output_tab_open_div' ), 10, 1 );
43
		add_action( 'cmb2_after_tab_closing_field_row', array( $this, 'output_tab_closing_div' ), 10, 1 );
44
		add_action( 'cmb2_render_tab_closing', array( $this, 'cmb2_render_callback_for_tab_closing' ), 10, 5 );
45
		add_filter( 'cmb2_sanitize_tab_closing', array( $this, 'cmb2_sanitize_tab_closing_callback' ), 10, 2 );
46
		add_action( 'cmb2_after_form', array( $this, 'navigation_js' ), 10, 4 );
47
		add_filter( 'cmb2_options_page_redirect_url', array( $this, 'add_tab_argument' ), 10, 1 );
48
	}
49
50
	/**
51
	 * Return an instance of this class.
52
	 *
53
	 * @since 1.0.0
54
	 *
55
	 * @return    object \lsx\sharing\classes\admin\Settings_Theme()    A single instance of this class.
56
	 */
57
	public static function get_instance() {
58
		// If the single instance hasn't been set, set it now.
59
		if ( null == self::$instance ) {
60
			self::$instance = new self();
61
		}
62
		return self::$instance;
63
	}
64
65
	/**
66
	 * Disable CMB2 styles on front end forms.
67
	 *
68
	 * @return bool $enabled Whether to enable (enqueue) styles.
69
	 */
70
	public function disable_cmb2_styles( $enabled ) {
71
		if ( is_admin() ) {
0 ignored issues
show
Bug introduced by
The function is_admin was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
		if ( /** @scrutinizer ignore-call */ is_admin() ) {
Loading history...
72
			$current_screen = get_current_screen();
0 ignored issues
show
Bug introduced by
The function get_current_screen was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
			$current_screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
73
			if ( is_object( $current_screen ) && 'settings_page_lsx-sharing-settings' === $current_screen->id ) {
74
				$enabled = false;
75
			}
76
		}
77
		return $enabled;
78
	}
79
80
	/**
81
	 * Generates the tabbed navigation for the settings page.
82
	 *
83
	 * @param string $cmb_id
84
	 * @param string $object_id
85
	 * @param string $object_type
86
	 * @param object $cmb2_obj
87
	 * @return void
88
	 */
89
	public function generate_navigation( $cmb_id, $object_id, $object_type, $cmb2_obj ) {
90
		if ( 'lsx_sharing_settings' === $cmb_id && 'lsx-sharing-settings' === $object_id && 'options-page' === $object_type ) {
91
			$this->navigation      = array();
92
			$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...
93
			if ( isset( $cmb2_obj->meta_box['fields'] ) && ! empty( $cmb2_obj->meta_box['fields'] ) ) {
94
				foreach ( $cmb2_obj->meta_box['fields'] as $field_index => $field ) {
95
					if ( 'title' === $field['type'] ) {
96
						$this->navigation[ $field_index ] = $field['name'];
97
					}
98
				}
99
			}
100
			$this->output_navigation();
101
		}
102
	}
103
104
	/**
105
	 * Outputs the WP style navigation for the Settings page.
106
	 *
107
	 * @return void
108
	 */
109
	public function output_navigation() {
110
		if ( ! empty( $this->navigation ) ) {
111
			?>
112
			<div class="wp-filter hide-if-no-js">
113
				<ul class="filter-links">
114
					<?php
115
					$first_tab    = true;
116
					$total        = count( $this->navigation );
117
					$count        = 0;
118
					$separator    = ' |';
119
					$selected_tab = '';
120
					if ( isset( $_GET['cmb_tab'] ) && '' !== $_GET['cmb_tab'] ) {
121
						$selected_tab  = sanitize_text_field( $_GET['cmb_tab'] );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
						$selected_tab  = /** @scrutinizer ignore-call */ sanitize_text_field( $_GET['cmb_tab'] );
Loading history...
122
						$selected_tab  = 'settings_' . $selected_tab;
123
					}
124
					foreach ( $this->navigation as $key => $label ) {
125
						$count++;
126
						$current_css = '';
127
						if ( ( true === $first_tab && '' === $selected_tab ) || $key === $selected_tab ) {
128
							$first_tab   = false;
129
							$current_css = 'current';
130
						}
131
						if ( $count === $total ) {
132
							$separator = '';
133
						}
134
						?>
135
							<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>
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
							<li><a href="#" class="<?php echo /** @scrutinizer ignore-call */ esc_attr( $current_css ); ?>" data-sort="<?php echo esc_attr( $key ); ?>_tab"><?php echo esc_attr( $label ); ?></a><?php echo esc_attr( $separator ); ?></li>
Loading history...
136
						<?php
137
					}
138
					?>
139
				</ul>
140
			</div>
141
			<?php
142
		}
143
	}
144
145
	/**
146
	 * Outputs the opening tab div.
147
	 *
148
	 * @param object $field CMB2_Field();
149
	 * @return void
150
	 */
151
	public function output_tab_open_div( $field ) {
152
		if ( true === $this->is_options_page && isset( $field->args['type'] ) && 'title' === $field->args['type'] ) {
0 ignored issues
show
introduced by
The condition true === $this->is_options_page is always false.
Loading history...
153
			?>
154
			<div id="<?php echo esc_attr( $field->args['id'] ); ?>_tab" class="tab tab-nav hidden">
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
			<div id="<?php echo /** @scrutinizer ignore-call */ esc_attr( $field->args['id'] ); ?>_tab" class="tab tab-nav hidden">
Loading history...
155
			<?php
156
		}
157
	}
158
159
	/**
160
	 * Outputs the opening closing div.
161
	 *
162
	 * @param object $field CMB2_Field();
163
	 * @return void
164
	 */
165
	public function output_tab_closing_div( $field ) {
166
		if ( true === $this->is_options_page && isset( $field->args['type'] ) && 'tab_closing' === $field->args['type'] ) {
0 ignored issues
show
introduced by
The condition true === $this->is_options_page is always false.
Loading history...
167
			?>
168
			</div>
169
			<?php
170
		}
171
	}
172
173
	public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, $object_id, $object_type, $field_type_object ) {
0 ignored issues
show
Unused Code introduced by
The parameter $escaped_value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
	public function cmb2_render_callback_for_tab_closing( $field, /** @scrutinizer ignore-unused */ $escaped_value, $object_id, $object_type, $field_type_object ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
	public function cmb2_render_callback_for_tab_closing( /** @scrutinizer ignore-unused */ $field, $escaped_value, $object_id, $object_type, $field_type_object ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object_type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
	public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, $object_id, /** @scrutinizer ignore-unused */ $object_type, $field_type_object ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $object_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
	public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, /** @scrutinizer ignore-unused */ $object_id, $object_type, $field_type_object ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $field_type_object is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

173
	public function cmb2_render_callback_for_tab_closing( $field, $escaped_value, $object_id, $object_type, /** @scrutinizer ignore-unused */ $field_type_object ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
174
		return;
175
	}
176
177
	public function cmb2_sanitize_tab_closing_callback( $override_value, $value ) {
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

177
	public function cmb2_sanitize_tab_closing_callback( $override_value, /** @scrutinizer ignore-unused */ $value ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $override_value is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

177
	public function cmb2_sanitize_tab_closing_callback( /** @scrutinizer ignore-unused */ $override_value, $value ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
		return '';
179
	}
180
181
	/**
182
	 * Outputs the Script for the tabbed navigation.
183
	 *
184
	 * @param string $cmb_id
185
	 * @param string $object_id
186
	 * @param string $object_type
187
	 * @return void
188
	 */
189
	public function navigation_js( $cmb_id, $object_id, $object_type ) {
190
		if ( 'lsx_sharing_settings' === $cmb_id && 'lsx-sharing-settings' === $object_id && 'options-page' === $object_type ) {
191
			?>
192
			<script>
193
				var LSX_SHARING_CMB2 = Object.create( null );
194
195
				;( function( $, window, document, undefined ) {
196
197
					'use strict';
198
199
					LSX_SHARING_CMB2.document = $(document);
200
201
					<?php $this->navigation_js_init(); ?>
202
203
					<?php $this->navigation_js_add_tab_input(); ?>
204
205
					<?php $this->navigation_js_prep_navigation(); ?>
206
207
					<?php $this->navigation_js_watch_navigation(); ?>
208
209
					<?php $this->navigation_js_url_param(); ?>
210
211
					LSX_SHARING_CMB2.document.ready( function() {
212
						LSX_SHARING_CMB2.init();
213
					} );
214
215
				} )( jQuery, window, document );
216
			</script>
217
			<?php
218
		}
219
	}
220
221
	/**
222
	 * This will add the tab selection to the url.
223
	 *
224
	 * @param string $url
225
	 * @return void
226
	 */
227
	public function add_tab_argument( $url ) {
228
		if ( isset( $_POST['cmb_tab'] ) && '' !== $_POST['cmb_tab'] ) { // @codingStandardsIgnoreLine
229
			$tab_selection = sanitize_text_field( $_POST['cmb_tab'] ); // @codingStandardsIgnoreLine
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

229
			$tab_selection = /** @scrutinizer ignore-call */ sanitize_text_field( $_POST['cmb_tab'] ); // @codingStandardsIgnoreLine
Loading history...
230
			$tab_selection = str_replace( array( 'settings_', '_tab' ), '', $tab_selection ); // @codingStandardsIgnoreLine
231
			if ( 'single' !== $tab_selection ) {
232
				$url = add_query_arg( 'cmb_tab', $tab_selection, $url );
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

232
				$url = /** @scrutinizer ignore-call */ add_query_arg( 'cmb_tab', $tab_selection, $url );
Loading history...
233
			} else {
234
				$url = remove_query_arg( 'cmb_tab', $url );
0 ignored issues
show
Bug introduced by
The function remove_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

234
				$url = /** @scrutinizer ignore-call */ remove_query_arg( 'cmb_tab', $url );
Loading history...
235
			}
236
		}
237
		return $url;
238
	}
239
240
	/**
241
	 * The JS function to init the navigation JS.
242
	 *
243
	 * @return void
244
	 */
245
	public function navigation_js_init() {
246
		?>
247
			/**
248
			* Start the JS Class
249
			*/
250
			LSX_SHARING_CMB2.init = function() {
251
				var tab = LSX_SHARING_CMB2.urlParam( 'cmb_tab' );
252
				if ( 0 === tab || '0' === tab ) {
253
					tab = '';
254
			}
255
			LSX_SHARING_CMB2.addTabInput( tab );
256
			LSX_SHARING_CMB2.prepNavigation( tab );
257
			LSX_SHARING_CMB2.watchNavigation();
258
			};
259
		<?php
260
	}
261
	public function navigation_js_add_tab_input() {
262
		?>
263
		LSX_SHARING_CMB2.addTabInput = function( tab = '' ) {
264
			var counter = 1;
265
			$( "form.cmb-form" ).append('<input type="hidden" name="cmb_tab" value="' + tab + '" />');
266
		}
267
		<?php
268
	}
269
270
	public function navigation_js_url_param() {
271
		?>
272
		LSX_SHARING_CMB2.urlParam = function(name){
273
			var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
274
			if ( results == null ){
275
				return 0;
276
			} else {
277
				return results[1] || 0;
278
			}
279
		}
280
		<?php
281
	}
282
	/**
283
	 * The JS function to init the watch navigation JS.
284
	 *
285
	 * @return void
286
	 */
287
	public function navigation_js_watch_navigation() {
288
		?>
289
		LSX_SHARING_CMB2.watchNavigation = function() {
290
			$( ".wp-filter li a" ).on( 'click', function(event){
291
				event.preventDefault();
292
				// Change the current Tab heading.
293
				$( ".wp-filter li a" ).removeClass('current');
294
				$( this ).addClass('current');
295
296
				// Change the current tab div.
297
				var target = $( this ).attr('data-sort');
298
				$( ".tab.tab-nav.current" ).hide().removeClass('current');
299
				$( "#"+target ).show().addClass('current');
300
				$( 'input[name="cmb_tab"]').val(target);
301
			});
302
		};
303
		<?php
304
	}
305
306
	/**
307
	 * Preps the navigation for the js functions.
308
	 *
309
	 * @return void
310
	 */
311
	public function navigation_js_prep_navigation() {
312
		?>
313
		LSX_SHARING_CMB2.prepNavigation = function( tab = '' ) {
314
			var counter = 1;
315
			$( ".tab.tab-nav" ).each(function(){
316
				console.log( tab );
317
				if ( ( 1 !== counter && '' === tab ) || ( '' !== tab && 'settings_' + tab + '_tab' !== $( this ).attr('id') ) ) {
318
					$( this ).hide().removeClass('hidden');
319
				} else {
320
					$( this ).addClass( 'current' ).removeClass('hidden');
321
				}
322
				counter++;
323
			});
324
		}
325
		<?php
326
	}
327
}
328