WPSEO_Upgrade::deduplicate_unindexed_indexable_rows()privateYoast 1.0

De-duplicates indexables that have more than one "unindexed" rows for the same object. Keeps the newest indexable.

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

Хуков нет.

Возвращает

null. Ничего.

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

// private - только в коде основоного (родительского) класса
$result = $this->deduplicate_unindexed_indexable_rows();

Код WPSEO_Upgrade::deduplicate_unindexed_indexable_rows() Yoast 20.0

private function deduplicate_unindexed_indexable_rows() {
	global $wpdb;

	// If migrations haven't been completed successfully the following may give false errors. So suppress them.
	$show_errors       = $wpdb->show_errors;
	$wpdb->show_errors = false;

	$indexable_table = Model::get_table_name( 'Indexable' );

	$query =
		"SELECT
			MAX(id) as newest_id,
			object_id,
			object_type
		FROM
			$indexable_table
		WHERE
			post_status = 'unindexed'
			AND object_type IN ( 'term', 'post', 'user' )
		GROUP BY
			object_id,
			object_type
		HAVING
			count(*) > 1";

	// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way.
	// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
	// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: Is it prepared already.
	$duplicates = $wpdb->get_results( $query, ARRAY_A );
	// phpcs:enable

	if ( empty( $duplicates ) ) {
		$wpdb->show_errors = $show_errors;

		return;
	}

	// Users, terms and posts may share the same object_id. So delete them in separate, more performant, queries.
	$delete_queries = [
		$this->get_indexable_deduplication_query_for_type( 'post', $duplicates, $wpdb ),
		$this->get_indexable_deduplication_query_for_type( 'term', $duplicates, $wpdb ),
		$this->get_indexable_deduplication_query_for_type( 'user', $duplicates, $wpdb ),
	];

	foreach ( $delete_queries as $delete_query ) {
		if ( ! empty( $delete_query ) ) {
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Reason: Most performant way.
			// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: No relevant caches.
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- Reason: Is it prepared already.
			$wpdb->query( $delete_query );
			// phpcs:enable
		}
	}

	$wpdb->show_errors = $show_errors;
}