WPSEO_Plugin_Importer::meta_key_clone
Helper function to clone meta keys and (optionally) change their values in bulk.
Метод класса: WPSEO_Plugin_Importer{}
Хуков нет.
Возвращает
true|false. Clone status.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->meta_key_clone( $old_key, $new_key, $replace_values );
- $old_key(строка) (обязательный)
- The existing meta key.
- $new_key(строка) (обязательный)
- The new meta key.
- $replace_values(массив)
- An array, keys old value, values new values.
По умолчанию:[]
Код WPSEO_Plugin_Importer::meta_key_clone() WPSEO Plugin Importer::meta key clone Yoast 27.6
protected function meta_key_clone( $old_key, $new_key, $replace_values = [] ) {
global $wpdb;
// First we create a temp table with all the values for meta_key.
$result = $wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- This is intentional + temporary.
"CREATE TEMPORARY TABLE tmp_meta_table SELECT * FROM {$wpdb->postmeta} WHERE meta_key = %s",
$old_key,
),
);
if ( $result === false ) {
$this->set_missing_db_rights_status();
return false;
}
// Delete all the values in our temp table for posts that already have data for $new_key.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM tmp_meta_table WHERE post_id IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s )",
WPSEO_Meta::$meta_prefix . $new_key,
),
);
/*
* We set meta_id to NULL so on re-insert into the postmeta table, MYSQL can set
* new meta_id's and we don't get duplicates.
*/
$wpdb->query( 'UPDATE tmp_meta_table SET meta_id = NULL' );
// Now we rename the meta_key.
$wpdb->query(
$wpdb->prepare(
'UPDATE tmp_meta_table SET meta_key = %s',
WPSEO_Meta::$meta_prefix . $new_key,
),
);
$this->meta_key_clone_replace( $replace_values );
// With everything done, we insert all our newly cloned lines into the postmeta table.
$wpdb->query( "INSERT INTO {$wpdb->postmeta} SELECT * FROM tmp_meta_table" );
// Now we drop our temporary table.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- This is intentional + a temporary table.
$wpdb->query( 'DROP TEMPORARY TABLE IF EXISTS tmp_meta_table' );
return true;
}