PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/p/snapshot/new-ui-tester.php

https://bitbucket.org/matthewselby/wpdev
PHP | 293 lines | 213 code | 61 blank | 19 comment | 40 complexity | 1c0b131f3d92038501b8948c23d6cdd1 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-3.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, MIT, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. if ( class_exists( 'WPMUDEVSnapshot_New_Ui_Tester' ) ) {
  3. return;
  4. }
  5. class WPMUDEVSnapshot_New_Ui_Tester {
  6. public function dashboard() {
  7. $this->render( 'dashboard' );
  8. }
  9. public function snapshots() {
  10. if ( isset( $_REQUEST['snapshot-action'] ) && 'new' === $_REQUEST['snapshot-action'] ) {
  11. $this->render( 'snapshots/snapshot', false, array( 'action' => 'add', 'item' => array() ) );
  12. return;
  13. }
  14. if ( isset( $_REQUEST['item'], WPMUDEVSnapshot::instance()->config_data['items'][ sanitize_text_field( $_REQUEST['item'] ) ] ) ) {
  15. $item = WPMUDEVSnapshot::instance()->config_data['items'][ sanitize_text_field( $_REQUEST['item'] ) ];
  16. $snapshot_action = 'default';
  17. if ( isset( $_REQUEST['snapshot-action'] ) ) {
  18. $snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
  19. }
  20. $force_backup = false;
  21. switch ( $snapshot_action ) {
  22. case 'backup':
  23. $force_backup = true;
  24. case 'edit':
  25. $this->render( 'snapshots/snapshot', false, array( 'action' => 'update', 'item' => $item, 'force_backup' => $force_backup ) );
  26. break;
  27. case 'restore':
  28. if ( ( isset( $_GET['snapshot-data-item'] ) ) && ( isset( $item['data'][ intval( $_GET['snapshot-data-item'] ) ] ) ) ) {
  29. $data_item_key = intval( $_GET['snapshot-data-item'] );
  30. $this->render( 'snapshots/restore', false, array( 'item' => $item, 'data_item_key' => $data_item_key ) );
  31. } else {
  32. $this->render( 'snapshots/item', false, array( 'item' => $item ) );
  33. }
  34. break;
  35. default:
  36. $this->render( 'snapshots/item', false, array( 'item' => $item ) );
  37. }
  38. } else {
  39. $snapshots = WPMUDEVSnapshot::instance()->config_data['items'];
  40. $count_all_snapshots = count( $snapshots );
  41. $all_destinations = WPMUDEVSnapshot::instance()->config_data['destinations'];
  42. $filter = ( isset( $_GET['destination'] ) ) ? sanitize_text_field( $_GET['destination'] ) : '';
  43. if ( $filter !== '' && isset( $all_destinations[ $filter ] ) ) {
  44. $filtred_snapshot = array();
  45. foreach ( $snapshots as $key => $snapshot ) {
  46. if ( isset( $snapshot['destination'] ) && $snapshot['destination'] === $filter ) {
  47. $filtred_snapshot[ $key ] = $snapshot;
  48. }
  49. }
  50. $snapshots = $filtred_snapshot;
  51. }
  52. $results_count = count( $snapshots );
  53. $per_page = 20;
  54. //Max number of pages
  55. $max_pages = ceil( $results_count / $per_page );
  56. $paged = ( ! isset( $_GET['paged'] ) ) ? 1 : intval( $_GET['paged'] );
  57. $offset = $per_page * ( $paged - 1 );
  58. $data = array(
  59. 'snapshots' => $snapshots,
  60. 'results_count' => $results_count,
  61. 'count_all_snapshots' => $count_all_snapshots,
  62. 'per_page' => $per_page,
  63. 'max_pages' => $max_pages,
  64. 'paged' => $paged,
  65. 'offset' => $offset,
  66. 'filter' => $filter
  67. );
  68. $this->render( "snapshots", false, $data );
  69. }
  70. }
  71. /*public function create_snapshot(){
  72. $this->render( "snapshots/partials/create-snapshot-progress", "Create Snapshot" );
  73. }*/
  74. public function destinations() {
  75. $snapshot_action = 'default';
  76. if ( isset( $_REQUEST['snapshot-action'] ) ) {
  77. $snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
  78. }
  79. switch ( $snapshot_action ) {
  80. case 'add':
  81. case 'edit':
  82. case 'update':
  83. $this->render( 'destinations/add/index' );
  84. break;
  85. default:
  86. $this->render( 'destinations' );
  87. }
  88. }
  89. public function managed_backups() {
  90. $model = new Snapshot_Model_Full_Backup;
  91. $is_dashboard_active = $model->is_dashboard_active();
  92. $is_dashboard_installed = $is_dashboard_active
  93. ? true
  94. : $model->is_dashboard_installed();
  95. $has_dashboard_key = $model->has_dashboard_key();
  96. $is_client = $is_dashboard_installed && $is_dashboard_active && $has_dashboard_key;
  97. $apiKey = $model->get_config( 'secret-key', '' );
  98. $has_snapshot_key = $is_client && Snapshot_Model_Full_Remote_Api::get()->get_token() != false && ! empty( $apiKey );
  99. if ( ! $is_client ) {
  100. $this->render( "managed-backups/get-started", false, array( 'model' => $model ) );
  101. } else if ( ! $has_snapshot_key ) {
  102. $this->render( "managed-backups/activate", false, array( 'model' => $model ) );
  103. } else {
  104. $snapshot_action = 'default';
  105. if ( isset( $_REQUEST['snapshot-action'] ) ) {
  106. $snapshot_action = sanitize_text_field( $_REQUEST['snapshot-action'] );
  107. }
  108. function __snapshot_sort_managed_backups_array ( $a, $b ){
  109. return $b['timestamp'] - $a['timestamp'];
  110. }
  111. switch ( $snapshot_action ) {
  112. case 'backup':
  113. $this->render( "managed-backups/new-backup", false, array( 'model' => $model ) );
  114. break;
  115. case 'restore':
  116. $item = false;
  117. if ( isset( $_GET['item'] ) ) {
  118. $item = $model->get_backup( sanitize_text_field( $_GET['item'] ) );
  119. }
  120. if ( $item ) {
  121. $this->render( "managed-backups/restore", false, array( 'model' => $model, 'item' => $item ) );
  122. break;
  123. }
  124. default:
  125. $backups = $model->get_backups();
  126. usort( $backups, '__snapshot_sort_managed_backups_array' );
  127. $last_backup = reset( $backups );
  128. $filter = ( isset( $_GET['date'] ) ) ? sanitize_text_field( $_GET['date'] ) : '';
  129. $timestamps = wp_list_pluck( $backups, 'timestamp' );
  130. $months = array();
  131. foreach ( $timestamps as $key => $month ) {
  132. $months[ date( 'mY', $month ) ] = date( 'F Y', $month );
  133. }
  134. if ( $filter !== '' && is_int( $filter ) ) {
  135. $filtred_snapshot = array();
  136. foreach ( $backups as $key => $snapshot ) {
  137. if ( isset( $snapshot['timestamp'] ) && date( 'mY', $snapshot['timestamp'] ) === $filter ) {
  138. $filtred_snapshot[ $key ] = $snapshot;
  139. }
  140. }
  141. $backups = $filtred_snapshot;
  142. }
  143. $results_count = count( $backups );
  144. $per_page = 20;
  145. //Max number of pages
  146. $max_pages = ceil( $results_count / $per_page );
  147. $paged = ( ! isset( $_GET['paged'] ) ) ? 1 : intval( $_GET['paged'] );
  148. $offset = $per_page * ( $paged - 1 );
  149. $apiKey = $model->get_config( 'secret-key', '' );
  150. $data = array(
  151. "model" => $model,
  152. "last_backup" => $last_backup,
  153. "hasApikey" => ! empty( $apiKey ),
  154. "apiKey" => $apiKey,
  155. "apiKeyUrl" => $model->get_current_secret_key_link(),
  156. 'backups' => $backups,
  157. 'results_count' => $results_count,
  158. 'per_page' => $per_page,
  159. 'max_pages' => $max_pages,
  160. 'paged' => $paged,
  161. 'offset' => $offset,
  162. 'filter' => $filter,
  163. 'months' => $months
  164. );
  165. $this->render( 'managed_backups', false, $data );
  166. }
  167. }
  168. }
  169. public function import() {
  170. $this->render( 'import' );
  171. }
  172. public function settings() {
  173. $this->render( 'settings' );
  174. }
  175. /**
  176. * @param string $file
  177. * @param bool $deprecated
  178. * @param array $params
  179. * @param bool $return
  180. * @param bool $footer
  181. *
  182. * @return string
  183. */
  184. public function render( $file, $deprecated = false, $params = array(), $return = false, $footer = true ) {
  185. $template_filename = "views/$file.php";
  186. $template_file = plugin_dir_url( plugin_basename( __FILE__ ) ) . $template_filename;
  187. if ( ! file_exists( $template_file ) ) {
  188. $template_file = trailingslashit( dirname( __FILE__ ) ) . $template_filename;
  189. }
  190. if ( $return ) {
  191. ob_start();
  192. }
  193. extract( $params, EXTR_SKIP );
  194. include $template_file;
  195. if ( $footer ) {
  196. $this->render( 'common/footer', false, array(), false, false );
  197. }
  198. if ( $return ) {
  199. return ob_get_clean();
  200. }
  201. foreach ( $params as $param ) {
  202. unset( $param );
  203. }
  204. return null;
  205. }
  206. /**
  207. * Print the form errors for a particular field, if there are any
  208. *
  209. * @param $field
  210. */
  211. public function input_error_message( $field ) {
  212. if ( ! isset( WPMUDEVSnapshot::instance()->form_errors[ $field ] ) ) {
  213. return;
  214. }
  215. $field_errors = (array) WPMUDEVSnapshot::instance()->form_errors[ $field ];
  216. echo '<div class="error-text">';
  217. foreach ( $field_errors as $error ) {
  218. echo '<p>', esc_html( $error ), '</p>';
  219. }
  220. echo '</div>';
  221. }
  222. public function input_error_class( $field, $echo = true ) {
  223. if ( ! isset( WPMUDEVSnapshot::instance()->form_errors[ $field ] ) ) {
  224. return '';
  225. }
  226. $class = ' validation-error';
  227. if ( $echo ) {
  228. echo $class;
  229. }
  230. return $class;
  231. }
  232. }