WP_Posts_List_Table::get_views(): array

In this article

Return

array

Source

protected function get_views() {
	global $locked_post_status, $avail_post_stati;

	$post_type = $this->screen->post_type;

	if ( ! empty( $locked_post_status ) ) {
		return array();
	}

	$status_links = array();
	$num_posts    = wp_count_posts( $post_type, 'readable' );
	$total_posts  = array_sum( (array) $num_posts );
	$class        = '';

	$current_user_id = get_current_user_id();
	$all_args        = array( 'post_type' => $post_type );
	$mine            = '';

	// Subtract post types that are not included in the admin all list.
	foreach ( get_post_stati( array( 'show_in_admin_all_list' => false ) ) as $state ) {
		$total_posts -= $num_posts->$state;
	}

	if ( $this->user_posts_count && $this->user_posts_count !== $total_posts ) {
		if ( isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ) ) {
			$class = 'current';
		}

		$mine_args = array(
			'post_type' => $post_type,
			'author'    => $current_user_id,
		);

		$mine_inner_html = sprintf(
			/* translators: %s: Number of posts. */
			_nx(
				'Mine <span class="count">(%s)</span>',
				'Mine <span class="count">(%s)</span>',
				$this->user_posts_count,
				'posts'
			),
			number_format_i18n( $this->user_posts_count )
		);

		$mine = array(
			'url'     => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
			'label'   => $mine_inner_html,
			'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
		);

		$all_args['all_posts'] = 1;
		$class                 = '';
	}

	$all_inner_html = sprintf(
		/* translators: %s: Number of posts. */
		_nx(
			'All <span class="count">(%s)</span>',
			'All <span class="count">(%s)</span>',
			$total_posts,
			'posts'
		),
		number_format_i18n( $total_posts )
	);

	$status_links['all'] = array(
		'url'     => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
		'label'   => $all_inner_html,
		'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
	);

	if ( $mine ) {
		$status_links['mine'] = $mine;
	}

	foreach ( get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' ) as $status ) {
		$class = '';

		$status_name = $status->name;

		if ( ! in_array( $status_name, $avail_post_stati, true ) || empty( $num_posts->$status_name ) ) {
			continue;
		}

		if ( isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'] ) {
			$class = 'current';
		}

		$status_args = array(
			'post_status' => $status_name,
			'post_type'   => $post_type,
		);

		$status_label = sprintf(
			translate_nooped_plural( $status->label_count, $num_posts->$status_name ),
			number_format_i18n( $num_posts->$status_name )
		);

		$status_links[ $status_name ] = array(
			'url'     => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
			'label'   => $status_label,
			'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
		);
	}

	if ( ! empty( $this->sticky_posts_count ) ) {
		$class = ! empty( $_REQUEST['show_sticky'] ) ? 'current' : '';

		$sticky_args = array(
			'post_type'   => $post_type,
			'show_sticky' => 1,
		);

		$sticky_inner_html = sprintf(
			/* translators: %s: Number of posts. */
			_nx(
				'Sticky <span class="count">(%s)</span>',
				'Sticky <span class="count">(%s)</span>',
				$this->sticky_posts_count,
				'posts'
			),
			number_format_i18n( $this->sticky_posts_count )
		);

		$sticky_link = array(
			'sticky' => array(
				'url'     => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
				'label'   => $sticky_inner_html,
				'current' => ! empty( $_REQUEST['show_sticky'] ),
			),
		);

		// Sticky comes after Publish, or if not listed, after All.
		$split        = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ), true );
		$status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) );
	}

	return $this->get_views_links( $status_links );
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.