Yoast\WP\SEO\Repositories

Indexable_Cleanup_Repository::cleanup_orphaned_from_table()publicYoast 1.0

Cleans orphaned rows from a yoast table.

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

Хуков нет.

Возвращает

int|true|false. The number of deleted rows, false if the query fails.

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

$Indexable_Cleanup_Repository = new Indexable_Cleanup_Repository();
$Indexable_Cleanup_Repository->cleanup_orphaned_from_table( $table, $column, $limit );
$table(строка) (обязательный)
The table to clean up.
$column(строка) (обязательный)
The table column the cleanup will rely on.
$limit(int) (обязательный)
The limit we'll apply to the queries.

Код Indexable_Cleanup_Repository::cleanup_orphaned_from_table() Yoast 23.5

public function cleanup_orphaned_from_table( $table, $column, $limit ) {
	global $wpdb;

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

	// Warning: If this query is changed, make sure to update the query in cleanup_orphaned_from_table in Premium as well.
	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Reason: There is no unescaped user input.
	$query = $wpdb->prepare(
		"
		SELECT table_to_clean.{$column}
		FROM {$table} table_to_clean
		LEFT JOIN {$indexable_table} AS indexable_table
		ON table_to_clean.{$column} = indexable_table.id
		WHERE indexable_table.id IS NULL
		AND table_to_clean.{$column} IS NOT NULL
		LIMIT %d",
		$limit
	);
	// phpcs:enable

	// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
	$orphans = $wpdb->get_col( $query );

	if ( empty( $orphans ) ) {
		return 0;
	}

	// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Reason: Already prepared.
	return $wpdb->query( "DELETE FROM $table WHERE {$column} IN( " . \implode( ',', $orphans ) . ' )' );
}