Inpsyde\MultilingualPress\SiteDuplication\SiteDuplicator::duplicate_site PHP Method

duplicate_site() public method

Duplicates a complete site to the new site just created.
Since: 3.0.0
public duplicate_site ( integer $new_site_id ) : boolean
$new_site_id integer The ID of the new site.
return boolean Whether or not a site was duplicated successfully.
    public function duplicate_site($new_site_id)
    {
        if (empty($_POST['blog']['basedon']) || 1 > $_POST['blog']['basedon']) {
            return false;
        }
        $source_site_id = (int) $_POST['blog']['basedon'];
        // Switch to the source site.
        switch_to_blog($source_site_id);
        $table_prefix = $this->db->prefix;
        $mapped_domain = $this->get_mapped_domain();
        // Switch to the new site.
        switch_to_blog($new_site_id);
        $admin_email = get_option('admin_email');
        $siteurl = get_option('siteurl');
        // Important: FIRST, duplicate the tables, and THEN overwrite things. ;)
        $this->duplicate_tables($source_site_id, $table_prefix);
        $this->set_urls($siteurl, $mapped_domain);
        $this->set_admin_email($admin_email);
        update_option('blogname', stripslashes($_POST['blog']['title']));
        $this->rename_user_roles_option($table_prefix);
        // Set the search engine visibility.
        if (isset($_POST['blog']['visibility'])) {
            update_option('blog_public', (bool) $_POST['blog']['visibility']);
        }
        $this->handle_plugins();
        $this->handle_theme();
        $this->handle_content_relations($source_site_id, $new_site_id);
        $this->attachment_copier->copy_attachments($source_site_id);
        // Switch all the way back to the original site.
        restore_current_blog();
        restore_current_blog();
        /**
         * Fires after successful site duplication.
         *
         * @since 3.0.0
         *
         * @param int $source_site_id The ID of the source site.
         * @param int $new_site_id    The ID of the new site.
         */
        do_action('multilingualpress.duplicated_site', $source_site_id, $new_site_id);
        return true;
    }