* * Gitium is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * Gitium is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Gitium. If not, see . * * @package Gitium */ class Gitium_Submenu_Status extends Gitium_Menu { public function __construct() { parent::__construct( $this->gitium_menu_slug, $this->gitium_menu_slug ); if ( current_user_can( GITIUM_MANAGE_OPTIONS_CAPABILITY ) ) { add_action( GITIUM_ADMIN_MENU_ACTION, array( $this, 'admin_menu' ) ); add_action( 'admin_init', array( $this, 'save_changes' ) ); add_action( 'admin_init', array( $this, 'save_ignorelist' ) ); add_action( 'admin_init', array( $this, 'disconnect_repository' ) ); } } public function admin_menu() { add_menu_page( __( 'Git Status', 'gitium' ), 'Gitium', GITIUM_MANAGE_OPTIONS_CAPABILITY, $this->menu_slug, array( $this, 'page' ), plugins_url( 'img/gitium.png', dirname( __FILE__ ) ) ); $submenu_hook = add_submenu_page( $this->menu_slug, __( 'Git Status', 'gitium' ), __( 'Status', 'gitium' ), GITIUM_MANAGE_OPTIONS_CAPABILITY, $this->menu_slug, array( $this, 'page' ) ); new Gitium_Help( $submenu_hook, 'status' ); } private function get_change_meanings() { return array( '??' => __( 'untracked', 'gitium' ), 'rM' => __( 'modified on remote', 'gitium' ), 'rA' => __( 'added to remote', 'gitium' ), 'rD' => __( 'deleted from remote', 'gitium' ), 'D' => __( 'deleted from work tree', 'gitium' ), 'M' => __( 'updated in work tree', 'gitium' ), 'A' => __( 'added to work tree', 'gitium' ), 'AM' => __( 'added to work tree', 'gitium' ), 'R' => __( 'deleted from work tree', 'gitium' ), ); } public function humanized_change( $change ) { $meaning = $this->get_change_meanings(); if ( isset( $meaning[ $change ] ) ) { return $meaning[ $change ]; } if ( 0 === strpos( $change, 'R ' ) ) { $old_filename = substr( $change, 2 ); $change = sprintf( __( 'renamed from `%s`', 'gitium' ), $old_filename ); } return $change; } public function save_ignorelist() { $gitium_ignore_path = filter_input(INPUT_POST, 'GitiumIgnorePath', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if ( ! isset( $gitium_ignore_path ) ) { return; } else { $path = $gitium_ignore_path; } check_admin_referer( 'gitium-admin' ); if ( $this->git->set_gitignore( join( "\n", array_unique( array_merge( explode( "\n", $this->git->get_gitignore() ), array( $path ) ) ) ) ) ) { gitium_commit_and_push_gitignore_file( $path ); $this->success_redirect( __( 'The file `.gitignore` is saved!', 'gitium' ), $this->gitium_menu_slug ); } else { $this->redirect( __( 'The file `.gitignore` could not be saved!', 'gitium' ), false, $this->gitium_menu_slug ); } } public function save_changes() { $gitium_save_changes = filter_input(INPUT_POST, 'GitiumSubmitSaveChanges', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $gitium_commit_msg = filter_input(INPUT_POST, 'commitmsg', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if ( ! isset( $gitium_save_changes ) ) { return; } check_admin_referer( 'gitium-admin' ); gitium_enable_maintenance_mode() or wp_die( __( 'Could not enable the maintenance mode!', 'gitium' ) ); $commitmsg = sprintf( __( 'Merged changes from %s on %s', 'gitium' ), get_site_url(), date( 'm.d.Y' ) ); if ( isset( $gitium_commit_msg ) && ! empty( $gitium_commit_msg ) ) { $commitmsg = $gitium_commit_msg; } $commits = array(); $current_user = wp_get_current_user(); // Get local status and behind commits $local_status = $this->git->local_status(); $behind_commits = count( $this->git->get_behind_commits() ); if ( $this->git->is_dirty() && $this->git->add() > 0 ) { $commit = $this->git->commit( $commitmsg, $current_user->display_name, $current_user->user_email ); if ( ! $commit ) { gitium_disable_maintenance_mode(); $this->redirect( __( 'Could not commit!', 'gitium' ) ); } $commits[] = $commit; } $merge_success = gitium_merge_and_push( $commits ); gitium_disable_maintenance_mode(); if ( ! $merge_success ) { $this->redirect( __( 'Merge failed: ', 'gitium' ) . $this->git->get_last_error() ); } // Determine message based on previous conditions if ( $behind_commits > 0 && empty( $local_status[1] ) ) { $this->success_redirect( sprintf( __( 'Pull done!', 'gitium' ) ) ); } else{ $this->success_redirect( sprintf( __( 'Pushed commit: `%s`', 'gitium' ), $commitmsg ) ); } } private function show_ahead_and_behind_info( $changes = '' ) { $branch = $this->git->get_remote_tracking_branch(); $ahead = count( $this->git->get_ahead_commits() ); $behind = count( $this->git->get_behind_commits() ); ?>

%s.', 'gitium' ), $branch ); ?> 

$type ) : $counter++; echo ( 0 != $counter % 2 ) ? '' : ''; echo '' . esc_html( $path ) . ''; echo '
' . __( 'Add this file to the `.gitignore` list.', 'gitium' ) . '
'; echo ''; if ( is_dir( ABSPATH . '/' . $path ) && is_dir( ABSPATH . '/' . trailingslashit( $path ) . '.git' ) ) { // test if is submodule _e( 'Submodules are not supported in this version.', 'gitium' ); } else { echo '' . esc_html( $this->humanized_change( $type ) ) . ''; } echo ''; echo ''; endforeach; } private function show_git_changes_table( $changes = '' ) { ?> '; else : $this->show_git_changes_table_rows( $changes ); endif; ?>

'; _e( 'Nothing to commit, working directory clean.', 'gitium' ); echo '

git->local_status(); $behind_commits = count( $this->git->get_behind_commits() ); // Determine button value based on conditions if ( $behind_commits > 0 && !empty( $local_status[1] ) ) { $button_value = __( 'Pull & Push changes', 'gitium' ); } else if ( $behind_commits > 0 ) { $button_value = __( 'Pull changes', 'gitium' ); } else if ( !empty( $local_status[1] ) ) { $button_value = __( 'Push changes', 'gitium' ); } // Check if there are any changes to display the form if ( !empty( $changes ) ) : ?>

/> 

 

git->get_remote_url() ); ?>

show_ahead_and_behind_info( $changes ); $this->show_git_changes_table( $changes ); $this->show_git_changes_table_submit_buttons( $changes ); ?>
show_disconnect_repository_button(); ?>
show_message(); _gitium_status( true ); $this->changes_page(); } }