/** * Print result message box error / updated * @since 0.2.0 * @param array $form_message messages to print */ public static function result_message($form_message) { if (isset($form_message['error'])) { echo '<div id="message" class="error">'; echo ' <p>' . $form_message['error'] . '</p>'; echo '</div>'; } else { echo '<div id="message" class="updated">'; echo ' <p>'; echo ' <strong>' . $form_message['msg'] . ' : ' . '</strong>'; switch_to_blog($form_message['site_id']); $user = get_current_user_id(); echo ' <a href="' . get_dashboard_url($user) . '">' . MUCD_NETWORK_PAGE_DUPLICATE_DASHBOARD . '</a> - '; echo ' <a href="' . get_site_url() . '">' . MUCD_NETWORK_PAGE_DUPLICATE_VISIT . '</a> - '; echo ' <a href="' . admin_url('customize.php') . '">' . MUCD_NETWORK_CUSTOMIZE . '</a>'; if ($log_url = MUCD_Duplicate::log_url()) { echo ' - <a href="' . $log_url . '">' . MUCD_NETWORK_PAGE_DUPLICATE_VIEW_LOG . '</a>'; } restore_current_blog(); echo ' </p>'; echo '</div>'; } }
/** * Stop process on SQL Error, print and log error, removes the new blog * @since 0.2.0 * @param string $sql_query the query * @param string $sql_error the error */ public static function sql_error($sql_query, $sql_error) { $error_1 = 'ERROR SQL ON : ' . $sql_query; MUCD_Duplicate::write_log($error_1); $error_2 = 'WPDB ERROR : ' . $sql_error; MUCD_Duplicate::write_log($error_2); MUCD_Duplicate::write_log('Duplication interrupted on SQL ERROR'); echo '<br />Duplication failed :<br /><br />' . $error_1 . '<br /><br />' . $error_2 . '<br /><br />'; if ($log_url = MUCD_Duplicate::log_url()) { echo '<a href="' . $log_url . '">' . MUCD_NETWORK_PAGE_DUPLICATE_VIEW_LOG . '</a>'; } MUCD_Functions::remove_blog(self::$to_site_id); wp_die(); }
/** * Init log object * @since 0.2.0 * @param array $data data from FORM */ public static function init_log($data) { // INIT LOG AND SAVE OPTION if (isset($data['log']) && $data['log'] == 'yes') { if (isset($data['log-path']) && !empty($data['log-path'])) { $log_name = @date('Y_m_d_His') . '-' . $data['domain'] . '.log'; if (substr($data['log-path'], -1) != "/") { $data['log-path'] = $data['log-path'] . '/'; } MUCD_Duplicate::$log = new MUCD_Log(true, $data['log-path'], $log_name); } } else { MUCD_Duplicate::$log = new MUCD_Log(false); } }
/** * Duplicate a site in a multisite install. * * ## OPTIONS * * --slug=<slug> * : Path for the new site. Subdomain on subdomain installs, directory on subdirectory installs. * * --source=<site_id> * : ID of the site to duplicate. * * [--title=<title>] * : Title of the new site. Default: prettified slug. * * [--email=<email>] * : Email for Admin user. User will be created if none exists. Assignement to Super Admin if not included. * * [--network_id=<network-id>] * : Network to associate new site with. Defaults to current network (typically 1). * * [--private] * : If set, the new site will be non-public (not indexed) * * [--porcelain] * : If set, only the site id will be output on success. * * [--v] * : If set, print more details about the new site (Verbose mode). Do not work if --procelain is set. * * [--do_not_copy_files] * : If set, files of the duplicated site will not be copied. * * [--keep_users] * : If set, the new site will have the same users as the duplicated site. * * [--log=<dir_path>] * : If set, a log will be written in this directory (please check this directory is writable). * * @alias clone * * @synopsis --slug=<slug> --source=<site_id> [--title=<title>] [--email=<email>] [--network_id=<network-id>] [--private] [--porcelain] [--v] [--do_not_copy_files] [--keep_users] [--log=<dir_path>] */ public function __invoke($_, $assoc_args) { if (!is_multisite()) { WP_CLI::error('This is not a multisite install.'); } global $wpdb, $current_site; $base = $assoc_args['slug']; $title = isset($assoc_args['title']) ? $assoc_args['title'] : ucfirst($base); $email = empty($assoc_args['email']) ? '' : $assoc_args['email']; // Network if (!empty($assoc_args['network_id'])) { $network = MUCD_Functions::get_network($assoc_args['network_id']); if ($network === false) { WP_CLI::error(sprintf('Network with id %d does not exist.', $assoc_args['network_id'])); } } else { $network = $current_site; } $network_id = $network->id; $public = !isset($assoc_args['private']); // Sanitize if (preg_match('|^([a-zA-Z0-9-])+$|', $base)) { $base = strtolower($base); } // If not a subdomain install, make sure the domain isn't a reserved word if (!is_subdomain_install()) { $subdirectory_reserved_names = apply_filters('subdirectory_reserved_names', array('page', 'comments', 'blog', 'files', 'feed')); if (in_array($base, $subdirectory_reserved_names)) { WP_CLI::error('The following words are reserved and cannot be used as blog names: ' . implode(', ', $subdirectory_reserved_names)); } } // Check for valid email, if not, use the first Super Admin found // Probably a more efficient way to do this so we dont query for the // User twice if super admin $email = sanitize_email($email); if (empty($email) || !is_email($email)) { $super_admins = get_super_admins(); $email = ''; if (!empty($super_admins) && is_array($super_admins)) { // Just get the first one $super_login = $super_admins[0]; $super_user = get_user_by('login', $super_login); if ($super_user) { $email = $super_user->user_email; } } } if (is_subdomain_install()) { $newdomain = $base . '.' . preg_replace('|^www\\.|', '', $network->domain); $path = $network->path; } else { $newdomain = $network->domain; $path = $network->path . $base . '/'; } // Source ? $source = $assoc_args['source']; if (!intval($source) != 0) { WP_CLI::error($source . ' is not a valid site ID.'); } if (!MUCD_Functions::site_exists($source)) { WP_CLI::error('There is no site with ID=' . $source . '. The site to duplicate must be an existing site of the network.'); } // Copy files ? $copy_files = isset($assoc_args['do_not_copy_files']) ? 'no' : 'yes'; // Keep users ? $keep_users = isset($assoc_args['keep_users']) ? 'yes' : 'no'; // Write log if (isset($assoc_args['log'])) { $log = 'yes'; $log_path = $assoc_args['log']; } else { $log = 'no'; $log_path = ''; } $data = array('source' => $source, 'domain' => $base, 'title' => $title, 'email' => $email, 'copy_files' => $copy_files, 'keep_users' => $keep_users, 'log' => $log, 'log-path' => $log_path, 'from_site_id' => $source, 'newdomain' => $newdomain, 'path' => $path, 'public' => $public, 'network_id' => $network_id); $wpdb->hide_errors(); $form_message = MUCD_Duplicate::duplicate_site($data); $wpdb->show_errors(); if (isset($form_message['error'])) { WP_CLI::error($form_message['error']); } else { if (isset($assoc_args['porcelain'])) { WP_CLI::line($form_message['site_id']); } else { switch_to_blog($form_message['site_id']); if (!isset($assoc_args['v'])) { WP_CLI::success('Site ' . $form_message['site_id'] . ' created: ' . get_site_url()); } else { // Verbose mode WP_CLI::success($form_message['msg']); $user = get_current_user_id(); WP_CLI::line("ID : " . $form_message['site_id']); WP_CLI::line("Title : " . get_bloginfo('name')); WP_CLI::line("Front : " . get_site_url()); WP_CLI::line("Dashboard : " . admin_url()); WP_CLI::line("Customize : " . admin_url('customize.php')); } restore_current_blog(); } } }
<div class="wrap"> <h2 id="duplicate-site"><?php echo MUCD_NETWORK_PAGE_DUPLICATE_TITLE; ?> </h2> <?php if (MUCD_Duplicate::log_error()) { MUCD_Admin::log_error_message(); } if (isset($form_message)) { MUCD_Admin::result_message($form_message); } ?> <form method="post" action="<?php echo network_admin_url('sites.php?page=' . MUCD_SLUG_NETWORK_ACTION . '&action=' . MUCD_SLUG_ACTION_DUPLICATE); ?> "> <?php wp_nonce_field(MUCD_DOMAIN); ?> <table class="form-table"> <tr class="form-required"> <th scope='row'><?php echo MUCD_NETWORK_PAGE_DUPLICATE_FIELD_SOURCE; ?> </th> <td> <?php
/** * Stop process on Creating dir Error, print and log error, removes the new blog * @since 0.2.0 * @param string $dir_path the path */ public static function mkdir_error($dir_path) { $error_1 = 'ERROR DURING FILE COPY : CANNOT CREATE ' . $dir_path; MUCD_Duplicate::write_log($error_1); $error_2 = sprintf(MUCD_NETWORK_PAGE_DUPLICATE_COPY_FILE_ERROR, MUCD_Functions::get_primary_upload_dir()); MUCD_Duplicate::write_log($error_2); MUCD_Duplicate::write_log('Duplication interrupted on FILE COPY ERROR'); echo '<br />Duplication failed :<br /><br />' . $error_1 . '<br /><br />' . $error_2 . '<br /><br />'; if ($log_url = MUCD_Duplicate::log_url()) { echo '<a href="' . $log_url . '">' . MUCD_NETWORK_PAGE_DUPLICATE_VIEW_LOG . '</a>'; } MUCD_Functions::remove_blog(self::$to_site_id); wp_die(); }