Exemplo n.º 1
0
 /**
  * Copy tables from a site to another
  * @since 0.2.0
  * @param  int $from_site_id duplicated site id
  * @param  int $to_site_id   new site id
  */
 public static function db_copy_tables($from_site_id, $to_site_id)
 {
     global $wpdb;
     // Source Site information
     $from_site_prefix = $wpdb->get_blog_prefix($from_site_id);
     // prefix
     $from_site_prefix_length = strlen($from_site_prefix);
     // prefix length
     // Destination Site information
     $to_site_prefix = $wpdb->get_blog_prefix($to_site_id);
     // prefix
     $to_site_prefix_length = strlen($to_site_prefix);
     // prefix length
     // Options that should be preserved in the new blog.
     $saved_options = MUCD_Option::get_saved_option();
     foreach ($saved_options as $option_name => $option_value) {
         $saved_options[$option_name] = get_blog_option($to_site_id, $option_name);
     }
     // Bugfix : escape '_' , '%' and '/' character for mysql 'like' queries
     $from_site_prefix_like = $wpdb->esc_like($from_site_prefix);
     // SCHEMA - TO FIX for HyperDB
     // XTEC ************ MODIFICAT - Add support for HyperDB
     // 2015.10.28 @dgras
     $sql_query = $wpdb->prepare('SELECT DISTINCT TABLE_SCHEMA FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE \'%s\'', $from_site_prefix_like . "%");
     $schema = MUCD_Data::do_sql_query($sql_query, 'var');
     //************ ORIGINAL
     /*
     $schema = DB_NAME;
     */
     //************ FI
     // Get sources Tables
     if ($from_site_id == MUCD_PRIMARY_SITE_ID) {
         $from_site_table = MUCD_Data::get_primary_tables($from_site_prefix);
     } else {
         $sql_query = $wpdb->prepare('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = \'%s\' AND TABLE_NAME LIKE \'%s\'', $schema, $from_site_prefix_like . '%');
         $from_site_table = MUCD_Data::do_sql_query($sql_query, 'col');
     }
     foreach ($from_site_table as $table) {
         $table_name = $to_site_prefix . substr($table, $from_site_prefix_length);
         // Drop table if exists
         MUCD_Data::do_sql_query('DROP TABLE IF EXISTS `' . $table_name . '`');
         // Create new table from source table
         MUCD_Data::do_sql_query('CREATE TABLE IF NOT EXISTS `' . $table_name . '` LIKE `' . $schema . '`.`' . $table . '`');
         // Populate database with data from source table
         MUCD_Data::do_sql_query('INSERT `' . $table_name . '` SELECT * FROM `' . $schema . '`.`' . $table . '`');
     }
     // apply key options from new blog.
     switch_to_blog($to_site_id);
     foreach ($saved_options as $option_name => $option_value) {
         update_option($option_name, $option_value);
     }
     restore_current_blog();
 }