From 4f2c63bbf6b1fe24718d17aa0d52fde8e2d9fd47 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 3 May 2021 13:57:04 +0000 Subject: [PATCH] Coding Standards: Use strict comparison in `wp-admin/includes/class-wp-posts-list-table.php`. Includes minor code layout fixes for better readability. See #52627. Built from https://develop.svn.wordpress.org/trunk@50804 git-svn-id: http://core.svn.wordpress.org/trunk@50413 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-comments-list-table.php | 4 +- .../includes/class-wp-media-list-table.php | 12 +- .../includes/class-wp-posts-list-table.php | 118 ++++++++++++------ wp-includes/version.php | 2 +- 4 files changed, 97 insertions(+), 39 deletions(-) diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php index 45f63631d8..681cdd0885 100644 --- a/wp-admin/includes/class-wp-comments-list-table.php +++ b/wp-admin/includes/class-wp-comments-list-table.php @@ -413,7 +413,9 @@ class WP_Comments_List_Table extends WP_List_Table { } } - if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && current_user_can( 'moderate_comments' ) && $has_items ) { + if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && $has_items + && current_user_can( 'moderate_comments' ) + ) { wp_nonce_field( 'bulk-destroy', '_destroy_nonce' ); $title = ( 'spam' === $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' ); submit_button( $title, 'apply', 'delete_all', false ); diff --git a/wp-admin/includes/class-wp-media-list-table.php b/wp-admin/includes/class-wp-media-list-table.php index 872591b312..b3eb0b32cc 100644 --- a/wp-admin/includes/class-wp-media-list-table.php +++ b/wp-admin/includes/class-wp-media-list-table.php @@ -171,6 +171,7 @@ class WP_Media_List_Table extends WP_List_Table { */ protected function get_bulk_actions() { $actions = array(); + if ( MEDIA_TRASH ) { if ( $this->is_trash ) { $actions['untrash'] = __( 'Restore' ); @@ -208,7 +209,9 @@ class WP_Media_List_Table extends WP_List_Table { submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) ); - if ( $this->is_trash && current_user_can( 'edit_others_posts' ) && $this->has_items() ) { + if ( $this->is_trash && $this->has_items() + && current_user_can( 'edit_others_posts' ) + ) { submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false ); } ?> @@ -336,6 +339,7 @@ class WP_Media_List_Table extends WP_List_Table { } else { $column_key = 'taxonomy-' . $taxonomy; } + $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name; } @@ -346,8 +350,10 @@ class WP_Media_List_Table extends WP_List_Table { $posts_columns['comments'] = '' . __( 'Comments' ) . ''; } } + /* translators: Column name. */ $posts_columns['date'] = _x( 'Date', 'column name' ); + /** * Filters the Media list table columns. * @@ -424,12 +430,15 @@ class WP_Media_List_Table extends WP_List_Table { > @@ -598,6 +607,7 @@ class WP_Media_List_Table extends WP_List_Table { if ( $taxonomy ) { $terms = get_the_terms( $post->ID, $taxonomy ); + if ( is_array( $terms ) ) { $out = array(); foreach ( $terms as $t ) { diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index 35b640f920..860e48e58f 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -84,33 +84,47 @@ class WP_Posts_List_Table extends WP_List_Table { $post_type = $this->screen->post_type; $post_type_object = get_post_type_object( $post_type ); - $exclude_states = get_post_stati( + $exclude_states = get_post_stati( array( 'show_in_admin_all_list' => false, ) ); + $this->user_posts_count = (int) $wpdb->get_var( $wpdb->prepare( - " - SELECT COUNT( 1 ) - FROM $wpdb->posts - WHERE post_type = %s - AND post_status NOT IN ( '" . implode( "','", $exclude_states ) . "' ) - AND post_author = %d - ", + "SELECT COUNT( 1 ) + FROM $wpdb->posts + WHERE post_type = %s + AND post_status NOT IN ( '" . implode( "','", $exclude_states ) . "' ) + AND post_author = %d", $post_type, get_current_user_id() ) ); - if ( $this->user_posts_count && ! current_user_can( $post_type_object->cap->edit_others_posts ) && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['all_posts'] ) && empty( $_REQUEST['author'] ) && empty( $_REQUEST['show_sticky'] ) ) { + if ( $this->user_posts_count + && ! current_user_can( $post_type_object->cap->edit_others_posts ) + && empty( $_REQUEST['post_status'] ) && empty( $_REQUEST['all_posts'] ) + && empty( $_REQUEST['author'] ) && empty( $_REQUEST['show_sticky'] ) + ) { $_GET['author'] = get_current_user_id(); } $sticky_posts = get_option( 'sticky_posts' ); + if ( 'post' === $post_type && $sticky_posts ) { - $sticky_posts = implode( ', ', array_map( 'absint', (array) $sticky_posts ) ); - $this->sticky_posts_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( 1 ) FROM $wpdb->posts WHERE post_type = %s AND post_status NOT IN ('trash', 'auto-draft') AND ID IN ($sticky_posts)", $post_type ) ); + $sticky_posts = implode( ', ', array_map( 'absint', (array) $sticky_posts ) ); + + $this->sticky_posts_count = (int) $wpdb->get_var( + $wpdb->prepare( + "SELECT COUNT( 1 ) + FROM $wpdb->posts + WHERE post_type = %s + AND post_status NOT IN ('trash', 'auto-draft') + AND ID IN ($sticky_posts)", + $post_type + ) + ); } } @@ -151,7 +165,10 @@ class WP_Posts_List_Table extends WP_List_Table { // Is going to call wp(). $avail_post_stati = wp_edit_posts_query(); - $this->set_hierarchical_display( is_post_type_hierarchical( $this->screen->post_type ) && 'menu_order title' === $wp_query->query['orderby'] ); + $this->set_hierarchical_display( + is_post_type_hierarchical( $this->screen->post_type ) + && 'menu_order title' === $wp_query->query['orderby'] + ); $post_type = $this->screen->post_type; $per_page = $this->get_items_per_page( 'edit_' . $post_type . '_per_page' ); @@ -170,7 +187,7 @@ class WP_Posts_List_Table extends WP_List_Table { $total_items = $post_counts[ $_REQUEST['post_status'] ]; } elseif ( isset( $_REQUEST['show_sticky'] ) && $_REQUEST['show_sticky'] ) { $total_items = $this->sticky_posts_count; - } elseif ( isset( $_GET['author'] ) && get_current_user_id() == $_GET['author'] ) { + } elseif ( isset( $_GET['author'] ) && get_current_user_id() === (int) $_GET['author'] ) { $total_items = $this->user_posts_count; } else { $total_items = array_sum( $post_counts ); @@ -244,6 +261,7 @@ class WP_Posts_List_Table extends WP_List_Table { $class_html = ''; $aria_current = ''; + if ( ! empty( $class ) ) { $class_html = sprintf( ' class="%s"', @@ -293,7 +311,7 @@ class WP_Posts_List_Table extends WP_List_Table { } if ( $this->user_posts_count && $this->user_posts_count !== $total_posts ) { - if ( isset( $_GET['author'] ) && ( $_GET['author'] == $current_user_id ) ) { + if ( isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ) ) { $class = 'current'; } @@ -335,6 +353,7 @@ class WP_Posts_List_Table extends WP_List_Table { ); $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class ); + if ( $mine ) { $status_links['mine'] = $mine; } @@ -513,6 +532,7 @@ class WP_Posts_List_Table extends WP_List_Table { $slug = str_replace( 'post-format-', '', $used_post_format->slug ); // Pretty, translated version of the post format slug. $pretty_name = get_post_format_string( $slug ); + // Skip the standard post format. if ( 'standard' === $slug ) { continue; @@ -565,7 +585,9 @@ class WP_Posts_List_Table extends WP_List_Table { } } - if ( $this->is_trash && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) && $this->has_items() ) { + if ( $this->is_trash && $this->has_items() + && current_user_can( get_post_type_object( $this->screen->post_type )->cap->edit_others_posts ) + ) { submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false ); } ?> @@ -603,7 +625,13 @@ class WP_Posts_List_Table extends WP_List_Table { $mode_class = esc_attr( 'table-view-' . $mode ); - return array( 'widefat', 'fixed', 'striped', $mode_class, is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts' ); + return array( + 'widefat', + 'fixed', + 'striped', + $mode_class, + is_post_type_hierarchical( $this->screen->post_type ) ? 'pages' : 'posts', + ); } /** @@ -658,8 +686,15 @@ class WP_Posts_List_Table extends WP_List_Table { } $post_status = ! empty( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : 'all'; - if ( post_type_supports( $post_type, 'comments' ) && ! in_array( $post_status, array( 'pending', 'draft', 'future' ), true ) ) { - $posts_columns['comments'] = '' . __( 'Comments' ) . ''; + + if ( post_type_supports( $post_type, 'comments' ) + && ! in_array( $post_status, array( 'pending', 'draft', 'future' ), true ) + ) { + $posts_columns['comments'] = sprintf( + '%2$s', + esc_attr__( 'Comments' ), + __( 'Comments' ) + ); } $posts_columns['date'] = __( 'Date' ); @@ -789,23 +824,21 @@ class WP_Posts_List_Table extends WP_List_Table { * If searching, ignore hierarchy and treat everything as top level */ if ( empty( $_REQUEST['s'] ) ) { - $top_level_pages = array(); $children_pages = array(); foreach ( $pages as $page ) { - // Catch and repair bad pages. - if ( $page->post_parent == $page->ID ) { + if ( $page->post_parent === $page->ID ) { $page->post_parent = 0; $wpdb->update( $wpdb->posts, array( 'post_parent' => 0 ), array( 'ID' => $page->ID ) ); clean_post_cache( $page ); } - if ( 0 == $page->post_parent ) { - $top_level_pages[] = $page; - } else { + if ( $page->post_parent > 0 ) { $children_pages[ $page->post_parent ][] = $page; + } else { + $top_level_pages[] = $page; } } @@ -892,24 +925,30 @@ class WP_Posts_List_Table extends WP_List_Table { } // If the page starts in a subtree, print the parents. - if ( $count == $start && $page->post_parent > 0 ) { + if ( $count === $start && $page->post_parent > 0 ) { $my_parents = array(); $my_parent = $page->post_parent; + while ( $my_parent ) { // Get the ID from the list or the attribute if my_parent is an object. $parent_id = $my_parent; + if ( is_object( $my_parent ) ) { $parent_id = $my_parent->ID; } $my_parent = get_post( $parent_id ); $my_parents[] = $my_parent; + if ( ! $my_parent->post_parent ) { break; } + $my_parent = $my_parent->post_parent; } + $num_parents = count( $my_parents ); + while ( $my_parent = array_pop( $my_parents ) ) { $to_display[ $my_parent->ID ] = $level - $num_parents; $num_parents--; @@ -1004,6 +1043,7 @@ class WP_Posts_List_Table extends WP_List_Table { if ( 0 === $this->current_level && (int) $post->post_parent > 0 ) { // Sent level 0 by accident, by default, or because we don't know the actual level. $find_main_page = (int) $post->post_parent; + while ( $find_main_page > 0 ) { $parent = get_post( $find_main_page ); @@ -1067,6 +1107,7 @@ class WP_Posts_List_Table extends WP_List_Table { $post_type_object = get_post_type_object( $post->post_type ); echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name ); } + echo "\n"; if ( 'excerpt' === $mode @@ -1208,16 +1249,21 @@ class WP_Posts_List_Table extends WP_List_Table { } else { $taxonomy = false; } + if ( $taxonomy ) { $taxonomy_object = get_taxonomy( $taxonomy ); $terms = get_the_terms( $post->ID, $taxonomy ); + if ( is_array( $terms ) ) { $term_links = array(); + foreach ( $terms as $t ) { $posts_in_term_qv = array(); + if ( 'post' !== $post->post_type ) { $posts_in_term_qv['post_type'] = $post->post_type; } + if ( $taxonomy_object->query_var ) { $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug; } else { @@ -1312,9 +1358,10 @@ class WP_Posts_List_Table extends WP_List_Table { $GLOBALS['post'] = $post; setup_postdata( $post ); - $classes = 'iedit author-' . ( get_current_user_id() == $post->post_author ? 'self' : 'other' ); + $classes = 'iedit author-' . ( get_current_user_id() === (int) $post->post_author ? 'self' : 'other' ); $lock_holder = wp_check_post_lock( $post->ID ); + if ( $lock_holder ) { $classes .= ' wp-locked'; } @@ -1402,6 +1449,7 @@ class WP_Posts_List_Table extends WP_List_Table { _x( 'Trash', 'verb' ) ); } + if ( 'trash' === $post->post_status || ! EMPTY_TRASH_DAYS ) { $actions['delete'] = sprintf( '%s', @@ -1501,7 +1549,6 @@ class WP_Posts_List_Table extends WP_List_Table { $flat_taxonomies = array(); foreach ( $taxonomy_names as $taxonomy_name ) { - $taxonomy = get_taxonomy( $taxonomy_name ); $show_in_quick_edit = $taxonomy->show_in_quick_edit; @@ -1537,7 +1584,6 @@ class WP_Posts_List_Table extends WP_List_Table { 'comments' => true, 'author' => true, ); - ?>
@@ -1549,6 +1595,7 @@ class WP_Posts_List_Table extends WP_List_Table { $quick_edit_classes = "quick-edit-row quick-edit-row-$hclass inline-edit-{$screen->post_type}"; $bulk = 0; + while ( $bulk < 2 ) : $classes = $inline_edit_classes . ' '; $classes .= $bulk ? $bulk_edit_classes : $quick_edit_classes; @@ -1597,10 +1644,10 @@ class WP_Posts_List_Table extends WP_List_Table { post_type, 'author' ) ) : + if ( post_type_supports( $screen->post_type, 'author' ) ) { $authors_dropdown = ''; - if ( current_user_can( $post_type_object->cap->edit_others_posts ) ) : + if ( current_user_can( $post_type_object->cap->edit_others_posts ) ) { $users_opt = array( 'hide_if_only_one_author' => false, 'who' => 'authors', @@ -1628,20 +1675,19 @@ class WP_Posts_List_Table extends WP_List_Table { $users_opt = apply_filters( 'quick_edit_dropdown_authors_args', $users_opt, $bulk ); $authors = wp_dropdown_users( $users_opt ); - if ( $authors ) : + + if ( $authors ) { $authors_dropdown = ''; - endif; - endif; // current_user_can( 'edit_others_posts' ) - ?> + } + } // current_user_can( 'edit_others_posts' ) - diff --git a/wp-includes/version.php b/wp-includes/version.php index 4434021342..9dfa2b4b55 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.8-alpha-50803'; +$wp_version = '5.8-alpha-50804'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.