예제 #1
0
 /**
  * Run the push.
  *
  * This is where all options for SitePushCore are set, and the relevant pushes are run.
  *
  * @param SitePushCore $my_push
  * @param array $push_options options for this push from $_REQUEST
  * @return bool TRUE if push completed without errors, FALSE otherwise
  */
 public function do_the_push($my_push, $push_options)
 {
     //if we are going to do a push, check that we were referred from options page as expected
     check_admin_referer('sitepush-dopush', 'sitepush-nonce');
     //final check everything is OK
     $this->final_check($my_push);
     if (SitePushErrors::count_errors('all-errors')) {
         return FALSE;
     }
     $start_micro_time = function_exists('microtime') ? microtime(TRUE) : 0;
     $start_time = time();
     $my_push->add_result("Push started at " . date('r'), 1);
     //track if we have actually tried to push anything
     $done_push = FALSE;
     $my_push->sites_conf_path = $this->options->sites_conf;
     $my_push->dbs_conf_path = $this->options->dbs_conf;
     $my_push->source = $push_options['source'];
     $my_push->dest = $push_options['dest'];
     $my_push->dry_run = $push_options['dry_run'] ? TRUE : FALSE;
     $my_push->do_backup = $push_options['do_backup'] ? TRUE : FALSE;
     $my_push->source_backup_path = $this->options->backup_path;
     $my_push->dest_backup_path = $this->options->backup_path;
     $my_push->echo_output = TRUE;
     //initialise some parameters
     $push_files = FALSE;
     $results = array();
     //should be empty at end if everything worked as expected
     $db_types = array();
     $this->do_action('push_begin', $my_push, $push_options);
     /* -------------------------------------------------------------- */
     /* !Push WordPress Files */
     /* -------------------------------------------------------------- */
     if ($push_options['push_uploads']) {
         $push_files = TRUE;
         $my_push->push_uploads = TRUE;
     }
     if ($push_options['push_themes']) {
         $push_files = TRUE;
         $my_push->push_themes = TRUE;
     }
     if ($push_options['push_theme'] && !$push_options['push_themes']) {
         //pushes current (child) theme
         $push_files = TRUE;
         $my_push->theme = _deprecated_get_theme_stylesheet();
     }
     if ($push_options['push_plugins']) {
         $push_files = TRUE;
         $my_push->push_plugins = TRUE;
     }
     if ($push_options['push_mu_plugins']) {
         $push_files = TRUE;
         $my_push->push_mu_plugins = TRUE;
     }
     if ($push_options['push_wp_core']) {
         $push_files = TRUE;
         $my_push->push_wp_files = TRUE;
     }
     //do the push
     if ($push_files) {
         $this->do_action('pre_push_files', $my_push, $push_options);
         $results[] = $my_push->push_files();
         $this->do_action('post_push_files', $my_push, $push_options);
         $done_push = TRUE;
     }
     /* -------------------------------------------------------------- */
     /* !Push WordPress Database */
     /* -------------------------------------------------------------- */
     $db_push = FALSE;
     if ($push_options['db_all_tables']) {
         $db_types[] = 'all_tables';
         $db_push = TRUE;
     } else {
         //we only check other options if we're not pushing whole DB
         if ($push_options['db_post_content']) {
             $db_types[] = 'content';
         }
         if ($push_options['db_comments']) {
             $db_types[] = 'comments';
         }
         if ($push_options['db_users']) {
             $db_types[] = 'users';
         }
         if ($push_options['db_options']) {
             $db_types[] = 'options';
         }
         if ($push_options['db_multisite_tables']) {
             $db_types[] = 'multisite';
         }
         if ($push_options['db_custom_table_groups']) {
             $db_types[] = $push_options['db_custom_table_groups'];
         }
         if ($db_types) {
             $db_push = TRUE;
         }
     }
     $restore_options = FALSE;
     if ($db_push) {
         //save various options which we don't want overwritten if we are doing a pull
         $restore_options = $this->options->get_current_site() == $push_options['dest'];
         if ($restore_options) {
             $current_options = get_option('sitepush_options');
             $current_user_options = $this->get_all_user_options();
             $current_active_plugins = get_option('active_plugins');
             //if we don't delete the options before DB push then WP won't restore options properly if
             //option wasn't present in DB we are pulling from
             delete_option('sitepush_options');
             $this->delete_all_user_options();
         }
         //push DB
         $this->do_action('pre_push_db', $my_push, $push_options);
         $results[] = $my_push->push_db($db_types);
         $this->do_action('post_push_db', $my_push, $push_options);
         $done_push = TRUE;
     }
     /* -------------------------------------------------------------- */
     /* !Clear Cache */
     /* -------------------------------------------------------------- */
     if ($push_options['clear_cache'] && $this->options->cache_key) {
         $this->do_action('pre_clear_cache', $my_push, $push_options);
         $my_push->clear_cache();
         $this->do_action('post_clear_cache', $my_push, $push_options);
     } elseif ($push_options['clear_cache'] && !$this->options->cache_key) {
         SitePushErrors::add_error("Could not clear the destination cache because the cache secret key is not set.", 'warning');
     }
     /* -------------------------------------------------------------- */
     /* !Other things to do */
     /* -------------------------------------------------------------- */
     //normally result should be empty - results to display are captured in class and output separately
     //if anything is output here it probably means something went wrong
     //clean the array of empty elements
     $cleaned_results = array();
     foreach ($results as $result) {
         if (trim($result)) {
             $cleaned_results[] = $result;
         }
     }
     //save current site & user options back to DB so options on site we are pulling from won't overwrite
     if ($restore_options) {
         $this->options->update($current_options);
         $this->save_all_user_options($current_user_options);
         //deactivating sitepush ensures that when we update option cached value isn't used
         //we reactivate again after this if clause just to make sure it's active
         deactivate_plugins(SITEPUSH_BASENAME);
         update_option('active_plugins', $current_active_plugins);
     }
     //make sure sitepush is still activated and save our options to DB so if we have pulled DB from elsewhere we don't overwrite sitepush options
     activate_plugin(SITEPUSH_BASENAME);
     $my_push->add_result("Push completed at " . date('r'), 1);
     $duration = time() - $start_time;
     if ($duration >= 3600) {
         $time_took = gmdate('H:i:s', $duration);
     } elseif ($duration >= 60) {
         $time_took = gmdate('i:s', $duration);
     } elseif ($duration < 10 && $start_micro_time) {
         $time_took = microtime(TRUE) - $start_micro_time . " seconds";
     } else {
         $time_took = "{$duration} seconds";
     }
     $my_push->add_result("Push took {$time_took}", 1);
     $this->do_action('push_complete', $my_push, $push_options);
     return SitePushErrors::is_error() ? FALSE : $done_push;
 }