/app/rule/page/class-ms-rule-page-model.php

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