MS_Rule_Special_Model

Membership Special Pages Rule class.

Defined (1)

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

/app/rule/special/class-ms-rule-special-model.php  
  1. class MS_Rule_Special_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_Special::RULE_ID; 
  9.  
  10. /** 
  11. * Available special pages 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. protected $_content = null; 
  16.  
  17. /** 
  18. * Analysis information on which page type was detected. 
  19. * @since 1.0.0 
  20. * @var string 
  21. */ 
  22. public $_matched_type = ''; 
  23.  
  24. /** 
  25. * Cache the result of is_special_page 
  26. * @since 1.0.0 
  27. * @var bool 
  28. */ 
  29. public $_is_special = null; 
  30.  
  31. /** 
  32. * Cache the result of has_rule_for_current_page 
  33. * @since 1.0.0 
  34. * @var bool 
  35. */ 
  36. public $_has_rule = null; 
  37.  
  38. /** 
  39. * Returns the active flag for a specific rule. 
  40. * State depends on Add-on 
  41. * @since 1.0.0 
  42. * @return bool 
  43. */ 
  44. static public function is_active() { 
  45. return MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SPECIAL_PAGES ); 
  46.  
  47. /** 
  48. * Checks if the current page is a special page that can be handled by this 
  49. * rule 
  50. * @since 1.0.0 
  51. * @return bool 
  52. */ 
  53. static public function is_special_page() { 
  54. if ( null === $this->_is_special ) { 
  55. $this->_is_special = is_home() 
  56. || is_front_page() 
  57. || is_404() 
  58. || is_search() 
  59. || is_archive() 
  60. || is_author() 
  61. || is_date() 
  62. || is_time(); 
  63.  
  64. return $this->_is_special; 
  65.  
  66. /** 
  67. * Verify access to the current page. 
  68. * @since 1.0.0 
  69. * @param int $id The page_id to verify access. 
  70. * @return bool|null True if has access, false otherwise. 
  71. * Null means: Rule not relevant for current page. 
  72. */ 
  73. public function has_access( $id, $admin_has_access = true ) { 
  74. $has_access = null; 
  75.  
  76. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SPECIAL_PAGES ) ) { 
  77. if ( $this->has_rule_for_current_page() ) { 
  78. $has_access = false; 
  79.  
  80. if ( $this->check_current_page( $this->rule_value ) ) { 
  81. if ( ! $this->get_membership()->is_base() ) { 
  82. $has_access = true; 
  83.  
  84. return apply_filters( 
  85. 'ms_rule_special_model_has_access',  
  86. $has_access,  
  87. $id,  
  88. $this 
  89. ); 
  90.  
  91. /** 
  92. * Checks if the current page is a special page and if the special page is 
  93. * protected by this rule. 
  94. * @since 1.0.0 
  95. * @return bool 
  96. */ 
  97. public function has_rule_for_current_page() { 
  98. if ( null === $this->_has_rule ) { 
  99. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_SPECIAL_PAGES ) ) { 
  100. $this->_has_rule = false; 
  101. } else { 
  102. $base = $this->get_membership()->get_base(); 
  103. $base_rule = $base->get_rule( $this->rule_type ); 
  104. $this->_has_rule = $this->check_current_page( $base_rule->rule_value ); 
  105.  
  106. return $this->_has_rule; 
  107.  
  108. /** 
  109. * Checks if the current page can be accessed by the specified rules 
  110. * @since 1.0.0 
  111. * @param array $rules List of allowed pages. 
  112. * @return bool 
  113. */ 
  114. protected function check_current_page( $rules ) { 
  115. $result = false; 
  116.  
  117. foreach ( $rules as $key => $active ) { 
  118. if ( ! $active ) { continue; } 
  119.  
  120. /** 
  121. * The item order is critical, in case a page has multiple flags 
  122. * like "Front" and "Home" and "Archive". 
  123. * In this example "Archive" might be denied but "Front" allowed,  
  124. * so we have to define a hierarchy which flag is actually used. 
  125. */ 
  126. switch ( $key ) { 
  127. case 'front': $result = is_front_page(); break; 
  128. case 'home': $result = is_home(); break; 
  129. case 'notfound': $result = is_404(); break; 
  130. case 'search': $result = is_search(); break; 
  131. case 'attachment': $result = is_attachment(); break; 
  132. case 'single': $result = is_singular(); break; 
  133. case 'archive': $result = is_archive(); break; 
  134. case 'author': $result = is_author(); break; 
  135. case 'date': $result = is_date(); break; 
  136. case 'year': $result = is_year(); break; 
  137. case 'month': $result = is_month(); break; 
  138. case 'day': $result = is_day(); break; 
  139. case 'time': $result = is_time(); break; 
  140.  
  141. if ( $result ) { 
  142. $this->matched_type = $key; 
  143. break; 
  144.  
  145. return apply_filters( 
  146. 'ms_rule_special_model_check_current_page',  
  147. $result,  
  148. $rules 
  149. ); 
  150.  
  151. /** 
  152. * Returns a list of special pages that can be configured by this rule. 
  153. * @since 1.0.0 
  154. * @return array List of special pages. 
  155. */ 
  156. protected function get_special_pages() { 
  157. if ( ! is_array( $this->_content ) ) { 
  158. $this->_content = array(); 
  159. $front_type = get_option( 'show_on_front' ); 
  160.  
  161. $front_url = MS_Helper_Utility::home_url( '/' ); 
  162. if ( 'page' === $front_type ) { 
  163. $home_url = get_permalink( get_option( 'page_for_posts' ) ); 
  164. } else { 
  165. $home_url = $front_url; 
  166.  
  167. $arch_year = get_year_link( '' ); 
  168. $arch_month = get_month_link( '', '' ); 
  169. $arch_day = get_day_link( '', '', '' ); 
  170. $arch_hour = esc_url_raw( 
  171. add_query_arg( 'hour', '15', $arch_day ) 
  172. ); 
  173.  
  174. // Archive pages 
  175. $this->_content['archive'] = (object) array( 
  176. 'label' => __( 'Any Archive page', 'membership2' ),  
  177. 'url' => '',  
  178. ); 
  179. $this->_content['author'] = (object) array( 
  180. 'label' => __( 'Author Archives', 'membership2' ),  
  181. 'url' => '',  
  182. ); 
  183. $this->_content['date'] = (object) array( 
  184. 'label' => __( 'Any Date or Time Archive', 'membership2' ),  
  185. 'url' => '',  
  186. ); 
  187. $this->_content['year'] = (object) array( 
  188. 'label' => __( 'Archive: Year', 'membership2' ),  
  189. 'url' => $arch_year,  
  190. ); 
  191. $this->_content['month'] = (object) array( 
  192. 'label' => __( 'Archive: Month', 'membership2' ),  
  193. 'url' => $arch_month,  
  194. ); 
  195. $this->_content['day'] = (object) array( 
  196. 'label' => __( 'Archive: Day', 'membership2' ),  
  197. 'url' => $arch_day,  
  198. ); 
  199. $this->_content['time'] = (object) array( 
  200. 'label' => __( 'Archive: Time', 'membership2' ),  
  201. 'url' => $arch_hour,  
  202. ); 
  203.  
  204. // Singular pages 
  205. $this->_content['front'] = (object) array( 
  206. 'label' => __( 'Front Page', 'membership2' ),  
  207. 'url' => $front_url,  
  208. ); 
  209. $this->_content['home'] = (object) array( 
  210. 'label' => __( 'Blog Index', 'membership2' ),  
  211. 'url' => $home_url,  
  212. ); 
  213. $this->_content['notfound'] = (object) array( 
  214. 'label' => __( '404 Not Found', 'membership2' ),  
  215. 'url' => '',  
  216. ); 
  217. $this->_content['search'] = (object) array( 
  218. 'label' => __( 'Search Results', 'membership2' ),  
  219. 'url' => '',  
  220. ); 
  221. $this->_content['single'] = (object) array( 
  222. 'label' => __( 'Any single page or post', 'membership2' ),  
  223. 'url' => '',  
  224. ); 
  225. $this->_content['attachment'] = (object) array( 
  226. 'label' => __( 'Any attachment page', 'membership2' ),  
  227. 'url' => '',  
  228. ); 
  229.  
  230. return $this->_content; 
  231.  
  232. /** 
  233. * Get the total content count. 
  234. * Used in Dashboard to display how many special pages are protected. 
  235. * @since 1.0.0 
  236. * @param $args The query post args 
  237. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  238. * @return int The total content count. 
  239. */ 
  240. public function get_content_count( $args = null ) { 
  241. $args['posts_per_page'] = 0; 
  242. $args['offset'] = false; 
  243. $count = count( $this->get_contents( $args ) ); 
  244.  
  245. return apply_filters( 
  246. 'ms_rule_special_model_get_content_count',  
  247. $count,  
  248. $args 
  249. ); 
  250.  
  251. /** 
  252. * Get content to protect. 
  253. * Used in Dashboard to display a list of special pages. 
  254. * @since 1.0.0 
  255. * @param $args The query post args 
  256. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  257. * @return array The contents array. 
  258. */ 
  259. public function get_contents( $args = null ) { 
  260. $special_pages = $this->get_special_pages(); 
  261. $contents = array(); 
  262.  
  263. $filter = $this->get_exclude_include( $args ); 
  264. if ( is_array( $filter->include ) ) { 
  265. $special_pages = array_intersect_key( $special_pages, array_flip( $filter->include ) ); 
  266. } elseif ( is_array( $filter->exclude ) ) { 
  267. $special_pages = array_diff_key( $special_pages, array_flip( $filter->exclude ) ); 
  268.  
  269. foreach ( $special_pages as $id => $data ) { 
  270. $content = (object) array(); 
  271.  
  272. // Search the special page name... 
  273. if ( ! empty( $args['s'] ) ) { 
  274. if ( false === stripos( $data->label, $args['s'] ) ) { 
  275. continue; 
  276.  
  277. $content->id = $id; 
  278. $content->type = MS_Rule_Special::RULE_ID; 
  279. $content->name = $data->label; 
  280. $content->post_title = $data->label; 
  281. $content->url = $data->url; 
  282. $content->access = $this->get_rule_value( $content->id ); 
  283.  
  284. $contents[ $content->id ] = $content; 
  285.  
  286. // If not visitor membership, just show Membership2 
  287. if ( ! $this->is_base_rule ) { 
  288. $contents = array_intersect_key( $contents, $this->rule_value ); 
  289.  
  290. if ( ! empty( $args['rule_status'] ) ) { 
  291. $contents = $this->filter_content( $args['rule_status'], $contents ); 
  292.  
  293. if ( ! empty( $args['posts_per_page'] ) ) { 
  294. $total = $args['posts_per_page']; 
  295. $offset = ! empty( $args['offset'] ) ? $args['offset'] : 0; 
  296. $contents = array_slice( $contents, $offset, $total ); 
  297.  
  298. return apply_filters( 
  299. 'ms_rule_special_model_get_contents',  
  300. $contents,  
  301. $this 
  302. ); 
  303.