• Resolved igianni84

    (@igianni84)


    Hi, I can no longer duplicate sites via API. My code is the following and it has always worked for 2 years.

    The problem is about ns_cloner() that is “null object”.

    The strange thing is that even trying to duplicate the site from the wordpress panel I get an error: he necessarily wants the “URL and Key” fields although I don’t have to duplicate a remote site.

    Obviously if from wordpress I put URL and Key, the duplication works. But I think the problem with my script is due to these two parameters.

    You can help me?

    <?php
      $nome_sito = "test";
      $categoria_sito = 4;
      $descrizione_sito = "Test site";
    
      $request = array(
        'clone_mode'     => 'core',
        'source_id'      => $categoria_sito,
        'target_name'    => $nome_sito,
        'target_title'   => $descrizione_sito,
        'do_copy_posts'   => 1,
        'do_copy_files'   => 1,
        'do_copy_users'   => 1,
        '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;
    
      $cloner->exit_processes();
      // 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();
         file_put_contents($log, "$timestamp - progress inside dowile:". json_encode($progress) . PHP_EOL, FILE_APPEND);
         // Pause, so we're not constantly hammering the server with progress checks.
         sleep( 3 );
      } while ( ('complete' !== $progress['status']) && ('reported' !== $progress['status']) );
      $target_id = ns_cloner_request()->get( 'target_id' );
      // 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();
      file_put_contents($log, "$timestamp - reports:". json_encode($reports) . PHP_EOL, FILE_APPEND);
      header("Content-type: application/json");
      $progress['id_sito'] = $target_id;
    
      if($progress['id_sito'] == 0){}
        sleep( 3 );
        $progress['id_sito'] = get_blog_id_from_url($nome_sito);
      }
      echo json_encode($progress);
    ?>

    [Moderator note: Please, No bumping].

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Duplication not work via API’ is closed to new replies.