Ejemplo n.º 1
0
 /**
  * Processes the request to pull changes into the current branch.
  * @access public
  */
 public function process_pull()
 {
     // Determine whether this is a request from the dashboard or a POST request.
     $from_dash = check_ajax_referer('dashboard_nonce', 'security', false);
     if ($from_dash == false) {
         if ($this->git->config_revisr_option('auto-pull') !== 'true') {
             wp_die(__('Cheatin’ uh?', 'revisr'));
         }
         $remote = new Revisr_Remote();
         $remote->check_token();
     }
     $this->git->reset();
     $this->git->fetch();
     $commits_since = $this->git->run("log {$this->git->branch}..{$this->git->remote}/{$this->git->branch} --pretty=oneline");
     if (is_array($commits_since)) {
         // Iterate through the commits to pull and add them to the database.
         foreach ($commits_since as $commit) {
             $commit_hash = substr($commit, 0, 7);
             $commit_msg = substr($commit, 40);
             $show_files = $this->git->run('show --pretty="format:" --name-status ' . $commit_hash);
             if (is_array($show_files)) {
                 $files_changed = array_filter($show_files);
                 $post = array('post_title' => $commit_msg, 'post_content' => '', 'post_type' => 'revisr_commits', 'post_status' => 'publish');
                 $post_id = wp_insert_post($post);
                 add_post_meta($post_id, 'commit_hash', $commit_hash);
                 add_post_meta($post_id, 'branch', $this->git->branch);
                 add_post_meta($post_id, 'files_changed', count($files_changed));
                 add_post_meta($post_id, 'committed_files', $files_changed);
                 $view_link = get_admin_url() . "post.php?post={$post_id}&action=edit";
                 $msg = sprintf(__('Pulled <a href="%s">#%s</a> from %s/%s.', 'revisr'), $view_link, $commit_hash, $this->git->remote, $this->git->branch);
                 Revisr_Admin::log($msg, 'pull');
             }
         }
     }
     if ($this->git->config_revisr_option('import-pulls') === 'true') {
         $this->db->backup();
         $undo_hash = $this->git->current_commit();
         $this->git->run("config --add revisr.last-db-backup {$undo_hash}");
     }
     // Pull the changes or return an error on failure.
     $this->git->pull();
 }
    /**
     * Displays/updates the "Reset DB" settings field.
     * @access public
     */
    public function reset_db_callback()
    {
        if (isset($_GET['settings-updated'])) {
            if (isset($this->options['reset_db'])) {
                $this->git->config_revisr_option('import-checkouts', 'true');
            } else {
                $this->git->run('config --unset-all revisr.import-checkouts');
            }
            if (isset($this->options['import_db'])) {
                $this->git->config_revisr_option('import-pulls', 'true');
            } else {
                $this->git->run('config --unset-all revisr.import-pulls');
            }
        }
        $get_reset = $this->git->run('config revisr.import-checkouts');
        $get_import = $this->git->run('config revisr.import-pulls');
        printf('<input type="checkbox" id="reset_db" name="revisr_database_settings[reset_db]" %s /><label for="reset_db">%s</label><br><br>
			<input type="checkbox" id="import_db" name="revisr_database_settings[import_db]" %s /><label for="import_db">%s</label><br><br>
			<p class="description revisr-description">%s</p>', is_array($get_reset) ? "checked" : '', __('Import database when changing branches?', 'revisr'), is_array($get_import) ? "checked" : '', __('Import database when pulling commits?', 'revisr'), __('If checked, Revisr will automatically import the above tracked tables while pulling from or checking out a branch. The tracked tables will be backed up beforehand to provide a restore point immediately prior to the import. Use this feature with caution and only after verifying that you have a full backup of your website.', 'revisr'));
    }