~canonical-sysadmins/wordpress/4.5.2

« back to all changes in this revision

Viewing changes to wp-admin/includes/screen.php

  • Committer: Manuel Seelaus
  • Date: 2015-12-09 17:47:18 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: manuel.seelaus@canonical.com-20151209174718-coxethm2swbeqksy
Merge WP4.4 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
 * @return array
52
52
 */
53
53
function get_hidden_columns( $screen ) {
54
 
        if ( is_string( $screen ) )
 
54
        if ( is_string( $screen ) ) {
55
55
                $screen = convert_to_screen( $screen );
56
 
 
57
 
        return (array) get_user_option( 'manage' . $screen->id . 'columnshidden' );
 
56
        }
 
57
 
 
58
        $hidden = get_user_option( 'manage' . $screen->id . 'columnshidden' );
 
59
 
 
60
        if ( ! $hidden ) {
 
61
                $hidden = array();
 
62
 
 
63
                /**
 
64
                 * Filter the default list of hidden columns.
 
65
                 *
 
66
                 * @since 4.4.0
 
67
                 *
 
68
                 * @param array     $hidden An array of columns hidden by default.
 
69
                 * @param WP_Screen $screen WP_Screen object of the current screen.
 
70
                 */
 
71
                $hidden = apply_filters( 'default_hidden_columns', $hidden, $screen );
 
72
        }
 
73
 
 
74
        /**
 
75
         * Filter the list of hidden columns.
 
76
         *
 
77
         * @since 4.4.0
 
78
         *
 
79
         * @param array     $hidden An array of hidden columns.
 
80
         * @param WP_Screen $screen WP_Screen object of the current screen.
 
81
         */
 
82
        return apply_filters( 'hidden_columns', $hidden, $screen );
58
83
}
59
84
 
60
85
/**
193
218
function set_current_screen( $hook_name = '' ) {
194
219
        WP_Screen::get( $hook_name )->set_current_screen();
195
220
}
196
 
 
197
 
/**
198
 
 * A class representing the admin screen.
199
 
 *
200
 
 * @since 3.3.0
201
 
 * @access public
202
 
 */
203
 
final class WP_Screen {
204
 
        /**
205
 
         * Any action associated with the screen. 'add' for *-add.php and *-new.php screens. Empty otherwise.
206
 
         *
207
 
         * @since 3.3.0
208
 
         * @var string
209
 
         * @access public
210
 
         */
211
 
        public $action;
212
 
 
213
 
        /**
214
 
         * The base type of the screen. This is typically the same as $id but with any post types and taxonomies stripped.
215
 
         * For example, for an $id of 'edit-post' the base is 'edit'.
216
 
         *
217
 
         * @since 3.3.0
218
 
         * @var string
219
 
         * @access public
220
 
         */
221
 
        public $base;
222
 
 
223
 
        /**
224
 
         * The number of columns to display. Access with get_columns().
225
 
         *
226
 
         * @since 3.4.0
227
 
         * @var int
228
 
         * @access private
229
 
         */
230
 
        private $columns = 0;
231
 
 
232
 
        /**
233
 
         * The unique ID of the screen.
234
 
         *
235
 
         * @since 3.3.0
236
 
         * @var string
237
 
         * @access public
238
 
         */
239
 
        public $id;
240
 
 
241
 
        /**
242
 
         * Which admin the screen is in. network | user | site | false
243
 
         *
244
 
         * @since 3.5.0
245
 
         * @var string
246
 
         * @access protected
247
 
         */
248
 
        protected $in_admin;
249
 
 
250
 
        /**
251
 
         * Whether the screen is in the network admin.
252
 
         *
253
 
         * Deprecated. Use in_admin() instead.
254
 
         *
255
 
         * @since 3.3.0
256
 
         * @deprecated 3.5.0
257
 
         * @var bool
258
 
         * @access public
259
 
         */
260
 
        public $is_network;
261
 
 
262
 
        /**
263
 
         * Whether the screen is in the user admin.
264
 
         *
265
 
         * Deprecated. Use in_admin() instead.
266
 
         *
267
 
         * @since 3.3.0
268
 
         * @deprecated 3.5.0
269
 
         * @var bool
270
 
         * @access public
271
 
         */
272
 
        public $is_user;
273
 
 
274
 
        /**
275
 
         * The base menu parent.
276
 
         * This is derived from $parent_file by removing the query string and any .php extension.
277
 
         * $parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a $parent_base of 'edit'.
278
 
         *
279
 
         * @since 3.3.0
280
 
         * @var string
281
 
         * @access public
282
 
         */
283
 
        public $parent_base;
284
 
 
285
 
        /**
286
 
         * The parent_file for the screen per the admin menu system.
287
 
         * Some $parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'.
288
 
         *
289
 
         * @since 3.3.0
290
 
         * @var string
291
 
         * @access public
292
 
         */
293
 
        public $parent_file;
294
 
 
295
 
        /**
296
 
         * The post type associated with the screen, if any.
297
 
         * The 'edit.php?post_type=page' screen has a post type of 'page'.
298
 
         * The 'edit-tags.php?taxonomy=$taxonomy&post_type=page' screen has a post type of 'page'.
299
 
         *
300
 
         * @since 3.3.0
301
 
         * @var string
302
 
         * @access public
303
 
         */
304
 
        public $post_type;
305
 
 
306
 
        /**
307
 
         * The taxonomy associated with the screen, if any.
308
 
         * The 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'.
309
 
         * @since 3.3.0
310
 
         * @var string
311
 
         * @access public
312
 
         */
313
 
        public $taxonomy;
314
 
 
315
 
        /**
316
 
         * The help tab data associated with the screen, if any.
317
 
         *
318
 
         * @since 3.3.0
319
 
         * @var array
320
 
         * @access private
321
 
         */
322
 
        private $_help_tabs = array();
323
 
 
324
 
        /**
325
 
         * The help sidebar data associated with screen, if any.
326
 
         *
327
 
         * @since 3.3.0
328
 
         * @var string
329
 
         * @access private
330
 
         */
331
 
        private $_help_sidebar = '';
332
 
 
333
 
        /**
334
 
         * Stores old string-based help.
335
 
         *
336
 
         * @static
337
 
         * @access private
338
 
         *
339
 
         * @var array
340
 
         */
341
 
        private static $_old_compat_help = array();
342
 
 
343
 
        /**
344
 
         * The screen options associated with screen, if any.
345
 
         *
346
 
         * @since 3.3.0
347
 
         * @var array
348
 
         * @access private
349
 
         */
350
 
        private $_options = array();
351
 
 
352
 
        /**
353
 
         * The screen object registry.
354
 
         *
355
 
         * @since 3.3.0
356
 
         *
357
 
         * @static
358
 
         * @access private
359
 
         *
360
 
         * @var array
361
 
         */
362
 
        private static $_registry = array();
363
 
 
364
 
        /**
365
 
         * Stores the result of the public show_screen_options function.
366
 
         *
367
 
         * @since 3.3.0
368
 
         * @var bool
369
 
         * @access private
370
 
         */
371
 
        private $_show_screen_options;
372
 
 
373
 
        /**
374
 
         * Stores the 'screen_settings' section of screen options.
375
 
         *
376
 
         * @since 3.3.0
377
 
         * @var string
378
 
         * @access private
379
 
         */
380
 
        private $_screen_settings;
381
 
 
382
 
        /**
383
 
         * Fetches a screen object.
384
 
         *
385
 
         * @since 3.3.0
386
 
         * @access public
387
 
         *
388
 
         * @static
389
 
         *
390
 
         * @global string $hook_suffix
391
 
         *
392
 
         * @param string|WP_Screen $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen.
393
 
         *                                        Defaults to the current $hook_suffix global.
394
 
         * @return WP_Screen Screen object.
395
 
         */
396
 
        public static function get( $hook_name = '' ) {
397
 
                if ( $hook_name instanceof WP_Screen ) {
398
 
                        return $hook_name;
399
 
                }
400
 
 
401
 
                $post_type = $taxonomy = null;
402
 
                $in_admin = false;
403
 
                $action = '';
404
 
 
405
 
                if ( $hook_name )
406
 
                        $id = $hook_name;
407
 
                else
408
 
                        $id = $GLOBALS['hook_suffix'];
409
 
 
410
 
                // For those pesky meta boxes.
411
 
                if ( $hook_name && post_type_exists( $hook_name ) ) {
412
 
                        $post_type = $id;
413
 
                        $id = 'post'; // changes later. ends up being $base.
414
 
                } else {
415
 
                        if ( '.php' == substr( $id, -4 ) )
416
 
                                $id = substr( $id, 0, -4 );
417
 
 
418
 
                        if ( 'post-new' == $id || 'link-add' == $id || 'media-new' == $id || 'user-new' == $id ) {
419
 
                                $id = substr( $id, 0, -4 );
420
 
                                $action = 'add';
421
 
                        }
422
 
                }
423
 
 
424
 
                if ( ! $post_type && $hook_name ) {
425
 
                        if ( '-network' == substr( $id, -8 ) ) {
426
 
                                $id = substr( $id, 0, -8 );
427
 
                                $in_admin = 'network';
428
 
                        } elseif ( '-user' == substr( $id, -5 ) ) {
429
 
                                $id = substr( $id, 0, -5 );
430
 
                                $in_admin = 'user';
431
 
                        }
432
 
 
433
 
                        $id = sanitize_key( $id );
434
 
                        if ( 'edit-comments' != $id && 'edit-tags' != $id && 'edit-' == substr( $id, 0, 5 ) ) {
435
 
                                $maybe = substr( $id, 5 );
436
 
                                if ( taxonomy_exists( $maybe ) ) {
437
 
                                        $id = 'edit-tags';
438
 
                                        $taxonomy = $maybe;
439
 
                                } elseif ( post_type_exists( $maybe ) ) {
440
 
                                        $id = 'edit';
441
 
                                        $post_type = $maybe;
442
 
                                }
443
 
                        }
444
 
 
445
 
                        if ( ! $in_admin )
446
 
                                $in_admin = 'site';
447
 
                } else {
448
 
                        if ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN )
449
 
                                $in_admin = 'network';
450
 
                        elseif ( defined( 'WP_USER_ADMIN' ) && WP_USER_ADMIN )
451
 
                                $in_admin = 'user';
452
 
                        else
453
 
                                $in_admin = 'site';
454
 
                }
455
 
 
456
 
                if ( 'index' == $id )
457
 
                        $id = 'dashboard';
458
 
                elseif ( 'front' == $id )
459
 
                        $in_admin = false;
460
 
 
461
 
                $base = $id;
462
 
 
463
 
                // If this is the current screen, see if we can be more accurate for post types and taxonomies.
464
 
                if ( ! $hook_name ) {
465
 
                        if ( isset( $_REQUEST['post_type'] ) )
466
 
                                $post_type = post_type_exists( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : false;
467
 
                        if ( isset( $_REQUEST['taxonomy'] ) )
468
 
                                $taxonomy = taxonomy_exists( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : false;
469
 
 
470
 
                        switch ( $base ) {
471
 
                                case 'post' :
472
 
                                        if ( isset( $_GET['post'] ) )
473
 
                                                $post_id = (int) $_GET['post'];
474
 
                                        elseif ( isset( $_POST['post_ID'] ) )
475
 
                                                $post_id = (int) $_POST['post_ID'];
476
 
                                        else
477
 
                                                $post_id = 0;
478
 
 
479
 
                                        if ( $post_id ) {
480
 
                                                $post = get_post( $post_id );
481
 
                                                if ( $post )
482
 
                                                        $post_type = $post->post_type;
483
 
                                        }
484
 
                                        break;
485
 
                                case 'edit-tags' :
486
 
                                        if ( null === $post_type && is_object_in_taxonomy( 'post', $taxonomy ? $taxonomy : 'post_tag' ) )
487
 
                                                $post_type = 'post';
488
 
                                        break;
489
 
                        }
490
 
                }
491
 
 
492
 
                switch ( $base ) {
493
 
                        case 'post' :
494
 
                                if ( null === $post_type )
495
 
                                        $post_type = 'post';
496
 
                                $id = $post_type;
497
 
                                break;
498
 
                        case 'edit' :
499
 
                                if ( null === $post_type )
500
 
                                        $post_type = 'post';
501
 
                                $id .= '-' . $post_type;
502
 
                                break;
503
 
                        case 'edit-tags' :
504
 
                                if ( null === $taxonomy )
505
 
                                        $taxonomy = 'post_tag';
506
 
                                // The edit-tags ID does not contain the post type. Look for it in the request.
507
 
                                if ( null === $post_type ) {
508
 
                                        $post_type = 'post';
509
 
                                        if ( isset( $_REQUEST['post_type'] ) && post_type_exists( $_REQUEST['post_type'] ) )
510
 
                                                $post_type = $_REQUEST['post_type'];
511
 
                                }
512
 
 
513
 
                                $id = 'edit-' . $taxonomy;
514
 
                                break;
515
 
                }
516
 
 
517
 
                if ( 'network' == $in_admin ) {
518
 
                        $id   .= '-network';
519
 
                        $base .= '-network';
520
 
                } elseif ( 'user' == $in_admin ) {
521
 
                        $id   .= '-user';
522
 
                        $base .= '-user';
523
 
                }
524
 
 
525
 
                if ( isset( self::$_registry[ $id ] ) ) {
526
 
                        $screen = self::$_registry[ $id ];
527
 
                        if ( $screen === get_current_screen() )
528
 
                                return $screen;
529
 
                } else {
530
 
                        $screen = new WP_Screen();
531
 
                        $screen->id     = $id;
532
 
                }
533
 
 
534
 
                $screen->base       = $base;
535
 
                $screen->action     = $action;
536
 
                $screen->post_type  = (string) $post_type;
537
 
                $screen->taxonomy   = (string) $taxonomy;
538
 
                $screen->is_user    = ( 'user' == $in_admin );
539
 
                $screen->is_network = ( 'network' == $in_admin );
540
 
                $screen->in_admin   = $in_admin;
541
 
 
542
 
                self::$_registry[ $id ] = $screen;
543
 
 
544
 
                return $screen;
545
 
        }
546
 
 
547
 
        /**
548
 
         * Makes the screen object the current screen.
549
 
         *
550
 
         * @see set_current_screen()
551
 
         * @since 3.3.0
552
 
         *
553
 
         * @global WP_Screen $current_screen
554
 
         * @global string    $taxnow
555
 
         * @global string    $typenow
556
 
         */
557
 
        public function set_current_screen() {
558
 
                global $current_screen, $taxnow, $typenow;
559
 
                $current_screen = $this;
560
 
                $taxnow = $this->taxonomy;
561
 
                $typenow = $this->post_type;
562
 
 
563
 
                /**
564
 
                 * Fires after the current screen has been set.
565
 
                 *
566
 
                 * @since 3.0.0
567
 
                 *
568
 
                 * @param WP_Screen $current_screen Current WP_Screen object.
569
 
                 */
570
 
                do_action( 'current_screen', $current_screen );
571
 
        }
572
 
 
573
 
        /**
574
 
         * Constructor
575
 
         *
576
 
         * @since 3.3.0
577
 
         * @access private
578
 
         */
579
 
        private function __construct() {}
580
 
 
581
 
        /**
582
 
         * Indicates whether the screen is in a particular admin
583
 
         *
584
 
         * @since 3.5.0
585
 
         *
586
 
         * @param string $admin The admin to check against (network | user | site).
587
 
         *                      If empty any of the three admins will result in true.
588
 
         * @return bool True if the screen is in the indicated admin, false otherwise.
589
 
         */
590
 
        public function in_admin( $admin = null ) {
591
 
                if ( empty( $admin ) )
592
 
                        return (bool) $this->in_admin;
593
 
 
594
 
                return ( $admin == $this->in_admin );
595
 
        }
596
 
 
597
 
        /**
598
 
         * Sets the old string-based contextual help for the screen.
599
 
         *
600
 
         * For backwards compatibility.
601
 
         *
602
 
         * @since 3.3.0
603
 
         *
604
 
         * @static
605
 
         *
606
 
         * @param WP_Screen $screen A screen object.
607
 
         * @param string $help Help text.
608
 
         */
609
 
        public static function add_old_compat_help( $screen, $help ) {
610
 
                self::$_old_compat_help[ $screen->id ] = $help;
611
 
        }
612
 
 
613
 
        /**
614
 
         * Set the parent information for the screen.
615
 
         * This is called in admin-header.php after the menu parent for the screen has been determined.
616
 
         *
617
 
         * @since 3.3.0
618
 
         *
619
 
         * @param string $parent_file The parent file of the screen. Typically the $parent_file global.
620
 
         */
621
 
        public function set_parentage( $parent_file ) {
622
 
                $this->parent_file = $parent_file;
623
 
                list( $this->parent_base ) = explode( '?', $parent_file );
624
 
                $this->parent_base = str_replace( '.php', '', $this->parent_base );
625
 
        }
626
 
 
627
 
        /**
628
 
         * Adds an option for the screen.
629
 
         * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add screen options.
630
 
         *
631
 
         * @since 3.3.0
632
 
         *
633
 
         * @param string $option Option ID
634
 
         * @param mixed $args Option-dependent arguments.
635
 
         */
636
 
        public function add_option( $option, $args = array() ) {
637
 
                $this->_options[ $option ] = $args;
638
 
        }
639
 
 
640
 
        /**
641
 
         * Remove an option from the screen.
642
 
         *
643
 
         * @since 3.8.0
644
 
         *
645
 
         * @param string $option Option ID.
646
 
         */
647
 
        public function remove_option( $option ) {
648
 
                unset( $this->_options[ $option ] );
649
 
        }
650
 
 
651
 
        /**
652
 
         * Remove all options from the screen.
653
 
         *
654
 
         * @since 3.8.0
655
 
         */
656
 
        public function remove_options() {
657
 
                $this->_options = array();
658
 
        }
659
 
 
660
 
        /**
661
 
         * Get the options registered for the screen.
662
 
         *
663
 
         * @since 3.8.0
664
 
         *
665
 
         * @return array Options with arguments.
666
 
         */
667
 
        public function get_options() {
668
 
                return $this->_options;
669
 
        }
670
 
 
671
 
        /**
672
 
         * Gets the arguments for an option for the screen.
673
 
         *
674
 
         * @since 3.3.0
675
 
         *
676
 
         * @param string $option Option name.
677
 
         * @param string $key    Optional. Specific array key for when the option is an array.
678
 
         *                       Default false.
679
 
         * @return string The option value if set, null otherwise.
680
 
         */
681
 
        public function get_option( $option, $key = false ) {
682
 
                if ( ! isset( $this->_options[ $option ] ) )
683
 
                        return null;
684
 
                if ( $key ) {
685
 
                        if ( isset( $this->_options[ $option ][ $key ] ) )
686
 
                                return $this->_options[ $option ][ $key ];
687
 
                        return null;
688
 
                }
689
 
                return $this->_options[ $option ];
690
 
        }
691
 
 
692
 
        /**
693
 
         * Gets the help tabs registered for the screen.
694
 
         *
695
 
         * @since 3.4.0
696
 
         *
697
 
         * @return array Help tabs with arguments.
698
 
         */
699
 
        public function get_help_tabs() {
700
 
                return $this->_help_tabs;
701
 
        }
702
 
 
703
 
        /**
704
 
         * Gets the arguments for a help tab.
705
 
         *
706
 
         * @since 3.4.0
707
 
         *
708
 
         * @param string $id Help Tab ID.
709
 
         * @return array Help tab arguments.
710
 
         */
711
 
        public function get_help_tab( $id ) {
712
 
                if ( ! isset( $this->_help_tabs[ $id ] ) )
713
 
                        return null;
714
 
                return $this->_help_tabs[ $id ];
715
 
        }
716
 
 
717
 
        /**
718
 
         * Add a help tab to the contextual help for the screen.
719
 
         * Call this on the load-$pagenow hook for the relevant screen.
720
 
         *
721
 
         * @since 3.3.0
722
 
         *
723
 
         * @param array $args
724
 
         * - string   - title    - Title for the tab.
725
 
         * - string   - id       - Tab ID. Must be HTML-safe.
726
 
         * - string   - content  - Help tab content in plain text or HTML. Optional.
727
 
         * - callback - callback - A callback to generate the tab content. Optional.
728
 
         *
729
 
         */
730
 
        public function add_help_tab( $args ) {
731
 
                $defaults = array(
732
 
                        'title'    => false,
733
 
                        'id'       => false,
734
 
                        'content'  => '',
735
 
                        'callback' => false,
736
 
                );
737
 
                $args = wp_parse_args( $args, $defaults );
738
 
 
739
 
                $args['id'] = sanitize_html_class( $args['id'] );
740
 
 
741
 
                // Ensure we have an ID and title.
742
 
                if ( ! $args['id'] || ! $args['title'] )
743
 
                        return;
744
 
 
745
 
                // Allows for overriding an existing tab with that ID.
746
 
                $this->_help_tabs[ $args['id'] ] = $args;
747
 
        }
748
 
 
749
 
        /**
750
 
         * Removes a help tab from the contextual help for the screen.
751
 
         *
752
 
         * @since 3.3.0
753
 
         *
754
 
         * @param string $id The help tab ID.
755
 
         */
756
 
        public function remove_help_tab( $id ) {
757
 
                unset( $this->_help_tabs[ $id ] );
758
 
        }
759
 
 
760
 
        /**
761
 
         * Removes all help tabs from the contextual help for the screen.
762
 
         *
763
 
         * @since 3.3.0
764
 
         */
765
 
        public function remove_help_tabs() {
766
 
                $this->_help_tabs = array();
767
 
        }
768
 
 
769
 
        /**
770
 
         * Gets the content from a contextual help sidebar.
771
 
         *
772
 
         * @since 3.4.0
773
 
         *
774
 
         * @return string Contents of the help sidebar.
775
 
         */
776
 
        public function get_help_sidebar() {
777
 
                return $this->_help_sidebar;
778
 
        }
779
 
 
780
 
        /**
781
 
         * Add a sidebar to the contextual help for the screen.
782
 
         * Call this in template files after admin.php is loaded and before admin-header.php is loaded to add a sidebar to the contextual help.
783
 
         *
784
 
         * @since 3.3.0
785
 
         *
786
 
         * @param string $content Sidebar content in plain text or HTML.
787
 
         */
788
 
        public function set_help_sidebar( $content ) {
789
 
                $this->_help_sidebar = $content;
790
 
        }
791
 
 
792
 
        /**
793
 
         * Gets the number of layout columns the user has selected.
794
 
         *
795
 
         * The layout_columns option controls the max number and default number of
796
 
         * columns. This method returns the number of columns within that range selected
797
 
         * by the user via Screen Options. If no selection has been made, the default
798
 
         * provisioned in layout_columns is returned. If the screen does not support
799
 
         * selecting the number of layout columns, 0 is returned.
800
 
         *
801
 
         * @since 3.4.0
802
 
         *
803
 
         * @return int Number of columns to display.
804
 
         */
805
 
        public function get_columns() {
806
 
                return $this->columns;
807
 
        }
808
 
 
809
 
        /**
810
 
         * Render the screen's help section.
811
 
         *
812
 
         * This will trigger the deprecated filters for backwards compatibility.
813
 
         *
814
 
         * @since 3.3.0
815
 
         *
816
 
         * @global string $screen_layout_columns
817
 
         */
818
 
        public function render_screen_meta() {
819
 
 
820
 
                /**
821
 
                 * Filter the legacy contextual help list.
822
 
                 *
823
 
                 * @since 2.7.0
824
 
                 * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
825
 
                 *                   get_current_screen()->remove_help_tab() instead.
826
 
                 *
827
 
                 * @param array     $old_compat_help Old contextual help.
828
 
                 * @param WP_Screen $this            Current WP_Screen instance.
829
 
                 */
830
 
                self::$_old_compat_help = apply_filters( 'contextual_help_list', self::$_old_compat_help, $this );
831
 
 
832
 
                $old_help = isset( self::$_old_compat_help[ $this->id ] ) ? self::$_old_compat_help[ $this->id ] : '';
833
 
 
834
 
                /**
835
 
                 * Filter the legacy contextual help text.
836
 
                 *
837
 
                 * @since 2.7.0
838
 
                 * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
839
 
                 *                   get_current_screen()->remove_help_tab() instead.
840
 
                 *
841
 
                 * @param string    $old_help  Help text that appears on the screen.
842
 
                 * @param string    $screen_id Screen ID.
843
 
                 * @param WP_Screen $this      Current WP_Screen instance.
844
 
                 *
845
 
                 */
846
 
                $old_help = apply_filters( 'contextual_help', $old_help, $this->id, $this );
847
 
 
848
 
                // Default help only if there is no old-style block of text and no new-style help tabs.
849
 
                if ( empty( $old_help ) && ! $this->get_help_tabs() ) {
850
 
 
851
 
                        /**
852
 
                         * Filter the default legacy contextual help text.
853
 
                         *
854
 
                         * @since 2.8.0
855
 
                         * @deprecated 3.3.0 Use get_current_screen()->add_help_tab() or
856
 
                         *                   get_current_screen()->remove_help_tab() instead.
857
 
                         *
858
 
                         * @param string $old_help_default Default contextual help text.
859
 
                         */
860
 
                        $default_help = apply_filters( 'default_contextual_help', '' );
861
 
                        if ( $default_help )
862
 
                                $old_help = '<p>' . $default_help . '</p>';
863
 
                }
864
 
 
865
 
                if ( $old_help ) {
866
 
                        $this->add_help_tab( array(
867
 
                                'id'      => 'old-contextual-help',
868
 
                                'title'   => __('Overview'),
869
 
                                'content' => $old_help,
870
 
                        ) );
871
 
                }
872
 
 
873
 
                $help_sidebar = $this->get_help_sidebar();
874
 
 
875
 
                $help_class = 'hidden';
876
 
                if ( ! $help_sidebar )
877
 
                        $help_class .= ' no-sidebar';
878
 
 
879
 
                // Time to render!
880
 
                ?>
881
 
                <div id="screen-meta" class="metabox-prefs">
882
 
 
883
 
                        <div id="contextual-help-wrap" class="<?php echo esc_attr( $help_class ); ?>" tabindex="-1" aria-label="<?php esc_attr_e('Contextual Help Tab'); ?>">
884
 
                                <div id="contextual-help-back"></div>
885
 
                                <div id="contextual-help-columns">
886
 
                                        <div class="contextual-help-tabs">
887
 
                                                <ul>
888
 
                                                <?php
889
 
                                                $class = ' class="active"';
890
 
                                                foreach ( $this->get_help_tabs() as $tab ) :
891
 
                                                        $link_id  = "tab-link-{$tab['id']}";
892
 
                                                        $panel_id = "tab-panel-{$tab['id']}";
893
 
                                                        ?>
894
 
 
895
 
                                                        <li id="<?php echo esc_attr( $link_id ); ?>"<?php echo $class; ?>>
896
 
                                                                <a href="<?php echo esc_url( "#$panel_id" ); ?>" aria-controls="<?php echo esc_attr( $panel_id ); ?>">
897
 
                                                                        <?php echo esc_html( $tab['title'] ); ?>
898
 
                                                                </a>
899
 
                                                        </li>
900
 
                                                <?php
901
 
                                                        $class = '';
902
 
                                                endforeach;
903
 
                                                ?>
904
 
                                                </ul>
905
 
                                        </div>
906
 
 
907
 
                                        <?php if ( $help_sidebar ) : ?>
908
 
                                        <div class="contextual-help-sidebar">
909
 
                                                <?php echo $help_sidebar; ?>
910
 
                                        </div>
911
 
                                        <?php endif; ?>
912
 
 
913
 
                                        <div class="contextual-help-tabs-wrap">
914
 
                                                <?php
915
 
                                                $classes = 'help-tab-content active';
916
 
                                                foreach ( $this->get_help_tabs() as $tab ):
917
 
                                                        $panel_id = "tab-panel-{$tab['id']}";
918
 
                                                        ?>
919
 
 
920
 
                                                        <div id="<?php echo esc_attr( $panel_id ); ?>" class="<?php echo $classes; ?>">
921
 
                                                                <?php
922
 
                                                                // Print tab content.
923
 
                                                                echo $tab['content'];
924
 
 
925
 
                                                                // If it exists, fire tab callback.
926
 
                                                                if ( ! empty( $tab['callback'] ) )
927
 
                                                                        call_user_func_array( $tab['callback'], array( $this, $tab ) );
928
 
                                                                ?>
929
 
                                                        </div>
930
 
                                                <?php
931
 
                                                        $classes = 'help-tab-content';
932
 
                                                endforeach;
933
 
                                                ?>
934
 
                                        </div>
935
 
                                </div>
936
 
                        </div>
937
 
                <?php
938
 
                // Setup layout columns
939
 
 
940
 
                /**
941
 
                 * Filter the array of screen layout columns.
942
 
                 *
943
 
                 * This hook provides back-compat for plugins using the back-compat
944
 
                 * filter instead of add_screen_option().
945
 
                 *
946
 
                 * @since 2.8.0
947
 
                 *
948
 
                 * @param array     $empty_columns Empty array.
949
 
                 * @param string    $screen_id     Screen ID.
950
 
                 * @param WP_Screen $this          Current WP_Screen instance.
951
 
                 */
952
 
                $columns = apply_filters( 'screen_layout_columns', array(), $this->id, $this );
953
 
 
954
 
                if ( ! empty( $columns ) && isset( $columns[ $this->id ] ) )
955
 
                        $this->add_option( 'layout_columns', array('max' => $columns[ $this->id ] ) );
956
 
 
957
 
                if ( $this->get_option( 'layout_columns' ) ) {
958
 
                        $this->columns = (int) get_user_option("screen_layout_$this->id");
959
 
 
960
 
                        if ( ! $this->columns && $this->get_option( 'layout_columns', 'default' ) )
961
 
                                $this->columns = $this->get_option( 'layout_columns', 'default' );
962
 
                }
963
 
                $GLOBALS[ 'screen_layout_columns' ] = $this->columns; // Set the global for back-compat.
964
 
 
965
 
                // Add screen options
966
 
                if ( $this->show_screen_options() )
967
 
                        $this->render_screen_options();
968
 
                ?>
969
 
                </div>
970
 
                <?php
971
 
                if ( ! $this->get_help_tabs() && ! $this->show_screen_options() )
972
 
                        return;
973
 
                ?>
974
 
                <div id="screen-meta-links">
975
 
                <?php if ( $this->get_help_tabs() ) : ?>
976
 
                        <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
977
 
                        <button type="button" id="contextual-help-link" class="button show-settings" aria-controls="contextual-help-wrap" aria-expanded="false"><?php _e( 'Help' ); ?></button>
978
 
                        </div>
979
 
                <?php endif;
980
 
                if ( $this->show_screen_options() ) : ?>
981
 
                        <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
982
 
                        <button type="button" id="show-settings-link" class="button show-settings" aria-controls="screen-options-wrap" aria-expanded="false"><?php _e( 'Screen Options' ); ?></button>
983
 
                        </div>
984
 
                <?php endif; ?>
985
 
                </div>
986
 
                <?php
987
 
        }
988
 
 
989
 
        /**
990
 
         *
991
 
         * @global array $wp_meta_boxes
992
 
         *
993
 
         * @return bool
994
 
         */
995
 
        public function show_screen_options() {
996
 
                global $wp_meta_boxes;
997
 
 
998
 
                if ( is_bool( $this->_show_screen_options ) )
999
 
                        return $this->_show_screen_options;
1000
 
 
1001
 
                $columns = get_column_headers( $this );
1002
 
 
1003
 
                $show_screen = ! empty( $wp_meta_boxes[ $this->id ] ) || $columns || $this->get_option( 'per_page' );
1004
 
 
1005
 
                switch ( $this->base ) {
1006
 
                        case 'widgets':
1007
 
                                $this->_screen_settings = '<p><a id="access-on" href="widgets.php?widgets-access=on">' . __('Enable accessibility mode') . '</a><a id="access-off" href="widgets.php?widgets-access=off">' . __('Disable accessibility mode') . "</a></p>\n";
1008
 
                                break;
1009
 
                        case 'post' :
1010
 
                                $expand = '<div class="editor-expand hidden"><label for="editor-expand-toggle">';
1011
 
                                $expand .= '<input type="checkbox" id="editor-expand-toggle"' . checked( get_user_setting( 'editor_expand', 'on' ), 'on', false ) . ' />';
1012
 
                                $expand .= __( 'Enable full-height editor and distraction-free functionality.' ) . '</label></div>';
1013
 
                                $this->_screen_settings = $expand;
1014
 
                                break;
1015
 
                        default:
1016
 
                                $this->_screen_settings = '';
1017
 
                                break;
1018
 
                }
1019
 
 
1020
 
                /**
1021
 
                 * Filter the screen settings text displayed in the Screen Options tab.
1022
 
                 *
1023
 
                 * This filter is currently only used on the Widgets screen to enable
1024
 
                 * accessibility mode.
1025
 
                 *
1026
 
                 * @since 3.0.0
1027
 
                 *
1028
 
                 * @param string    $screen_settings Screen settings.
1029
 
                 * @param WP_Screen $this            WP_Screen object.
1030
 
                 */
1031
 
                $this->_screen_settings = apply_filters( 'screen_settings', $this->_screen_settings, $this );
1032
 
 
1033
 
                if ( $this->_screen_settings || $this->_options )
1034
 
                        $show_screen = true;
1035
 
 
1036
 
                /**
1037
 
                 * Filter whether to show the Screen Options tab.
1038
 
                 *
1039
 
                 * @since 3.2.0
1040
 
                 *
1041
 
                 * @param bool      $show_screen Whether to show Screen Options tab.
1042
 
                 *                               Default true.
1043
 
                 * @param WP_Screen $this        Current WP_Screen instance.
1044
 
                 */
1045
 
                $this->_show_screen_options = apply_filters( 'screen_options_show_screen', $show_screen, $this );
1046
 
                return $this->_show_screen_options;
1047
 
        }
1048
 
 
1049
 
        /**
1050
 
         * Render the screen options tab.
1051
 
         *
1052
 
         * @since 3.3.0
1053
 
         *
1054
 
         * @global array $wp_meta_boxes
1055
 
         */
1056
 
        public function render_screen_options() {
1057
 
                global $wp_meta_boxes;
1058
 
 
1059
 
                $columns = get_column_headers( $this );
1060
 
                $hidden  = get_hidden_columns( $this );
1061
 
 
1062
 
                ?>
1063
 
                <div id="screen-options-wrap" class="hidden" tabindex="-1" aria-label="<?php esc_attr_e('Screen Options Tab'); ?>">
1064
 
                <form id="adv-settings" method="post">
1065
 
                <?php if ( isset( $wp_meta_boxes[ $this->id ] ) || $this->get_option( 'per_page' ) || ( $columns && empty( $columns['_title'] ) ) ) : ?>
1066
 
                        <h5><?php _e( 'Show on screen' ); ?></h5>
1067
 
                <?php
1068
 
                endif;
1069
 
 
1070
 
                if ( isset( $wp_meta_boxes[ $this->id ] ) ) : ?>
1071
 
                        <div class="metabox-prefs">
1072
 
                                <?php
1073
 
                                        meta_box_prefs( $this );
1074
 
 
1075
 
                                        if ( 'dashboard' === $this->id && has_action( 'welcome_panel' ) && current_user_can( 'edit_theme_options' ) ) {
1076
 
                                                if ( isset( $_GET['welcome'] ) ) {
1077
 
                                                        $welcome_checked = empty( $_GET['welcome'] ) ? 0 : 1;
1078
 
                                                        update_user_meta( get_current_user_id(), 'show_welcome_panel', $welcome_checked );
1079
 
                                                } else {
1080
 
                                                        $welcome_checked = get_user_meta( get_current_user_id(), 'show_welcome_panel', true );
1081
 
                                                        if ( 2 == $welcome_checked && wp_get_current_user()->user_email != get_option( 'admin_email' ) )
1082
 
                                                                $welcome_checked = false;
1083
 
                                                }
1084
 
                                                echo '<label for="wp_welcome_panel-hide">';
1085
 
                                                echo '<input type="checkbox" id="wp_welcome_panel-hide"' . checked( (bool) $welcome_checked, true, false ) . ' />';
1086
 
                                                echo _x( 'Welcome', 'Welcome panel' ) . "</label>\n";
1087
 
                                        }
1088
 
                                ?>
1089
 
                                <br class="clear" />
1090
 
                        </div>
1091
 
                        <?php endif;
1092
 
                        if ( $columns ) :
1093
 
                                if ( ! empty( $columns['_title'] ) ) : ?>
1094
 
                        <h5><?php echo $columns['_title']; ?></h5>
1095
 
                        <?php endif; ?>
1096
 
                        <div class="metabox-prefs">
1097
 
                                <?php
1098
 
                                $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
1099
 
 
1100
 
                                foreach ( $columns as $column => $title ) {
1101
 
                                        // Can't hide these for they are special
1102
 
                                        if ( in_array( $column, $special ) )
1103
 
                                                continue;
1104
 
                                        if ( empty( $title ) )
1105
 
                                                continue;
1106
 
 
1107
 
                                        if ( 'comments' == $column )
1108
 
                                                $title = __( 'Comments' );
1109
 
                                        $id = "$column-hide";
1110
 
                                        echo '<label for="' . $id . '">';
1111
 
                                        echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
1112
 
                                        echo "$title</label>\n";
1113
 
                                }
1114
 
                                ?>
1115
 
                                <br class="clear" />
1116
 
                        </div>
1117
 
                <?php endif;
1118
 
 
1119
 
                $this->render_screen_layout();
1120
 
                $this->render_per_page_options();
1121
 
                echo $this->_screen_settings;
1122
 
 
1123
 
                ?>
1124
 
                <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
1125
 
                </form>
1126
 
                </div>
1127
 
                <?php
1128
 
        }
1129
 
 
1130
 
        /**
1131
 
         * Render the option for number of columns on the page
1132
 
         *
1133
 
         * @since 3.3.0
1134
 
         */
1135
 
        public function render_screen_layout() {
1136
 
                if ( ! $this->get_option('layout_columns') )
1137
 
                        return;
1138
 
 
1139
 
                $screen_layout_columns = $this->get_columns();
1140
 
                $num = $this->get_option( 'layout_columns', 'max' );
1141
 
 
1142
 
                ?>
1143
 
                <h5 class="screen-layout"><?php _e('Screen Layout'); ?></h5>
1144
 
                <div class='columns-prefs'><?php
1145
 
                        _e('Number of Columns:');
1146
 
                        for ( $i = 1; $i <= $num; ++$i ):
1147
 
                                ?>
1148
 
                                <label class="columns-prefs-<?php echo $i; ?>">
1149
 
                                        <input type='radio' name='screen_columns' value='<?php echo esc_attr( $i ); ?>'
1150
 
                                                <?php checked( $screen_layout_columns, $i ); ?> />
1151
 
                                        <?php echo esc_html( $i ); ?>
1152
 
                                </label>
1153
 
                                <?php
1154
 
                        endfor; ?>
1155
 
                </div>
1156
 
                <?php
1157
 
        }
1158
 
 
1159
 
        /**
1160
 
         * Render the items per page option
1161
 
         *
1162
 
         * @since 3.3.0
1163
 
         */
1164
 
        public function render_per_page_options() {
1165
 
                if ( null === $this->get_option( 'per_page' ) ) {
1166
 
                        return;
1167
 
                }
1168
 
 
1169
 
                $per_page_label = $this->get_option( 'per_page', 'label' );
1170
 
                if ( null === $per_page_label ) {
1171
 
                        $per_page_label = __( 'Number of items per page:' );
1172
 
                }
1173
 
 
1174
 
                $option = $this->get_option( 'per_page', 'option' );
1175
 
                if ( ! $option ) {
1176
 
                        $option = str_replace( '-', '_', "{$this->id}_per_page" );
1177
 
                }
1178
 
 
1179
 
                $per_page = (int) get_user_option( $option );
1180
 
                if ( empty( $per_page ) || $per_page < 1 ) {
1181
 
                        $per_page = $this->get_option( 'per_page', 'default' );
1182
 
                        if ( ! $per_page ) {
1183
 
                                $per_page = 20;
1184
 
                        }
1185
 
                }
1186
 
 
1187
 
                if ( 'edit_comments_per_page' == $option ) {
1188
 
                        $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
1189
 
 
1190
 
                        /** This filter is documented in wp-admin/includes/class-wp-comments-list-table.php */
1191
 
                        $per_page = apply_filters( 'comments_per_page', $per_page, $comment_status );
1192
 
                } elseif ( 'categories_per_page' == $option ) {
1193
 
                        /** This filter is documented in wp-admin/includes/class-wp-terms-list-table.php */
1194
 
                        $per_page = apply_filters( 'edit_categories_per_page', $per_page );
1195
 
                } else {
1196
 
                        /** This filter is documented in wp-admin/includes/class-wp-list-table.php */
1197
 
                        $per_page = apply_filters( $option, $per_page );
1198
 
                }
1199
 
 
1200
 
                // Back compat
1201
 
                if ( isset( $this->post_type ) ) {
1202
 
                        /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
1203
 
                        $per_page = apply_filters( 'edit_posts_per_page', $per_page, $this->post_type );
1204
 
                }
1205
 
 
1206
 
                ?>
1207
 
                <div class="screen-options">
1208
 
                        <?php if ( $per_page_label ) : ?>
1209
 
                                <label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label>
1210
 
                                <input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
1211
 
                                        id="<?php echo esc_attr( $option ); ?>" maxlength="3"
1212
 
                                        value="<?php echo esc_attr( $per_page ); ?>" />
1213
 
                        <?php endif;
1214
 
 
1215
 
                        echo get_submit_button( __( 'Apply' ), 'button', 'screen-options-apply', false ); ?>
1216
 
                        <input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
1217
 
                </div>
1218
 
                <?php
1219
 
        }
1220
 
}