Yoast\WP\SEO\Repositories

SEO_Links_Repository::get_incoming_link_counts_for_indexable_ids()publicYoast 1.0

Returns incoming link counts for a number of indexables.

Метод класса: SEO_Links_Repository{}

Хуков нет.

Возвращает

Массив. An array of associative arrays, each containing a indexable id and incoming property.

Использование

$SEO_Links_Repository = new SEO_Links_Repository();
$SEO_Links_Repository->get_incoming_link_counts_for_indexable_ids( $indexable_ids );
$indexable_ids(массив) (обязательный)
The indexable IDs.

Код SEO_Links_Repository::get_incoming_link_counts_for_indexable_ids() Yoast 22.3

public function get_incoming_link_counts_for_indexable_ids( $indexable_ids ) {
	if ( empty( $indexable_ids ) ) {
		return [];
	}

	// This query only returns ID's with an incoming count > 0. We need to restore any ID's with 0 incoming links later.
	$indexable_counts = $this->query()
		->select_expr( 'COUNT( id )', 'incoming' )
		->select( 'target_indexable_id' )
		->where_in( 'target_indexable_id', $indexable_ids )
		->group_by( 'target_indexable_id' )
		->find_array();

	// If the above query fails, do not update anything.
	if ( ! \is_array( $indexable_counts ) ) {
		return [];
	}

	// Get all ID's returned from the query and set them as keys for easy access.
	$returned_ids = \array_flip( \array_column( $indexable_counts, 'target_indexable_id' ) );

	// Loop over the original ID's and search them in the returned ID's. If they don't exist, add them with an incoming count of 0.
	foreach ( $indexable_ids as $id ) {
		// Cast the ID to string, as the arrays only contain stringified versions of the ID.
		$id = \strval( $id );
		if ( isset( $returned_ids[ $id ] ) === false ) {
			$indexable_counts[] = [
				'incoming'            => '0',
				'target_indexable_id' => $id,
			];
		}
	}

	return $indexable_counts;
}