WP_MS_Sites_List_Table::prepare_items()

In this article

Prepares the list of sites for display.

Source

public function prepare_items() {
	global $mode, $s, $wpdb;

	if ( ! empty( $_REQUEST['mode'] ) ) {
		$mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
		set_user_setting( 'sites_list_mode', $mode );
	} else {
		$mode = get_user_setting( 'sites_list_mode', 'list' );
	}

	$per_page = $this->get_items_per_page( 'sites_network_per_page' );

	$pagenum = $this->get_pagenum();

	$s    = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
	$wild = '';
	if ( str_contains( $s, '*' ) ) {
		$wild = '*';
		$s    = trim( $s, '*' );
	}

	/*
	 * If the network is large and a search is not being performed, show only
	 * the latest sites with no paging in order to avoid expensive count queries.
	 */
	if ( ! $s && wp_is_large_network() ) {
		if ( ! isset( $_REQUEST['orderby'] ) ) {
			$_GET['orderby']     = '';
			$_REQUEST['orderby'] = '';
		}
		if ( ! isset( $_REQUEST['order'] ) ) {
			$_GET['order']     = 'DESC';
			$_REQUEST['order'] = 'DESC';
		}
	}

	$args = array(
		'number'     => (int) $per_page,
		'offset'     => (int) ( ( $pagenum - 1 ) * $per_page ),
		'network_id' => get_current_network_id(),
	);

	if ( empty( $s ) ) {
		// Nothing to do.
	} elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s )
		|| preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s )
		|| preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s )
		|| preg_match( '/^[0-9]{1,3}\.$/', $s )
	) {
		// IPv4 address.
		$sql = $wpdb->prepare(
			"SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s",
			$wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' )
		);

		$reg_blog_ids = $wpdb->get_col( $sql );

		if ( $reg_blog_ids ) {
			$args['site__in'] = $reg_blog_ids;
		}
	} elseif ( is_numeric( $s ) && empty( $wild ) ) {
		$args['ID'] = $s;
	} else {
		$args['search'] = $s;

		if ( ! is_subdomain_install() ) {
			$args['search_columns'] = array( 'path' );
		}
	}

	$order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
	if ( 'registered' === $order_by ) {
		// 'registered' is a valid field name.
	} elseif ( 'lastupdated' === $order_by ) {
		$order_by = 'last_updated';
	} elseif ( 'blogname' === $order_by ) {
		if ( is_subdomain_install() ) {
			$order_by = 'domain';
		} else {
			$order_by = 'path';
		}
	} elseif ( 'blog_id' === $order_by ) {
		$order_by = 'id';
	} elseif ( ! $order_by ) {
		$order_by = false;
	}

	$args['orderby'] = $order_by;

	if ( $order_by ) {
		$args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? 'DESC' : 'ASC';
	}

	if ( wp_is_large_network() ) {
		$args['no_found_rows'] = true;
	} else {
		$args['no_found_rows'] = false;
	}

	// Take into account the role the user has selected.
	$status = isset( $_REQUEST['status'] ) ? wp_unslash( trim( $_REQUEST['status'] ) ) : '';
	if ( in_array( $status, array( 'public', 'archived', 'mature', 'spam', 'deleted' ), true ) ) {
		$args[ $status ] = 1;
	}

	/**
	 * Filters the arguments for the site query in the sites list table.
	 *
	 * @since 4.6.0
	 *
	 * @param array $args An array of get_sites() arguments.
	 */
	$args = apply_filters( 'ms_sites_list_table_query_args', $args );

	$_sites = get_sites( $args );
	if ( is_array( $_sites ) ) {
		update_site_cache( $_sites );

		$this->items = array_slice( $_sites, 0, $per_page );
	}

	$total_sites = get_sites(
		array_merge(
			$args,
			array(
				'count'  => true,
				'offset' => 0,
				'number' => 0,
			)
		)
	);

	$this->set_pagination_args(
		array(
			'total_items' => $total_sites,
			'per_page'    => $per_page,
		)
	);
}

Hooks

apply_filters( ‘ms_sites_list_table_query_args’, array $args )

Filters the arguments for the site query in the sites list table.

Changelog

VersionDescription
3.1.0Introduced.

User Contributed Notes

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