WPSEO_Upgrade::get_indexable_deduplication_query_for_type()protectedYoast 1.0

Creates a query for de-duplicating indexables for a particular type.

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

Хуков нет.

Возвращает

Строку. The query that removes all but one duplicate for each object of the object type.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb );
$object_type(строка) (обязательный)
The object type to deduplicate.
$duplicates(строка|array<array<int,int,string>>) (обязательный)
The result of the duplicate query.
$wpdb(wpdb) (обязательный)
The wpdb object.

Код WPSEO_Upgrade::get_indexable_deduplication_query_for_type() Yoast 25.0

protected function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) {
	$filtered_duplicates = array_filter(
		$duplicates,
		static function ( $duplicate ) use ( $object_type ) {
			return $duplicate['object_type'] === $object_type;
		}
	);

	if ( empty( $filtered_duplicates ) ) {
		return '';
	}

	$object_ids           = wp_list_pluck( $filtered_duplicates, 'object_id' );
	$newest_indexable_ids = wp_list_pluck( $filtered_duplicates, 'newest_id' );

	$replacements   = array_merge( [ Model::get_table_name( 'Indexable' ), 'object_id' ], array_values( $object_ids ), array_values( $newest_indexable_ids ) );
	$replacements[] = $object_type;

	// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
	// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way.
	return $wpdb->prepare(
		'DELETE FROM
			%i
		WHERE
			%i IN ( ' . implode( ', ', array_fill( 0, count( $filtered_duplicates ), '%d' ) ) . ' )
			AND id NOT IN ( ' . implode( ', ', array_fill( 0, count( $filtered_duplicates ), '%d' ) ) . ' )
			AND object_type = %s',
		$replacements
	);
}