MS_Model_Rule

Membership Rule Management class.

Defined (1)

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

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