/** * Commits the database to the repository and pushes if needed. * @access public * @param boolean $insert_post Whether to insert a new commit custom_post_type. */ public function commit_db($insert_post = false) { $commit_msg = escapeshellarg(__('Backed up the database with Revisr.', 'revisr')); $file = $this->upload_dir['basedir'] . '/' . $this->sql_file; $add = Revisr_Git::run("add {$file}"); $commit = Revisr_Git::run("commit -m {$commit_msg}"); if ($add === false || $commit === false) { $error = __('There was an error committing the database.', 'revisr'); $this->maybe_return($error); } //Insert the corresponding post if necessary. if ($insert_post === true) { $post = array('post_title' => $commit_msg, 'post_content' => '', 'post_type' => 'revisr_commits', 'post_status' => 'publish'); $post_id = wp_insert_post($post); $commit_hash = Revisr_Git::run('rev-parse --short HEAD'); add_post_meta($post_id, 'commit_hash', $commit_hash[0]); add_post_meta($post_id, 'db_hash', $commit_hash[0]); add_post_meta($post_id, 'branch', $this->branch); add_post_meta($post_id, 'files_changed', '0'); add_post_meta($post_id, 'committed_files', array()); } //Push changes if necessary. $this->git->auto_push(); }
/** * Displays the "Commit Details" meta box on a previous commit. * @access public */ public function view_commit_meta() { // Get details about the commit. $commit_hash = isset($_GET['commit']) ? esc_attr($_GET['commit']) : ''; $commit = Revisr_Git::get_commit_details($commit_hash); $revert_url = get_admin_url() . 'admin-post.php?action=revisr_revert_form&commit=' . $commit_hash . '&TB_iframe=true&width=350&height=200'; $time_format = __('M j, Y @ G:i'); $timestamp = sprintf(__('Committed on: <strong>%s</strong>', 'revisr'), date_i18n($time_format, $commit['time'])); if (false === $commit['status']) { $commit['status'] = __('Error', 'revisr'); $revert_btn = '<a class="button button-primary disabled" href="#">' . __('Revert to this Commit', 'revisr') . '</a>'; } else { $revert_btn = '<a class="button button-primary thickbox" href="' . $revert_url . '" title="' . __('Revert', 'revisr') . '">' . __('Revert to this Commit', 'revisr') . '</a>'; } ?> <div id="minor-publishing"> <div id="misc-publishing-actions"> <div class="misc-pub-section revisr-pub-status"> <label for="post_status"><?php _e('Status:', 'revisr'); ?> </label> <span><strong><?php echo $commit['status']; ?> </strong></span> </div> <div class="misc-pub-section revisr-pub-branch"> <label for="revisr-branch" class="revisr-octicon-label"><?php _e('Branch:', 'revisr'); ?> </label> <span><strong><?php echo $commit['branch']; ?> </strong></span> </div> <div class="misc-pub-section curtime misc-pub-curtime"> <span id="timestamp" class="revisr-timestamp"><?php echo $timestamp; ?> </span> </div> <?php if ($commit['tag'] !== '') { ?> <div class="misc-pub-section revisr-git-tag"> <label for="revisr-tag" class="revisr-octicon-label"><?php _e('Tagged:', 'revisr'); ?> </label> <span><strong><?php echo $commit['tag']; ?> </strong></span> </div> <?php } ?> </div><!-- /#misc-publishing-actions --> </div> <div id="major-publishing-actions"> <div id="delete-action"></div> <div id="publishing-action"> <span id="revisr-spinner" class="spinner"></span> <?php echo $revert_btn; ?> </div> <div class="clear"></div> </div> <?php }
/** * revert-form.php * * Displays the form to revert to a specific commit. * * @package Revisr * @license GPLv3 * @link https://revisr.io * @copyright Expanded Fronts, LLC */ // Disallow direct access. if (!defined('ABSPATH')) { exit; } $commit = Revisr_Git::get_commit_details($_GET['commit']); $styles_url = REVISR_URL . 'assets/css/thickbox.css?v=' . REVISR_VERSION; ?> <link href="<?php echo $styles_url; ?> " rel="stylesheet" type="text/css"> <form action="<?php echo get_admin_url() . 'admin-post.php'; ?> " method="post"> <div class="revisr-tb-description">
* view-commit.php * * Displays details on an existing commit. * * @package Revisr * @license GPLv3 * @link https://revisr.io * @copyright Expanded Fronts, LLC */ // Prevent direct access. if (!defined('ABSPATH')) { exit; } // Get details about the commit. $commit_hash = isset($_GET['commit']) ? esc_attr($_GET['commit']) : ''; $commit = Revisr_Git::get_commit_details($commit_hash); $subject = esc_attr($commit['subject']); ?> <div class="wrap"> <h1><?php _e('View Commit', 'revisr'); ?> <a href="<?php echo get_admin_url(); ?> admin.php?page=revisr_new_commit" class="page-title-action"><?php _e('Add New', 'revisr'); ?> </a></h1>
/** * Shows the files that were added in a given commit. * @access public */ public function committed_files_meta() { $commit = Revisr_Admin::get_commit_details(get_the_ID()); if (count($commit['committed_files']) !== 0) { foreach ($commit['committed_files'] as $file) { $output = maybe_unserialize($file); } } echo '<div id="message"></div><div id="committed_files_result">'; if (isset($output)) { printf(__('<br><strong>%s</strong> files were included in this commit. Double-click files marked as "Modified" to view the changes in a diff.', 'revisr'), $commit['files_changed']); echo '<input id="commit_hash" name="commit_hash" value="' . $commit['commit_hash'] . '" type="hidden" />'; echo '<br><br><select id="committed" multiple="multiple" size="6">'; // Display the files that were included in the commit. foreach ($output as $result) { $result = str_replace('"', '', $result); $short_status = substr($result, 0, 3); $file = substr($result, 2); $status = Revisr_Git::get_status($short_status); printf('<option class="committed" value="%s">%s [%s]</option>', $result, $file, $status); } echo '</select>'; } else { _e('No files were included in this commit.', 'revisr'); } echo '</div>'; }
/** * Custom messages for commits. * @access public * @param array $messages The messages to pass back to the commits. */ public function revisr_commits_custom_messages($messages) { $post = get_post(); $messages['revisr_commits'] = array(0 => '', 1 => __('Commit updated.', 'revisr_commits'), 2 => __('Custom field updated.', 'revisr_commits'), 3 => __('Custom field deleted.', 'revisr_commits'), 4 => __('Commit updated.', 'revisr_commits'), 5 => isset($_GET['revision']) ? sprintf(__('Commit restored to revision from %s', 'revisr_commits'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => __('Committed files on branch <strong>' . Revisr_Git::current_branch() . '</strong>.', 'revisr_commits'), 7 => __('Commit saved.', 'revisr_commits'), 8 => __('Commit submitted.', 'revisr_commits'), 9 => sprintf(__('Commit scheduled for: <strong>%1$s</strong>.', 'revisr_commits'), date_i18n(__('M j, Y @ G:i', 'revisr_commits'), strtotime($post->post_date))), 10 => __('Commit draft updated.', 'revisr_commits')); return $messages; }
/** * Stages the array of files passed through the New Commit screen. * @access public * @param array $staged_files The files to add/remove */ public function stage_files($staged_files) { $errors = array(); foreach ($staged_files as $result) { $file = substr($result, 3); $status = Revisr_Git::get_status(substr($result, 0, 2)); if ($status == __('Deleted', 'revisr')) { if ($this->run("rm {$file}") === false) { $errors[] = $file; } } else { if ($this->run("add {$file}") === false) { $errors[] = $file; } } } if (!empty($errors)) { $msg = __('There was an error staging the files. Please check the settings and try again.', 'revisr'); Revisr_Admin::alert($msg, true); Revisr_Admin::log(__('Error staging files.', 'revisr'), 'error'); } }
/** * Displays the number of committed files and the commit hash for commits. * @access public * @param string $column The column to add. */ public function custom_columns($column) { global $post; $post_id = get_the_ID(); switch ($column) { case "hash": echo Revisr_Git::get_hash($post_id); break; case "branch": $branch_meta = get_post_meta($post_id, "branch"); if (isset($branch_meta[0])) { echo $branch_meta[0]; } break; case "tag": $tag_meta = get_post_meta($post_id, "git_tag"); if (isset($tag_meta[0])) { echo $tag_meta[0]; } break; case "files_changed": $files_meta = get_post_meta($post_id, "files_changed"); if (isset($files_meta[0])) { echo $files_meta[0]; } break; } }
/** * Shows the files that were added in a given commit. * @access public */ public function committed_files() { if (get_post_type($_POST['id']) != 'revisr_commits') { exit; } check_ajax_referer('committed_nonce', 'security'); $committed_files = get_post_custom_values('committed_files', $_POST['id']); $commit_hash = get_post_custom_values('commit_hash', $_POST['id']); if (is_array($committed_files)) { foreach ($committed_files as $file) { $output = maybe_unserialize($file); } } if (isset($output)) { printf(__('<br><strong>%s</strong> files were included in this commit. Double-click files marked as "Modified" to view the changes in a diff.', 'revisr'), count($output)); echo "<input id='commit_hash' name='commit_hash' value='{$commit_hash[0]}' type='hidden' />"; echo '<br><br><select id="committed" multiple="multiple" size="6">'; foreach ($output as $result) { $short_status = substr($result, 0, 3); $file = substr($result, 2); $status = Revisr_Git::get_status($short_status); printf('<option class="committed" value="%s">%s [%s]</option>', $result, $file, $status); } echo '</select>'; } else { _e('No files were included in this commit.', 'revisr'); } exit; }
<th><?php _e('Branch', 'revisr'); ?> </th> <th class="center-td"><?php _e('Commits', 'revisr'); ?> </th> <th class="center-td"><?php _e('Actions', 'revisr'); ?> </th> </tr> </thead> <?php $git = new Revisr_Git(); $output = $git->get_branches(); if (is_array($output)) { foreach ($output as $key => $value) { $branch = substr($value, 2); $num_commits = Revisr_Admin::count_commits($branch); if (substr($value, 0, 1) === "*") { echo "<tr>\n\t\t\t\t\t\t\t\t\t<td><strong>{$branch} (current branch)</strong></td>\n\t\t\t\t\t\t\t\t\t<td class='center-td'>{$num_commits}</td>\n\t\t\t\t\t\t\t\t\t<td class='center-td'>\n\t\t\t\t\t\t\t\t\t\t<a class='button disabled branch-btn' onclick='preventDefault()' href='#'>Checkout</a>\n\t\t\t\t\t\t\t\t\t\t<a class='button disabled branch-btn' onclick='preventDefault()' href='#'>Merge</a>\n\t\t\t\t\t\t\t\t\t\t<a class='button disabled branch-btn' onclick='preventDefault()' href='#'>Delete</a>\n\t\t\t\t\t\t\t\t\t</td></tr>"; } else { $checkout_url = get_admin_url() . "admin-post.php?action=process_checkout&branch={$branch}"; $merge_url = get_admin_url() . "admin-post.php?action=merge_branch_form&branch={$branch}&TB_iframe=true&width=350&height=200"; $delete_url = get_admin_url() . "admin-post.php?action=delete_branch_form&branch={$branch}&TB_iframe=true&width=350&height=200"; $pull_remote_url = get_admin_url() . "admin-post.php?action=pull_remote_form&remote_branch={$branch}&TB_iframe=true&width=350&height=200"; ?> <tr> <td><?php
<?php /** * Displays the settings page. * * @package Revisr * @license GPLv3 * @link https://revisr.io * @copyright 2014 Expanded Fronts, LLC */ if (isset($_GET['settings-updated']) && $_GET['settings-updated'] == "true") { $git = new Revisr_Git(); $options = Revisr_Admin::options(); if (isset($options['gitignore'])) { chdir(ABSPATH); file_put_contents(".gitignore", $options['gitignore']); } if (isset($options['username']) && $options['username'] != "") { Revisr_Git::run('config user.name "' . $options['username'] . '"'); } if (isset($options['email']) && $options['email'] != "") { Revisr_Git::run('config user.email "' . $options['email'] . '"'); } if (isset($options['remote_url']) && $options['remote_url'] != "") { Revisr_Git::run('config remote.origin.url ' . $options['remote_url']); } Revisr_Git::run("add .gitignore"); $commit_msg = __('Updated .gitignore.', 'revisr'); Revisr_Git::run("commit -m \"{$commit_msg}\""); $git->auto_push(); chdir($git->dir);
/** * Reverts a table to an earlier commit. * @access private * @param string $table The table to revert. * @param string $commit The commit to revert to. * @return boolean */ private function revert_table($table, $commit) { $checkout = $this->git->run("checkout {$commit} {$this->upload_dir['basedir']}/revisr-backups/revisr_{$table}.sql"); return $checkout; }
_e('Branch', 'revisr'); ?> </th> <th class="center-td"><?php _e('Commits', 'revisr'); ?> </th> <th class="center-td"><?php _e('Actions', 'revisr'); ?> </th> </tr> </thead> <?php $admin = new Revisr_Admin(); $output = Revisr_Git::run('branch'); if (is_array($output)) { foreach ($output as $key => $value) { $branch = substr($value, 2); $num_commits = $admin->count_commits($branch); if (substr($value, 0, 1) === "*") { echo "<tr>\n\t\t\t\t\t\t\t\t\t<td><strong>{$branch} (current branch)</strong></td>\n\t\t\t\t\t\t\t\t\t<td class='center-td'>{$num_commits}</td>\n\t\t\t\t\t\t\t\t\t<td class='center-td'>\n\t\t\t\t\t\t\t\t\t\t<a class='button disabled branch-btn' onclick='preventDefault()' href='#'>Checkout</a>\n\t\t\t\t\t\t\t\t\t\t<a class='button disabled branch-btn' onclick='preventDefault()' href='#'>Delete</a>\n\t\t\t\t\t\t\t\t\t</td></tr>"; } else { $checkout_url = get_admin_url() . "admin-post.php?action=checkout&branch={$branch}"; $delete_url = get_admin_url() . "admin-post.php?action=delete_branch_form&branch={$branch}&TB_iframe=true&width=350&height=150"; ?> <tr> <td><?php echo $branch; ?> </td>
/** * Checks to see if the provided URL for a remote repository is valid. * @access public */ public function verify_remote() { //"Ping" the remote repository to verify that it exists. $ping = Revisr_Git::run("ls-remote " . $_REQUEST['remote'] . " HEAD"); if ($ping === false) { _e('Remote not found...', 'revisr'); } else { _e('Success!', 'revisr'); } exit; }
/** * Tests the get_status() method. */ function test_get_status() { $test_modified = Revisr_Git::get_status('MM'); $test_deleted = Revisr_Git::get_status('DD'); $test_added = Revisr_Git::get_status('AA'); $test_renamed = Revisr_Git::get_status('RR'); $test_untracked = Revisr_Git::get_status('??'); $test_invalid = Revisr_Git::get_status('$$'); $this->assertEquals('Modified', $test_modified); $this->assertEquals('Deleted', $test_deleted); $this->assertEquals('Added', $test_added); $this->assertEquals('Renamed', $test_renamed); $this->assertEquals('Untracked', $test_untracked); $this->assertFalse($test_invalid); }
<?php /** * Displays the main dashboard page. * * @package Revisr * @license GPLv3 * @link https://revisr.io * @copyright 2014 Expanded Fronts, LLC */ // Disallow direct access. if (!defined('ABSPATH')) { exit; } $git = new Revisr_Git(); $loader_url = REVISR_URL . 'assets/img/loader.gif'; wp_enqueue_script('revisr_dashboard'); wp_localize_script('revisr_dashboard', 'dashboard_vars', array('ajax_nonce' => wp_create_nonce('dashboard_nonce'), 'discard_msg' => __('Are you sure you want to discard your uncommitted changes?', 'revisr'), 'push_msg' => __('Are you sure you want to discard your uncommitted changes and push to the remote?', 'revisr'), 'pull_msg' => __('Are you sure you want to discard your uncommitted changes and pull from the remote?', 'revisr'))); ?> <div class="wrap"> <div id="icon-options-general" class="icon32"></div> <h2><?php _e('Revisr - Dashboard', 'revisr'); ?> </h2> <div id="revisr-alert-container"> <div id="revisr-loading-alert" class="revisr-alert updated"><p><?php _e('Loading...', 'revisr'); ?> </p></div> <div id="revisr-processing-request" class="revisr-alert updated" style="display:none;"><p><?php
public function remote_url_callback() { $check_remote = Revisr_Git::run('config --get remote.origin.url'); if (isset($this->options['remote_url']) && $this->options['remote_url'] != '') { $remote_url = esc_attr($this->options['remote_url']); } elseif ($check_remote != false) { $remote_url = $check_remote[0]; } else { $remote_url = ''; } printf('<input type="text" id="remote_url" name="revisr_remote_settings[remote_url]" value="%s" class="regular-text" placeholder="https://*****:*****@host.com/user/example.git" /><span id="verify-remote"></span> <br><span class="description">Useful if you need to authenticate over "https://" instead of SSH, or if the remote has not already been set through Git.</span>', $remote_url); }
/** * Checks to make sure that exec is enabled and Git is installed correctly on the server. * @access public */ public static function check_compatibility() { $error = ''; if (!function_exists('exec')) { $error .= __('<p><strong>WARNING:</strong> Your server does not appear to support php exec() and/or passthru(). <br> These functions are necessary for Revisr to work correctly. Contact your web host if you\'re not sure how to activate these functions.</p>', 'revisr'); return $error; } if (Revisr_Git::run('version') === false || Revisr_Git::run('status') === false) { $error .= __('<p><strong>WARNING:</strong> No Git repository detected. Revisr requires that Git be installed on the server and the parent WordPress installation be in the root directory of a Git repository.</p>', 'revisr'); return $error; } $top_level = Revisr_Git::run('rev-parse --show-toplevel'); $git_dir = $top_level[0] . '/.git/'; if (!is_writable($git_dir)) { $error .= __('<p><strong>WARNING:</strong> Revisr cannot write to the ".git/" directory.<br>Please make sure that write permissions are set for this directory. The recommended settings are 755 for directories, and 644 for files.'); return $error; } }