/app/view/settings/page/class-ms-view-settings-page-setup.php

  1. <?php 
  2. /** 
  3. * Displays the Setup form. 
  4. * Used in both the success popup when creating the first membership and in the 
  5. * settings page. 
  6. * 
  7. * @since 1.0.0 
  8. * @package Membership2 
  9. * @subpackage Model 
  10. */ 
  11. class MS_View_Settings_Page_Setup extends MS_View { 
  12.  
  13. /** 
  14. * Type of form displayed. Used to determine height of the popup. 
  15. * 
  16. * @var string 
  17. */ 
  18. protected $form_type = 'full'; 
  19.  
  20. /** 
  21. * Displays the settings form. 
  22. * 
  23. * @since 1.0.0 
  24. * @return string 
  25. */ 
  26. public function to_html() { 
  27. if ( ! empty( $_REQUEST['full_popup'] ) ) { 
  28. $show_wizard_done = true; 
  29. } else { 
  30. $show_wizard_done = MS_Plugin::instance()->settings->is_first_membership; 
  31.  
  32. if ( $show_wizard_done ) { 
  33. $this->form_type = 'full'; 
  34. $code = $this->html_full_form(); 
  35. } else { 
  36. $this->form_type = 'short'; 
  37. $code = $this->html_short_form(); 
  38.  
  39. return $code; 
  40.  
  41. /** 
  42. * Display the small "completed" form 
  43. * 
  44. * @since 1.0.0 
  45. * @return string HTML Code 
  46. */ 
  47. public function html_short_form() { 
  48. $code = sprintf( 
  49. '<center>%1$s</center>',  
  50. sprintf( 
  51. __( 'You can now go to page %sProtection Rules%s to set up access levels for this Membership.', 'membership2' ),  
  52. sprintf( '<a href="%1$s">', MS_Controller_Plugin::get_admin_url( 'protection' ) ),  
  53. '</a>' 
  54. ); 
  55.  
  56. return $code; 
  57.  
  58. /** 
  59. * Display the full settings form, used either by first membership 
  60. * "completed" popup and also by the general settings tab. 
  61. * 
  62. * @since 1.0.0 
  63. * @return string HTML code 
  64. */ 
  65. public function html_full_form() { 
  66. $fields = $this->prepare_fields(); 
  67.  
  68. ob_start(); 
  69. ?> 
  70. <div class="ms-setup-form"> 
  71. <?php if ( ! MS_Plugin::is_network_wide() ) : ?> 
  72. <div class="ms-setup-nav"> 
  73. <div class="ms-title"> 
  74. <i class="ms-icon dashicons dashicons-menu"></i> 
  75. <?php _e( 'Please select pages you want to appear in your Navigation', 'membership2' ); ?> 
  76. </div> 
  77. <div class="ms-description"> 
  78. <?php 
  79. printf( 
  80. __( 'You can always change those later by going to %1$s in your admin sidebar.', 'membership2' ),  
  81. sprintf( 
  82. '<a href="%1$s" target="_blank">%2$s</a>',  
  83. admin_url( 'nav-menus.php' ),  
  84. __( 'Appearance' ) . ' » ' . __( 'Menus' ) 
  85. ); 
  86. ?> 
  87. </div> 
  88. <?php echo $this->show_menu_controls(); ?> 
  89. </div> 
  90. <?php else : ?> 
  91. <div class="ms-setup-site"> 
  92. <div class="ms-title"> 
  93. <i class="ms-icon dashicons dashicons-admin-network"></i> 
  94. <?php _e( 'Select the Site that hosts Membership 2 Pages', 'membership2' ); ?> 
  95. </div> 
  96. <div class="ms-description"> 
  97. <?php _e( 'When you change the site new Membership 2 Pages are created on the selected site. You can customize or replace these pages at any time.', 'membership2' ); ?> 
  98. </div> 
  99. <?php 
  100. $site_options = MS_Helper_Settings::get_blogs(); 
  101. $site_fields = array( 
  102. array( 
  103. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  104. 'id' => 'network_site',  
  105. 'title' => __( 'Select the site that hosts the Membership 2 Pages', 'membership2' ),  
  106. 'value' => MS_Model_Pages::get_site_info( 'id' ),  
  107. 'field_options' => $site_options,  
  108. 'class' => 'ms-site-options',  
  109. ),  
  110. array( 
  111. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  112. 'name' => 'action',  
  113. 'value' => 'network_site',  
  114. ),  
  115. array( 
  116. 'type' => MS_Helper_Html::INPUT_TYPE_HIDDEN,  
  117. 'name' => '_wpnonce',  
  118. 'value' => wp_create_nonce( 'network_site' ),  
  119. ),  
  120. array( 
  121. 'type' => MS_Helper_Html::INPUT_TYPE_SUBMIT,  
  122. 'value' => __( 'Save', 'membership2' ),  
  123. ),  
  124. array( 
  125. 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON,  
  126. 'class' => 'ms-setup-pages-cancel',  
  127. 'value' => __( 'Cancel', 'membership2' ),  
  128. ),  
  129. ); 
  130. ?> 
  131. <div class="ms-setup-pages-site"> 
  132. <div class="ms-setup-pages-site-info"><?php 
  133. printf( 
  134. __( 'Membership pages are located on site %s', 'membership2' ),  
  135. '<strong>' . MS_Model_Pages::get_site_info( 'title' ) . '</strong>' 
  136. ); 
  137. ?> 
  138. <a href="#change-site" class="ms-setup-pages-change-site"><?php 
  139. _e( 'Change site...', 'membership2' ); 
  140. ?></a></div> 
  141. <div class="ms-setup-pages-site-form cf" style="display:none;"> 
  142. <?php 
  143. foreach ( $site_fields as $field ) { 
  144. MS_Helper_Html::html_element( $field ); 
  145. ?> 
  146. </div> 
  147. </div> 
  148. </div> 
  149. <?php endif; ?> 
  150. <div class="ms-setup-pages"> 
  151. <div class="ms-title"> 
  152. <i class="ms-icon dashicons dashicons-admin-page"></i> 
  153. <?php _e( 'Membership 2 Pages', 'membership2' ); ?> 
  154. </div> 
  155. <div class="ms-description"> 
  156. <?php _e( 'Set Up Membership 2 Pages that will be displayed on your website.', 'membership2' ); ?> 
  157. </div> 
  158. <?php 
  159.  
  160. if ( is_array( $fields['pages'] ) ) { 
  161. $page_types = array_keys( $fields['pages'] ); 
  162. $page_types_menu = array( 
  163. 'memberships',  
  164. 'register',  
  165. 'account',  
  166. ); 
  167. $page_types_rest = array_diff( $page_types, $page_types_menu ); 
  168. $groups = array( 
  169. 'in-menu' => $page_types_menu,  
  170. 'no-menu' => $page_types_rest,  
  171. ); 
  172.  
  173. $pages_site_id = MS_Model_Pages::get_site_info( 'id' ); 
  174. MS_Factory::select_blog( $pages_site_id ); 
  175.  
  176. foreach ( $groups as $group_key => $group_items ) : 
  177. printf( '<div class="ms-pages-group %1$s">', esc_attr( $group_key ) ); 
  178.  
  179. foreach ( $group_items as $key ) : 
  180. $field = $fields['pages'][$key]; 
  181. ?> 
  182. <div class="ms-settings-page-wrapper"> 
  183. <?php MS_Helper_Html::html_element( $field ); ?> 
  184. <div class="ms-action"> 
  185. <?php 
  186. MS_Helper_Html::html_link( 
  187. array( 
  188. 'id' => 'url_page_' . $field['value'],  
  189. 'url' => '',  
  190. 'value' => __( 'View Page', 'membership2' ),  
  191. 'target' => '_blank',  
  192. 'data_ms' => array( 
  193. 'base' => MS_Helper_Utility::get_home_url( 
  194. $pages_site_id, 'index.php?page_id=' 
  195. ),  
  196. ); 
  197. ?> 
  198. <span> | </span> 
  199. <?php 
  200. MS_Helper_Html::html_link( 
  201. array( 
  202. 'id' => 'edit_url_page_' . $field['value'],  
  203. 'url' => '',  
  204. 'value' => __( 'Edit Page', 'membership2' ),  
  205. 'target' => '_blank',  
  206. 'data_ms' => array( 
  207. 'base' => get_admin_url( 
  208. $pages_site_id, 'post.php?action=edit&post=' 
  209. ),  
  210. ); 
  211. ?> 
  212. </div> 
  213. </div> 
  214. <?php 
  215. endforeach; 
  216.  
  217. echo '</div>'; 
  218. endforeach; 
  219. } else { 
  220. echo $fields['pages']; 
  221.  
  222. MS_Factory::revert_blog(); 
  223. ?> 
  224. </div> 
  225. </div> 
  226. <?php 
  227.  
  228. $html = ob_get_clean(); 
  229.  
  230. return apply_filters( 
  231. 'ms_view_settings_page_setup_to_html',  
  232. $html 
  233. ); 
  234.  
  235. /** 
  236. * Prepare the HTML fields that can be displayed 
  237. * 
  238. * @since 1.0.0 
  239. * 
  240. * @return array 
  241. */ 
  242. protected function prepare_fields() { 
  243. // Prepare the return value. 
  244. $nav = array(); 
  245. $pages = array(); 
  246.  
  247. MS_Model_Pages::create_missing_pages(); 
  248. $page_types = MS_Model_Pages::get_page_types(); 
  249. $page_types_menu = array( 
  250. 'memberships',  
  251. 'register',  
  252. 'account',  
  253. ); 
  254. $page_types_rest = array_diff( $page_types, $page_types_menu ); 
  255.  
  256. // Prepare NAV fields. 
  257. $menu_action = MS_Controller_Pages::AJAX_ACTION_TOGGLE_MENU; 
  258. $menu_nonce = wp_create_nonce( $menu_action ); 
  259. foreach ( $page_types_menu as $type ) { 
  260. $nav_exists = MS_Model_Pages::has_menu( $type ); 
  261. $nav[$type] = array( 
  262. 'type' => MS_Helper_Html::INPUT_TYPE_RADIO_SLIDER,  
  263. 'id' => 'nav_' . $type,  
  264. 'value' => $nav_exists,  
  265. 'title' => $page_types[$type],  
  266. 'ajax_data' => array( 
  267. 'action' => $menu_action,  
  268. 'item' => $type,  
  269. '_wpnonce' => $menu_nonce,  
  270. ),  
  271. ); 
  272.  
  273. $nav['sep'] = array( 
  274. 'type' => MS_Helper_Html::TYPE_HTML_SEPARATOR,  
  275. ); 
  276.  
  277. // Prepare PAGES fields. 
  278. $pages_action = MS_Controller_Pages::AJAX_ACTION_UPDATE_PAGES; 
  279. $pages_nonce = wp_create_nonce( $pages_action ); 
  280.  
  281. foreach ( $page_types as $type => $label ) { 
  282. $page_id = MS_Model_Pages::get_setting( $type ); 
  283. $title = sprintf( 
  284. '<strong>%1$s</strong><span class="lbl-details">: %2$s</span>',  
  285. $label,  
  286. MS_Model_Pages::get_description( $type ) 
  287. ); 
  288.  
  289. $pages[ $type ] = array( 
  290. 'id' => $type,  
  291. 'type' => MS_Helper_Html::INPUT_TYPE_WP_PAGES,  
  292. 'title' => $title,  
  293. 'value' => $page_id,  
  294. 'field_options' => array( 
  295. 'no_item' => __( '- Select a page -', 'membership2' ),  
  296. ),  
  297. 'ajax_data' => array( 
  298. 'field' => $type,  
  299. 'action' => $pages_action,  
  300. '_wpnonce' => $pages_nonce,  
  301. ),  
  302. ); 
  303.  
  304. $fields = array( 
  305. 'nav' => $nav,  
  306. 'pages' => $pages,  
  307. ); 
  308.  
  309. return apply_filters( 
  310. 'ms_view_settings_page_setup_prepare_fields',  
  311. $fields,  
  312. $this 
  313. ); 
  314.  
  315. /** 
  316. * Outputs the HTML code to toggle Membership2 menu items. 
  317. * 
  318. * @since 1.0.0 
  319. * @return string 
  320. */ 
  321. public function show_menu_controls() { 
  322. $code = ''; 
  323. $can_create_nav = MS_Model_Pages::can_edit_menus(); 
  324.  
  325. if ( $can_create_nav ) { 
  326. $fields = $this->prepare_fields(); 
  327. foreach ( $fields['nav'] as $field ) { 
  328. $code .= MS_Helper_Html::html_element( $field, true ); 
  329. } else { 
  330. $button = array( 
  331. 'id' => 'create_menu',  
  332. 'type' => MS_Helper_Html::INPUT_TYPE_BUTTON,  
  333. 'value' => __( 'Okay, create the menu', 'membership2' ),  
  334. 'ajax_data' => array( 
  335. 'action' => MS_Controller_Pages::AJAX_ACTION_CREATE_MENU,  
  336. '_wpnonce' => wp_create_nonce( MS_Controller_Pages::AJAX_ACTION_CREATE_MENU ),  
  337. ); 
  338. $code = sprintf( 
  339. '<div style="padding-left:10px"><p><em>%s</em></p><p>%s</p></div>',  
  340. __( 'Wait! You did not create a menu yet...<br>Let us create it now, so you can choose which pages to display to your visitors!', 'membership2' ),  
  341. MS_Helper_Html::html_element( $button, true ) 
  342. ); 
  343.  
  344. return '<div class="ms-nav-controls">' . $code . '</div>'; 
  345.  
  346. /** 
  347. * Returns the height needed to display this dialog inside a popup without 
  348. * adding scrollbars 
  349. * 
  350. * @since 1.0.0 
  351. * @return int Popup height 
  352. */ 
  353. public function dialog_height() { 
  354. switch ( $this->form_type ) { 
  355. case 'short': 
  356. $height = 200; 
  357. break; 
  358.  
  359. case 'full': 
  360. default: 
  361. if ( MS_Model_Pages::can_edit_menus() ) { 
  362. $height = 412; 
  363. } else { 
  364. $height = 460; 
  365. break; 
  366.  
  367. return $height; 
  368.  
.