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

/wp-content/plugins/membership/app/view/settings/page/class-ms-view-settings-page-setup.php

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