/app/view/settings/class-ms-view-settings-edit.php

  1. <?php 
  2. /** 
  3. * Renders Membership Plugin Settings. 
  4. * 
  5. * Extends MS_View for rendering methods and magic methods. 
  6. * 
  7. * @uses MS_Helper_Html Helper used to create form elements and vertical navigation. 
  8. * 
  9. * @since 1.0.0 
  10. */ 
  11. class MS_View_Settings_Edit extends MS_View { 
  12.  
  13. /** 
  14. * Overrides parent's to_html() method. 
  15. * 
  16. * Creates an output buffer, outputs the HTML and grabs the buffer content before releasing it. 
  17. * Creates a wrapper 'ms-wrap' HTML element to contain content and navigation. The content inside 
  18. * the navigation gets loaded with dynamic method calls. 
  19. * e.g. if key is 'settings' then render_settings() gets called, if 'bob' then render_bob(). 
  20. * 
  21. * @todo Could use callback functions to call dynamic methods from within the helper, thus 
  22. * creating the navigation with a single method call and passing method pointers in the $tabs array. 
  23. * 
  24. * @since 1.0.0 
  25. * 
  26. * @return object 
  27. */ 
  28. public function to_html() { 
  29. $this->check_simulation(); 
  30.  
  31. // Setup navigation tabs. 
  32. $tabs = $this->data['tabs']; 
  33. $desc = array(); 
  34.  
  35. ob_start(); 
  36. // Render tabbed interface. 
  37. ?> 
  38. <div class="ms-wrap wrap"> 
  39. <?php 
  40. $desc = $this->advanced_forms( $desc ); 
  41.  
  42. MS_Helper_Html::settings_header( 
  43. array( 
  44. 'title' => __( 'Membership 2 Settings', 'membership2' ),  
  45. 'title_icon_class' => 'wpmui-fa wpmui-fa-cog',  
  46. 'desc' => $desc,  
  47. ); 
  48. $active_tab = MS_Helper_Html::html_admin_vertical_tabs( $tabs ); 
  49.  
  50. // Call the appropriate form to render. 
  51. $tab_name = str_replace( '-', '_', $active_tab ); 
  52. $callback_name = 'render_tab_' . $tab_name; 
  53. $render_callback = apply_filters( 
  54. 'ms_view_settings_edit_render_callback',  
  55. array( $this, $callback_name ),  
  56. $active_tab,  
  57. $this->data 
  58. ); 
  59. ?> 
  60. <div class="ms-settings ms-settings-<?php echo esc_attr( $tab_name ); ?>"> 
  61. <?php 
  62. $html = call_user_func( $render_callback ); 
  63. $html = apply_filters( 'ms_view_settings_' . $callback_name, $html ); 
  64. echo $html; 
  65. ?> 
  66. </div> 
  67. </div> 
  68. <?php 
  69. $this->render_settings_footer( $tab_name ); 
  70.  
  71. $html = ob_get_clean(); 
  72.  
  73. return $html; 
  74.  
  75. /** ====================================================================== * 
  76. * ADVANCED-FORMS 
  77. * ====================================================================== */ 
  78.  
  79. /** 
  80. * Display advanced setting forms that can be triggered via an URL param. 
  81. * 
  82. * @since 1.0.0 
  83. * 
  84. * @param array $desc Array of items to display in the settings header. 
  85. * @return array New Array of items to display. Might include a HTML form. 
  86. */ 
  87. protected function advanced_forms( $desc ) { 
  88. // A "Reset" button that can be added via URL param 
  89. // Intentionally not translated (purpose is dev/testing) 
  90. if ( ! empty( $_GET['reset'] ) ) { 
  91. $reset_url = MS_Controller_Plugin::get_admin_url( 
  92. 'settings',  
  93. array( 'reset' => 1 ) 
  94. ); 
  95. $reset_url = esc_url_raw( 
  96. add_query_arg( 
  97. MS_Model_Upgrade::get_token( 'reset' ),  
  98. $reset_url 
  99. ); 
  100. $cancel_url = esc_url_raw( remove_query_arg( 'reset' ) ); 
  101.  
  102. $desc[] = sprintf( 
  103. '<div class="error" style="width:600px;margin:20px auto;text-align:center"><p><b>%1$s</b></p><hr />%2$s</div>',  
  104. 'Careful: This will completely erase all your Membership2 settings and details!',  
  105. sprintf( 
  106. '<form method="POST" action="%s" style="padding:20px 0">' . 
  107. '<label style="line-height:28px">' . 
  108. '<input type="checkbox" name="confirm" value="yes" /> Yes, reset everything!' . 
  109. '</label><p>' . 
  110. '<button class="button-primary">Do it!</button> ' . 
  111. '<a href="%s" class="button">Cancel</a>' . 
  112. '</p></form>',  
  113. $reset_url,  
  114. $cancel_url 
  115. ); 
  116.  
  117. // A "Resore" button that can be added via URL param 
  118. // Intentionally not translated (purpose is dev/testing) 
  119. if ( ! empty( $_GET['restore'] ) ) { 
  120. $restore_url = MS_Controller_Plugin::get_admin_url( 
  121. 'settings',  
  122. array( 'restore' => 1 ) 
  123. ); 
  124. $restore_url = esc_url_raw( 
  125. add_query_arg( 
  126. MS_Model_Upgrade::get_token( 'restore' ),  
  127. $restore_url 
  128. ); 
  129. $cancel_url = esc_url_raw( remove_query_arg( 'restore' ) ); 
  130. $options = array(); 
  131. $files = lib3()->updates->plugin( 'membership2' ); 
  132. $files = lib3()->updates->list_files( 'json' ); 
  133. foreach ( $files as $file ) { 
  134. $parts = explode( '-', $file ); 
  135. if ( 3 == count( $parts ) ) { 
  136. $version = str_replace( 'upgrade_', '', $parts[0] ); 
  137. $version = str_replace( '_', '.', $version ); 
  138. $date = substr( $parts[1], 0, 4 ) . '-' . substr( $parts[1], 4, 2 ) . '-' . substr( $parts[1], 6, 2 ); 
  139. $time = substr( $parts[2], 0, 2 ) . ':' . substr( $parts[2], 2, 2 ) . ':' . substr( $parts[2], 4, 2 ); 
  140. $label = sprintf( 
  141. '%2$s (%3$s) - Upgrade to %1$s',  
  142. $version,  
  143. $date,  
  144. $time 
  145. ); 
  146. } else { 
  147. $label = $file; 
  148. $options[$label] = sprintf( 
  149. '<option value="%1$s">%2$s</option>',  
  150. $file,  
  151. $label 
  152. ); 
  153. krsort( $options ); 
  154.  
  155. $desc[] = sprintf( 
  156. '<div class="error" style="width:600px;margin:20px auto;text-align:center"><p><b>%1$s</b></p><hr />%2$s</div>',  
  157. 'Careful: This will overwrite and replace existing data with old data from the Snapshot!',  
  158. sprintf( 
  159. '<form method="POST" action="%s" style="padding:20px 0">' . 
  160. '<label style="line-height:28px">Snapshot:</label><p>' . 
  161. '<select name="restore_snapshot">' . implode( '', $options ) . '</select>' . 
  162. '</p><label style="line-height:28px">' . 
  163. '<input type="checkbox" name="confirm" value="yes" /> Yes, overwrite data!' . 
  164. '</label><p>' . 
  165. '<button class="button-primary">Do it!</button> ' . 
  166. '<a href="%s" class="button">Cancel</a>' . 
  167. '</p></form>',  
  168. $restore_url,  
  169. $cancel_url 
  170. ); 
  171.  
  172. return $desc; 
  173.  
  174.  
  175. /** ====================================================================== * 
  176. * SETTINGS-FOOTER 
  177. * ====================================================================== */ 
  178.  
  179. /** 
  180. * Display a footer below the Settings box. 
  181. * The footer will show information on the next scheduled cron jobs and also 
  182. * allow the user to run these jobs instantly. 
  183. * 
  184. * @since 1.0.0 
  185. * @param string $tab_name Name of the currently open settings-tab. 
  186. */ 
  187. protected function render_settings_footer( $tab_name ) { 
  188. if ( 'general' != $tab_name ) { return; } 
  189.  
  190. $status_stamp = wp_next_scheduled( 'ms_cron_check_membership_status' ) - time(); 
  191. $email_stamp = wp_next_scheduled( 'ms_cron_process_communications' ) - time(); 
  192.  
  193. if ( $status_stamp > 0 ) { 
  194. $status_delay = sprintf( 
  195. __( 'in %s hrs %s min', 'membership2' ),  
  196. floor( ($status_stamp - 1) / 3600 ),  
  197. date( 'i', $status_stamp ) 
  198. ); 
  199. } else { 
  200. $status_delay = __( '(now...)', 'membership2' ); 
  201.  
  202. if ( $email_stamp > 0 ) { 
  203. $email_delay = sprintf( 
  204. __( 'in %s hrs %s min', 'membership2' ),  
  205. floor( ($email_stamp - 1) / 3600 ),  
  206. date( 'i', $email_stamp ) 
  207. ); 
  208. } else { 
  209. $email_delay = __( '(now...)', 'membership2' ); 
  210.  
  211. $status_url = esc_url_raw( 
  212. add_query_arg( array( 'run_cron' => 'ms_cron_check_membership_status' ) ) 
  213. ); 
  214. $email_url = esc_url_raw( 
  215. add_query_arg( array( 'run_cron' => 'ms_cron_process_communications' ) ) 
  216. ); 
  217. $lbl_run = __( 'Run now!', 'membership2' ); 
  218.  
  219. echo '<div class="cf ms-settings-footer"><div class="ms-tab-container"> </div>'; 
  220. echo '<div>'; 
  221.  
  222. if ( MS_Plugin::get_modifier( 'MS_LOCK_SUBSCRIPTIONS' ) ) { 
  223. _e( 'Membership Status Checks are disabled.', 'membership2' ); 
  224. echo ' '; 
  225. } else { 
  226. printf( 
  227. __( 'Check Membership Status changes %s.' ) . ' ',  
  228. '<a href="' . $status_url . '" title="' . $lbl_run . '">' . $status_delay . '</a>' 
  229. ); 
  230.  
  231. if ( MS_Plugin::get_modifier( 'MS_STOP_EMAILS' ) ) { 
  232. _e( 'Sending Email Responses is disabled.', 'membership2' ); 
  233. } else { 
  234. $count = MS_Model_Communication::get_queue_count(); 
  235. if ( ! $count ) { 
  236. $msg = __( 'No pending Email Responses found', 'membership2' ); 
  237. } elseif ( 1 == $count ) { 
  238. $msg = __( 'Send 1 pending Email Response %1$s', 'membership2' ); 
  239. } else { 
  240. $msg = __( 'Send %2$s pending Email Responses %1$s', 'membership2' ); 
  241.  
  242. printf( 
  243. $msg,  
  244. '<a href="' . $email_url . '"title="' . $lbl_run . '">' . $email_delay . '</a>',  
  245. $count 
  246. ); 
  247.  
  248. echo '</div></div>'; 
  249.  
  250. /** ====================================================================== * 
  251. * GENERAL 
  252. * ====================================================================== */ 
  253.  
  254. public function render_tab_general() { 
  255. $tab = MS_Factory::create( 'MS_View_Settings_Page_General' ); 
  256. $tab->data = $this->data; 
  257.  
  258. return $tab->to_html(); 
  259.  
  260. /** ====================================================================== * 
  261. * PAYMENT 
  262. * ====================================================================== */ 
  263.  
  264. public function render_tab_payment() { 
  265. $tab = MS_Factory::create( 'MS_View_Settings_Page_Payment' ); 
  266. $tab->data = $this->data; 
  267.  
  268. return $tab->to_html(); 
  269.  
  270. /** ====================================================================== * 
  271. * PROTECTION MESSAGE 
  272. * ====================================================================== */ 
  273.  
  274. public function render_tab_messages() { 
  275. $tab = MS_Factory::create( 'MS_View_Settings_Page_Messages' ); 
  276. $tab->data = $this->data; 
  277.  
  278. return $tab->to_html(); 
  279.  
  280. /** ====================================================================== * 
  281. * AUTOMATED MESSAGES 
  282. * ====================================================================== */ 
  283.  
  284. public function render_tab_emails() { 
  285. $tab = MS_Factory::create( 'MS_View_Settings_Page_Communications' ); 
  286. $tab->data = $this->data; 
  287.  
  288. return $tab->to_html(); 
  289.  
  290. /** ====================================================================== * 
  291. * IMPORT 
  292. * ====================================================================== */ 
  293.  
  294. public function render_tab_import() { 
  295. $tab = MS_Factory::create( 'MS_View_Settings_Page_Import' ); 
  296. $tab->data = $this->data; 
  297.  
  298. return $tab->to_html(); 
  299.  
.