wp/wp-admin/includes/class-wp-posts-list-table.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
--- a/wp/wp-admin/includes/class-wp-posts-list-table.php	Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-admin/includes/class-wp-posts-list-table.php	Wed Sep 21 18:19:35 2022 +0200
@@ -84,35 +84,47 @@
 		$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 = intval(
-			$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
-		",
-					$post_type,
-					get_current_user_id()
-				)
+
+		$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",
+				$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
+				)
+			);
 		}
 	}
 
@@ -153,7 +165,10 @@
 		// 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' );
@@ -172,7 +187,7 @@
 				$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 );
@@ -246,6 +261,7 @@
 
 		$class_html   = '';
 		$aria_current = '';
+
 		if ( ! empty( $class ) ) {
 			$class_html = sprintf(
 				' class="%s"',
@@ -295,7 +311,7 @@
 		}
 
 		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';
 			}
 
@@ -337,6 +353,7 @@
 		);
 
 		$status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
+
 		if ( $mine ) {
 			$status_links['mine'] = $mine;
 		}
@@ -458,7 +475,8 @@
 				'selected'        => $cat,
 			);
 
-			echo '<label class="screen-reader-text" for="cat">' . __( 'Filter by category' ) . '</label>';
+			echo '<label class="screen-reader-text" for="cat">' . get_taxonomy( 'category' )->labels->filter_by_item . '</label>';
+
 			wp_dropdown_categories( $dropdown_options );
 		}
 	}
@@ -514,6 +532,7 @@
 				$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;
@@ -566,7 +585,9 @@
 			}
 		}
 
-		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 );
 		}
 		?>
@@ -604,7 +625,13 @@
 
 		$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',
+		);
 	}
 
 	/**
@@ -633,6 +660,11 @@
 		 * The dynamic portion of the hook name, `$post_type`, refers to the post
 		 * type slug.
 		 *
+		 * Possible hook names include:
+		 *
+		 *  - `manage_taxonomies_for_post_columns`
+		 *  - `manage_taxonomies_for_page_columns`
+		 *
 		 * @since 3.5.0
 		 *
 		 * @param string[] $taxonomies Array of taxonomy names to show columns for.
@@ -654,8 +686,15 @@
 		}
 
 		$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'] = '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>';
+
+		if ( post_type_supports( $post_type, 'comments' )
+			&& ! in_array( $post_status, array( 'pending', 'draft', 'future' ), true )
+		) {
+			$posts_columns['comments'] = sprintf(
+				'<span class="vers comment-grey-bubble" title="%1$s"><span class="screen-reader-text">%2$s</span></span>',
+				esc_attr__( 'Comments' ),
+				__( 'Comments' )
+			);
 		}
 
 		$posts_columns['date'] = __( 'Date' );
@@ -688,6 +727,11 @@
 		 *
 		 * The dynamic portion of the hook name, `$post_type`, refers to the post type slug.
 		 *
+		 * Possible hook names include:
+		 *
+		 *  - `manage_post_posts_columns`
+		 *  - `manage_page_posts_columns`
+		 *
 		 * @since 3.0.0
 		 *
 		 * @param string[] $post_columns An associative array of column headings.
@@ -780,23 +824,21 @@
 		 * 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 ) {
+				if ( $page->post_parent > 0 ) {
+					$children_pages[ $page->post_parent ][] = $page;
+				} else {
 					$top_level_pages[] = $page;
-				} else {
-					$children_pages[ $page->post_parent ][] = $page;
 				}
 			}
 
@@ -883,24 +925,30 @@
 			}
 
 			// 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--;
@@ -927,7 +975,19 @@
 	 * @param WP_Post $post The current WP_Post object.
 	 */
 	public function column_cb( $post ) {
-		if ( current_user_can( 'edit_post', $post->ID ) ) :
+		$show = current_user_can( 'edit_post', $post->ID );
+
+		/**
+		 * Filters whether to show the bulk edit checkbox for a post in its list table.
+		 *
+		 * By default the checkbox is only shown if the current user can edit the post.
+		 *
+		 * @since 5.7.0
+		 *
+		 * @param bool    $show Whether to show the checkbox.
+		 * @param WP_Post $post The current WP_Post object.
+		 */
+		if ( apply_filters( 'wp_list_table_show_post_checkbox', $show, $post ) ) :
 			?>
 			<label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>">
 				<?php
@@ -983,6 +1043,7 @@
 			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 );
 
@@ -1046,6 +1107,7 @@
 			$post_type_object = get_post_type_object( $post->post_type );
 			echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name );
 		}
+
 		echo "</strong>\n";
 
 		if ( 'excerpt' === $mode
@@ -1081,9 +1143,9 @@
 			$t_time = sprintf(
 				/* translators: 1: Post date, 2: Post time. */
 				__( '%1$s at %2$s' ),
-				/* translators: Post date format. See https://www.php.net/date */
+				/* translators: Post date format. See https://www.php.net/manual/datetime.format.php */
 				get_the_time( __( 'Y/m/d' ), $post ),
-				/* translators: Post time format. See https://www.php.net/date */
+				/* translators: Post time format. See https://www.php.net/manual/datetime.format.php */
 				get_the_time( __( 'g:i a' ), $post )
 			);
 
@@ -1187,16 +1249,21 @@
 		} 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 {
@@ -1221,7 +1288,7 @@
 				$term_links = apply_filters( 'post_column_taxonomy_links', $term_links, $taxonomy, $terms );
 
 				/* translators: Used between list items, there is a space after the comma. */
-				echo join( __( ', ' ), $term_links );
+				echo implode( __( ', ' ), $term_links );
 			} else {
 				echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . $taxonomy_object->labels->no_terms . '</span>';
 			}
@@ -1263,6 +1330,11 @@
 		 *
 		 * The dynamic portion of the hook name, `$post->post_type`, refers to the post type.
 		 *
+		 * Possible hook names include:
+		 *
+		 *  - `manage_post_posts_custom_column`
+		 *  - `manage_page_posts_custom_column`
+		 *
 		 * @since 3.1.0
 		 *
 		 * @param string $column_name The name of the column to display.
@@ -1286,9 +1358,10 @@
 		$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';
 		}
@@ -1323,9 +1396,9 @@
 	 *
 	 * @since 4.3.0
 	 *
-	 * @param object $post        Post being acted upon.
-	 * @param string $column_name Current column name.
-	 * @param string $primary     Primary column name.
+	 * @param WP_Post $post        Post being acted upon.
+	 * @param string  $column_name Current column name.
+	 * @param string  $primary     Primary column name.
 	 * @return string Row actions output for posts, or an empty string
 	 *                if the current column is not the primary column.
 	 */
@@ -1376,6 +1449,7 @@
 					_x( 'Trash', 'verb' )
 				);
 			}
+
 			if ( 'trash' === $post->post_status || ! EMPTY_TRASH_DAYS ) {
 				$actions['delete'] = sprintf(
 					'<a href="%s" class="submitdelete" aria-label="%s">%s</a>',
@@ -1475,7 +1549,6 @@
 		$flat_taxonomies         = array();
 
 		foreach ( $taxonomy_names as $taxonomy_name ) {
-
 			$taxonomy = get_taxonomy( $taxonomy_name );
 
 			$show_in_quick_edit = $taxonomy->show_in_quick_edit;
@@ -1511,7 +1584,6 @@
 			'comments'   => true,
 			'author'     => true,
 		);
-
 		?>
 
 		<form method="get">
@@ -1523,6 +1595,7 @@
 		$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;
@@ -1571,10 +1644,10 @@
 				<?php endif; // $bulk ?>
 
 				<?php
-				if ( post_type_supports( $screen->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',
@@ -1589,21 +1662,32 @@
 							$users_opt['show_option_none'] = __( '&mdash; No Change &mdash;' );
 						}
 
+						/**
+						 * Filters the arguments used to generate the Quick Edit authors drop-down.
+						 *
+						 * @since 5.6.0
+						 *
+						 * @see wp_dropdown_users()
+						 *
+						 * @param array $users_opt An array of arguments passed to wp_dropdown_users().
+						 * @param bool  $bulk      A flag to denote if it's a bulk action.
+						 */
+						$users_opt = apply_filters( 'quick_edit_dropdown_authors_args', $users_opt, $bulk );
+
 						$authors = wp_dropdown_users( $users_opt );
-						if ( $authors ) :
+
+						if ( $authors ) {
 							$authors_dropdown  = '<label class="inline-edit-author">';
 							$authors_dropdown .= '<span class="title">' . __( 'Author' ) . '</span>';
 							$authors_dropdown .= $authors;
 							$authors_dropdown .= '</label>';
-						endif;
-					endif; // current_user_can( 'edit_others_posts' )
-					?>
+						}
+					} // current_user_can( 'edit_others_posts' )
 
-					<?php
 					if ( ! $bulk ) {
 						echo $authors_dropdown;
 					}
-				endif; // post_type_supports( ... 'author' )
+				} // post_type_supports( ... 'author' )
 				?>
 
 				<?php if ( ! $bulk && $can_publish ) : ?>
@@ -1684,12 +1768,14 @@
 							 * Filters the arguments used to generate the Quick Edit page-parent drop-down.
 							 *
 							 * @since 2.7.0
+							 * @since 5.6.0 The `$bulk` parameter was added.
 							 *
 							 * @see wp_dropdown_pages()
 							 *
-							 * @param array $dropdown_args An array of arguments.
+							 * @param array $dropdown_args An array of arguments passed to wp_dropdown_pages().
+							 * @param bool  $bulk          A flag to denote if it's a bulk action.
 							 */
-							$dropdown_args = apply_filters( 'quick_edit_dropdown_pages_args', $dropdown_args );
+							$dropdown_args = apply_filters( 'quick_edit_dropdown_pages_args', $dropdown_args, $bulk );
 
 							wp_dropdown_pages( $dropdown_args );
 							?>