MS_Rule_Post_Model

Membership Post Rule class.

Defined (1)

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

/app/rule/post/class-ms-rule-post-model.php  
  1. class MS_Rule_Post_Model extends MS_Rule { 
  2.  
  3. /** 
  4. * A list of all posts that are allowed by any MS_Rule_Post. 
  5. * (this logic is needed to merge rules if multiple memberships is enabled) 
  6. * @since 1.0.0 
  7. * @var array 
  8. */ 
  9. static protected $allowed_ids = array(); 
  10.  
  11. /** 
  12. * A list of all posts that are not available by any MS_Rule_Post. 
  13. * (this logic is needed to merge rules if multiple memberships is enabled) 
  14. * @since 1.0.0 
  15. * @var array 
  16. */ 
  17. static protected $denied_ids = array(); 
  18.  
  19. /** 
  20. * Rule type. 
  21. * @since 1.0.0 
  22. * @var string $rule_type 
  23. */ 
  24. protected $rule_type = MS_Rule_Post::RULE_ID; 
  25.  
  26. /** 
  27. * Returns the active flag for a specific rule. 
  28. * State depends on Add-on 
  29. * @since 1.0.0 
  30. * @return bool 
  31. */ 
  32. static public function is_active() { 
  33. return MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_POST_BY_POST ); 
  34.  
  35. /** 
  36. * Set initial protection. 
  37. * @since 1.0.0 
  38. */ 
  39. public function protect_content() { 
  40. parent::protect_content(); 
  41.  
  42. $this->add_action( 'pre_get_posts', 'find_protected_posts', 99 ); 
  43. $this->add_action( 'pre_get_posts', 'protect_posts', 100 ); 
  44.  
  45. /** 
  46. * Protect post from showing. 
  47. * Related Action Hooks: 
  48. * - pre_get_posts 
  49. * @since 1.0.0 
  50. * @param WP_Query $query The WP_Query object to filter. 
  51. */ 
  52. public function find_protected_posts( $wp_query ) { 
  53. // List rather than on a single post 
  54. if ( ! $wp_query->is_singular 
  55. && empty( $wp_query->query_vars['pagename'] ) 
  56. && ( ! isset( $wp_query->query_vars['post_type'] ) 
  57. || in_array( $wp_query->query_vars['post_type'], array( 'post', '' ) ) 
  58. ) { 
  59. foreach ( $this->rule_value as $id => $value ) { 
  60. if ( $this->has_access( $id ) ) { 
  61. self::$allowed_ids[] = $id; 
  62. } else { 
  63. self::$denied_ids[] = $id; 
  64.  
  65. /** 
  66. * Protect post from showing. 
  67. * Related Action Hooks: 
  68. * - pre_get_posts 
  69. * @since 1.0.0 
  70. * @param WP_Query $query The WP_Query object to filter. 
  71. */ 
  72. public function protect_posts( $wp_query ) { 
  73. if ( empty( self::$denied_ids ) && empty( self::$allowed_ids ) ) { 
  74. return $wp_query; 
  75.  
  76. if ( ! empty( self::$denied_ids ) ) { 
  77. // Remove duplicate entries from the ID arrays. 
  78. self::$denied_ids = array_unique( self::$denied_ids, SORT_NUMERIC ); 
  79. self::$allowed_ids = array_unique( self::$allowed_ids, SORT_NUMERIC ); 
  80.  
  81. // Remove any post that is allowed from the denied_ids list. 
  82. self::$denied_ids = array_diff( 
  83. self::$denied_ids,  
  84. self::$allowed_ids 
  85. ); 
  86.  
  87. // Tell the WP query which posts are actually off limit for the user. 
  88. $wp_query->query_vars['post__not_in'] = array_merge( 
  89. $wp_query->query_vars['post__not_in'],  
  90. self::$denied_ids 
  91. ); 
  92.  
  93. self::$denied_ids = array(); 
  94. self::$allowed_ids = array(); 
  95.  
  96. do_action( 
  97. 'ms_rule_post_model_protect_posts',  
  98. $wp_query,  
  99. $this 
  100. ); 
  101.  
  102. /** 
  103. * Get the current post id. 
  104. * @since 1.0.0 
  105. * @return int The post id, or null if it is not a post. 
  106. */ 
  107. private function get_current_post_id() { 
  108. $post_id = null; 
  109. $post = get_queried_object(); 
  110.  
  111. if ( is_a( $post, 'WP_Post' ) && 'post' == $post->post_type ) { 
  112. $post_id = $post->ID; 
  113.  
  114. return apply_filters( 
  115. 'ms_rule_post_model_get_current_post_id',  
  116. $post_id,  
  117. $this 
  118. ); 
  119.  
  120. /** 
  121. * Verify access to the current page. 
  122. * @since 1.0.0 
  123. * @param int $id The page_id to verify access. 
  124. * @return bool|null True if has access, false otherwise. 
  125. * Null means: Rule not relevant for current page. 
  126. */ 
  127. public function has_access( $id, $admin_has_access = true ) { 
  128. $has_access = null; 
  129.  
  130. if ( empty( $id ) ) { 
  131. $id = $this->get_current_post_id(); 
  132. } else { 
  133. $post = get_post( $id ); 
  134. if ( ! is_a( $post, 'WP_Post' ) 
  135. || ( ! empty( $post->post_type ) && 'post' != $post->post_type ) 
  136. ) { 
  137. $id = 0; 
  138.  
  139. if ( ! empty( $id ) ) { 
  140. $has_access = parent::has_access( $id, $admin_has_access ); 
  141.  
  142. return apply_filters( 
  143. 'ms_rule_post_model_has_access',  
  144. $has_access,  
  145. $id,  
  146. $this 
  147. ); 
  148.  
  149. /** 
  150. * Verify if has dripped rules. 
  151. * @since 1.0.0 
  152. * @param string $id The content id to verify. 
  153. * @return boolean True if has dripped rules. 
  154. */ 
  155. public function has_dripped_rules( $post_id = null ) { 
  156. if ( empty( $post_id ) ) { 
  157. $post_id = $this->get_current_post_id(); 
  158.  
  159. return parent::has_dripped_rules( $post_id ); 
  160.  
  161. /** 
  162. * Get the total content count. 
  163. * @since 1.0.0 
  164. * @param $args The query post args 
  165. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  166. * @return int The total content count. 
  167. */ 
  168. public function get_content_count( $args = null ) { 
  169. $args = self::get_query_args( $args ); 
  170. $query = new WP_Query( $args ); 
  171.  
  172. $count = $query->found_posts; 
  173.  
  174. return apply_filters( 
  175. 'ms_rule_post_model_get_content_count',  
  176. $count,  
  177. $args 
  178. ); 
  179.  
  180. /** 
  181. * Get content to protect. 
  182. * @since 1.0.0 
  183. * @param $args The query post args 
  184. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  185. * @return array The contents array. 
  186. */ 
  187. public function get_contents( $args = null ) { 
  188. $args = self::get_query_args( $args ); 
  189.  
  190. $query = new WP_Query( $args ); 
  191. $posts = $query->posts; 
  192.  
  193. $contents = array(); 
  194. foreach ( $posts as $content ) { 
  195. $content->id = $content->ID; 
  196. $content->type = MS_Rule_Post::RULE_ID; 
  197. $content->name = $content->post_name; 
  198. $content->access = $this->get_rule_value( $content->id ); 
  199.  
  200. $contents[ $content->id ] = $content; 
  201.  
  202. return apply_filters( 
  203. 'ms_rule_post_model_get_contents',  
  204. $contents,  
  205. $this 
  206. ); 
  207.  
  208. /** 
  209. * Get the default query args. 
  210. * @since 1.0.0 
  211. * @param string $args The query post args. 
  212. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  213. * @return array The parsed args. 
  214. */ 
  215. public function get_query_args( $args = null ) { 
  216. return parent::prepare_query_args( $args, 'wp_query' ); 
  217.