Passed
Push — master ( 26770f...806cdd )
by Chris
03:20
created

uix::enqueue_admin_stylescripts()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 44
rs 8.9137
c 0
b 0
f 0
cc 6
nc 9
nop 0
1
<?php
2
/**
3
 * UIX Setting Class.
4
 *
5
 * @package   uix
6
 * @author    David Cramer
7
 * @license   GPL-2.0+
8
 * @link
9
 * @copyright 2016 David Cramer
10
 */
11
namespace lsx\ui;
12
13
/**
14
 * Settings class
15
 *
16
 * @package uix
17
 * @author  David Cramer
18
 */
19
class uix {
20
21
	/**
22
	 * The slug for this plugin
23
	 *
24
	 * @since 1.0.0
25
	 * @var      string
26
	 */
27
	protected $plugin_slug = null;
28
29
	/**
30
	 * List of registered pages
31
	 *
32
	 * @since 1.0.0
33
	 * @var      array
34
	 */
35
	protected $pages = array();
36
37
	/**
38
	 * List of registered metaboxes
39
	 *
40
	 * @since 1.0.0
41
	 * @var      array
42
	 */
43
	protected $metaboxes = array();
44
45
	/**
46
	 * Holds class instance
47
	 *
48
	 * @since 1.0.0
49
	 * @var      object|\uix
0 ignored issues
show
Bug introduced by
The type uix 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...
50
	 */
51
	protected static $instance = null;
52
53
	/**
54
	 * Holds the option screen prefix
55
	 *
56
	 * @since 1.0.0
57
	 * @var      string
58
	 */
59
	protected $plugin_screen_hook_suffix = null;
60
61
	/**
62
	 * Holds the current_tab being rendered
63
	 *
64
	 * @since 1.0.0
65
	 * @var      string
66
	 */
67
	public $current_tab = null;
68
69
	/**
70
	 * Initialize the plugin by setting localization, filters, and
71
	 * administration functions.
72
	 *
73
	 * @since  1.0.0
74
	 * @access private
75
	 */
76
	private function __construct( $slug ) {
77
78
79
		// set slug
80
		$this->plugin_slug = $slug;
81
82
		// add admin page
83
		add_action( 'admin_menu', array( $this, 'add_settings_pages' ), 25 );
84
85
		// add metaboxes
86
		add_action( 'add_meta_boxes', array( $this, 'add_metaboxes'), 25 );
87
88
		// save config
89
		add_action( 'wp_ajax_' . $this->plugin_slug . '_save_config', array( $this, 'save_config') );
90
91
		// save metabox
92
		add_action( 'save_post', array( $this, 'save_meta' ), 10, 2 );
93
94
	}
95
96
	/**
97
	 * Add metaboxes
98
	 *
99
	 * @since 0.0.1
100
	 *
101
	 * @uses "add_meta_boxes" hook
102
	 */
103
	public function add_metaboxes(){
104
105
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
106
107
		if( !is_object( $screen ) || $screen->base != 'post' ){
108
			return;
109
		}
110
		// post type metaboxes
111
		$configs = array();
112
113
		foreach( (array) $this->metaboxes as $metabox_slug => $metabox ){
114
115
			// only process this post type
116
			if( empty($metabox['post_type']) || !in_array( $screen->post_type, (array) $metabox['post_type'] ) || empty( $metabox[ 'name' ] )  ){
117
				continue;
118
			}
119
120
			add_meta_box(
121
				$metabox_slug,
122
				$metabox['name'],
123
				array( $this, 'render_metabox' ),
124
				$screen->post_type,
125
				$metabox['context'],
126
				$metabox['priority'],
127
				$metabox
128
			);
129
130
			// do scripts
131
			$uix = $this->get_metabox( $metabox_slug );
132
			if( false !== $uix ){
133
				$configs[ $metabox_slug ] = $uix;
134
			}
135
136
		}
137
		// scripts
138
		if( !empty( $configs ) ){
139
			$this->enqueue_metabox_stylescripts( $configs, $screen->post_type );
140
		}
141
142
	}
143
144
145
	public function render_metabox( $post, $metabox ){
146
147
		$uix = $metabox['args'];
148
149
		if( !empty( $uix['base_color'] ) ){
150
		?><style type="text/css">.uix-modal-title > h3,.wrap a.page-title-action:hover{background: <?php echo $uix['base_color']; ?>;}</style>
151
		<?php
152
		}
153
		?>
154
		<input id="uix_<?php echo esc_attr( $metabox['id'] ); ?>" name="uix[<?php echo esc_attr( $metabox['id'] ); ?>]" value="" type="hidden">
155
		<div class="uix-tab-canvas" data-app="<?php echo esc_attr( $metabox['id'] ); ?>"></div>
156
		<script type="text/html" data-template="<?php echo esc_attr( $metabox['id'] ); ?>">
157
			<?php
158
				if( !empty( $uix['template'] ) && file_exists( $uix['template'] ) ){
159
					include $uix['template'];
160
				}else{
161
					echo esc_html__( 'Template not found: ', $this->plugin_slug ) . $uix['template'];
162
				}
163
			?>
164
		</script>
165
		<?php if( !empty( $uix['partials'] ) ){
166
			foreach( $uix['partials'] as $partial_id => $partial ){
167
				?>
168
				<script type="text/html" id="__partial_<?php echo esc_attr( $partial_id ); ?>" data-handlebars-partial="<?php echo esc_attr( $partial_id ); ?>">
169
					<?php
170
						// include this tabs template
171
						if( !empty( $partial ) && file_exists( $partial ) ){
172
							include $partial;
173
						}else{
174
							echo esc_html__( 'Partial Template not found: ', $this->plugin_slug ) . $partial_id;
175
						}
176
					?>
177
				</script>
178
				<?php
179
			}
180
		}
181
182
183
		if( !empty( $uix['modals'] ) ){
184
			foreach( $uix['modals'] as $modal_id => $modal ){
185
				?>
186
				<script type="text/html" id="__modal_<?php echo esc_attr( $modal_id ); ?>" data-handlebars-partial="<?php echo esc_attr( $modal_id ); ?>">
187
					<?php
188
						// include this tabs template
189
						if( !empty( $modal ) && file_exists( $modal ) ){
190
							include $modal;
191
						}else{
192
							echo esc_html__( 'Modal Template not found: ', $this->plugin_slug ) . $modal_id;
193
						}
194
					?>
195
				</script>
196
				<?php
197
			}
198
		}
199
		?>
200
201
202
		<script type="text/html" id="__partial_save">
203
			<button class="button" type="button" data-modal-node="{{__node_path}}" data-app="{{__app}}" data-type="save"
204
				{{#if __callback}}data-callback="{{__callback}}"{{/if}}
205
				{{#if __before}}data-before="{{__before}}"{{/if}}
206
			>
207
				Save Changes
208
			</button>
209
		</script>
210
		<script type="text/html" id="__partial_create">
211
			<button class="button" type="button" data-modal-node="{{__node_path}}" data-app="{{__app}}" data-type="add"
212
				{{#if __callback}}data-callback="{{__callback}}"{{/if}}
213
				{{#if __before}}data-before="{{__before}}"{{/if}}
214
			>
215
				Create
216
			</button>
217
		</script>
218
		<script type="text/html" id="__partial_delete">
219
			<button style="float:left;" class="button" type="button" data-modal-node="{{__node_path}}" data-app="{{__app}}" data-type="delete"
220
				{{#if __callback}}data-callback="{{__callback}}"{{/if}}
221
				{{#if __before}}data-before="{{__before}}"{{/if}}
222
			>
223
				Remove
224
			</button>
225
		</script>
226
		<?php if( !empty( $uix['chromeless'] ) ){ ?>
227
		<script type="text/javascript">
228
			jQuery('#<?php echo $metabox['id']; ?>').addClass('uix-metabox');
229
		</script>
230
		<?php } ?>
231
		<script type="text/javascript">
232
			jQuery( document ).on('submit', '#post', function( e ){
233
234
				var uix_config = conduitPrepObject( '<?php echo $metabox['id']; ?>' );
235
				jQuery('#uix_<?php echo $metabox['id']; ?>').val( JSON.stringify( uix_config.<?php echo $metabox['id']; ?> ) );
236
237
			});
238
		</script>
239
		<?php
240
241
	}
242
243
	/**
244
	 * Return an instance of this class.
245
	 *
246
	 * @since 1.0.0
247
	 *
248
	 * @return    object|\uix\uix    A single instance of this class.
0 ignored issues
show
Bug introduced by
The type uix\uix 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...
249
	 */
250
	public static function get_instance( $slug ) {
251
252
		// If the single instance hasn't been set, set it now.
253
		if ( null == self::$instance ) {
0 ignored issues
show
introduced by
The condition null == self::instance is always false.
Loading history...
254
			self::$instance = new self( $slug );
255
		}
256
257
		return self::$instance;
258
259
	}
260
261
	/**
262
	 * Return a setting
263
	 *
264
	 * @since 1.0.0
265
	 * @return    string/array    the requested setting
0 ignored issues
show
Documentation Bug introduced by
The doc comment string/array at position 0 could not be parsed: Unknown type name 'string/array' at position 0 in string/array.
Loading history...
266
	 */
267
	public static function get_setting( $path, $manual = false ) {
268
269
		$path      = explode( '.', $path );
270
		$temp      = null;
271
		$page_slug = array_shift( $path );
272
273
		if ( null == self::$instance || true === $manual ) {
274
			if ( false === $manual ) {
275
				trigger_error( 'Cannot request a value without a UIX instance. Set second argument to TRUE for manual lookup.' );
276
277
				return;
278
			}
279
			// attempt a manual lookup - requires the full option name
280
			$option_tag = $page_slug;
281
		} else {
282
			if ( ! empty( self::$instance->pages[ $page_slug ]['option_name'] ) ) {
283
				$option_tag = self::$instance->pages[ $page_slug ]['option_name'];
284
			} else {
285
				$option_tag = '_' . self::$instance->plugin_slug . '_' . $page_slug;
286
			}
287
		}
288
		$temp = get_option( $option_tag );
289
		foreach ( $path as $index => $value ) {
290
			if ( ! isset( $temp[ $value ] ) ) {
291
				return null;
292
			}
293
			$temp = $temp[ $value ];
294
		}
295
296
		return $temp;
297
298
	}
299
300
	/**
301
	 * Register the admin pages
302
	 *
303
	 * @since 1.0.0
304
	 */
305
	public function register_pages( $pages ) {
306
307
		/**
308
		 * Filter settings pages to be created
309
		 *
310
		 * @param array $pages Page structures to be created
311
		 */
312
313
		$this->pages = apply_filters( $this->plugin_slug . '_set_admin_pages', $pages );
314
	}
315
316
	/**
317
	 * Register metaboxes
318
	 *
319
	 * @since 1.0.0
320
	 */
321
	public function register_metaboxes( $metaboxes ) {
322
		// register pages
323
		$this->metaboxes = $metaboxes;
324
	}
325
326
327
	/**
328
	 * Add defined contextual help to admin page
329
	 *
330
	 * @since 1.0.0
331
	 */
332
	public function add_help() {
333
334
335
		$page = $this->get_page();
336
337
		if ( ! empty( $page['help'] ) ) {
338
339
			$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
340
341
			foreach ( (array) $page['help'] as $help_slug => $help ) {
342
343
				if ( is_file( $help['content'] ) && file_exists( $help['content'] ) ) {
344
					ob_start();
345
					include $help['content'];
346
					$content = ob_get_clean();
347
				} else {
348
					$content = $help['content'];
349
				}
350
351
				$screen->add_help_tab( array(
352
					'id'      => $help_slug,
353
					'title'   => $help['title'],
354
					'content' => $content,
355
				) );
356
			}
357
358
			// Help sidebars are optional
359
			if ( ! empty( $page['help_sidebar'] ) ) {
360
				$screen->set_help_sidebar( $page['help_sidebar'] );
361
			}
362
		}
363
364
	}
365
366
	/**
367
	 * Saves a metabox config
368
	 *
369
	 * @uses "save_post" hook
370
	 *
371
	 * @since 0.0.1
372
	 */
373
	public function save_meta( $post_id ){
374
375
		if( !empty( $_POST['uix'] ) ){
376
377
			foreach( ( array) $_POST['uix'] as $slug => $data ){
378
				if( empty( $this->metaboxes[ $slug ] ) ){
379
					continue;
380
				}
381
382
				$uix = $this->metaboxes[ $slug ];
383
				$config = json_decode( stripslashes_deep( $data ), true );
384
385
				if( empty( $uix['meta_name'] ) ){
386
					$uix['meta_name'] = '_' . $this->plugin_slug . '_' . sanitize_text_field( $slug );
387
				}
388
				// get config object
389
390
				$config_object = update_post_meta( $post_id, $uix['meta_name'], $config );
391
392
			}
393
394
		}
395
396
	}
397
398
399
	/**
400
	 * Saves a config
401
	 *
402
	 * @uses "wp_ajax_uix_save_config" hook
403
	 *
404
	 * @since 0.0.1
405
	 */
406
	public function save_config() {
407
408
		if ( ! empty( $_POST['config'] ) ) {
409
410
			$config = json_decode( stripslashes_deep( $_POST['config'] ), true );
411
412
			if ( wp_verify_nonce( $_POST['uix_setup'], $this->plugin_slug ) ) {
413
414
				$page_slug = sanitize_text_field( $_POST['page_slug'] );
415
416
				if ( ! empty( $this->pages[ $page_slug ] ) ) {
417
					$params = null;
418
					if ( ! empty( $_POST['params'] ) ) {
419
						$params = $_POST['params'];
420
					}
421
					/**
422
					 * Filter page settings pre save
423
					 *
424
					 * @param array $page_config the page config array
425
					 * @param array $params      any defined save_params.
426
					 */
427
					$page = apply_filters( $this->plugin_slug . '_get_page_save', $this->pages[ $page_slug ], $params );
428
429
					/**
430
					 * Filter config object
431
					 *
432
					 * @param array $config the config array to save
433
					 * @param array $page   the page config to be saved for
434
					 */
435
					$config = apply_filters( $this->plugin_slug . '_pre_save_config', $config, $page );
436
437
438
					$success = __( 'Settings saved.', $this->plugin_slug );
439
					if ( ! empty( $page['saved_message'] ) ) {
440
						$success = $page['saved_message'];
441
					}
442
					$option_tag = '_' . $this->plugin_slug . '_' . $page_slug;
443
					if ( ! empty( $page['option_name'] ) ) {
444
						$option_tag = $page['option_name'];
445
					}
446
					// push backup if not autosave
447
					if ( empty( $_POST['autosave'] ) ) {
448
						$previous = get_option( $option_tag );
449
						if ( ! empty( $previous ) ) {
450
							update_option( $option_tag . '-' . current_time( 'timestamp' ), $previous );
451
						}
452
					}
453
					// save object
454
					update_option( $option_tag, $config );
455
					wp_send_json_success( $success );
456
				}
457
458
			} else {
459
				wp_send_json_error( esc_html__( 'Could not verify nonce', $this->plugin_slug ) );
460
			}
461
462
		}
463
464
		// nope
465
		wp_send_json_error( esc_html__( 'Could not save, sorry.', $this->plugin_slug ) );
466
	}
467
468
469
	/**
470
	 * Register and enqueue admin-specific style sheet.
471
	 *
472
	 * @since 1.0.0
473
	 *
474
	 * @return    null
475
	 */
476
	public function enqueue_admin_stylescripts() {
477
478
		$uix = $this->get_page();
479
		if ( false === $uix ) {
0 ignored issues
show
introduced by
The condition false === $uix is always false.
Loading history...
480
			return;
481
		}
482
483
		$uix['slug'] = $this->plugin_slug;
484
485
		// allow for minimized scripts
486
		$prefix  = '.min';
487
		$uix_url = plugin_dir_url( __FILE__ );
488
		if ( defined( 'DEBUG_SCRIPTS' ) ) {
489
			//$prefix = null;
490
		}
491
		// base styles
492
		wp_enqueue_style( $this->plugin_slug . '-base-icons', $uix_url . 'assets/css/icons' . $prefix . '.css' );
493
		wp_enqueue_style( $this->plugin_slug . '-base-styles', $uix_url . 'assets/css/admin' . $prefix . '.css' );
494
		// enqueue scripts
495
		wp_enqueue_script( 'handlebars', $uix_url . 'assets/js/handlebars.min-latest.js', array(), null, true );
496
		// if has modals
497
		if ( ! empty( $uix['modals'] ) ) {
498
			wp_enqueue_script( $this->plugin_slug . '-core-modals', $uix_url . 'assets/js/uix-modals' . $prefix . '.js', array(
499
				'jquery',
500
				'handlebars',
501
			), null, true );
502
		}
503
		wp_enqueue_script( $this->plugin_slug . '-helpers', $uix_url . 'assets/js/uix-helpers' . $prefix . '.js', array( 'handlebars' ), null, true );
504
		wp_enqueue_script( $this->plugin_slug . '-core-admin', $uix_url . 'assets/js/uix-core' . $prefix . '.js', array(
505
			'jquery',
506
			'handlebars',
507
		), null, true );
508
509
		// enqueue admin runtime styles
510
		$this->enqueue_set( $uix, $this->plugin_slug . '-' . $uix['page_slug'] );
511
512
		// enqueue tab specific runtime styles
513
		if ( ! empty( $uix['tabs'] ) ) {
514
			foreach ( $uix['tabs'] as $tab_slug => $tab ) {
515
				//$this->enqueue_set( $tab, $this->plugin_slug . '-' . $uix['page_slug'] . '-' . $tab_slug );
516
			}
517
		}
518
519
		wp_localize_script( $this->plugin_slug . '-core-admin', 'uix', $uix );
520
	}
521
522
	/**
523
	 * Register and enqueue admin-specific style sheet.
524
	 *
525
	 * @since 1.0.0
526
	 *
527
	 * @return    null
528
	 */
529
	public function enqueue_metabox_stylescripts( $metaboxes, $post_type ) {
530
531
532
		$uix = array(
533
			'config'	=> array(),
534
			'slug'		=> $this->plugin_slug,
535
			'page_slug'	=> $post_type
536
		);
537
538
539
		// allow for minimized scripts
540
		$prefix = '.min';
541
		$uix_url = plugin_dir_url( __FILE__ );
542
		if( defined( 'DEBUG_SCRIPTS' ) ){
543
			$prefix = null;
544
		}
545
		// base styles
546
		wp_enqueue_style( $this->plugin_slug . '-base-icons', $uix_url . 'assets/css/icons' . $prefix . '.css' );
547
		wp_enqueue_style( $this->plugin_slug . '-base-styles', $uix_url . 'assets/css/metabox' . $prefix . '.css' );
548
		// enqueue scripts
549
		wp_enqueue_script( 'handlebars', $uix_url . 'assets/js/handlebars.min-latest.js', array(), null, true );
550
		// if has modals
551
552
		wp_enqueue_script( $this->plugin_slug . '-helpers', $uix_url . 'assets/js/uix-helpers' . $prefix . '.js', array( 'handlebars' ), null, true );
553
		wp_enqueue_script( $this->plugin_slug . '-core-admin', $uix_url . 'assets/js/uix-core' . $prefix . '.js', array( 'jquery', 'handlebars' ), null, true );
554
555
		foreach( $metaboxes as $slug=>$metabox ){
556
			if( !empty( $metabox['modals'] ) ){
557
				$uix['modals'] = true;
558
			}
559
			$uix['config'][ $slug ] = $metabox['config'];
560
			// enqueue admin runtime styles
561
			$this->enqueue_set( $metabox, $this->plugin_slug . '-' . $slug );
562
563
		}
564
		if( !empty( $uix['modals'] ) ){
565
			wp_enqueue_script( $this->plugin_slug . '-core-modals', $uix_url . 'assets/js/uix-modals' . $prefix . '.js', array( 'jquery', 'handlebars' ), null, true );
566
		}
567
568
569
		wp_localize_script( $this->plugin_slug . '-core-admin', 'uix', $uix );
570
	}
571
572
573
	/**
574
	 * enqueue a set of styles and scripts
575
	 *
576
	 * @since 0.0.1
577
	 *
578
	 */
579
	private function enqueue_set( $set, $prefix ) {
580
		// go over the set to see if it has styles or scripts
581
582
		// setup default args for array type includes
583
		$arguments_array = array(
584
			"src"       => false,
585
			"deps"      => array(),
586
			"ver"       => false,
587
			"in_footer" => false,
588
			"media"     => false,
589
		);
590
591
		// enqueue set specific runtime styles
592
		if ( ! empty( $set['styles'] ) ) {
593
			foreach ( $set['styles'] as $style_key => $style ) {
594
				if ( is_int( $style_key ) ) {
595
					wp_enqueue_style( $style );
596
				} else {
597
					if ( is_array( $style ) ) {
598
						$args = array_merge( $arguments_array, $style );
599
						wp_enqueue_style( $prefix . '-' . $script_key, $args['src'], $args['deps'], $args['ver'], $args['in_footer'] );
600
					} else {
601
						wp_enqueue_style( $prefix . '-' . $style_key, $style );
602
					}
603
				}
604
			}
605
		}
606
		// enqueue set specific runtime scripts
607
		if ( ! empty( $set['scripts'] ) ) {
608
			foreach ( $set['scripts'] as $script_key => $script ) {
609
				if ( is_int( $script_key ) ) {
610
					wp_enqueue_script( $script );
611
				} else {
612
					if ( is_array( $script ) ) {
613
						$args = array_merge( $arguments_array, $script );
614
						wp_enqueue_script( $prefix . '-' . $script_key, $args['src'], $args['deps'], $args['ver'], $args['in_footer'] );
615
					} else {
616
						wp_enqueue_script( $prefix . '-' . $script_key, $script );
617
					}
618
				}
619
			}
620
		}
621
622
	}
623
624
	/**
625
	 * get the config for the current page
626
	 *
627
	 * @since 0.0.1
628
	 * @return array $page array structure of current uix page
629
	 */
630
	private function get_page() {
631
632
		// check that the scrren object is valid to be safe.
633
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
634
635
		if ( empty( $screen ) || ! is_object( $screen ) ) {
636
			return false;
637
		}
638
639
		// get the page slug from base ID
640
		$page_slug = array_search( $screen->base, $this->plugin_screen_hook_suffix );
0 ignored issues
show
Bug introduced by
$this->plugin_screen_hook_suffix of type string is incompatible with the type array expected by parameter $haystack of array_search(). ( Ignorable by Annotation )

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

640
		$page_slug = array_search( $screen->base, /** @scrutinizer ignore-type */ $this->plugin_screen_hook_suffix );
Loading history...
641
		if ( empty( $page_slug ) || empty( $this->pages[ $page_slug ] ) ) {
642
			return false; // in case its not found or the array item is no longer valid, just leave.
643
		}
644
		/**
645
		 * Filter page object
646
		 *
647
		 * @param array $page The page object array.
648
		 */
649
		$uix = apply_filters( $this->plugin_slug . '_get_page', $this->pages[ $page_slug ] );
650
651
		if ( empty( $uix['option_name'] ) ) {
652
			$uix['option_name'] = '_' . $this->plugin_slug . '_' . sanitize_text_field( $page_slug );
653
		}
654
		// get config object
655
		$config_object = get_option( $uix['option_name'], array() );
656
657
		$uix['page_slug'] = $page_slug;
658
		/**
659
		 * Filter config object
660
		 *
661
		 * @param array $config_object The object as retrieved from DB
662
		 * @param array $page_slug     The page slug this object belongs to.
663
		 */
664
		$uix['config'] = apply_filters( $this->plugin_slug . '_get_config', $config_object, $uix );
665
666
667
		return $uix;
668
	}
669
670
	/**
671
	 * get the config for the current metabox
672
	 *
673
	 * @since 0.0.1
674
	 *
675
	 * @return array $metabox array structure of current uix metabox
676
	 */
677
	private function get_metabox( $slug ){
678
		global $post;
679
680
		// check that the scrren object is valid to be safe.
681
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
682
683
		if( empty( $screen ) || !is_object( $screen ) || empty( $screen->post_type ) ){
684
			return false;
685
		}
686
687
		// get the page slug from base ID
688
		if( empty( $this->metaboxes[ $slug ] ) ){
689
			return false; // in case its not found or the array item is no longer valid, just leave.
690
		}
691
		/**
692
		 * Filter page object
693
		 *
694
		 * @param array $page The page object array.
695
		 */
696
		$uix = apply_filters( $this->plugin_slug . '_get_metabox', $this->metaboxes[ $slug ] );
697
698
		if( empty( $uix['meta_name'] ) ){
699
			$uix['meta_name'] = '_' . $this->plugin_slug . '_' . sanitize_text_field( $slug );
700
		}
701
		// get config object
702
703
		$config_object = get_post_meta( $post->ID, $uix['meta_name'], true );
704
705
		$uix['slug'] = $slug;
706
		/**
707
		 * Filter config object
708
		 *
709
		 * @param array $config_object The object as retrieved from DB
710
		 * @param array $slug The page slug this object belongs to.
711
		 */
712
		$uix['config'] = apply_filters( $this->plugin_slug . '_get_meta_config', $config_object, $uix );
713
714
		return $uix;
715
716
	}
717
718
	/**
719
	 * Add options page
720
	 *
721
	 * @since 0.0.1
722
	 * @uses  "admin_menu" hook
723
	 */
724
	public function add_settings_pages() {
725
726
		foreach ( (array) $this->pages as $page_slug => $page ) {
727
728
			if ( empty( $page['page_title'] ) || empty( $page['menu_title'] ) ) {
729
				continue;
730
			}
731
732
			$args = array(
733
				'capability' => 'manage_options',
734
				'icon'       => null,
735
				'position'   => null,
736
			);
737
			$args = array_merge( $args, $page );
738
739
			if ( ! empty( $page['parent'] ) ) {
740
741
				$this->plugin_screen_hook_suffix[ $page_slug ] = add_submenu_page(
742
					$args['parent'],
743
					$args['page_title'],
744
					$args['menu_title'],
745
					$args['capability'],
746
					$this->plugin_slug . '-' . $page_slug,
747
					array( $this, 'create_admin_page' )
748
				);
749
750
			} else {
751
752
				$this->plugin_screen_hook_suffix[ $page_slug ] = add_menu_page(
753
					$args['page_title'],
754
					$args['menu_title'],
755
					$args['capability'],
756
					$this->plugin_slug . '-' . $page_slug,
757
					array( $this, 'create_admin_page' ),
758
					$args['icon'],
759
					$args['position']
760
				);
761
			}
762
			add_action( 'admin_print_styles-' . $this->plugin_screen_hook_suffix[ $page_slug ], array(
763
				$this,
764
				'enqueue_admin_stylescripts',
765
			) );
766
			add_action( 'load-' . $this->plugin_screen_hook_suffix[ $page_slug ], array(
767
				$this,
768
				'add_help',
769
			) );
770
		}
771
	}
772
773
	/**
774
	 * Options page callback
775
	 *
776
	 * @since 0.0.1
777
	 */
778
	public function create_admin_page() {
779
780
		$uix           = $this->get_page();
781
		$template_path = plugin_dir_path( dirname( __FILE__ ) );
782
		?>
783
        <div class="wrap">
784
785
            <h1><?php esc_html_e( $uix['page_title'], $this->plugin_slug ); ?>
786
				<?php if ( ! empty( $uix['version'] ) ) { ?>
787
                    <small><?php esc_html_e( $uix['version'], $this->plugin_slug ); ?></small><?php } ?>
788
            </h1>
789
			<?php if ( ! empty( $uix['tabs'] ) ) { ?>
790
                <div class="wp-filter hide-if-no-js">
791
                    <ul class="filter-links">
792
						<?php foreach ( (array) $uix['tabs'] as $tab_slug => $tab ) {
793
							$display = "";
794
							if ( isset( $tab['disabled'] ) && true === $tab['disabled'] ) {
795
								$display = 'style="display:none;"';
796
							}
797
							?>
798
                            <li><a <?php echo $display; ?>
799
                                        data-tab="<?php echo esc_attr( $tab_slug ); ?>"
800
                                        href="#<?php echo esc_attr( $tab_slug ) ?>"><?php echo esc_html( $tab['menu_title'] ); ?></a>
801
                            </li>
802
						<?php } ?>
803
                    </ul>
804
                </div>
805
			<?php } ?>
806
			<?php wp_nonce_field( $this->plugin_slug, 'uix_setup' ); ?>
807
			<?php
808
			if ( ! empty( $uix['tabs'] ) ) {
809
				$this->current_tab = false;
0 ignored issues
show
Documentation Bug introduced by
The property $current_tab was declared of type string, but false is of type false. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
810
				foreach ( (array) $uix['tabs'] as $tab_slug => $tab ) {
811
					$this->current_tab = $tab_slug; ?>
812
                    <div class="uix-tab-canvas"
813
                         data-app="<?php echo esc_attr( $tab_slug ); ?>"></div>
814
                    <script type="text/html"
815
                            data-template="<?php echo esc_attr( $tab_slug ); ?>">
816
						<?php
817
						if ( ! empty( $tab['page_title'] ) ) {
818
							echo '<h2>' . $tab['page_title'];
819
						}
820
						if ( ! empty( $tab['page_description'] ) ) { ?>
821
                            <small><?php echo $tab['page_description']; ?></small> <?php }
822
						if ( ! empty( $tab['page_title'] ) ) {
823
							echo '</h2>';
824
						}
825
						// include this tabs template
826
						if ( ! empty( $tab['template'] ) && file_exists( $tab['template'] ) ) {
827
							include $tab['template'];
828
						} else {
829
							echo esc_html__( 'Template not found: ', $this->plugin_slug ) . $tab['page_title'];
830
						}
831
						?>
832
                    </script>
833
				<?php if ( ! empty( $tab['partials'] ) ){
834
				foreach ( $tab['partials'] as $partial_id => $partial ){
835
				?>
836
                    <script type="text/html"
837
                            id="__partial_<?php echo esc_attr( $partial_id ); ?>"
838
                            data-handlebars-partial="<?php echo esc_attr( $partial_id ); ?>">
839
						<?php
840
						// include this tabs template
841
						if ( ! empty( $partial ) && file_exists( $template_path . $partial ) ) {
842
							include $template_path . $partial;
843
						} else {
844
							echo esc_html__( 'Partial Template not found: ', $this->plugin_slug ) . $partial_id;
845
						}
846
						?>
847
                    </script>
848
					<?php
849
				}
850
				}
851
				}
852
				$this->current_tab = false;
853
			} else {
854
				if ( ! empty( $uix['template'] ) && file_exists( $uix['template'] ) ) {
855
					include $uix['template'];
856
				}
857
			}
858
			if ( ! empty( $uix['modals'] ) ) {
859
				foreach ( $uix['modals'] as $modal_id => $modal ) {
860
					?>
861
                    <script type="text/html"
862
                            id="__modal_<?php echo esc_attr( $modal_id ); ?>"
863
                            data-handlebars-partial="<?php echo esc_attr( $modal_id ); ?>">
864
						<?php
865
						// include this tabs template
866
						if ( ! empty( $modal ) && file_exists( $template_path . $modal ) ) {
867
							include $template_path . $modal;
868
						} else {
869
							echo esc_html__( 'Modal Template not found: ', $this->plugin_slug ) . $modal_id;
870
						}
871
						?>
872
                    </script>
873
					<?php
874
				}
875
			}
876
			?>
877
            <hr>
878
			<?php if ( ! empty( $uix['save_button'] ) ) { ?>
879
                <p class="uix-footer-bar"><button type="button" class="button button-primary" data-save-object="true">
880
					<?php esc_html_e( $uix['save_button'], $this->plugin_slug ); ?>
881
                </button>
882
                <span class="spinner uix-save-spinner"></span>
883
                <span class="save-confirm" style="display: none;"><span class="dashicons dashicons-yes"></span></span>
884
                </p>
885
			<?php } ?>
886
        </div>
887
888
        <script type="text/html" data-template="__notice">
889
            <div class="{{#if success}}updated{{else}}error{{/if}} notice uix-notice is-dismissible">
890
                <p>{{{data}}}</p>
891
                <button class="notice-dismiss" type="button">
892
                    <span class="screen-reader-text">Dismiss this notice.</span>
893
                </button>
894
            </div>
895
        </script>
896
        <script type="text/html" id="__partial_save">
897
            <button class="button" type="button"
898
                    data-modal-node="{{__node_path}}" data-app="{{__app}}"
899
                    data-type="save" {{#if
900
                    __callback}}data-callback="{{__callback}}" {{/if}}>
901
            Save Changes
902
            </button>
903
        </script>
904
        <script type="text/html" id="__partial_create">
905
            <button class="button" type="button"
906
                    data-modal-node="{{__node_path}}" data-app="{{__app}}"
907
                    data-type="add" {{#if
908
                    __callback}}data-callback="{{__callback}}" {{/if}}>
909
            Create
910
            </button>
911
        </script>
912
        <script type="text/html" id="__partial_delete">
913
            <button style="float:left;" class="button" type="button"
914
                    data-modal-node="{{__node_path}}" data-app="{{__app}}"
915
                    data-type="delete" {{#if
916
                    __callback}}data-callback="{{__callback}}" {{/if}}>
917
            Remove
918
            </button>
919
        </script>
920
		<?php
921
	}
922
923
}