WPSEO_Upgrade::get_indexable_deduplication_query_for_type()privateYoast 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.

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

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

Код WPSEO_Upgrade::get_indexable_deduplication_query_for_type() Yoast 20.0

private function get_indexable_deduplication_query_for_type( $object_type, $duplicates, $wpdb ) {
	$indexable_table = Model::get_table_name( 'Indexable' );

	$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' );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: Too hard to fix.
	// phpcs:disable WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Reason: we're passing an array instead.
	return $wpdb->prepare(
		"DELETE FROM
			$indexable_table
		WHERE
			object_id 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',
		array_merge( array_values( $object_ids ), array_values( $newest_indexable_ids ), [ $object_type ] )
	);
	// phpcs:enable
}