MS_Rule_Page_Model

Membership Page Rule class.

Defined (1)

The class is defined in the following location(s).

/app/rule/page/class-ms-rule-page-model.php  
  1. class MS_Rule_Page_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * Rule type. 
  5. * @since 1.0.0 
  6. * @var string $rule_type 
  7. */ 
  8. protected $rule_type = MS_Rule_Page::RULE_ID; 
  9.  
  10. /** 
  11. * Initialize the rule. 
  12. * @since 1.0.0 
  13. * @param int $membership_id 
  14. */ 
  15. public function __construct( $membership_id ) { 
  16. parent::__construct( $membership_id ); 
  17.  
  18. $this->add_filter( 
  19. 'ms_rule_exclude_items-' . $this->rule_type,  
  20. 'exclude_items',  
  21. 10, 2 
  22. ); 
  23.  
  24. /** 
  25. * Set initial protection (front-end only) 
  26. * @since 1.0.0 
  27. */ 
  28. public function protect_content() { 
  29. parent::protect_content(); 
  30.  
  31. $this->add_filter( 'get_pages', 'protect_pages', 99, 2 ); 
  32.  
  33. /** 
  34. * Filters protected pages. 
  35. * @since 1.0.0 
  36. * Related action hook: 
  37. * - get_pages 
  38. * @param array $pages The array of pages to filter. 
  39. * @param array $args Array of arguments used by WP to filter the posts. 
  40. * @return array Filtered array which doesn't include prohibited pages. 
  41. */ 
  42. public function protect_pages( $pages, $args ) { 
  43. // Do not filter custom post types with this rule! 
  44. if ( 'page' != $args['post_type'] ) { 
  45. return $pages; 
  46.  
  47. $rule_value = apply_filters( 
  48. 'ms_rule_page_model_protect_pages_rule_value',  
  49. $this->rule_value 
  50. ); 
  51. $membership = $this->get_membership(); 
  52.  
  53. if ( ! is_array( $pages ) ) { 
  54. $pages = (array) $pages; 
  55.  
  56. foreach ( $pages as $key => $page ) { 
  57. if ( ! self::has_access( $page->ID ) ) { 
  58. unset( $pages[ $key ] ); 
  59.  
  60. return apply_filters( 
  61. 'ms_rule_page_model_protect_pages',  
  62. $pages,  
  63. $this 
  64. ); 
  65.  
  66. /** 
  67. * Get the current page id. 
  68. * @since 1.0.0 
  69. * @return int The page id, or null if it is not a page. 
  70. */ 
  71. private function get_current_page_id() { 
  72. $page_id = null; 
  73. $post = get_queried_object(); 
  74.  
  75. if ( is_a( $post, 'WP_Post' ) && 'page' == $post->post_type ) { 
  76. $page_id = $post->ID; 
  77.  
  78. return apply_filters( 
  79. 'ms_rule_page_model_get_current_page_id',  
  80. $page_id,  
  81. $this 
  82. ); 
  83.  
  84. /** 
  85. * Verify access to the current page. 
  86. * @since 1.0.0 
  87. * @param int $id The page_id to verify access. 
  88. * @return bool|null True if has access, false otherwise. 
  89. * Null means: Rule not relevant for current page. 
  90. */ 
  91. public function has_access( $id, $admin_has_access = true ) { 
  92. $has_access = null; 
  93.  
  94. if ( empty( $id ) ) { 
  95. $id = $this->get_current_page_id(); 
  96. } else { 
  97. $post = get_post( $id ); 
  98. if ( ! is_a( $post, 'WP_Post' ) || 'page' != $post->post_type ) { 
  99. $id = 0; 
  100.  
  101. if ( ! empty( $id ) ) { 
  102. $has_access = false; 
  103. // Membership special pages has access 
  104. if ( MS_Model_Pages::is_membership_page( $id ) ) { 
  105. $has_access = true; 
  106. } else { 
  107. $has_access = parent::has_access( $id, $admin_has_access ); 
  108.  
  109. return apply_filters( 
  110. 'ms_rule_page_model_has_access',  
  111. $has_access,  
  112. $id,  
  113. $this 
  114. ); 
  115.  
  116. /** 
  117. * Verify if has dripped rules. 
  118. * @since 1.0.0 
  119. * @param string $id The content id to verify. 
  120. * @return boolean True if has dripped rules. 
  121. */ 
  122. public function has_dripped_rules( $page_id = null ) { 
  123. if ( empty( $page_id ) ) { 
  124. $page_id = $this->get_current_page_id(); 
  125.  
  126. return parent::has_dripped_rules( $page_id ); 
  127.  
  128. /** 
  129. * Get the total content count. 
  130. * @since 1.0.0 
  131. * @param $args The query post args 
  132. * @see @link http://codex.wordpress.org/Function_Reference/get_pages 
  133. * @return int The total content count. 
  134. */ 
  135. public function get_content_count( $args = null ) { 
  136. unset( $args['number'] ); 
  137. $args = $this->get_query_args( $args ); 
  138. $posts = get_pages( $args ); 
  139.  
  140. $count = count( $posts ); 
  141.  
  142. return apply_filters( 
  143. 'ms_rule_page_model_get_content_count',  
  144. $count,  
  145. $args 
  146. ); 
  147.  
  148. /** 
  149. * Get content to protect. 
  150. * @since 1.0.0 
  151. * @param $args The query post args 
  152. * @see @link http://codex.wordpress.org/Function_Reference/get_pages 
  153. * @return array The contents array. 
  154. */ 
  155. public function get_contents( $args = null ) { 
  156. /** 
  157. * The 'hierarchial' flag messes up the offset by skipping some children 
  158. * in some cases (i.e. it will always skip pages until the first result 
  159. * is a top-level page). We have to get all pages from 0 and paginate 
  160. * manually... 
  161. */ 
  162. $offset = absint( $args['offset'] ); 
  163. $limit = $offset + absint( $args['number'] ); 
  164. $args['offset'] = 0; 
  165. $args['number'] = 0; 
  166.  
  167. $args = $this->get_query_args( $args ); 
  168.  
  169. $pages = get_pages( $args ); 
  170. $contents = array(); 
  171. if ( 0 == $limit ) { $limit = count( $pages ); } 
  172.  
  173. for ( $num = $offset; $num < $limit; $num += 1 ) { 
  174. if ( ! isset( $pages[$num] ) ) { continue; } 
  175.  
  176. $content = $pages[$num]; 
  177. $name = $content->post_title; 
  178.  
  179. $parent = get_post( $content->post_parent ); 
  180. for ( $level = 0; $level < 5 && $parent; $level += 1 ) { 
  181. $name = '— ' . $name; 
  182. $parent = get_post( $parent->post_parent ); 
  183.  
  184. $content->id = $content->ID; 
  185. $content->type = MS_Rule_Page::RULE_ID; 
  186. $content->name = $name; 
  187. $content->access = $this->get_rule_value( $content->id ); 
  188.  
  189. $contents[ $content->id ] = $content; 
  190.  
  191. return apply_filters( 
  192. 'ms_rule_page_model_get_contents',  
  193. $contents,  
  194. $this 
  195. ); 
  196.  
  197. /** 
  198. * Get the default query args. 
  199. * @since 1.0.0 
  200. * @param string $args The query post args. 
  201. * @see @link http://codex.wordpress.org/Function_Reference/get_pages 
  202. * @return array The parsed args. 
  203. */ 
  204. public function get_query_args( $args = null ) { 
  205. return parent::prepare_query_args( $args, 'get_pages' ); 
  206.  
  207. /** 
  208. * Exclude the special Membership2 pages from the results as they 
  209. * cannot be protected. 
  210. * @since 1.0.0 
  211. * @param array $excluded 
  212. * @param array $args 
  213. * @return array 
  214. */ 
  215. public function exclude_items( $excluded, $args ) { 
  216. static $Page_Ids = null; 
  217.  
  218. if ( null === $Page_Ids ) { 
  219. $Page_Ids = array(); 
  220. $types = MS_Model_Pages::get_page_types(); 
  221. foreach ( $types as $type => $title ) { 
  222. $Page_Ids[] = MS_Model_Pages::get_setting( $type ); 
  223.  
  224. return array_merge( $excluded, $Page_Ids ); 
  225.