How to clone via API
-
Hi, it’s possible to clone a site via API or PHP code? I want create an automation to duplicate site without to login in wp-admin.
Thanks
-
Hi @igianni84
You bet! https://neversettle.it/documentation/ns-cloner/call-ns-cloner-copy-sites-plugins/
Cheers,
StephenThanks!
Hi, I’ve done some tests and it doesn’t seem to work. I used the following code, but it gives me a generic error (wordpress has stopped working). In the log file I read:
[20-Oct-2020 08:02:26 UTC] PHP Fatal error: Uncaught Exception: Access denied. in /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/settings/page/manager.php:105 Stack trace: #0 /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/base/document.php(1214): Elementor\Core\Settings\Page\Manager->ajax_before_save_settings(Array, 2037) #1 /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/base/document.php(609): Elementor\Core\Base\Document->save_settings(Array) #2 /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/kits/manager.php(186): Elementor\Core\Base\Document->update_settings(Array) #3 /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/kits/manager.php(274): Elementor\Core\Kits\Manager->update_kit_settings_based_on_option('site_name', 'desc') #4 /var/www/vhosts/negozioperfetto.com/httpdocs/wp-includes/class-wp-hook.php(289): Elementor\Core\Kits\Manager->Elementor\Core\Kits\{closure}('Il mio negozio ...', 'desc') # in /var/www/vhosts/negozioperfetto.com/httpdocs/wp-content/plugins/elementor/core/settings/page/manager.php on line 105 [20-Oct-2020 08:04:42 UTC] PHP Notice: Undefined variable: pm in /var/www/vhosts/negozioperfetto.com/httpdocs/cloner.php on line 53 [20-Oct-2020 08:04:42 UTC] PHP Fatal error: Uncaught Error: Call to a member function maybe_finish() on null in /var/www/vhosts/negozioperfetto.com/httpdocs/cloner.php:53 Stack trace: #0 {main} thrown in /var/www/vhosts/negozioperfetto.com/httpdocs/cloner.php on line 53
<?php require_once('wp-load.php'); $nome_sito = "nome1"; $email_cliente = "[email protected]"; $categoria_sito = 9; //l'id del sito da copiare $descrizione_sito = "desc"; //il title del sito $request = array( 'clone_mode' => 'core', 'source_id' => $categoria_sito, 'target_name' => $nome_sito, 'target_title' => $descrizione_sito, 'debug' => 1 // optional: enables logs ); // Register request with the cloner. foreach ( $request as $key => $value ) { ns_cloner_request()->set( $key, $value ); } // Get the cloner process object. $cloner = ns_cloner()->process_manager; // Begin cloning. $cloner->init(); // Check for errors (from invalid params, or already running process). $errors = $cloner->get_errors(); if ( ! empty( $errors ) ) { // Handle error(s) and exit } // Last you'll need to poll for completion to run the cleanup process // when content is done cloning. Could be via AJAX to avoid timeout, or like: do { // Attempt to run finish, if content is complete. $pm->maybe_finish(); $progress = $pm->get_progress(); // Pause, so we're not constantly hammering the server with progress checks. sleep( 5 ); } while ( 'reported' !== $progress['status'] ); // Once you've verified that $progress['status'] is 'reported', // you can get access the array of report data (whether successful or failed) via: $reports = ns_cloner()->report->get_all_reports(); print_r($reports);
Can you help me?
Sorry, that’s a typo in our example code. The variable
$pm
should be replaced with$cloner
(inside the do loop).Thanks! I have another question: where is a complete documentation about API? I need to know how do I do assign an user to a website during the cloning… how do I delete a site, etc…
P.S. I bought the PRO version ??
Thanks
Thanks for your purchase!
We don’t currently have very extensive documentation of the API; what we do have is here:https://neversettle.it/documentation/ns-cloner/ However, the plugin is also documented inline with pretty well with comments, and offers a number of different fairly self explanatory actions and filters.
Okay, fine thanks. Could you just tell me how I can also associate a user in the site duplication API? What value should I add inside the request array?
$request = array( 'clone_mode' => 'core', 'source_id' => $categoria_sito, 'target_name' => $nome_sito, 'target_title' => $descrizione_sito, 'debug' => 1 // optional: enables logs );
That’s not supported via the request array, but you can use the native WP function add_user_to_blog() – https://developer.www.ads-software.com/reference/functions/add_user_to_blog/
You could run it on the
ns_cloner_process_finish
action, and use ns_cloner_request()->get( ‘target_id’ ) to get the target blog id inside your custom action hook (for calling add_user_to_blog()).Hi, unfortunately I can’t duplicate sites via api. My code is as follows:
<?php /* Come categoria sito usare uno dei seguenti codici: 9 => Sito abbigliamento 2 => Sito casalinghi 3 => Sito elettronica 4 => Altro */ require_once('wp-load.php'); /* $nome_sito = $_POST['nome_sito']; $email_cliente = $_POST['email_cliente']; $categoria_sito = $_POST['categoria_sito']; //l'id del sito da copiare $descrizione_sito = $_POST['descrizione_sito']; //il title del sito */ $request = array( 'clone_mode' => 'core', 'source_id' => 9,//$categoria_sito, 'target_name' => "ciao2",//$nome_sito, 'target_title' => "questa è una prova",//$descrizione_sito, 'debug' => 1 // optional: enables logs ); // Register request with the cloner. foreach ( $request as $key => $value ) { ns_cloner_request()->set( $key, $value ); } // Get the cloner process object. $cloner = ns_cloner()->process_manager; // Begin cloning. $cloner->init(); // Check for errors (from invalid params, or already running process). $errors = $cloner->get_errors(); if ( ! empty( $errors ) ) { // Handle error(s) and exit } // Last you'll need to poll for completion to run the cleanup process // when content is done cloning. Could be via AJAX to avoid timeout, or like: do { // Attempt to run finish, if content is complete. $cloner->maybe_finish(); $progress = $cloner->get_progress(); // Pause, so we're not constantly hammering the server with progress checks. sleep( 3 ); } while ( 'reported' !== $progress['status'] ); // Once you've verified that $progress['status'] is 'reported', // you can get access the array of report data (whether successful or failed) via: $reports = ns_cloner()->report->get_all_reports(); print_r($reports);
If I run this script (you can check by going to https://negozioperfetto.com/cloner.php), it times out after a few minutes.
Why?
Unfortunately while we fully support all standard usage of the plugin itself via wp-admin, we are not able to provide in-depth support for custom code built around the Cloner. The documentation we provide on calling the Cloner by code is a courtesy to get you started, but it’s up to individual implementors to take it from there.
Two points that we can offer:
- As mentioned in the docs, the synchronous method using
sleep
is problematic for large sites or servers that don’t have large memory and timeout limits. It’s really meant more for testing and not suitable for production applications. - It could definitely be a security issue running programmatically from a public URL with no authentication like that. I assume your script is just for testing but especially since it’s posted on a public forum it would be a good idea to wrap that in some type of protection from unauthorized use.
Thanks for the reply. I understand you … you cannot give assistance on custom codes. But I’m trying to use your own example code, but it doesn’t work.
Obviously this file of mine is just an example that I will delete as soon as I can get it working.
@igianni84 The issue is with the new version of Elementor that supports Global CSS and New theme system. ver > 3
If you read the code it says the user must be logged in to save the updated options.
So simply add the:
require_once(‘wp-load.php’);
wp_set_current_user ( 1 );where 1 is the id of the super network admin.
hope this helps
I am sorry this did not worked as it introduced new issues with the cloning process.
I end up commenting the Elementor line on 105.
//if ( ! current_user_can( ‘edit_post’, $id ) ) {
// throw new \Exception( ‘Access denied.’, Exceptions::FORBIDDEN );
//}I know this is not a solution but I really needed the cloning process via API.
- As mentioned in the docs, the synchronous method using
- The topic ‘How to clone via API’ is closed to new replies.