/app/view/class-ms-view-protection.php

  1. <?php 
  2.  
  3. /** 
  4. * Render Accessible Content page. 
  5. * 
  6. * Extends MS_View for rendering methods and magic methods. 
  7. * 
  8. * @since 1.0.0 
  9. * @package Membership2 
  10. * @subpackage View 
  11. */ 
  12. class MS_View_Protection extends MS_View { 
  13.  
  14. /** 
  15. * Create view output. 
  16. * 
  17. * @since 1.0.0 
  18. * @return string 
  19. */ 
  20. public function to_html() { 
  21. $this->check_simulation(); 
  22. $this->check_network(); 
  23.  
  24. $desc = array( 
  25. __( 'Choose Pages, Categories etc. that you want to make <strong>unavailable</strong> to visitors, and non-members.', 'membership2' ),  
  26. ); 
  27.  
  28. ob_start(); 
  29. // Render tabbed interface. 
  30. ?> 
  31. <div class="ms-wrap wrap ms-wrap-membership2"> 
  32. <?php 
  33. MS_Helper_Html::settings_header( 
  34. array( 
  35. 'title' => __( 'Set-up Protection Rules', 'membership2' ),  
  36. 'desc' => $desc,  
  37. ); 
  38.  
  39. // Display a filter to switch between individual memberships. 
  40. $this->membership_filter(); 
  41.  
  42. // In network-wide protection mode allow user to select a site. 
  43. $this->site_filter(); 
  44.  
  45. $active_tab = $this->data['active_tab']; 
  46. MS_Helper_Html::html_admin_vertical_tabs( $this->data['tabs'], $active_tab ); 
  47.  
  48. // Call the appropriate form to render. 
  49. $callback_name = 'render_tab_' . str_replace( '-', '_', $active_tab ); 
  50. $render_callback = array( $this, $callback_name ); 
  51. $render_callback = apply_filters( 
  52. 'ms_view_protectedcontent_define-' . $active_tab,  
  53. $render_callback,  
  54. $this->data 
  55. ); 
  56.  
  57. if ( is_callable( $render_callback ) ) { 
  58. $html = call_user_func( $render_callback ); 
  59. } else { 
  60. // This is to notify devs that a file/hook is missing or wrong. 
  61. $html = '<div class="ms-settings">' . 
  62. '<div class="error below-h2"><p>' . 
  63. '<em>No View defined by hook "ms_view_protectedcontent_define-' . $active_tab . '"</em>' . 
  64. '</p></div>' . 
  65. '</div>'; 
  66.  
  67. $html = apply_filters( 
  68. 'ms_view_membership_protected_' . $active_tab,  
  69. $html 
  70. ); 
  71. echo $html; 
  72. ?> 
  73. </div> 
  74. <?php 
  75.  
  76. $html = ob_get_clean(); 
  77.  
  78. return apply_filters( 
  79. 'ms_view_membership_protectedcontent_to_html',  
  80. $html,  
  81. $this 
  82. ); 
  83.  
  84. /** 
  85. * Display a filter to select the current membership. 
  86. * 
  87. * @since 1.0.0 
  88. */ 
  89. protected function membership_filter() { 
  90. $memberships = MS_Model_Membership::get_membership_names( 
  91. array( 'active' => true ) 
  92. ); 
  93. $url = esc_url_raw( 
  94. remove_query_arg( array( 'membership_id', 'paged' ) ) 
  95. ); 
  96. $links = array(); 
  97.  
  98. $links['all'] = array( 
  99. 'label' => __( 'All', 'membership2' ),  
  100. 'url' => $url,  
  101. ); 
  102.  
  103. foreach ( $memberships as $id => $name ) { 
  104. if ( empty( $name ) ) { 
  105. $name = __( '(No Name)', 'membership2' ); 
  106.  
  107. $links['ms-' . $id] = array( 
  108. 'label' => esc_html( $name ),  
  109. 'url' => esc_url_raw( 
  110. add_query_arg( array( 'membership_id' => $id ), $url ) 
  111. ),  
  112. ); 
  113.  
  114. ?> 
  115. <div class="wp-filter"> 
  116. <ul class="filter-links"> 
  117. <?php foreach ( $links as $key => $item ) : 
  118. $is_current = MS_Helper_Utility::is_current_url( $item['url'] ); 
  119. $class = ( $is_current ? 'current' : '' ); 
  120. ?> 
  121. <li> 
  122. <a href="<?php echo esc_url( $item['url'] ); ?>" class="<?php echo esc_attr( $class ); ?>"> 
  123. <?php echo esc_html( $item['label'] ); ?> 
  124. </a> 
  125. </li> 
  126. <?php endforeach; ?> 
  127. </ul> 
  128. </div> 
  129. <?php 
  130.  
  131. /** 
  132. * When network-wide protection is enabled then allow the user to choose the 
  133. * source-site of the content. 
  134. * 
  135. * Protection options can only be changed on a site-by-site base. So if the 
  136. * user has 3 sites he can protect all pages on all sites but has to select 
  137. * each site individually here. 
  138. * 
  139. * @since 1.0.0 
  140. */ 
  141. protected function site_filter() { 
  142. if ( ! MS_Plugin::is_network_wide() ) { 
  143. return false; 
  144.  
  145. $sites = MS_Helper_Settings::get_blogs(); 
  146. $site_options = array(); 
  147. $current_blog_id = MS_Factory::current_blog_id(); 
  148. $admin_script = 'admin.php?'. $_SERVER['QUERY_STRING']; 
  149.  
  150. foreach ( $sites as $blog_id => $title ) { 
  151. $key = get_admin_url( $blog_id, $admin_script ); 
  152.  
  153. if ( $current_blog_id == $blog_id ) { 
  154. $current_value = $key; 
  155.  
  156. $site_options[ $key ] = $title; 
  157.  
  158. $site_list = array( 
  159. 'id' => 'select-site',  
  160. 'type' => MS_Helper_Html::INPUT_TYPE_SELECT,  
  161. 'value' => $current_value,  
  162. 'field_options' => $site_options,  
  163. ); 
  164.  
  165. ?> 
  166. <div class="ms-tab-container"> 
  167. <label class="ms-tab-link" for="select-site"> 
  168. <?php _e( 'Select Site', 'membership2' ); ?> 
  169. </label> 
  170. </div> 
  171. <div> 
  172. <?php lib3()->html->element( $site_list ); ?> 
  173. </div> 
  174. <?php 
  175.  
.