MS_Rule_Url_Model

Membership URL Group Rule class.

Defined (1)

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

/app/rule/url/class-ms-rule-url-model.php  
  1. class MS_Rule_Url_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_Url::RULE_ID; 
  9.  
  10. /** 
  11. * A list of all URLs that are allowed by the current membership. 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. protected $_allowed_urls = null; 
  16.  
  17. /** 
  18. * Returns the active flag for a specific rule. 
  19. * State depends on Add-on 
  20. * @since 1.0.0 
  21. * @return bool 
  22. */ 
  23. static public function is_active() { 
  24. return MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ); 
  25.  
  26. /** 
  27. * Verify access to the current content. 
  28. * @since 1.0.0 
  29. * @param int $id The post/CPT ID to verify access. Defaults to current URL. 
  30. * @return bool|null True if has access, false otherwise. 
  31. * Null means: Rule not relevant for current page. 
  32. */ 
  33. public function has_access( $id, $admin_has_access = true ) { 
  34. $has_access = null; 
  35.  
  36. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ) ) { 
  37. if ( ! $this->has_rule_for_current_url() ) { return null; } 
  38.  
  39. if ( ! empty( $id ) ) { 
  40. $url = get_permalink( $id ); 
  41. } else { 
  42. $url = MS_Helper_Utility::get_current_url(); 
  43.  
  44. $exclude = apply_filters( 
  45. 'ms_rule_url_model_excluded_urls',  
  46. array() 
  47. ); 
  48.  
  49. // Check for exclude list. 
  50. if ( $this->check_url_expression_match( $url, $exclude ) ) { 
  51. $has_access = true; 
  52. } else { 
  53. // The URL is protected and has no exception. Deny it by default. 
  54. $has_access = false; 
  55.  
  56. // Check for URL group. 
  57. $accessible = $this->get_accessible_urls(); 
  58. if ( $this->check_url_expression_match( $url, $accessible ) ) { 
  59. if ( $this->get_membership()->is_base() ) { 
  60. // For guests all defined URL groups are denied. 
  61. $has_access = false; 
  62. } else { 
  63. $has_access = true; 
  64.  
  65. return apply_filters( 
  66. 'ms_rule_url_model_has_access',  
  67. $has_access,  
  68. $id,  
  69. $this 
  70. ); 
  71.  
  72. /** 
  73. * Verify if current url has protection rules. 
  74. * @since 1.0.0 
  75. * @return boolean True if has access, false otherwise. 
  76. */ 
  77. protected function has_rule_for_current_url() { 
  78. $has_rules = false; 
  79.  
  80. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ) ) { 
  81. $url = MS_Helper_Utility::get_current_url(); 
  82. if ( $this->check_url_expression_match( $url, $this->get_protected_urls() ) ) { 
  83. $has_rules = true; 
  84.  
  85. return apply_filters( 
  86. 'ms_rule_url_model_has_access',  
  87. $has_rules,  
  88. $this 
  89. ); 
  90.  
  91. /** 
  92. * Verify if a post/custom post type has protection rules. 
  93. * @since 1.0.0 
  94. * @return boolean True if has access, false otherwise. 
  95. */ 
  96. public function has_rule_for_post( $post_id ) { 
  97. $has_rules = false; 
  98.  
  99. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_URL_GROUPS ) ) { 
  100. $url = get_permalink( $post_id ); 
  101. if ( $this->check_url_expression_match( $url, $this->get_protected_urls() ) ) { 
  102. $has_rules = true; 
  103.  
  104. return apply_filters( 
  105. 'ms_rule_url_model_has_rule_for_post',  
  106. $has_rules,  
  107. $this 
  108. ); 
  109.  
  110. /** 
  111. * Check url expression match. 
  112. * @since 1.0.0 
  113. * @param string $url The url to match. 
  114. * @param string[] $check_list The url list to verify match. 
  115. * @return boolean True if matches. 
  116. */ 
  117. public function check_url_expression_match( $url, $check_list ) { 
  118. $match = false; 
  119.  
  120. $check_list = lib3()->array->get( $check_list ); 
  121. if ( count( $check_list ) ) { 
  122. $check_list = array_map( 'strtolower', $check_list ); 
  123. $check_list = array_map( 'trim', $check_list ); 
  124.  
  125. // Straight match. 
  126. $check_list = array_merge( 
  127. $check_list,  
  128. array_map( 'untrailingslashit', $check_list ) 
  129. ); 
  130.  
  131. $url = strtolower( $url ); 
  132. foreach ( $check_list as $check ) { 
  133. if ( $match ) { break; } 
  134.  
  135. if ( false !== strpos( $url, $check ) ) { 
  136. $match = true; 
  137.  
  138. return apply_filters( 
  139. 'ms_rule_url_model_check_url_expression_match',  
  140. $match,  
  141. $url,  
  142. $check_list,  
  143. $this 
  144. ); 
  145.  
  146. /** 
  147. * Count protection rules quantity. 
  148. * @since 1.0.0 
  149. * @param bool $has_access_only Optional. Count rules for has_access status only. 
  150. * @return int $count The rule count result. 
  151. */ 
  152. public function count_rules( $has_access_only = true ) { 
  153. $count = count( $this->rule_value ); 
  154.  
  155. return apply_filters( 
  156. 'ms_rule_url_model_count_rules',  
  157. $count,  
  158. $this 
  159. ); 
  160.  
  161. /** 
  162. * Get the total content count. 
  163. * Used in Dashboard to display how many special pages are protected. 
  164. * @since 1.0.0 
  165. * @param $args Ignored 
  166. * @return int The total content count. 
  167. */ 
  168. public function get_content_count( $args = null ) { 
  169. $count = count( $this->get_protected_urls() ); 
  170.  
  171. return apply_filters( 
  172. 'ms_rule_url_model_get_content_count',  
  173. $count,  
  174. $args 
  175. ); 
  176.  
  177. /** 
  178. * Get content to protect. 
  179. * @since 1.0.0 
  180. * @param $args The filter args 
  181. * @return array The contents array. 
  182. */ 
  183. public function get_contents( $args = null ) { 
  184. $protected_urls = $this->get_protected_urls(); 
  185. $membership_urls = $this->rule_value; 
  186.  
  187. $contents = array(); 
  188. foreach ( $membership_urls as $hash => $value ) { 
  189. if ( ! isset( $protected_urls[$hash] ) ) { 
  190. continue; 
  191.  
  192. $content = new StdClass(); 
  193. $content->id = $hash; 
  194. $content->type = MS_Rule_Url::RULE_ID; 
  195. $content->name = $protected_urls[$hash]; 
  196. $content->url = $protected_urls[$hash]; 
  197. $content->access = $this->get_rule_value( $content->id ); 
  198. $contents[] = $content; 
  199.  
  200. return apply_filters( 
  201. 'ms_rule_url_model_get_contents',  
  202. $contents 
  203. ); 
  204.  
  205. /** 
  206. * Set access status to content. 
  207. * @since 1.0.0 
  208. * @param string $id The content id to set access to. 
  209. * @param int $access The access status to set. 
  210. */ 
  211. public function set_access( $hash, $access ) { 
  212. if ( $this->is_base_rule ) { 
  213. /** 
  214. * Base rule cannot modify URL access via this function! 
  215. * Values of the base-rule are modified via a special Ajax handler 
  216. * that directly calls `add_url()` 
  217. * @see MS_Rule_Url::process_form() 
  218. */ 
  219. return; 
  220.  
  221. if ( empty( $access ) ) { 
  222. $this->delete_url( $hash ); 
  223. } else { 
  224. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  225. $url = $base_rule->get_url_from_hash( $hash ); 
  226. $this->add_url( $url ); 
  227.  
  228. do_action( 'ms_rule_url_set_access', $hash, $access, $this ); 
  229.  
  230. /** 
  231. * Serializes this rule in a single array. 
  232. * We don't use the PHP `serialize()` function to serialize the whole object 
  233. * because a lot of unrequired and duplicate data will be serialized 
  234. * @since 1.0.0 
  235. * @return array The serialized values of the Rule. 
  236. */ 
  237. public function serialize() { 
  238. $result = $this->rule_value; 
  239. return $result; 
  240.  
  241. /** 
  242. * Populates the rule_value array with the specified value list. 
  243. * This function is used when de-serializing a membership to re-create the 
  244. * rules associated with the membership. 
  245. * @since 1.0.0 
  246. * @param array $values A list of allowed IDs. 
  247. */ 
  248. public function populate( $values ) { 
  249. foreach ( $values as $hash => $url ) { 
  250. $this->add_url( $url ); 
  251.  
  252. /** 
  253. * Adds a new URL to the rule 
  254. * @since 1.0.0 
  255. * @param string $url 
  256. */ 
  257. public function add_url( $url ) { 
  258. $url = trim( $url ); 
  259.  
  260. // Index is a hash to prevent duplicate URLs in the list. 
  261. $hash = md5( $url ); 
  262. $this->rule_value[ $hash ] = $url; 
  263.  
  264. /** 
  265. * Removes an URL from the rule. 
  266. * An URL can be deleted either by specifying an Hash (prefered) or the 
  267. * plain-text URL. If a Hash value is specified the URL is always ignored. 
  268. * @since 1.0.0 
  269. * @param string $hash The URL-hash. 
  270. * @param string $url Optional. The plain-text URL. 
  271. */ 
  272. public function delete_url( $hash, $url = null ) { 
  273. if ( ! empty( $hash ) ) { 
  274. unset( $this->rule_value[ $hash ] ); 
  275. } elseif ( ! empty( $url ) ) { 
  276. $url = trim( $url ); 
  277. $hash = md5( $url ); 
  278. unset( $this->rule_value[ $hash ] ); 
  279.  
  280. /** 
  281. * Returns the URL from a specific hash value. 
  282. * @since 1.0.0 
  283. * @param string $hash The URL-hash. 
  284. */ 
  285. public function get_url_from_hash( $hash ) { 
  286. $url = ''; 
  287.  
  288. $urls = $this->get_protected_urls(); 
  289. if ( isset( $urls[ $hash ] ) ) { 
  290. $url = $urls[ $hash ]; 
  291.  
  292. return $url; 
  293.  
  294. /** 
  295. * Returns a list with all protected URLs. 
  296. * @since 1.0.0 
  297. * @param string $hash The URL-hash. 
  298. */ 
  299. public function get_protected_urls() { 
  300. static $Protected_Urls = null; 
  301.  
  302. if ( null === $Protected_Urls ) { 
  303. $base_rule = MS_Model_Membership::get_base()->get_rule( $this->rule_type ); 
  304. $Protected_Urls = $base_rule->rule_value; 
  305.  
  306. return $Protected_Urls; 
  307.  
  308. /** 
  309. * Returns a list with all accessible URLs. 
  310. * @since 1.0.0 
  311. * @param string $hash The URL-hash. 
  312. */ 
  313. public function get_accessible_urls() { 
  314. $accessible_Urls = $this->get_protected_urls(); 
  315. foreach ( $accessible_Urls as $key => $access ) { 
  316. if ( empty( $this->rule_value[$key] ) ) { 
  317. unset( $accessible_Urls[$key] ); 
  318.  
  319. return $accessible_Urls;