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

  1. <?php 
  2. /** 
  3. * Membership Rule Management class. 
  4. * 
  5. * @since 1.0.0 
  6. */ 
  7. class MS_Model_Rule extends MS_Model { 
  8.  
  9. /** 
  10. * Access status constants. 
  11. * 
  12. * @since 1.0.0 
  13. * 
  14. * @var bool 
  15. */ 
  16. const RULE_VALUE_NO_ACCESS = false; 
  17. const RULE_VALUE_HAS_ACCESS = true; 
  18. const RULE_VALUE_UNDEFINED = null; 
  19.  
  20. /** 
  21. * Filter type constants. 
  22. * 
  23. * @since 1.0.0 
  24. */ 
  25. const FILTER_PROTECTED = 'protected'; 
  26. const FILTER_NOT_PROTECTED = 'not_protected'; 
  27. const FILTER_DRIPPED = 'dripped'; 
  28.  
  29. /** 
  30. * Dripped type constants. 
  31. * 
  32. * @since 1.0.0 
  33. * @var string Available Drip-Types. 
  34. */ 
  35. const DRIPPED_TYPE_INSTANTLY = 'instantly'; 
  36. const DRIPPED_TYPE_SPEC_DATE = 'specific_date'; 
  37. const DRIPPED_TYPE_FROM_REGISTRATION = 'from_registration'; 
  38.  
  39. /** 
  40. * Collection of meta-information on a rule. 
  41. * @see register_rule() 
  42. * 
  43. * @since 1.0.0 
  44. * @var array 
  45. */ 
  46. static protected $rule_meta = null; 
  47.  
  48. /** 
  49. * Remember if we already called 'prepare_class' 
  50. * 
  51. * @since 1.0.0 
  52. * @var bool 
  53. */ 
  54. static protected $prepared = false; 
  55.  
  56. /** 
  57. * Initializes the rules. 
  58. * By creating the rule-object here we make sure that the rule is 
  59. * initialized correctly. 
  60. * 
  61. * To add a new rule only 2 steps are required: 
  62. * 1. Create a new file-structure in the app/rule/ directory 
  63. * 2. Load the rule in this function 
  64. * 
  65. * @since 1.0.0 
  66. */ 
  67. static public function prepare_class() { 
  68. MS_Factory::load( 'MS_Rule_Category' ); 
  69. MS_Factory::load( 'MS_Rule_Content' ); 
  70. MS_Factory::load( 'MS_Rule_Media' ); 
  71. MS_Factory::load( 'MS_Rule_MemberCaps' ); 
  72. MS_Factory::load( 'MS_Rule_MemberRoles' ); 
  73. MS_Factory::load( 'MS_Rule_MenuItem' ); 
  74. MS_Factory::load( 'MS_Rule_Page' ); 
  75. MS_Factory::load( 'MS_Rule_Post' ); 
  76. MS_Factory::load( 'MS_Rule_ReplaceLocation' ); 
  77. MS_Factory::load( 'MS_Rule_ReplaceMenu' ); 
  78. MS_Factory::load( 'MS_Rule_Shortcode' ); 
  79. MS_Factory::load( 'MS_Rule_Special' ); 
  80. MS_Factory::load( 'MS_Rule_Url' ); 
  81.  
  82. self::$prepared = true; 
  83.  
  84. /** 
  85. * Makes sure all rules are registered. 
  86. * 
  87. * @since 1.0.0 
  88. */ 
  89. static private function prepare() { 
  90. if ( ! self::$prepared ) { 
  91. // This will call prepare_class() above. 
  92. MS_Factory::load( 'MS_Model_Rule' ); 
  93.  
  94. /** 
  95. * Register meta-information on a rule 
  96. * 
  97. * @since 1.0.0 
  98. * @param string $id ID of the rule. 
  99. * @param string $class Name of the rule class, used to find Model-class. 
  100. * @param string $title Rule Title for display. 
  101. * @param int $priority Loading-priority (0 - 999), lower is earlier. 
  102. */ 
  103. static public function register_rule( $id, $class, $title, $priority = 0, $dripped = false ) { 
  104. if ( ! is_array( self::$rule_meta ) ) { 
  105. self::$rule_meta = array( 
  106. 'title' => array(),  
  107. 'class' => array(),  
  108. 'model_class' => array(),  
  109. 'order' => array(),  
  110. 'dripped' => array(),  
  111. ); 
  112.  
  113. self::$rule_meta['title'][ $id ] = $title; 
  114. self::$rule_meta['class'][ $id ] = $class; 
  115. self::$rule_meta['model_class'][ $id ] = $class . '_Model'; 
  116.  
  117. if ( $dripped ) { 
  118. self::$rule_meta['dripped'][] = $id; 
  119.  
  120. $priority = min( $priority, 999 ); 
  121. $priority = max( $priority, 0 ); 
  122.  
  123. $real_priority = $priority * 20; 
  124. while ( isset( self::$rule_meta['order'][ $real_priority ] ) ) { 
  125. $real_priority += 1; 
  126. self::$rule_meta['order'][ $real_priority ] = $id; 
  127.  
  128. /** 
  129. * Rule types. 
  130. * 
  131. * This array is ordered in the hierarchy way. 
  132. * First one has more priority than the last one. 
  133. * This hierarchy is used to determine access to Membership2. 
  134. * 
  135. * @since 1.0.0 
  136. * @return array $rule_types { 
  137. * @type in $priority The rule type priority in the execution sequence. 
  138. * @type string $rule_type The rule type. 
  139. * } 
  140. */ 
  141. public static function get_rule_types() { 
  142. static $Types = null; 
  143.  
  144. if ( null === $Types ) { 
  145. self::prepare(); 
  146.  
  147. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  148. $rule_types = self::$rule_meta['order']; 
  149.  
  150. $rule_types = apply_filters( 'ms_rule_get_rule_types', $rule_types ); 
  151. $rule_type = ksort( $rule_types ); 
  152.  
  153. $Types = $rule_types; 
  154.  
  155. return $Types; 
  156.  
  157. /** 
  158. * Rule types and respective classes. 
  159. * 
  160. * @since 1.0.0 
  161. * @return array { 
  162. * @type string $rule_type The rule type constant. 
  163. * @type string $class_name The rule type class. 
  164. * } 
  165. */ 
  166. public static function get_rule_type_classes() { 
  167. static $Rule_Classes = null; 
  168.  
  169. if ( null === $Rule_Classes ) { 
  170. self::prepare(); 
  171.  
  172. $Rule_Classes = self::$rule_meta['model_class']; 
  173.  
  174. $Rule_Classes = apply_filters( 
  175. 'ms_rule_get_rule_type_classes',  
  176. $Rule_Classes 
  177. ); 
  178.  
  179. return $Rule_Classes; 
  180.  
  181. /** 
  182. * Rule types and respective titles. 
  183. * 
  184. * @since 1.0.0 
  185. * @return array { 
  186. * @type string $rule_type The rule type constant. 
  187. * @type string $rule_title The rule title. 
  188. * } 
  189. */ 
  190. public static function get_rule_type_titles() { 
  191. static $Rule_Titles = null; 
  192.  
  193. if ( null === $Rule_Titles ) { 
  194. self::prepare(); 
  195.  
  196. $Rule_Titles = self::$rule_meta['title']; 
  197.  
  198. $Rule_Titles = apply_filters( 
  199. 'ms_rule_get_rule_type_titles',  
  200. $Rule_Titles 
  201. ); 
  202.  
  203. return $Rule_Titles; 
  204.  
  205. /** 
  206. * Dripped Rule types. 
  207. * 
  208. * Return only rule types with dripped rules. 
  209. * 
  210. * @since 1.0.0 
  211. * @return string[] $rule_type The rule type constant. 
  212. */ 
  213. public static function get_dripped_rule_types() { 
  214. static $Dripped_Rules = null; 
  215.  
  216. if ( null === $Dripped_Rules ) { 
  217. self::prepare(); 
  218.  
  219. $Dripped_Rules = self::$rule_meta['dripped']; 
  220.  
  221. $Dripped_Rules = apply_filters( 
  222. 'ms_rule_get_dripped_rule_types',  
  223. $Dripped_Rules 
  224. ); 
  225.  
  226. return $Dripped_Rules; 
  227.  
  228. /** 
  229. * Get dripped types. 
  230. * 
  231. * @since 1.0.0 
  232. * @return array { 
  233. * @type string $dripped_type The dripped type constant. 
  234. * @type string $dripped_type_desc The dripped type description. 
  235. * } 
  236. */ 
  237. public static function get_dripped_types() { 
  238. $dripped_types = array( 
  239. self::DRIPPED_TYPE_INSTANTLY => __( 'Instantly', 'membership2' ),  
  240. self::DRIPPED_TYPE_SPEC_DATE => __( 'On specific Date', 'membership2' ),  
  241. self::DRIPPED_TYPE_FROM_REGISTRATION => __( 'Relative to Subscription', 'membership2' ),  
  242. ); 
  243.  
  244. return apply_filters( 
  245. 'ms_rule_get_dripped_types',  
  246. $dripped_types 
  247. ); 
  248.  
.