/** * Runs a WPDB query * @since 0.2.0 * @param string $sql_query the query * @param string $type type of result * @param boolean $log log the query, or not * @return $results of the query */ public static function do_sql_query($sql_query, $type = '', $log = TRUE) { global $wpdb; $wpdb->hide_errors(); switch ($type) { case 'col': $results = $wpdb->get_col($sql_query); break; case 'row': $results = $wpdb->get_row($sql_query); break; case 'var': $results = $wpdb->get_var($sql_query); break; case 'results': $results = $wpdb->get_results($sql_query, ARRAY_A); break; default: $results = $wpdb->query($sql_query); break; } if ($log) { MUCD_Duplicate::write_log('SQL :' . $sql_query); MUCD_Duplicate::write_log('Result :' . var_export($results, true)); } if ($wpdb->last_error != "") { MUCD_Data::sql_error($sql_query, $wpdb->last_error); } return $results; }
/** * Main function of the plugin : duplicates a site * @since 0.2.0 * @param array $data parameters from form * @return $form_message result messages of the process */ public static function duplicate_site($data) { global $wpdb; $form_message = array(); $wpdb->hide_errors(); self::init_log($data); $email = $data['email']; $domain = $data['domain']; $newdomain = $data['newdomain']; $path = $data['path']; $title = $data['title']; $from_site_id = $data['from_site_id']; $keep_users = $data['keep_users']; $copy_file = $data['copy_files']; $public = $data['public']; $network_id = $data['network_id']; MUCD_Duplicate::write_log('Start site duplication : from site ' . $from_site_id); MUCD_Duplicate::write_log('Admin email : ' . $email); MUCD_Duplicate::write_log('Domain : ' . $newdomain); MUCD_Duplicate::write_log('Path : ' . $path); MUCD_Duplicate::write_log('Site title : ' . $title); $user_id = MUCD_Duplicate::create_admin($email, $domain); if (is_wp_error($user_id)) { wp_cache_flush(); $form_message['error'] = $user_id->get_error_message(); return $form_message; } // Create new site $to_site_id = wpmu_create_blog($newdomain, $path, $title, $user_id, array('public' => $public), $network_id); $wpdb->show_errors(); if (is_wp_error($to_site_id)) { wp_cache_flush(); $form_message['error'] = $to_site_id->get_error_message(); return $form_message; } // User rights adjustments if (!is_super_admin($user_id) && !get_user_option('primary_blog', $user_id)) { update_user_option($user_id, 'primary_blog', $to_site_id, true); } MUCD_Duplicate::bypass_server_limit(); // Copy Site - File if ($copy_file == 'yes') { do_action('mucd_before_copy_files', $from_site_id, $to_site_id); $result = MUCD_Files::copy_files($from_site_id, $to_site_id); do_action('mucd_after_copy_files', $from_site_id, $to_site_id); } // Copy Site - Data do_action('mucd_before_copy_data', $from_site_id, $to_site_id); $result = MUCD_Data::copy_data($from_site_id, $to_site_id); do_action('mucd_after_copy_data', $from_site_id, $to_site_id); // Copy Site - Users if ($keep_users == 'yes') { do_action('mucd_before_copy_users', $from_site_id, $to_site_id); $result = MUCD_Duplicate::copy_users($from_site_id, $to_site_id); do_action('mucd_after_copy_users', $from_site_id, $to_site_id); } update_blog_option($to_site_id, 'mucd_duplicable', "no"); $form_message['msg'] = MUCD_NETWORK_PAGE_DUPLICATE_NOTICE_CREATED; $form_message['site_id'] = $to_site_id; MUCD_Duplicate::write_log('End site duplication : new site ID = ' . $to_site_id); wp_cache_flush(); return $form_message; }