• Resolved troyeccles

    (@troyeccles)


    I’m using a plugin called “AR Member” with an add-on that will create a subsite when a subscriber signs up.

    Does NS Cloner have any kind of hook that will allow me to integrate with AR Member so that once a new subsite is created, NS Cloner will automatically clone an existing subsite using the /subdirectory that the subscriber requests?

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author Never Settle

    (@neversettle)

    Hi @troyeccles

    Please see our docs and I think you might find some helpful information to get you started ??

    https://support.neversettle.it/article/22-how-to-call-ns-cloner-to-copy-sites-from-other-plugins

    Thread Starter troyeccles

    (@troyeccles)

    Thank you. Was a great start but, although I can clone manually, this isn’t working for me. The process starts and the logs just kind of stop. No errors either.

    Same with debug.log.

    I’m wondering…I’m triggering the clone process FROM a subsite (a basic theme I’ve made with a button to trigger the cloning process onto that very same site (and, obviously, overwrite it). Is it a case that the cloning process has be managed from the network admin site rather than a subsite?

    thank you for your help so far ??

    Thread Starter troyeccles

    (@troyeccles)

    If it helps, I'm using (in part) this code snippet...
    
    // Check if NS Cloner is active
        if (is_plugin_active('ns-cloner-pro/ns-cloner-pro.php')) {
            // Get the current site's ID
            $current_site_id = get_current_blog_id();         // Get the details of the current site
            $current_site_details = get_blog_details($current_site_id);

            // Get the site's URL and title
            $site_url = $current_site_details->siteurl;
            $site_title = $current_site_details->blogname;

            $parsed_url = parse_url($site_url);
            $host_explode = explode('.', $parsed_url['host']);
            $subdomain = $host_explode[0]; $request = array(
                'clone_mode' => 'clone_over',
                'source_id' => 92, // Specified subdomain ID to clone
                'clone_over_target_ids' => $target_site_ids, // Target site ID(s) to clone over
                'target_name' => $subdomain, // Subdomain or subdirectory for the new site
                'target_title' => 'New Site Title', // Title for the new site
                'tables' => 'all', // Or list of tables to clone
                'post_types' => 'all', // Or list of post types to clone
                'create_new_admin' => false, // Or true if you want to create a new admin user
                'copy_users' => true, // Or false if you don't want to copy existing users
                'debug' => 1
            ); As mentioned, I'm triggering this code FROM the site I want to clone over.
    Maybe there's something horrible wrong with what I'm doing here but the process starts but then, for some reason just fails.
    Thread Starter troyeccles

    (@troyeccles)

    Actually, upon checking, it’s throwing these errors in debug.log:

    [06-Jul-2023 04:07:49 UTC] PHP Warning: count(): Parameter must be an array or an object that implements Countable in /home/xxxxxxxx/xxxxxxxx/public_html/wp-content/plugins/ns-cloner-pro/sections/class-ns-cloner-section-clone-over.php on line 81
    [06-Jul-2023 04:07:49 UTC] PHP Warning: in_array() expects parameter 2 to be array, null given in /home/xxxxxxxxx/xxxxxx/public_html/wp-content/plugins/ns-cloner-pro/sections/class-ns-cloner-section-clone-over.php on line 84

    I don’t know if that helps you?

    Plugin Author Never Settle

    (@neversettle)

    Hey @troyeccles ,

    Please check if you have the “Banned Names” setting filled in your multisite network settings {website-url}/wp-admin/network/settings.php

    Thread Starter troyeccles

    (@troyeccles)

    No, it’s clear.

    Plugin Author Never Settle

    (@neversettle)

    Please try add the default values, it should take care of the error. This can be seen in the screenshot on this page https://developer.www.ads-software.com/advanced-administration/multisite/admin/settings/ We are working on a fix to check if this field is empty

    Thread Starter troyeccles

    (@troyeccles)

    I can also clone-over manually. So it doesn’t appear to be a plugin conflict or theme issue.

    Thread Starter troyeccles

    (@troyeccles)

    Sorry, I’m an idiot and was looking at the ‘banned email domains’ which was empty. I have the default ‘banned names’ in already.

    Thread Starter troyeccles

    (@troyeccles)

    Is the “Clone Over” functionality only available from the Network Admin interface? Because I have a plugin (code snippet above) that runs from a subsite. Could that be the issue here?

    Plugin Author Never Settle

    (@neversettle)

    Thanks for confirming. We will work on better support for PHP 8.x as it seems that the errors are due to certain variables not set or not eligible for certain checks

    Thread Starter troyeccles

    (@troyeccles)

    I’m using 7.4

    Plugin Author Never Settle

    (@neversettle)

    Do you mind sharing the full code you are using. Here is a sample snippet of code for a custom clone

    <?php
    $array_of_post_types = array(
    	'attachment',
    	'page',
    	'post',
    	'revision',
    	'wp_global_styles',
    );
    $array_of_tables = array(
    	'wp_commentmeta',
    	'wp_comments',
    	'wp_links',
    	'wp_options',
    	'wp_postmeta',
    	'wp_posts',
    	'wp_term_relationships',
    	'wp_term_taxonomy',
    	'wp_termmeta',
    	'wp_terms',
    );
    $request         = array(
    	'clone_mode'          => 'core',
    	'source_id'           => 1, // any blog/site id on network.
    	'target_name'         => 'test1',
    	'target_title'        => 'test1',
    	'tables_to_clone'     => $array_of_tables,
    	'debug'               => 1,
    	'do_copy_posts'       => 1,
    	'post_types_to_clone' => $array_of_post_types, // can customize post types . . .
    	'do_copy_files'       => 1,
    	// 'do_copy_users' => 0,
    	'flush_cache'         => 1,
    	'clone_nonce'         => wp_create_nonce( 'ns_cloner' ),
    );
    
    // This is required to bootstrap the required plugin classes.
    ns_cloner()->init();
    
    foreach ( $request as $key => $value ) {
    	ns_cloner_request()->set( $key, $value );
    }
    ns_cloner_request()->set_up_vars();
    ns_cloner_request()->save();
    // Run init to begin.
    ns_cloner()->process_manager->init();
    Thread Starter troyeccles

    (@troyeccles)

    // Enqueue the script
    function enqueue_ajax_script() {
        wp_enqueue_script('my-ajax-script', plugin_dir_url(__FILE__) . 'js/my-ajax-script.js', array('jquery'), '1.0', true);
    
        // Make the AJAX URL available to the JS script
        wp_localize_script('my-ajax-script', 'my_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
    }
    add_action('wp_enqueue_scripts', 'enqueue_ajax_script');
    
    // Handle the Ajax request on the server side
    function handle_my_clone_action() {
        // Include necessary WordPress files
        if (!function_exists('is_plugin_active')) {
            require_once(ABSPATH . '/wp-admin/includes/plugin.php');
        }
    
        // Check if NS Cloner is active
        if (is_plugin_active('ns-cloner-pro/ns-cloner-pro.php')) {
            // Get the current site's ID
            $current_site_id = get_current_blog_id();
    
            // Get the details of the current site
            $current_site_details = get_blog_details($current_site_id);
            
            // Get the site's URL and title
            $site_url = $current_site_details->siteurl;
            $current_site_title = get_bloginfo('name');
            
            $parsed_url = parse_url($site_url);
            $host_explode = explode('.', $parsed_url['host']);
            $subdomain = $host_explode[0];
    
    $request = array(
        'clone_mode' => 'clone_over',
        'source_id' => 92, // Specified subdomain ID to clone
            'target_name' => 'nstest', // Changed to a fixed target
        'target_title' => 'NSTest Site', // Changed to a fixed title
        'do_copy_posts' => 1, // or list of post types to clone
        'do_copy_files' => 1, // or list of files to clone
        'do_copy_users' => 1, // or 0 if you don't want to copy existing users
        'debug' => 1
    );
    
    
    
            // Trigger the cloning process using NS Cloner
            ns_cloner()->schedule->add($request, time(), 'Trigger for NS Cloner');
            
            // Send a success JSON response
            wp_send_json_success('Cloning process started.');
        } else {
            wp_send_json_error('NS Cloner is not active or not installed.');
        }
    }
    
    add_action('wp_ajax_my_clone_action', 'handle_my_clone_action', 20); // If the user is logged in
    add_action('wp_ajax_nopriv_my_clone_action', 'handle_my_clone_action', 20); // If the user is not logged in

    [ Please do not bump. ]

    Plugin Author Never Settle

    (@neversettle)

    Hey @troyeccles

    For clone over you will need to set clone_over_target_ids parameter, which should be an array of ids of sites to clone over

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Hook to clone a site automatically?’ is closed to new replies.