/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.  
  69. MS_Factory::load( 'MS_Rule_Category' ); 
  70. MS_Factory::load( 'MS_Rule_Content' ); 
  71. MS_Factory::load( 'MS_Rule_Media' ); 
  72. MS_Factory::load( 'MS_Rule_MemberCaps' ); 
  73. MS_Factory::load( 'MS_Rule_MemberRoles' ); 
  74. MS_Factory::load( 'MS_Rule_MenuItem' ); 
  75. MS_Factory::load( 'MS_Rule_Page' ); 
  76. MS_Factory::load( 'MS_Rule_Post' ); 
  77. MS_Factory::load( 'MS_Rule_ReplaceLocation' ); 
  78. MS_Factory::load( 'MS_Rule_ReplaceMenu' ); 
  79. MS_Factory::load( 'MS_Rule_Shortcode' ); 
  80. MS_Factory::load( 'MS_Rule_Special' ); 
  81. MS_Factory::load( 'MS_Rule_Url' ); 
  82.  
  83. self::$prepared = true; 
  84.  
  85. /** 
  86. * Makes sure all rules are registered. 
  87. * 
  88. * @since 1.0.0 
  89. */ 
  90. static private function prepare() { 
  91. if ( ! self::$prepared ) { 
  92. // This will call prepare_class() above. 
  93. MS_Factory::load( 'MS_Model_Rule' ); 
  94.  
  95. /** 
  96. * Register meta-information on a rule 
  97. * 
  98. * @since 1.0.0 
  99. * @param string $id ID of the rule. 
  100. * @param string $class Name of the rule class, used to find Model-class. 
  101. * @param string $title Rule Title for display. 
  102. * @param int $priority Loading-priority (0 - 999), lower is earlier. 
  103. */ 
  104. static public function register_rule( $id, $class, $title, $priority = 0, $dripped = false ) { 
  105. if ( ! is_array( self::$rule_meta ) ) { 
  106. self::$rule_meta = array( 
  107. 'title' => array(),  
  108. 'class' => array(),  
  109. 'model_class' => array(),  
  110. 'order' => array(),  
  111. 'dripped' => array(),  
  112. ); 
  113.  
  114. self::$rule_meta['title'][ $id ] = $title; 
  115. self::$rule_meta['class'][ $id ] = $class; 
  116. self::$rule_meta['model_class'][ $id ] = $class . '_Model'; 
  117.  
  118. if ( $dripped ) { 
  119. self::$rule_meta['dripped'][] = $id; 
  120.  
  121. $priority = min( $priority, 999 ); 
  122. $priority = max( $priority, 0 ); 
  123.  
  124. $real_priority = $priority * 20; 
  125. while ( isset( self::$rule_meta['order'][ $real_priority ] ) ) { 
  126. $real_priority += 1; 
  127. self::$rule_meta['order'][ $real_priority ] = $id; 
  128.  
  129. /** 
  130. * Rule types. 
  131. * 
  132. * This array is ordered in the hierarchy way. 
  133. * First one has more priority than the last one. 
  134. * This hierarchy is used to determine access to Membership2. 
  135. * 
  136. * @since 1.0.0 
  137. * @return array $rule_types { 
  138. * @type in $priority The rule type priority in the execution sequence. 
  139. * @type string $rule_type The rule type. 
  140. * } 
  141. */ 
  142. public static function get_rule_types() { 
  143. static $Types = null; 
  144.  
  145. if ( null === $Types ) { 
  146. self::prepare(); 
  147.  
  148. $settings = MS_Factory::load( 'MS_Model_Settings' ); 
  149. $rule_types = self::$rule_meta['order']; 
  150.  
  151. $rule_types = apply_filters( 'ms_rule_get_rule_types', $rule_types ); 
  152. $rule_type = ksort( $rule_types ); 
  153.  
  154. $Types = $rule_types; 
  155.  
  156. return $Types; 
  157.  
  158. /** 
  159. * Rule types and respective classes. 
  160. * 
  161. * @since 1.0.0 
  162. * @return array { 
  163. * @type string $rule_type The rule type constant. 
  164. * @type string $class_name The rule type class. 
  165. * } 
  166. */ 
  167. public static function get_rule_type_classes() { 
  168. static $Rule_Classes = null; 
  169.  
  170. if ( null === $Rule_Classes ) { 
  171. self::prepare(); 
  172.  
  173. $Rule_Classes = self::$rule_meta['model_class']; 
  174.  
  175. $Rule_Classes = apply_filters( 
  176. 'ms_rule_get_rule_type_classes',  
  177. $Rule_Classes 
  178. ); 
  179.  
  180. return $Rule_Classes; 
  181.  
  182. /** 
  183. * Rule types and respective titles. 
  184. * 
  185. * @since 1.0.0 
  186. * @return array { 
  187. * @type string $rule_type The rule type constant. 
  188. * @type string $rule_title The rule title. 
  189. * } 
  190. */ 
  191. public static function get_rule_type_titles() { 
  192. static $Rule_Titles = null; 
  193.  
  194. if ( null === $Rule_Titles ) { 
  195. self::prepare(); 
  196.  
  197. $Rule_Titles = self::$rule_meta['title']; 
  198.  
  199. $Rule_Titles = apply_filters( 
  200. 'ms_rule_get_rule_type_titles',  
  201. $Rule_Titles 
  202. ); 
  203.  
  204. return $Rule_Titles; 
  205.  
  206. /** 
  207. * Dripped Rule types. 
  208. * 
  209. * Return only rule types with dripped rules. 
  210. * 
  211. * @since 1.0.0 
  212. * @return string[] $rule_type The rule type constant. 
  213. */ 
  214. public static function get_dripped_rule_types() { 
  215. static $Dripped_Rules = null; 
  216.  
  217. if ( null === $Dripped_Rules ) { 
  218. self::prepare(); 
  219.  
  220. $Dripped_Rules = self::$rule_meta['dripped']; 
  221.  
  222. $Dripped_Rules = apply_filters( 
  223. 'ms_rule_get_dripped_rule_types',  
  224. $Dripped_Rules 
  225. ); 
  226.  
  227. return $Dripped_Rules; 
  228.  
  229. /** 
  230. * Get dripped types. 
  231. * 
  232. * @since 1.0.0 
  233. * @return array { 
  234. * @type string $dripped_type The dripped type constant. 
  235. * @type string $dripped_type_desc The dripped type description. 
  236. * } 
  237. */ 
  238. public static function get_dripped_types() { 
  239. $dripped_types = array( 
  240. self::DRIPPED_TYPE_INSTANTLY => __( 'Instantly', 'membership2' ),  
  241. self::DRIPPED_TYPE_SPEC_DATE => __( 'On specific Date', 'membership2' ),  
  242. self::DRIPPED_TYPE_FROM_REGISTRATION => __( 'Relative to Subscription', 'membership2' ),  
  243. ); 
  244.  
  245. return apply_filters( 
  246. 'ms_rule_get_dripped_types',  
  247. $dripped_types 
  248. ); 
.