~canonical-sysadmins/wordpress/4.8.3

« back to all changes in this revision

Viewing changes to wp-admin/includes/class-wp-list-table.php

  • Committer: Ryan Finnie
  • Date: 2015-08-31 16:09:47 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: ryan.finnie@canonical.com-20150831160947-1h6rfxby9z1ec62u
Merge WP4.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
250
250
         *
251
251
         * @param array $args An associative array with information about the pagination
252
252
         * @access protected
 
253
         *
 
254
         * @param array|string $args
253
255
         */
254
256
        protected function set_pagination_args( $args ) {
255
257
                $args = wp_parse_args( $args, array(
455
457
         * @since 3.1.0
456
458
         * @access public
457
459
         *
458
 
         * @return string|bool The action name or False if no action was selected
 
460
         * @return string|false The action name or False if no action was selected
459
461
         */
460
462
        public function current_action() {
461
463
                if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) )
495
497
                }
496
498
                $out .= '</div>';
497
499
 
 
500
                $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
 
501
 
498
502
                return $out;
499
503
        }
500
504
 
504
508
         * @since 3.1.0
505
509
         * @access protected
506
510
         *
 
511
         * @global wpdb      $wpdb
 
512
         * @global WP_Locale $wp_locale
 
513
         *
507
514
         * @param string $post_type
508
515
         */
509
516
        protected function months_dropdown( $post_type ) {
607
614
         * @param int $pending_comments Number of pending comments.
608
615
         */
609
616
        protected function comments_bubble( $post_id, $pending_comments ) {
610
 
                $pending_phrase = sprintf( __( '%s pending' ), number_format( $pending_comments ) );
611
 
 
612
 
                if ( $pending_comments )
613
 
                        echo '<strong>';
614
 
 
615
 
                echo "<a href='" . esc_url( add_query_arg( 'p', $post_id, admin_url( 'edit-comments.php' ) ) ) . "' title='" . esc_attr( $pending_phrase ) . "' class='post-com-count'><span class='comment-count'>" . number_format_i18n( get_comments_number() ) . "</span></a>";
616
 
 
617
 
                if ( $pending_comments )
618
 
                        echo '</strong>';
 
617
                $approved_comments = get_comments_number();
 
618
 
 
619
                $approved_comments_number = number_format_i18n( $approved_comments );
 
620
                $pending_comments_number = number_format_i18n( $pending_comments );
 
621
 
 
622
                $approved_only_phrase = sprintf( _n( '%s comment', '%s comments', $approved_comments ), $approved_comments_number );
 
623
                $approved_phrase = sprintf( _n( '%s approved comment', '%s approved comments', $approved_comments ), $approved_comments_number );
 
624
                $pending_phrase = sprintf( _n( '%s pending comment', '%s pending comments', $pending_comments ), $pending_comments_number );
 
625
 
 
626
                // No comments at all.
 
627
                if ( ! $approved_comments && ! $pending_comments ) {
 
628
                        printf( '<span aria-hidden="true">—</span><span class="screen-reader-text">%s</span>',
 
629
                                __( 'No comments' )
 
630
                        );
 
631
                // Approved comments have different display depending on some conditions.
 
632
                } elseif ( $approved_comments ) {
 
633
                        printf( '<a href="%s" class="post-com-count post-com-count-approved"><span class="comment-count-approved" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
 
634
                                esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'approved' ), admin_url( 'edit-comments.php' ) ) ),
 
635
                                $approved_comments_number,
 
636
                                $pending_comments ? $approved_phrase : $approved_only_phrase
 
637
                        );
 
638
                } else {
 
639
                        printf( '<span class="post-com-count post-com-count-no-comments"><span class="comment-count comment-count-no-comments" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></span>',
 
640
                                $approved_comments_number,
 
641
                                $pending_comments ? __( 'No approved comments' ) : __( 'No comments' )
 
642
                        );
 
643
                }
 
644
 
 
645
                if ( $pending_comments ) {
 
646
                        printf( '<a href="%s" class="post-com-count post-com-count-pending"><span class="comment-count-pending" aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
 
647
                                esc_url( add_query_arg( array( 'p' => $post_id, 'comment_status' => 'moderated' ), admin_url( 'edit-comments.php' ) ) ),
 
648
                                $pending_comments_number,
 
649
                                $pending_phrase
 
650
                        );
 
651
                }
619
652
        }
620
653
 
621
654
        /**
629
662
        public function get_pagenum() {
630
663
                $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
631
664
 
632
 
                if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
 
665
                if ( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] )
633
666
                        $pagenum = $this->_pagination_args['total_pages'];
634
667
 
635
668
                return max( 1, $pagenum );
686
719
                        $infinite_scroll = $this->_pagination_args['infinite_scroll'];
687
720
                }
688
721
 
689
 
                $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
 
722
                $output = '<span class="displaying-num">' . sprintf( _n( '%s item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
690
723
 
691
724
                $current = $this->get_pagenum();
692
725
 
696
729
 
697
730
                $page_links = array();
698
731
 
699
 
                $disable_first = $disable_last = '';
700
 
                if ( $current == 1 ) {
701
 
                        $disable_first = ' disabled';
702
 
                }
703
 
                if ( $current == $total_pages ) {
704
 
                        $disable_last = ' disabled';
705
 
                }
706
 
                $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
707
 
                        'first-page' . $disable_first,
708
 
                        esc_attr__( 'Go to the first page' ),
709
 
                        esc_url( remove_query_arg( 'paged', $current_url ) ),
710
 
                        '&laquo;'
711
 
                );
712
 
 
713
 
                $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
714
 
                        'prev-page' . $disable_first,
715
 
                        esc_attr__( 'Go to the previous page' ),
716
 
                        esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
717
 
                        '&lsaquo;'
718
 
                );
 
732
                $total_pages_before = '<span class="paging-input">';
 
733
                $total_pages_after  = '</span>';
 
734
 
 
735
                $disable_first = $disable_last = $disable_prev = $disable_next = false;
 
736
 
 
737
                if ( $current == 1 ) {
 
738
                        $disable_first = true;
 
739
                        $disable_prev = true;
 
740
                }
 
741
                if ( $current == 2 ) {
 
742
                        $disable_first = true;
 
743
                }
 
744
                if ( $current == $total_pages ) {
 
745
                        $disable_last = true;
 
746
                        $disable_next = true;
 
747
                }
 
748
                if ( $current == $total_pages - 1 ) {
 
749
                        $disable_last = true;
 
750
                }
 
751
 
 
752
                if ( $disable_first ) {
 
753
                        $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo;</span>';
 
754
                } else {
 
755
                        $page_links[] = sprintf( "<a class='first-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 
756
                                esc_url( remove_query_arg( 'paged', $current_url ) ),
 
757
                                __( 'First page' ),
 
758
                                '&laquo;'
 
759
                        );
 
760
                }
 
761
 
 
762
                if ( $disable_prev ) {
 
763
                        $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&lsaquo;</span>';
 
764
                } else {
 
765
                        $page_links[] = sprintf( "<a class='prev-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 
766
                                esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
 
767
                                __( 'Previous page' ),
 
768
                                '&lsaquo;'
 
769
                        );
 
770
                }
719
771
 
720
772
                if ( 'bottom' == $which ) {
721
 
                        $html_current_page = $current;
 
773
                        $html_current_page  = $current;
 
774
                        $total_pages_before = '<span class="screen-reader-text">' . __( 'Current Page' ) . '</span><span id="table-paging" class="paging-input">';
722
775
                } else {
723
 
                        $html_current_page = sprintf( "%s<input class='current-page' id='current-page-selector' title='%s' type='text' name='paged' value='%s' size='%d' />",
724
 
                                '<label for="current-page-selector" class="screen-reader-text">' . __( 'Select Page' ) . '</label>',
725
 
                                esc_attr__( 'Current page' ),
 
776
                        $html_current_page = sprintf( "%s<input class='current-page' id='current-page-selector' type='text' name='paged' value='%s' size='%d' aria-describedby='table-paging' />",
 
777
                                '<label for="current-page-selector" class="screen-reader-text">' . __( 'Current Page' ) . '</label>',
726
778
                                $current,
727
779
                                strlen( $total_pages )
728
780
                        );
729
781
                }
730
782
                $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
731
 
                $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
732
 
 
733
 
                $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
734
 
                        'next-page' . $disable_last,
735
 
                        esc_attr__( 'Go to the next page' ),
736
 
                        esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
737
 
                        '&rsaquo;'
738
 
                );
739
 
 
740
 
                $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
741
 
                        'last-page' . $disable_last,
742
 
                        esc_attr__( 'Go to the last page' ),
743
 
                        esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
744
 
                        '&raquo;'
745
 
                );
 
783
                $page_links[] = $total_pages_before . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . $total_pages_after;
 
784
 
 
785
                if ( $disable_next ) {
 
786
                        $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&rsaquo;</span>';
 
787
                } else {
 
788
                        $page_links[] = sprintf( "<a class='next-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 
789
                                esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
 
790
                                __( 'Next page' ),
 
791
                                '&rsaquo;'
 
792
                        );
 
793
                }
 
794
 
 
795
                if ( $disable_last ) {
 
796
                        $page_links[] = '<span class="tablenav-pages-navspan" aria-hidden="true">&raquo;</span>';
 
797
                } else {
 
798
                        $page_links[] = sprintf( "<a class='last-page' href='%s'><span class='screen-reader-text'>%s</span><span aria-hidden='true'>%s</span></a>",
 
799
                                esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
 
800
                                __( 'Last page' ),
 
801
                                '&raquo;'
 
802
                        );
 
803
                }
746
804
 
747
805
                $pagination_links_class = 'pagination-links';
748
806
                if ( ! empty( $infinite_scroll ) ) {
792
850
        }
793
851
 
794
852
        /**
 
853
         * Gets the name of the default primary column.
 
854
         *
 
855
         * @since 4.3.0
 
856
         * @access protected
 
857
         *
 
858
         * @return string Name of the default primary column, in this case, an empty string.
 
859
         */
 
860
        protected function get_default_primary_column_name() {
 
861
                $columns = $this->get_columns();
 
862
                $column = '';
 
863
 
 
864
                // We need a primary defined so responsive views show something,
 
865
                // so let's fall back to the first non-checkbox column.
 
866
                foreach( $columns as $col => $column_name ) {
 
867
                        if ( 'cb' === $col ) {
 
868
                                continue;
 
869
                        }
 
870
 
 
871
                        $column = $col;
 
872
                        break;
 
873
                }
 
874
 
 
875
                return $column;
 
876
        }
 
877
 
 
878
        /**
 
879
         * Gets the name of the primary column.
 
880
         *
 
881
         * @since 4.3.0
 
882
         * @access protected
 
883
         *
 
884
         * @return string The name of the primary column.
 
885
         */
 
886
        protected function get_primary_column_name() {
 
887
                $columns = $this->get_columns();
 
888
                $default = $this->get_default_primary_column_name();
 
889
 
 
890
                // If the primary column doesn't exist fall back to the
 
891
                // first non-checkbox column.
 
892
                if ( ! isset( $columns[ $default ] ) ) {
 
893
                        $default = WP_List_Table::get_default_primary_column_name();
 
894
                }
 
895
 
 
896
                /**
 
897
                 * Filter the name of the primary column for the current list table.
 
898
                 *
 
899
                 * @since 4.3.0
 
900
                 *
 
901
                 * @param string $default Column name default for the specific list table, e.g. 'name'.
 
902
                 * @param string $context Screen ID for specific list table, e.g. 'plugins'.
 
903
                 */
 
904
                $column  = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
 
905
 
 
906
                if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
 
907
                        $column = $default;
 
908
                }
 
909
 
 
910
                return $column;
 
911
        }
 
912
 
 
913
        /**
795
914
         * Get a list of all, hidden and sortable columns, with filter applied
796
915
         *
797
916
         * @since 3.1.0
800
919
         * @return array
801
920
         */
802
921
        protected function get_column_info() {
803
 
                if ( isset( $this->_column_headers ) )
804
 
                        return $this->_column_headers;
 
922
                // $_column_headers is already set / cached
 
923
                if ( isset( $this->_column_headers ) && is_array( $this->_column_headers ) ) {
 
924
                        // Back-compat for list tables that have been manually setting $_column_headers for horse reasons.
 
925
                        // In 4.3, we added a fourth argument for primary column.
 
926
                        $column_headers = array( array(), array(), array(), $this->get_primary_column_name() );
 
927
                        foreach ( $this->_column_headers as $key => $value ) {
 
928
                                $column_headers[ $key ] = $value;
 
929
                        }
 
930
 
 
931
                        return $column_headers;
 
932
                }
805
933
 
806
934
                $columns = get_column_headers( $this->screen );
807
935
                $hidden = get_hidden_columns( $this->screen );
831
959
                        $sortable[$id] = $data;
832
960
                }
833
961
 
834
 
                $this->_column_headers = array( $columns, $hidden, $sortable );
 
962
                $primary = $this->get_primary_column_name();
 
963
                $this->_column_headers = array( $columns, $hidden, $sortable, $primary );
835
964
 
836
965
                return $this->_column_headers;
837
966
        }
856
985
         * @since 3.1.0
857
986
         * @access public
858
987
         *
 
988
         * @staticvar int $cb_counter
 
989
         *
859
990
         * @param bool $with_id Whether to set the id attribute or not
860
991
         */
861
992
        public function print_column_headers( $with_id = true ) {
862
 
                list( $columns, $hidden, $sortable ) = $this->get_column_info();
 
993
                list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
863
994
 
864
995
                $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
865
996
                $current_url = remove_query_arg( 'paged', $current_url );
884
1015
                foreach ( $columns as $column_key => $column_display_name ) {
885
1016
                        $class = array( 'manage-column', "column-$column_key" );
886
1017
 
887
 
                        $style = '';
888
 
                        if ( in_array( $column_key, $hidden ) )
889
 
                                $style = 'display:none;';
890
 
 
891
 
                        $style = ' style="' . $style . '"';
 
1018
                        if ( in_array( $column_key, $hidden ) ) {
 
1019
                                $class[] = 'hidden';
 
1020
                        }
892
1021
 
893
1022
                        if ( 'cb' == $column_key )
894
1023
                                $class[] = 'check-column';
895
1024
                        elseif ( in_array( $column_key, array( 'posts', 'comments', 'links' ) ) )
896
1025
                                $class[] = 'num';
897
1026
 
 
1027
                        if ( $column_key === $primary ) {
 
1028
                                $class[] = 'column-primary';
 
1029
                        }
 
1030
 
898
1031
                        if ( isset( $sortable[$column_key] ) ) {
899
1032
                                list( $orderby, $desc_first ) = $sortable[$column_key];
900
1033
 
911
1044
                                $column_display_name = '<a href="' . esc_url( add_query_arg( compact( 'orderby', 'order' ), $current_url ) ) . '"><span>' . $column_display_name . '</span><span class="sorting-indicator"></span></a>';
912
1045
                        }
913
1046
 
 
1047
                        $tag = ( 'cb' === $column_key ) ? 'td' : 'th';
 
1048
                        $scope = ( 'th' === $tag ) ? 'scope="col"' : '';
914
1049
                        $id = $with_id ? "id='$column_key'" : '';
915
1050
 
916
1051
                        if ( !empty( $class ) )
917
1052
                                $class = "class='" . join( ' ', $class ) . "'";
918
1053
 
919
 
                        echo "<th scope='col' $id $class $style>$column_display_name</th>";
 
1054
                        echo "<$tag $scope $id $class>$column_display_name</$tag>";
920
1055
                }
921
1056
        }
922
1057
 
930
1065
                $singular = $this->_args['singular'];
931
1066
 
932
1067
                $this->display_tablenav( 'top' );
933
 
 
934
1068
?>
935
1069
<table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
936
1070
        <thead>
1046
1180
                echo '</tr>';
1047
1181
        }
1048
1182
 
 
1183
        /**
 
1184
         *
 
1185
         * @param object $item
 
1186
         * @param string $column_name
 
1187
         */
1049
1188
        protected function column_default( $item, $column_name ) {}
1050
1189
 
 
1190
        /**
 
1191
         *
 
1192
         * @param object $item
 
1193
         */
1051
1194
        protected function column_cb( $item ) {}
1052
1195
 
1053
1196
        /**
1059
1202
         * @param object $item The current item
1060
1203
         */
1061
1204
        protected function single_row_columns( $item ) {
1062
 
                list( $columns, $hidden ) = $this->get_column_info();
 
1205
                list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
1063
1206
 
1064
1207
                foreach ( $columns as $column_name => $column_display_name ) {
1065
 
                        $class = "class='$column_name column-$column_name'";
1066
 
 
1067
 
                        $style = '';
1068
 
                        if ( in_array( $column_name, $hidden ) )
1069
 
                                $style = ' style="display:none;"';
1070
 
 
1071
 
                        $attributes = "$class$style";
 
1208
                        $classes = "$column_name column-$column_name";
 
1209
                        if ( $primary === $column_name ) {
 
1210
                                $classes .= ' has-row-actions column-primary';
 
1211
                        }
 
1212
 
 
1213
                        if ( in_array( $column_name, $hidden ) ) {
 
1214
                                $classes .= ' hidden';
 
1215
                        }
 
1216
 
 
1217
                        // Comments column uses HTML in the display name with screen reader text.
 
1218
                        // Instead of using esc_attr(), we strip tags to get closer to a user-friendly string.
 
1219
                        $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
 
1220
 
 
1221
                        $attributes = "class='$classes' $data";
1072
1222
 
1073
1223
                        if ( 'cb' == $column_name ) {
1074
1224
                                echo '<th scope="row" class="check-column">';
1075
1225
                                echo $this->column_cb( $item );
1076
1226
                                echo '</th>';
1077
 
                        }
1078
 
                        elseif ( method_exists( $this, 'column_' . $column_name ) ) {
 
1227
                        } elseif ( method_exists( $this, '_column_' . $column_name ) ) {
 
1228
                                echo call_user_func(
 
1229
                                        array( $this, '_column_' . $column_name ),
 
1230
                                        $item,
 
1231
                                        $classes,
 
1232
                                        $data,
 
1233
                                        $primary
 
1234
                                );
 
1235
                        } elseif ( method_exists( $this, 'column_' . $column_name ) ) {
1079
1236
                                echo "<td $attributes>";
1080
1237
                                echo call_user_func( array( $this, 'column_' . $column_name ), $item );
 
1238
                                echo $this->handle_row_actions( $item, $column_name, $primary );
1081
1239
                                echo "</td>";
1082
 
                        }
1083
 
                        else {
 
1240
                        } else {
1084
1241
                                echo "<td $attributes>";
1085
1242
                                echo $this->column_default( $item, $column_name );
 
1243
                                echo $this->handle_row_actions( $item, $column_name, $primary );
1086
1244
                                echo "</td>";
1087
1245
                        }
1088
1246
                }
1089
1247
        }
1090
1248
 
1091
1249
        /**
 
1250
         * Generates and display row actions links for the list table.
 
1251
         *
 
1252
         * @since 4.3.0
 
1253
         * @access protected
 
1254
         *
 
1255
         * @param object $item        The item being acted upon.
 
1256
         * @param string $column_name Current column name.
 
1257
         * @param string $primary     Primary column name.
 
1258
         * @return string The row actions output. In this case, an empty string.
 
1259
         */
 
1260
        protected function handle_row_actions( $item, $column_name, $primary ) {
 
1261
                return $column_name == $primary ? '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>' : '';
 
1262
        }
 
1263
 
 
1264
        /**
1092
1265
         * Handle an incoming ajax request (called from admin-ajax.php)
1093
1266
         *
1094
1267
         * @since 3.1.0
1110
1283
 
1111
1284
                if ( isset( $this->_pagination_args['total_items'] ) ) {
1112
1285
                        $response['total_items_i18n'] = sprintf(
1113
 
                                _n( '1 item', '%s items', $this->_pagination_args['total_items'] ),
 
1286
                                _n( '%s item', '%s items', $this->_pagination_args['total_items'] ),
1114
1287
                                number_format_i18n( $this->_pagination_args['total_items'] )
1115
1288
                        );
1116
1289
                }