/** * Removes completely a blog from the network * @since 0.2.0 * @param int $blog_id the blog id */ public static function remove_blog($blog_id) { switch_to_blog($blog_id); $wp_upload_info = wp_upload_dir(); $dir = str_replace(' ', "\\ ", trailingslashit($wp_upload_info['basedir'])); restore_current_blog(); wpmu_delete_blog($blog_id, true); // wpmu_delete_blog leaves an empty site upload directory, that we want to remove : MUCD_Files::rrmdir($dir); }
/** * Copy files from one directory to another * @since 0.2.0 * @param string $src source directory path * @param string $dst destination directory path * @param array $exclude_dirs directories to ignore */ public static function recurse_copy($src, $dst, $exclude_dirs = array()) { $dir = opendir($src); @mkdir($dst); while (false !== ($file = readdir($dir))) { if ($file != '.' && $file != '..') { if (is_dir($src . '/' . $file)) { if (!in_array($file, $exclude_dirs)) { MUCD_Files::recurse_copy($src . '/' . $file, $dst . '/' . $file); } } else { copy($src . '/' . $file, $dst . '/' . $file); } } } closedir($dir); }
/** * 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; }
/** * Initialize file before writing * @since 0.2.0 * @return boolean True on success, False on failure */ private function init_file() { if (MUCD_Files::init_dir($this->log_dir_path) !== false) { if (!($this->fp = @fopen($this->log_file_path, 'a'))) { return false; } chmod($this->log_file_path, 0777); return true; } return false; }