diff --git a/lib/data.php b/lib/data.php index c66019d..98be994 100644 --- a/lib/data.php +++ b/lib/data.php @@ -59,6 +59,9 @@ public static function db_copy_tables( $from_site_id, $to_site_id ) { $from_site_table = self::do_sql_query($sql_query, 'col'); } + // disable foreign key checks so order of cloning doesn't matter + self::disable_foreign_key_checks(); + foreach ($from_site_table as $table) { $table_name = $to_site_prefix . substr( $table, $from_site_prefix_length ); @@ -74,6 +77,9 @@ public static function db_copy_tables( $from_site_id, $to_site_id ) { } + // reenable foreign key checks + self::enable_foreign_key_checks(); + // apply key options from new blog. self::db_restore_data( $to_site_id, $saved_options ); @@ -373,8 +379,30 @@ public static function sql_error($sql_query, $sql_error) { echo '' . MUCD_NETWORK_PAGE_DUPLICATE_VIEW_LOG . ''; } MUCD_Functions::remove_blog(self::$to_site_id); + self::enable_foreign_key_checks(); wp_die(); } + /** + * Disable database foreign key checks + * @return void + */ + private static function disable_foreign_key_checks() { + self::$foreign_key_checks_disabled = true; + self::do_sql_query('SET FOREIGN_KEY_CHECKS=0;'); + } + + /** + * Enable database foreign key checks if it was disabled before + * @return void + */ + private static function enable_foreign_key_checks() { + if (self::$foreign_key_checks_disabled) { + // change variable before executing the query to prevent + // endless recursion loop in case of error + self::$foreign_key_checks_disabled = false; + self::do_sql_query('SET FOREIGN_KEY_CHECKS=1;'); + } + } } -} \ No newline at end of file +}