/**
  * Processes the request to revert to an earlier commit.
  * @access public
  */
 public function process_revert()
 {
     if (isset($_GET['revert_nonce']) && wp_verify_nonce($_GET['revert_nonce'], 'revert')) {
         $branch = $_GET['branch'];
         $commit = $_GET['commit_hash'];
         $commit_msg = sprintf(__('Reverted to commit: #%s.', 'revisr'), $commit);
         if ($branch != $this->git->branch) {
             $this->git->checkout($branch);
         }
         $this->git->reset('--hard', 'HEAD', true);
         $this->git->reset('--hard', $commit);
         $this->git->reset('--soft', 'HEAD@{1}');
         $this->git->run('add -A');
         $this->git->commit($commit_msg);
         $this->git->auto_push();
         $post_url = get_admin_url() . "post.php?post=" . $_GET['post_id'] . "&action=edit";
         $msg = sprintf(__('Reverted to commit <a href="%s">#%s</a>.', 'revisr'), $post_url, $commit);
         $email_msg = sprintf(__('%s was reverted to commit #%s', 'revisr'), get_bloginfo(), $commit);
         Revisr_Admin::log($msg, 'revert');
         Revisr_Admin::notify(get_bloginfo() . __(' - Commit Reverted', 'revisr'), $email_msg);
         $redirect = get_admin_url() . "admin.php?page=revisr";
         wp_redirect($redirect);
     } else {
         wp_die(__('You are not authorized to access this page.', 'revisr'));
     }
 }
    /**
     * 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'));
    }
 /**
  * Unsets unused views, replaced with branches.
  * @access public
  * @param  array $views The global views for the post type.
  */
 public function custom_views($views)
 {
     $output = $this->git->run('branch');
     global $wp_query;
     if (is_array($output)) {
         foreach ($output as $key => $value) {
             $branch = substr($value, 2);
             $class = $wp_query->query_vars['meta_value'] == $branch ? ' class="current"' : '';
             $views["{$branch}"] = sprintf(__('<a href="%s"' . $class . '>' . ucwords($branch) . ' <span class="count">(%d)</span></a>'), admin_url('edit.php?post_type=revisr_commits&branch=' . $branch), Revisr_Admin::count_commits($branch));
         }
         $class = '';
         if ($_GET['branch'] == 'all') {
             $class = ' class="current"';
         }
         $views['all'] = sprintf(__('<a href="%s"%s>All Branches <span class="count">(%d)</span></a>', 'revisr'), admin_url('edit.php?post_type=revisr_commits&branch=all'), $class, Revisr_Admin::count_commits('all'));
         unset($views['publish']);
         unset($views['draft']);
         unset($views['trash']);
         if (isset($views)) {
             return $views;
         }
     }
 }
    /**
     * Shows a list of the pending files on the current branch. Clicking a modified file shows the diff.
     * @access public
     */
    public function pending_files()
    {
        check_ajax_referer('pending_nonce', 'security');
        $output = $this->git->status();
        $total_pending = count($output);
        $text = sprintf(__('There are <strong>%s</strong> untracked files that can be added to this commit on branch <strong>%s</strong>.', 'revisr'), $total_pending, $this->git->branch);
        echo "<br>" . $text . "<br><br>";
        _e('Use the boxes below to select the files to include in this commit. Only files in the "Staged Files" section will be included.<br>Double-click files marked as "Modified" to view the changes to the file.<br><br>', 'revisr');
        echo "<input id='backup_db_cb' type='checkbox' name='backup_db'><label for='backup_db_cb'>" . __('Backup database?', 'revisr') . "</label><br><br>";
        if (is_array($output)) {
            ?>
				<!-- Staging -->
				<div class="stage-container">
					<p><strong><?php 
            _e('Staged Files', 'revisr');
            ?>
</strong></p>
					<select id='staged' multiple="multiple" name="staged_files[]" size="6">
					<?php 
            // Clean up output from git status and echo the results.
            foreach ($output as $result) {
                $short_status = substr($result, 0, 3);
                $file = substr($result, 3);
                $status = Revisr_Git::get_status($short_status);
                echo "<option class='pending' value='{$result}'>{$file} [{$status}]</option>";
            }
            ?>
					</select>
					<div class="stage-nav">
						<input id="unstage-file" type="button" class="button button-primary stage-nav-button" value="<?php 
            _e('Unstage Selected', 'revisr');
            ?>
" onclick="unstage_file()" />
						<br>
						<input id="unstage-all" type="button" class="button stage-nav-button" value="<?php 
            _e('Unstage All', 'revisr');
            ?>
" onclick="unstage_all()" />
					</div>
				</div><!-- /Staging -->		
				<br>
				<!-- Unstaging -->
				<div class="stage-container">
					<p><strong><?php 
            _e('Unstaged Files', 'revisr');
            ?>
</strong></p>
					<select id="unstaged" multiple="multiple" size="6">
					</select>
					<div class="stage-nav">
						<input id="stage-file" type="button" class="button button-primary stage-nav-button" value="<?php 
            _e('Stage Selected', 'revisr');
            ?>
" onclick="stage_file()" />
						<br>
						<input id="stage-all" type="button" class="button stage-nav-button" value="<?php 
            _e('Stage All', 'revisr');
            ?>
" onclick="stage_all()" />
					</div>
				</div><!-- /Unstaging -->
			<?php 
        }
        exit;
    }