PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/membership/app/rule/url/class-ms-rule-url-model.php

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