PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/gitium/inc/class-gitium-submenu-configure.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 243 lines | 201 code | 27 blank | 15 comment | 22 complexity | b1e2c26ef38a821e9c8c8e3170ab16d7 MD5 | raw file
  1. <?php
  2. /* Copyright 2014-2016 Presslabs SRL <ping@presslabs.com>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License, version 2, as
  5. published by the Free Software Foundation.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. class Gitium_Submenu_Configure extends Gitium_Menu {
  15. public function __construct() {
  16. parent::__construct( $this->gitium_menu_slug, $this->gitium_menu_slug );
  17. if ( current_user_can( 'manage_options' ) ) {
  18. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  19. add_action( 'admin_init', array( $this, 'regenerate_keypair' ) );
  20. add_action( 'admin_init', array( $this, 'gitium_warning' ) );
  21. add_action( 'admin_init', array( $this, 'init_repo' ) );
  22. add_action( 'admin_init', array( $this, 'choose_branch' ) );
  23. add_action( 'admin_init', array( $this, 'disconnect_repository' ) );
  24. }
  25. }
  26. public function admin_menu() {
  27. add_menu_page(
  28. __( 'Git Configuration', 'gitium' ),
  29. 'Gitium',
  30. 'manage_options',
  31. $this->menu_slug,
  32. array( $this, 'page' ),
  33. plugins_url( 'img/gitium.png', dirname( __FILE__ ) )
  34. );
  35. $submenu_hook = add_submenu_page(
  36. $this->menu_slug,
  37. __( 'Git Configuration', 'gitium' ),
  38. __( 'Configuration', 'gitium' ),
  39. 'manage_options',
  40. $this->menu_slug,
  41. array( $this, 'page' )
  42. );
  43. new Gitium_Help( $submenu_hook, 'configuration' );
  44. }
  45. public function regenerate_keypair() {
  46. if ( ! isset( $_POST['GitiumSubmitRegenerateKeypair'] ) ) {
  47. return;
  48. }
  49. check_admin_referer( 'gitium-admin' );
  50. gitium_get_keypair( true );
  51. $this->success_redirect( __( 'Keypair successfully regenerated.', 'gitium' ) );
  52. }
  53. public function gitium_warning() {
  54. if ( ! isset( $_POST['GitiumSubmitWarning'] ) ) {
  55. return;
  56. }
  57. check_admin_referer( 'gitium-admin' );
  58. $this->git->remove_wp_content_from_version_control();
  59. }
  60. public function init_process( $remote_url ) {
  61. $git = $this->git;
  62. $git->init();
  63. $git->add_remote_url( $remote_url );
  64. $git->fetch_ref();
  65. if ( count( $git->get_remote_branches() ) == 0 ) {
  66. $git->add( 'wp-content', '.gitignore' );
  67. $current_user = wp_get_current_user();
  68. $git->commit( __( 'Initial commit', 'gitium' ), $current_user->display_name, $current_user->user_email );
  69. if ( ! $git->push( 'master' ) ) {
  70. $git->cleanup();
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. public function init_repo() {
  77. if ( ! isset( $_POST['GitiumSubmitFetch'] ) || ! isset( $_POST['remote_url'] ) ) {
  78. return;
  79. }
  80. check_admin_referer( 'gitium-admin' );
  81. if ( empty( $_POST['remote_url'] ) ) {
  82. $this->redirect( __( 'Please specify a valid repo.', 'gitium' ) );
  83. }
  84. if ( $this->init_process( $_POST['remote_url'] ) ) {
  85. $this->success_redirect();
  86. } else {
  87. $this->redirect( __( 'Could not push to remote', 'gitium' ) . ' ' . $_POST['remote_url'] );
  88. }
  89. }
  90. public function choose_branch() {
  91. if ( ! isset( $_POST['GitiumSubmitMergeAndPush'] ) || ! isset( $_POST['tracking_branch'] ) ) {
  92. return;
  93. }
  94. check_admin_referer( 'gitium-admin' );
  95. $this->git->add();
  96. $branch = $_POST['tracking_branch'];
  97. $current_user = wp_get_current_user();
  98. $commit = $this->git->commit( __( 'Merged existing code from ', 'gitium' ) . get_home_url(), $current_user->display_name, $current_user->user_email );
  99. if ( ! $commit ) {
  100. $this->git->cleanup();
  101. $this->redirect( __( 'Could not create initial commit -> ', 'gitium' ) . $this->git->get_last_error() );
  102. }
  103. if ( ! $this->git->merge_initial_commit( $commit, $branch ) ) {
  104. $this->git->cleanup();
  105. $this->redirect( __( 'Could not merge the initial commit -> ', 'gitium' ) . $this->git->get_last_error() );
  106. }
  107. $this->git->push( $branch );
  108. $this->success_redirect();
  109. }
  110. private function setup_step_1_remote_url() {
  111. ?>
  112. <tr>
  113. <th scope="row"><label for="remote_url"><?php _e( 'Remote URL', 'gitium' ); ?></label></th>
  114. <td>
  115. <input type="text" class="regular-text" name="remote_url" id="remote_url" placeholder="git@github.com:user/example.git" value="">
  116. <p class="description"><?php _e( 'This URL provide access to a Git repository via SSH, HTTPS, or Subversion.', 'gitium' ); ?><br />
  117. <?php _e( 'If you need to authenticate over "https://" instead of SSH use: <code>https://user:pass@github.com/user/example.git</code>', 'gitium' ); ?></p>
  118. </td>
  119. </tr>
  120. <?php
  121. }
  122. private function setup_step_1_key_pair() {
  123. if ( ! defined( 'GIT_KEY_FILE' ) || GIT_KEY_FILE == '' ) :
  124. list( $git_public_key, ) = gitium_get_keypair(); ?>
  125. <tr>
  126. <th scope="row"><label for="key_pair"><?php _e( 'Key pair', 'gitium' ); ?></label></th>
  127. <td>
  128. <p>
  129. <input type="text" class="regular-text" name="key_pair" id="key_pair" value="<?php echo esc_attr( $git_public_key ); ?>" readonly="readonly">
  130. <input type="submit" name="GitiumSubmitRegenerateKeypair" class="button" value="<?php _e( 'Regenerate Key', 'gitium' ); ?>" />
  131. </p>
  132. <p class="description"><?php _e( 'If your code use ssh keybased authentication for git you need to allow write access to your repository using this key.', 'gitium' ); ?><br />
  133. <?php _e( 'Checkout instructions for <a href="https://help.github.com/articles/generating-ssh-keys#step-3-add-your-ssh-key-to-github" target="_blank">github</a> or <a href="https://confluence.atlassian.com/display/BITBUCKET/Add+an+SSH+key+to+an+account#AddanSSHkeytoanaccount-HowtoaddakeyusingSSHforOSXorLinux" target="_blank">bitbucket</a>.', 'gitium' ); ?>
  134. </p>
  135. </td>
  136. </tr>
  137. <?php endif;
  138. }
  139. private function setup_warning() {
  140. ?>
  141. <div class="wrap">
  142. <h2><?php _e( 'Warning!', 'gitium' ); ?></h2>
  143. <form name="gitium_form_warning" id="gitium_form_warning" action="" method="POST">
  144. <?php wp_nonce_field( 'gitium-admin' ); ?>
  145. <p><code>wp-content</code> is already under version control. You <a onclick="document.getElementById('gitium_form_warning').submit();" style="color:red;" href="#">must remove it from version control</a> in order to continue.</p>
  146. <p><strong>NOTE</strong> by doing this you WILL LOSE commit history, but NOT the actual files.</p>
  147. <input type="hidden" name="GitiumSubmitWarning" class="button-primary" value="1" />
  148. </form>
  149. </div>
  150. <?php
  151. }
  152. private function setup_step_1() {
  153. ?>
  154. <div class="wrap">
  155. <h2><?php _e( 'Configuration step 1', 'gitium' ); ?></h2>
  156. <p><?php _e( 'If you need help to set this up, please click on the "Help" button from the top right corner of this screen.' ); ?></p>
  157. <form action="" method="POST">
  158. <?php wp_nonce_field( 'gitium-admin' ); ?>
  159. <table class="form-table">
  160. <?php $this->setup_step_1_remote_url(); ?>
  161. <?php $this->setup_step_1_key_pair(); ?>
  162. </table>
  163. <p class="submit">
  164. <input type="submit" name="GitiumSubmitFetch" class="button-primary" value="<?php _e( 'Fetch', 'gitium' ); ?>" />
  165. </p>
  166. </form>
  167. </div>
  168. <?php
  169. }
  170. private function setup_step_2() {
  171. $git = $this->git; ?>
  172. <div class="wrap">
  173. <h2><?php _e( 'Configuration step 2', 'gitium' ); ?></h2>
  174. <p><?php _e( 'If you need help to set this up, please click on the "Help" button from the top right corner of this screen.' ); ?></p>
  175. <form action="" method="POST">
  176. <?php wp_nonce_field( 'gitium-admin' ); ?>
  177. <table class="form-table">
  178. <tr>
  179. <th scope="row"><label for="tracking_branch"><?php _e( 'Choose tracking branch', 'gitium' ); ?></label></th>
  180. <td>
  181. <select name="tracking_branch" id="tracking_branch">
  182. <?php foreach ( $git->get_remote_branches() as $branch ) : ?>
  183. <option value="<?php echo esc_attr( $branch ); ?>"><?php echo esc_html( $branch ); ?></option>
  184. <?php endforeach; ?>
  185. </select>
  186. <p class="description"><?php _e( 'Your code origin is set to', 'gitium' ); ?> <code><?php echo esc_html( $git->get_remote_url() ); ?></code></p>
  187. </td>
  188. </tr>
  189. </table>
  190. <p class="submit">
  191. <input type="submit" name="GitiumSubmitMergeAndPush" class="button-primary" value="<?php _e( 'Merge & Push', 'gitium' ); ?>" />
  192. </p>
  193. </form>
  194. <?php
  195. $this->show_disconnect_repository_button();
  196. ?>
  197. </div>
  198. <?php
  199. }
  200. public function page() {
  201. $this->show_message();
  202. if ( wp_content_is_versioned() ) {
  203. return $this->setup_warning();
  204. }
  205. if ( ! $this->git->is_versioned() || ! $this->git->get_remote_url() ) {
  206. return $this->setup_step_1();
  207. }
  208. if ( ! $this->git->get_remote_tracking_branch() ) {
  209. return $this->setup_step_2();
  210. }
  211. _gitium_status( true );
  212. }
  213. }