MS_Model_Membership

Membership model.

Defined (1)

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

/app/model/class-ms-model-membership.php  
  1. class MS_Model_Membership extends MS_Model_CustomPostType { 
  2.  
  3. /** 
  4. * Model custom post type. 
  5. * @since 1.0.0 
  6. * @internal self::get_post_type() to get this value! 
  7. * @var string $POST_TYPE 
  8. */ 
  9. protected static $POST_TYPE = 'ms_membership'; 
  10.  
  11. /** 
  12. * Membership type constant. 
  13. * @since 1.0.0 
  14. * @see $type $type property. 
  15. */ 
  16. const TYPE_STANDARD = 'simple'; 
  17.  
  18. /** 
  19. * Membership type constant. 
  20. * @since 1.0.0 
  21. * @see $type $type property. 
  22. */ 
  23. const TYPE_DRIPPED = 'dripped'; 
  24.  
  25. /** 
  26. * Membership type constant. 
  27. * System membership, hidden, created automatically. 
  28. * @since 1.0.0 
  29. * @see $type $type property. 
  30. */ 
  31. const TYPE_BASE = 'base'; 
  32.  
  33. /** 
  34. * Membership type constant. 
  35. * Guest membership, only one membership possible. 
  36. * @since 1.0.0 
  37. * @see $type $type property. 
  38. */ 
  39. const TYPE_GUEST = 'guest'; 
  40.  
  41. /** 
  42. * Membership type constant. 
  43. * User membership, only one membership possible. 
  44. * @since 1.0.0 
  45. * @see $type $type property. 
  46. */ 
  47. const TYPE_USER = 'user'; 
  48.  
  49. /** 
  50. * Membership payment type constants. 
  51. * @since 1.0.0 
  52. * @see $payment_type $payment_type property. 
  53. */ 
  54. const PAYMENT_TYPE_PERMANENT = 'permanent'; 
  55.  
  56. /** 
  57. * Membership payment type constants. 
  58. * @since 1.0.0 
  59. * @see $payment_type $payment_type property. 
  60. */ 
  61. const PAYMENT_TYPE_FINITE = 'finite'; 
  62.  
  63. /** 
  64. * Membership payment type constants. 
  65. * @since 1.0.0 
  66. * @see $payment_type $payment_type property. 
  67. */ 
  68. const PAYMENT_TYPE_DATE_RANGE = 'date-range'; 
  69.  
  70. /** 
  71. * Membership payment type constants. 
  72. * The only type that auto-renews without asking the user! 
  73. * @since 1.0.0 
  74. * @see $payment_type $payment_type property. 
  75. */ 
  76. const PAYMENT_TYPE_RECURRING = 'recurring'; 
  77.  
  78. /** 
  79. * Membership type. 
  80. * Default is TYPE_STANDARD. 
  81. * @since 1.0.0 
  82. * @var string $type 
  83. */ 
  84. protected $type = self::TYPE_STANDARD; 
  85.  
  86. /** 
  87. * Membership payment type. 
  88. * Default is PAYMENT_TYPE_PERMANENT. 
  89. * @since 1.0.0 
  90. * @var string $payment_type 
  91. */ 
  92. protected $payment_type = self::PAYMENT_TYPE_PERMANENT; 
  93.  
  94. /** 
  95. * Membership active status. 
  96. * By default a new membership is active. 
  97. * @since 1.0.0 
  98. * @var bool $active 
  99. */ 
  100. protected $active = true; 
  101.  
  102. /** 
  103. * Membership private status. 
  104. * Private means that the membership will not be displayed on the default 
  105. * registration page and membership list. 
  106. * Users can still stubscribe to the membership via the shortcode 
  107. * [ms-membership-buy] or by otherwise reaching the subscription URL. 
  108. * @since 1.0.0 
  109. * @var bool $private 
  110. */ 
  111. protected $private = false; 
  112.  
  113. /** 
  114. * A priority value that is used to determine the effective override 
  115. * settings if a user has multiple memberships. 
  116. * @since 1.0.1.0 
  117. * @var int $priority 
  118. */ 
  119. protected $priority = 0; 
  120.  
  121. /** 
  122. * Membership free status. 
  123. * @since 1.0.0 
  124. * @var bool $free. 
  125. */ 
  126. protected $is_free = false; 
  127.  
  128. /** 
  129. * Membership price. 
  130. * @since 1.0.0 
  131. * @var float $price. 
  132. */ 
  133. protected $price = 0; 
  134.  
  135. /** 
  136. * A list of disabled gateways. 
  137. * @since 1.0.0 
  138. * @var array $disabled_gateways. 
  139. */ 
  140. protected $disabled_gateways = array(); 
  141.  
  142. /** 
  143. * Membership period for finite access. 
  144. * Used for payment_type PAYMENT_TYPE_FINITE. 
  145. * @since 1.0.0 
  146. * @see $payment_type $payment_type property. 
  147. * @var array $period { 
  148. * @type int $period_unit The period of time quantity. 
  149. * @type string $period_type The period type (days, weeks, months, years). 
  150. * } 
  151. */ 
  152. protected $period = array( 'period_unit' => 1, 'period_type' => 'days' ); 
  153.  
  154. /** 
  155. * Membership payment recurring period cycle. 
  156. * Used for the payment_type PAYMENT_TYPE_RECURRING. 
  157. * @since 1.0.0 
  158. * @see $payment_type $payment_type property. 
  159. * @var array $pay_cycle_period {@see $period $period property}. 
  160. */ 
  161. protected $pay_cycle_period = array( 'period_unit' => 1, 'period_type' => 'days' ); 
  162.  
  163. /** 
  164. * Defines how many payments are made before the membership ends. 
  165. * Used for the payment_type PAYMENT_TYPE_RECURRING. 
  166. * @since 1.0.0 
  167. * @see $payment_type $payment_type property. 
  168. * @var int 
  169. */ 
  170. protected $pay_cycle_repetitions = 0; 
  171.  
  172. /** 
  173. * Membership start date for date range payment type. 
  174. * Used for the payment_type PAYMENT_TYPE_DATE_RANGE. 
  175. * @since 1.0.0 
  176. * @see $payment_type $payment_type property. 
  177. * @var string The membership start date. 
  178. */ 
  179. protected $period_date_start = ''; 
  180.  
  181. /** 
  182. * Membership end date for date range payment type. 
  183. * Used for the payment_type PAYMENT_TYPE_DATE_RANGE. 
  184. * @since 1.0.0 
  185. * @see $payment_type $payment_type property. 
  186. * @var string The membership end date. 
  187. */ 
  188. protected $period_date_end = ''; 
  189.  
  190. /** 
  191. * Membership trial period enabled indicator. 
  192. * Requires the Trial Period Add-on to work. 
  193. * @since 1.0.0 
  194. * @var bool $trial_period_enabled. 
  195. */ 
  196. protected $trial_period_enabled = false; 
  197.  
  198. /** 
  199. * Membership trial price value. 
  200. * Requires the Trial Period Add-on to work. 
  201. * @since 1.0.0 
  202. * @internal This property has no effect yet. 
  203. * @var float $trial_price. 
  204. */ 
  205. protected $trial_price = 0; 
  206.  
  207. /** 
  208. * Membership trial period. 
  209. * @since 1.0.0 
  210. * @var array $trial_period {@see $period $period property}. 
  211. */ 
  212. protected $trial_period = array( 'period_unit' => 1, 'period_type' => 'days' ); 
  213.  
  214. /** 
  215. * Move to Membership when the current one expires. 
  216. * After current membership expire move to the indicated membership_id. 
  217. * This membership is assigned when the current membership expires. 
  218. * @see MS_Model_Relationship::check_membership_status() 
  219. * @since 1.0.0 
  220. * @var int $on_end_membership_id. 
  221. */ 
  222. protected $on_end_membership_id = 0; 
  223.  
  224. /** 
  225. * Membership setup completed flag. 
  226. * We need this to determine if payment options of the membership are edited 
  227. * the first time during the setup assistant, or later via the membership 
  228. * list. 
  229. * @since 1.0.0 
  230. * @internal 
  231. * @var bool $is_setup_completed. 
  232. */ 
  233. protected $is_setup_completed = false; 
  234.  
  235. /** 
  236. * Where the data came from. Can only be changed by data import tool. 
  237. * @since 1.0.0 
  238. * @internal 
  239. * @var string 
  240. */ 
  241. protected $source = ''; 
  242.  
  243. /** 
  244. * Relevant for imported items. This is the ID that was used by the import 
  245. * source. 
  246. * @since 1.0.1.0 
  247. * @internal 
  248. * @var string 
  249. */ 
  250. protected $source_id = ''; 
  251.  
  252. /** 
  253. * Membership composite Rules. 
  254. * These are protection rules for this membership only. 
  255. * Network-wide mode: The rules stored in here are the rules that apply 
  256. * to the currently selected site in the network! 
  257. * Example: 
  258. * When the network has 10 sites then $rule_values will have 10 "page" rules 
  259. * which are stored as "1:page", "2:page", ... 
  260. * However, the $_rules property will only have ONE "page" rule, and that's 
  261. * the one for the currently visible site! 
  262. * @since 1.0.0 
  263. * @internal 
  264. * @var array MS_Rule[]. 
  265. */ 
  266. protected $_rules = array(); 
  267.  
  268. /** 
  269. * Only used for serialization of the membership. 
  270. * @see __sleep() 
  271. * @since 1.0.0 
  272. * @internal 
  273. * @var array 
  274. */ 
  275. protected $rule_values = array(); 
  276.  
  277. /** 
  278. * Defines which members can NOT subscribe to the current membership. 
  279. * @since 1.0.1.0 
  280. * @internal 
  281. * @var array 
  282. */ 
  283. protected $update_denied = array(); 
  284.  
  285. /** 
  286. * Defines if the current membership replaces other memberships on 
  287. * subscription. 
  288. * @since 1.0.1.0 
  289. * @internal 
  290. * @var array 
  291. */ 
  292. protected $update_replace = array(); 
  293.  
  294. /** 
  295. * Used in simulation mode explaining why a page is allowed or denied. 
  296. * @since 1.0.0 
  297. * @internal 
  298. * @var array 
  299. */ 
  300. public $_access_reason = array(); 
  301.  
  302. /** 
  303. * Similar to $_access_reason, but only contains the rules that denied page 
  304. * access. 
  305. * @since 1.0.0 
  306. * @internal 
  307. * @var array 
  308. */ 
  309. public $_deny_rule = array(); 
  310.  
  311. /** 
  312. * Similar to $_access_reason, but only contains the rules that allowed page 
  313. * access. 
  314. * @since 1.0.0 
  315. * @internal 
  316. * @var array 
  317. */ 
  318. public $_allow_rule = array(); 
  319.  
  320. /** 
  321. * Stores the subscription-ID of the parent object. 
  322. * This value will only have a value when the Membership is loaded within 
  323. * the context of a subscription. 
  324. * @since 1.0.0 
  325. * @var int 
  326. */ 
  327. protected $subscription_id = 0; 
  328.  
  329. /** 
  330. * This property is used to build the signup list (shortcode). 
  331. * It's a temporary value that is not saved to database. 
  332. * @since 1.0.1.0 
  333. * @internal 
  334. * @var array 
  335. */ 
  336. public $_move_from = array(); 
  337.  
  338.  
  339. // 
  340. // 
  341. // 
  342. // -------------------------------------------------------------- COLLECTION 
  343.  
  344.  
  345. /** 
  346. * Returns the post-type of the current object. 
  347. * @since 1.0.0 
  348. * @api 
  349. * @return string The post-type name. 
  350. */ 
  351. public static function get_post_type() { 
  352. return parent::_post_type( self::$POST_TYPE ); 
  353.  
  354. /** 
  355. * Get custom register post type args for this model. 
  356. * @since 1.0.0 
  357. * @internal 
  358. */ 
  359. public static function get_register_post_type_args() { 
  360. $args = array( 
  361. 'label' => __( 'Membership2 Memberships', 'membership2' ),  
  362. 'description' => __( 'Memberships user can join to.', 'membership2' ),  
  363. 'show_ui' => false,  
  364. 'show_in_menu' => false,  
  365. 'menu_position' => 70, // below Users 
  366. 'menu_icon' => 'dashicons-lock',  
  367. 'public' => true,  
  368. 'has_archive' => false,  
  369. 'publicly_queryable' => false,  
  370. 'supports' => false,  
  371. 'hierarchical' => false,  
  372. ); 
  373.  
  374. return apply_filters( 
  375. 'ms_customposttype_register_args',  
  376. $args,  
  377. self::get_post_type() 
  378. ); 
  379.  
  380. /** 
  381. * Get membership types. 
  382. * @since 1.0.0 
  383. * @internal 
  384. * @return array { 
  385. * Returns array of $type => $title. 
  386. * @type string $type The membership type 
  387. * @type string $title The membership type title 
  388. * } 
  389. */ 
  390. static public function get_types() { 
  391. $types = array( 
  392. self::TYPE_STANDARD => __( 'Standard Membership', 'membership2' ),  
  393. self::TYPE_DRIPPED => __( 'Dripped Content Membership', 'membership2' ),  
  394. self::TYPE_GUEST => __( 'Guest Membership', 'membership2' ),  
  395. self::TYPE_USER => __( 'Default Membership', 'membership2' ),  
  396. self::TYPE_BASE => __( 'System Membership', 'membership2' ),  
  397. ); 
  398.  
  399. return apply_filters( 'ms_model_membership_get_types', $types ); 
  400.  
  401. /** 
  402. * Get membership payment types. 
  403. * @since 1.0.0 
  404. * @internal 
  405. * @return array { 
  406. * Returns array of $type => $title. 
  407. * @type string $type The membership payment type 
  408. * @type string $title The membership payment type title 
  409. * } 
  410. */ 
  411. public static function get_payment_types( $type = 'paid' ) { 
  412. if ( 'free' == $type ) { 
  413. $payment_types = array( 
  414. self::PAYMENT_TYPE_PERMANENT => __( 'Permanent access', 'membership2' ),  
  415. self::PAYMENT_TYPE_FINITE => __( 'Finite access', 'membership2' ),  
  416. self::PAYMENT_TYPE_DATE_RANGE => __( 'Date range access', 'membership2' ),  
  417. ); 
  418. } else { 
  419. $payment_types = array( 
  420. self::PAYMENT_TYPE_PERMANENT => __( 'One payment for permanent access', 'membership2' ),  
  421. self::PAYMENT_TYPE_FINITE => __( 'One payment for finite access', 'membership2' ),  
  422. self::PAYMENT_TYPE_DATE_RANGE => __( 'One payment for date range access', 'membership2' ),  
  423. self::PAYMENT_TYPE_RECURRING => __( 'Recurring payments', 'membership2' ),  
  424. ); 
  425.  
  426. return apply_filters( 
  427. 'ms_model_membership_get_payment_types',  
  428. $payment_types,  
  429. $type 
  430. ); 
  431.  
  432. /** 
  433. * Get available Memberships count. 
  434. * @since 1.0.0 
  435. * @internal 
  436. * @param $args The query post args 
  437. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  438. * @return int The membership count. 
  439. */ 
  440. public static function get_membership_count( $args = null ) { 
  441. $ids = self::get_membership_ids( $args ); 
  442. $count = count( $ids ); 
  443.  
  444. return apply_filters( 
  445. 'ms_model_membership_get_membership_count',  
  446. $count,  
  447. $args 
  448. ); 
  449.  
  450. /** 
  451. * Find out if the installation has at least one paid membership 
  452. * @since 1.0.0 
  453. * @internal 
  454. * @return bool 
  455. */ 
  456. public static function have_paid_membership() { 
  457. static $Have_Paid = null; 
  458.  
  459. if ( null === $Have_Paid ) { 
  460. global $wpdb; 
  461. // Using a custom WPDB query because building the meta-query is more 
  462. // complex than really required here... 
  463. $sql = " 
  464. SELECT COUNT( 1 ) 
  465. FROM {$wpdb->posts} p 
  466. INNER JOIN {$wpdb->postmeta} priv ON priv.post_id = p.ID AND priv.meta_key = %s 
  467. INNER JOIN {$wpdb->postmeta} free ON free.post_id = p.ID AND free.meta_key = %s 
  468. INNER JOIN {$wpdb->postmeta} pric ON pric.post_id = p.ID AND pric.meta_key = %s 
  469. INNER JOIN {$wpdb->postmeta} acti ON acti.post_id = p.ID AND acti.meta_key = %s 
  470. WHERE 
  471. p.post_type = %s 
  472. AND priv.meta_value != '1' 
  473. AND acti.meta_value = '1' 
  474. AND NOT ( 
  475. free.meta_value = '1' 
  476. OR pric.meta_value = '0' 
  477. "; 
  478.  
  479. $sql = $wpdb->prepare( 
  480. $sql,  
  481. 'private', // INNER JOIN 
  482. 'is_free', // INNER JOIN 
  483. 'price', // INNER JOIN 
  484. 'active', // INNER JOIN 
  485. self::get_post_type() // WHERE condition 
  486. ); 
  487.  
  488. $res = $wpdb->get_var( $sql ); 
  489.  
  490. $Have_Paid = apply_filters( 
  491. 'ms_model_membership_have_paid_membership',  
  492. intval( $res ) > 0 
  493. ); 
  494.  
  495. return $Have_Paid; 
  496.  
  497. /** 
  498. * Get WP_Query object arguments. 
  499. * Default search arguments for this custom post_type. 
  500. * @since 1.0.0 
  501. * @internal 
  502. * @param $args The query post args 
  503. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  504. * @return array $args The parsed args. 
  505. */ 
  506. public static function get_query_args( $args = null ) { 
  507. $defaults = apply_filters( 
  508. 'ms_model_membership_get_query_args_defaults',  
  509. array( 
  510. 'post_type' => self::get_post_type(),  
  511. 'order' => 'ASC',  
  512. 'orderby' => 'menu_order',  
  513. 'post_status' => 'any',  
  514. 'post_per_page' => -1,  
  515. 'nopaging' => true,  
  516. 'include_base' => false,  
  517. 'include_guest' => true,  
  518. ); 
  519.  
  520. $args = wp_parse_args( $args, $defaults ); 
  521.  
  522. if ( isset( $args['active'] ) ) { 
  523. $args['meta_query']['active'] = array( 
  524. 'key' => 'active',  
  525. 'value' => 1,  
  526. ); 
  527.  
  528. if ( lib3()->is_true( $args['active'] ) ) { 
  529. $args['meta_query']['active']['compare'] = '='; 
  530. } else { 
  531. $args['meta_query']['active']['compare'] = '!='; 
  532.  
  533. if ( ! lib3()->is_true( $args['include_base'] ) ) { 
  534. $args['meta_query']['base'] = array( 
  535. 'key' => 'type',  
  536. 'value' => self::TYPE_BASE,  
  537. 'compare' => '!=',  
  538. ); 
  539.  
  540. if ( ! lib3()->is_true( $args['include_guest'] ) ) { 
  541. $args['meta_query']['guest'] = array( 
  542. 'key' => 'type',  
  543. 'value' => self::TYPE_GUEST,  
  544. 'compare' => '!=',  
  545. ); 
  546. $args['meta_query']['user'] = array( 
  547. 'key' => 'type',  
  548. 'value' => self::TYPE_USER,  
  549. 'compare' => '!=',  
  550. ); 
  551.  
  552. return apply_filters( 
  553. 'ms_model_membership_get_query_args',  
  554. $args,  
  555. $defaults 
  556. ); 
  557.  
  558. /** 
  559. * Returns a list of Membership IDs that match the given WP_Query arguments. 
  560. * @since 1.0.1.0 
  561. * @internal 
  562. * @param $args The query post args. 
  563. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  564. * @return array A list of membership IDs. 
  565. */ 
  566. static public function get_membership_ids( $args = null ) { 
  567. static $Membership_IDs = array(); 
  568. $args = self::get_query_args( $args ); 
  569. $key = md5( json_encode( $args ) ); 
  570.  
  571. if ( ! isset( $Membership_IDs[$key] ) ) { 
  572. $Membership_IDs[$key] = array(); 
  573.  
  574. MS_Factory::select_blog(); 
  575. $query = new WP_Query( $args ); 
  576. $items = $query->posts; 
  577. MS_Factory::revert_blog(); 
  578.  
  579. /** 
  580. * We only cache the IDs to avoid re-querying the database. 
  581. * The positive side effect is, that the memory used by the 
  582. * membership list will be freed again after the calling function 
  583. * is done with it. 
  584. * If we cache the whole list here, it would not occupy memory for 
  585. * the whole request duration which can cause memory_limit errors. 
  586. * @see MS_Model_Relationship::get_subscriptions() 
  587. */ 
  588. foreach ( $items as $item ) { 
  589. $Membership_IDs[$key][] = $item->ID; 
  590.  
  591. return apply_filters( 
  592. 'ms_model_membership_get_membership_ids',  
  593. $Membership_IDs[$key],  
  594. $args 
  595. ); 
  596.  
  597. /** 
  598. * Get Memberships models. 
  599. * When no $args are specified then all memberships except the base 
  600. * membership will be returned. 
  601. * To include the base membership use: 
  602. * $args = array( 'include_base' => 1 ) 
  603. * To exclude the guest membership use: 
  604. * $args = array( 'include_guest' => 0 ) 
  605. * @since 1.0.0 
  606. * @internal 
  607. * @param $args The query post args 
  608. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  609. * @return MS_Model_Membership[] The selected memberships. 
  610. */ 
  611. static public function get_memberships( $args = null ) { 
  612. $ids = self::get_membership_ids( $args ); 
  613. $memberships = array(); 
  614.  
  615. foreach ( $ids as $id ) { 
  616. $memberships[] = MS_Factory::load( 
  617. 'MS_Model_Membership',  
  618. $id 
  619. ); 
  620.  
  621. return apply_filters( 
  622. 'ms_model_membership_get_memberships',  
  623. $memberships,  
  624. $args 
  625. ); 
  626.  
  627. /** 
  628. * Returns a list of the dripped memberships. 
  629. * @since 1.0.0 
  630. * @internal 
  631. * @param $args The query post args 
  632. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  633. * @return MS_Model_Membership[] The selected memberships. 
  634. */ 
  635. static public function get_dripped_memberships( $args = null ) { 
  636. $drip_args = array( 
  637. 'meta_query' => array( 
  638. array( 
  639. 'key' => 'type',  
  640. 'value' => self::TYPE_DRIPPED,  
  641. ),  
  642. ),  
  643. ); 
  644.  
  645. $drip_args = wp_parse_args( $drip_args, $args ); 
  646. $memberships = self::get_memberships( $drip_args ); 
  647.  
  648. return apply_filters( 
  649. 'ms_model_membership_get_dripped_memberships',  
  650. $memberships,  
  651. $args 
  652. ); 
  653.  
  654. /** 
  655. * Get membership names. 
  656. * Note that this function returns an array with membership_id as index,  
  657. * while the function get_memberships() returns an array with sort-order as 
  658. * index. 
  659. * @since 1.0.0 
  660. * @internal 
  661. * @param $args The query post args 
  662. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  663. * @param bool $include_base_membership Include base membership from the list. 
  664. * @return array { 
  665. * Returns array of $membership_id => $name 
  666. * @type int $membership_id The membership Id. 
  667. * @type string $name The membership name; 
  668. * } 
  669. */ 
  670. public static function get_membership_names( $args = null ) { 
  671. $items = self::get_memberships( $args ); 
  672.  
  673. $memberships = array(); 
  674. foreach ( $items as $item ) { 
  675. $memberships[ $item->id ] = $item->name; 
  676.  
  677. return apply_filters( 
  678. 'ms_model_membership_get_membership_names',  
  679. $memberships,  
  680. $args 
  681. ); 
  682.  
  683. /** 
  684. * Get membership eligible to signup. 
  685. * This function also checks for membership permissions and only display 
  686. * memberships that are available for the current member. 
  687. * @since 1.0.0 
  688. * @internal 
  689. * @param $args The query post args 
  690. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  691. * @param int[] $exclude_ids Optional. The membership ids to exclude. 
  692. * @param bool $only_names Optional. Return only array { 
  693. * @type int $membership_id The membership ID. 
  694. * @type string $membership_name The membership name. 
  695. * } 
  696. * @param bool $include_private If private memberships should be listed 
  697. * This param is only recognized in the admin section so admins can 
  698. * manually assign a private membership to a user. 
  699. * @return array Returns sorted array of memberships. Sorted by priority. 
  700. */ 
  701. public static function get_signup_membership_list( 
  702. $args = null,  
  703. $exclude_ids = null,  
  704. $only_names = false,  
  705. $include_private = false 
  706. ) { 
  707. $not_in = array(); 
  708. if ( is_array( $exclude_ids ) ) { 
  709. $not_in = $exclude_ids; 
  710. $args['post__not_in'] = array_unique( $not_in ); 
  711. $member = MS_Model_Member::get_current_member(); 
  712.  
  713. if ( ! is_admin() ) { 
  714. $include_private = false; 
  715. // List of private memberships (they are grouped in own array). 
  716. $private = array(); 
  717.  
  718. // Retrieve memberships user is not part of, using selected args 
  719. $memberships = self::get_memberships( $args ); 
  720.  
  721. // Check the upgrade-paths settings 
  722. foreach ( $memberships as $key => $ms ) { 
  723. if ( $ms->is_system() ) { 
  724. unset( $memberships[$key] ); 
  725. } elseif ( ! $member->can_subscribe_to( $ms->id ) ) { 
  726. unset( $memberships[$key] ); 
  727.  
  728. // Filter memberships based on status. 
  729. $order = array(); 
  730. foreach ( $memberships as $key => $membership ) { 
  731. // Remove if not active. 
  732. if ( ! $membership->active ) { 
  733. unset( $memberships[ $key ] ); 
  734. continue; 
  735.  
  736. if ( $membership->private ) { 
  737. if ( $include_private ) { 
  738. // Move the private memberships to a option-group. 
  739. $private[ $key ] = $memberships[ $key ]; 
  740. unset( $memberships[ $key ] ); 
  741. continue; 
  742.  
  743. if ( $only_names ) { 
  744. $ms_names = array(); 
  745. foreach ( $memberships as $ms ) { 
  746. $ms_names[ $ms->id ] = $ms->name; 
  747. if ( ! empty( $private ) ) { 
  748. $priv_key = __( 'Private Memberships', 'membership2' ); 
  749. $ms_names[ $priv_key ] = array(); 
  750. foreach ( $private as $ms ) { 
  751. $ms_names[ $priv_key ][ $ms->id ] = $ms->name; 
  752. $memberships = $ms_names; 
  753. } else { 
  754. $memberships = array_merge( $memberships, $private ); 
  755.  
  756. // Sort memberships by priority. 
  757. usort( 
  758. $memberships,  
  759. array( __CLASS__, 'sort_by_priority' ) 
  760. ); 
  761.  
  762. return apply_filters( 
  763. 'ms_model_membership_get_signup_membership_list',  
  764. $memberships,  
  765. $exclude_ids,  
  766. $only_names 
  767. ); 
  768.  
  769. /** 
  770. * Sort function used as second param by `uasort()` to sort a membership 
  771. * list by priority. 
  772. * Memberships with equal priority are sorted alphabeically. 
  773. * @since 1.0.1.0 
  774. * @param MS_Model_Membership $a 
  775. * @param MS_Model_Membership $b 
  776. * @return int -1: a < b | 0: a = b | +1: a > b 
  777. */ 
  778. static public function sort_by_priority( $a, $b ) { 
  779. if ( $a->priority == $b->priority ) { 
  780. return $a->name < $b->name ? -1 : 1; 
  781. } else { 
  782. return $a->priority - $b->priority; 
  783.  
  784. /** 
  785. * Verify if membership is valid. 
  786. * Verify if membership was not deleted, trying to load from DB. 
  787. * @since 1.0.0 
  788. * @api 
  789. * @param int $membership_id The membership id to verify. 
  790. * @return bool True if is valid. 
  791. */ 
  792. public static function is_valid_membership( $membership_id ) { 
  793. $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id, '_is_valid_' ); 
  794. $valid = ( $membership->id > 0 ); 
  795.  
  796. return apply_filters( 
  797. 'ms_model_membership_is_valid_membership',  
  798. $valid,  
  799. $membership_id 
  800. ); 
  801.  
  802. /** 
  803. * Get Membership2 membership. 
  804. * Create a new membership if membership does not exist. 
  805. * @since 1.0.0 
  806. * @internal 
  807. * @param string $type The membership to load [protected_content|role] 
  808. * @param book $create_missing If set to false then missing special 
  809. * memberships are not created. 
  810. * @return MS_Model_Membership The Membership2. 
  811. */ 
  812. public static function _get_system_membership( $type, $create_missing = true ) { 
  813. static $Special_Membership = array(); 
  814. $comp_key = $type; 
  815. $membership = false; 
  816.  
  817. if ( ! isset( $Special_Membership[$comp_key] ) ) { 
  818. $membership = false; 
  819. global $wpdb; 
  820.  
  821. MS_Factory::select_blog(); 
  822. /** 
  823. * We are using a normal SQL query instead of using the WP_Query object 
  824. * here, because the WP_Query object does some strange things sometimes: 
  825. * In some cases new Membership2 memberships were created when a 
  826. * guest accessed the page. 
  827. * By using a manual query we are very certain that only one 
  828. * base-membership exists on the database. 
  829. */ 
  830. $sql = " 
  831. SELECT ID 
  832. FROM {$wpdb->posts} p 
  833. INNER JOIN {$wpdb->postmeta} m_type ON m_type.post_id = p.ID 
  834. WHERE 
  835. p.post_type = %s 
  836. AND m_type.meta_key = %s 
  837. AND m_type.meta_value = %s 
  838. "; 
  839. $values = array( 
  840. self::get_post_type(),  
  841. 'type',  
  842. $type,  
  843. ); 
  844.  
  845. $sql = $wpdb->prepare( $sql, $values ); 
  846. $item = $wpdb->get_results( $sql ); 
  847. $base = array_shift( $item ); // Remove the base membership from the results 
  848. MS_Factory::revert_blog(); 
  849.  
  850. if ( ! empty( $base ) ) { 
  851. $membership = MS_Factory::load( 'MS_Model_Membership', $base->ID ); 
  852. } elseif ( $create_missing ) { 
  853. $names = self::get_types(); 
  854.  
  855. $description = __( 'Membership2 Core Membership', 'membership2' ); 
  856. $membership = MS_Factory::create( 'MS_Model_Membership' ); 
  857. $membership->name = $names[$type]; 
  858. $membership->title = $names[$type]; 
  859. $membership->description = $description; 
  860. $membership->type = $type; 
  861. $membership->save(); 
  862.  
  863. $Special_Membership[$comp_key] = $membership; 
  864.  
  865. return apply_filters( 
  866. 'ms_model_membership_get_system_membership',  
  867. $Special_Membership[$comp_key],  
  868. $type 
  869. ); 
  870.  
  871. /** 
  872. * Get Membership2 base membership. 
  873. * Create a new membership if membership does not exist. 
  874. * This is an internal membership which is never displayed anywhere. 
  875. * @since 1.0.0 
  876. * @api 
  877. * @return MS_Model_Membership The base membership. 
  878. */ 
  879. public static function get_base() { 
  880. static $Base_Membership = null; 
  881.  
  882. if ( null === $Base_Membership ) { 
  883. $Base_Membership = self::_get_system_membership( 
  884. self::TYPE_BASE 
  885. ); 
  886.  
  887. foreach ( $Base_Membership->_rules as $key => $rule ) { 
  888. $Base_Membership->_rules[$key]->is_base_rule = true; 
  889.  
  890. return apply_filters( 
  891. 'ms_model_membership_get_base',  
  892. $Base_Membership 
  893. ); 
  894.  
  895. /** 
  896. * Get special membership that is assigned to all guests. 
  897. * Create a new membership if membership does not exist. 
  898. * @since 1.0.0 
  899. * @api 
  900. * @param string $role A WordPress user-role. 
  901. * @return MS_Model_Membership The guest membership. 
  902. */ 
  903. public static function get_guest() { 
  904. static $Guest_Membership = null; 
  905.  
  906. if ( null === $Guest_Membership ) { 
  907. $Guest_Membership = self::_get_system_membership( 
  908. self::TYPE_GUEST,  
  909. false // Don't create this membership automatically 
  910. ); 
  911.  
  912. if ( ! $Guest_Membership ) { 
  913. $Guest_Membership = MS_Factory::create( 'MS_Model_Membership' ); 
  914.  
  915. return apply_filters( 
  916. 'ms_model_membership_get_guest',  
  917. $Guest_Membership 
  918. ); 
  919.  
  920. /** 
  921. * Get default membership for all logged-in users that did not yet subscribe 
  922. * to any membership. 
  923. * Create a new membership if membership does not exist. 
  924. * @since 1.0.0 
  925. * @api 
  926. * @param string $role A WordPress user-role. 
  927. * @return MS_Model_Membership The guest membership. 
  928. */ 
  929. public static function get_user() { 
  930. static $User_Membership = null; 
  931.  
  932. if ( null === $User_Membership ) { 
  933. $User_Membership = self::_get_system_membership( 
  934. self::TYPE_USER,  
  935. false // Don't create this membership automatically 
  936. ); 
  937.  
  938. if ( ! $User_Membership ) { 
  939. $User_Membership = MS_Factory::create( 'MS_Model_Membership' ); 
  940.  
  941. return apply_filters( 
  942. 'ms_model_membership_get_user',  
  943. $User_Membership 
  944. ); 
  945.  
  946. /** 
  947. * Checks if the specified string is a valid Membership-Type identifier. 
  948. * @since 1.0.1.0 
  949. * @param string $type A string to check against all known membership types. 
  950. * @return bool True if the string is a valid type. 
  951. */ 
  952. static public function is_valid_type( $type ) { 
  953. switch ( $type ) { 
  954. case self::TYPE_BASE: 
  955. case self::TYPE_GUEST: 
  956. case self::TYPE_USER: 
  957. case self::TYPE_DRIPPED: 
  958. $result = true; 
  959. break; 
  960.  
  961. default: 
  962. $result = false; 
  963. break; 
  964.  
  965. return apply_filters( 
  966. 'ms_model_membership_is_valid_type',  
  967. $result,  
  968. $type 
  969. ); 
  970.  
  971.  
  972. // 
  973. // 
  974. // 
  975. // ------------------------------------------------------------- SINGLE ITEM 
  976.  
  977.  
  978. /** 
  979. * Returns a list of variables that should be included in serialization,  
  980. * i.e. these values are the only ones that are stored in DB 
  981. * @since 1.0.0 
  982. * @internal 
  983. * @return array 
  984. */ 
  985. public function __sleep() { 
  986. /** 
  987. * Rule values are pre-processd before saving... 
  988. * Note: $this->_rules only contains rules for the *current* site, so 
  989. * all rules that are serialized here get the current-site prefix. 
  990. * Rules for the other sites are already in the $this->rule_values 
  991. * array and were not de-serialized on page load. 
  992. */ 
  993. $this->rule_values = lib3()->array->get( $this->rule_values ); 
  994. foreach ( $this->_rules as $rule_type => $rule ) { 
  995. $key = MS_Rule::rule_key( $rule_type ); 
  996.  
  997. $this->rule_values[$key] = $rule->serialize(); 
  998. if ( empty( $this->rule_values[$key] ) ) { 
  999. unset( $this->rule_values[$key] ); 
  1000.  
  1001. return array( 
  1002. 'id',  
  1003. 'name',  
  1004. 'title',  
  1005. 'description',  
  1006. 'rule_values',  
  1007. 'type',  
  1008. 'payment_type',  
  1009. 'active',  
  1010. 'private',  
  1011. 'is_free',  
  1012. 'disabled_gateways',  
  1013. 'price',  
  1014. 'period',  
  1015. 'pay_cycle_period',  
  1016. 'pay_cycle_repetitions',  
  1017. 'period_date_start',  
  1018. 'period_date_end',  
  1019. 'trial_period_enabled',  
  1020. 'trial_price',  
  1021. 'trial_period',  
  1022. 'on_end_membership_id',  
  1023. 'is_setup_completed',  
  1024. 'source',  
  1025. 'source_id',  
  1026. 'custom_data',  
  1027. 'update_denied',  
  1028. 'update_replace',  
  1029. ); 
  1030.  
  1031. /** 
  1032. * Set rules membership_id before saving. 
  1033. * @since 1.0.0 
  1034. * @internal 
  1035. */ 
  1036. public function before_save() { 
  1037. parent::before_save(); 
  1038.  
  1039. foreach ( $this->_rules as $rule ) { 
  1040. $rule->membership_id = $this->id; 
  1041.  
  1042. /** 
  1043. * Save model and move the object to the singleton cache if required. 
  1044. * @since 1.0.0 
  1045. */ 
  1046. public function save() { 
  1047. parent::save(); 
  1048. parent::store_singleton(); 
  1049.  
  1050. /** 
  1051. * After the membership was saved to DB we make sure that it is published. 
  1052. * Network-wide mode: We are still in the switched blog (main site) so 
  1053. * there is no need to call MS_Factory::select_blog() in this function. 
  1054. * @since 1.0.0 
  1055. * @internal 
  1056. */ 
  1057. public function after_save() { 
  1058. // It is important! The Membership2 membership must be public 
  1059. // so that the membership options are available for guest users. 
  1060. wp_publish_post( $this->id ); 
  1061.  
  1062. /** 
  1063. * Save custom values in the wp_posts table. 
  1064. * @since 1.0.1.0 
  1065. * @internal 
  1066. */ 
  1067. public function save_post_data( $post ) { 
  1068. if ( $this->is_system() ) { 
  1069. $this->priority = 0; 
  1070. } elseif ( $this->priority < 1 ) { 
  1071. $this->priority = 1; 
  1072.  
  1073. $post['menu_order'] = $this->priority; 
  1074. return $post; 
  1075.  
  1076. /** 
  1077. * Load custom values from the wp_posts table. 
  1078. * @since 1.0.1.0 
  1079. * @internal 
  1080. */ 
  1081. public function load_post_data( $post ) { 
  1082. $this->priority = $post->menu_order; 
  1083.  
  1084. /** 
  1085. * Permanently delete the membership. 
  1086. * @since 1.0.0 
  1087. * @api 
  1088. * @return bool 
  1089. */ 
  1090. public function delete() { 
  1091. do_action( 'ms_model_membership_before_delete', $this ); 
  1092. $res = false; 
  1093.  
  1094. if ( $this->is_base() ) { 
  1095. throw new Exception( 
  1096. 'Can not delete the system membership.' 
  1097. ); 
  1098.  
  1099. if ( ! empty( $this->id ) ) { 
  1100. if ( $this->get_members_count() > 0 ) { 
  1101. $subscriptions = MS_Model_Relationship::get_subscriptions( 
  1102. array( 'membership_id' => $this->id ),  
  1103. true 
  1104. ); 
  1105.  
  1106. foreach ( $subscriptions as $subscription ) { 
  1107. $subscription->delete(); 
  1108.  
  1109. $res = ( false !== wp_delete_post( $this->id, true ) ); 
  1110.  
  1111. do_action( 'ms_model_membership_after_delete', $this, $res ); 
  1112. return $res; 
  1113.  
  1114. /** 
  1115. * Merge current rules to Membership2. 
  1116. * Assure the membership rules get updated whenever Membership2 is changed. 
  1117. * @since 1.0.0 
  1118. * @internal 
  1119. */ 
  1120. public function prepare_obj() { 
  1121. parent::prepare_obj(); 
  1122.  
  1123. if ( false !== strpos( $this->_factory_id, '_is_valid_' ) ) { 
  1124. // This object only checks if the item ID is valid. 
  1125. // No need to load any rules yet... 
  1126. return; 
  1127.  
  1128. foreach ( $this->rule_values as $key => $values ) { 
  1129. // Skip rules without any values. 
  1130. if ( empty( $values ) ) { continue; } 
  1131.  
  1132. // Network-wide: Only instanciate rules for the *current* site! 
  1133. if ( ! MS_Rule::is_current_site( $key ) ) { continue; } 
  1134.  
  1135. // Key could be "type" of "site:type" format. 
  1136. $rule_type = MS_Rule::rule_type( $key ); 
  1137.  
  1138. // At this point we have an empty rule-instance 
  1139. $rule = $this->get_rule( $rule_type ); 
  1140.  
  1141. // Now we populate that rule-instance with site-specific settings. 
  1142. $rule->populate( $values ); 
  1143.  
  1144. // validate rules using Membership2 rules 
  1145. if ( ! $this->is_base() && $this->is_valid() ) { 
  1146. $this->merge_protection_rules(); 
  1147.  
  1148. /** 
  1149. * Get current payment type description. 
  1150. * Description to show in the admin list table. 
  1151. * @since 1.0.0 
  1152. * @api 
  1153. * @return string The current payment type description. 
  1154. */ 
  1155. public function get_payment_type_desc() { 
  1156. $desc = __( 'N/A', 'membership2' ); 
  1157. $has_payment = ! $this->is_free(); 
  1158.  
  1159. switch ( $this->payment_type ) { 
  1160. case self::PAYMENT_TYPE_FINITE: 
  1161. if ( $has_payment ) { 
  1162. $desc = sprintf( 
  1163. __( 'Pay for %1$s', 'membership2' ),  
  1164. MS_Helper_Period::get_period_desc( $this->period, true ) 
  1165. ); 
  1166. } else { 
  1167. $desc = sprintf( 
  1168. __( 'Free for %1$s', 'membership2' ),  
  1169. MS_Helper_Period::get_period_desc( $this->period, true ) 
  1170. ); 
  1171. break; 
  1172.  
  1173. case self::PAYMENT_TYPE_DATE_RANGE: 
  1174. if ( $has_payment ) { 
  1175. $desc = sprintf( 
  1176. __( 'Pay from %1$s to %2$s', 'membership2' ),  
  1177. $this->period_date_start,  
  1178. $this->period_date_end 
  1179. ); 
  1180. } else { 
  1181. $desc = sprintf( 
  1182. __( 'Free from %1$s to %2$s', 'membership2' ),  
  1183. $this->period_date_start,  
  1184. $this->period_date_end 
  1185. ); 
  1186. break; 
  1187.  
  1188. case self::PAYMENT_TYPE_RECURRING: 
  1189. if ( $has_payment ) { 
  1190. $desc = __( 'Pay each %1$s', 'membership2' ); 
  1191. if ( 1 == $this->pay_cycle_repetitions ) { 
  1192. $desc = __( 'Single payment', 'membership2' ); 
  1193. } elseif ( $this->pay_cycle_repetitions > 1 ) { 
  1194. $desc .= ', ' . __( '%2$s payments', 'membership2' ); 
  1195. } else { 
  1196. $desc = __( 'Free access', 'membership2' ); 
  1197.  
  1198. $desc = sprintf( 
  1199. $desc,  
  1200. MS_Helper_Period::get_period_desc( $this->pay_cycle_period ),  
  1201. $this->pay_cycle_repetitions 
  1202. ); 
  1203. break; 
  1204.  
  1205. case self::PAYMENT_TYPE_PERMANENT: 
  1206. default: 
  1207. if ( $has_payment ) { 
  1208. $desc = __( 'Single payment', 'membership2' ); 
  1209. } else { 
  1210. $desc = __( 'Free access', 'membership2' ); 
  1211. break; 
  1212.  
  1213. return apply_filters( 
  1214. 'ms_model_membership_get_payment_type_desc',  
  1215. $desc,  
  1216. $this 
  1217. ); 
  1218.  
  1219. /** 
  1220. * Returns true if the current membership is free. 
  1221. * A membership is free when... 
  1222. * ... it is explicitely marked as "free" 
  1223. * ... the price is 0.00 
  1224. * ... it is a parent membership that cannot be signed up for 
  1225. * @since 1.0.0 
  1226. * @api 
  1227. * @return bool 
  1228. */ 
  1229. public function is_free() { 
  1230. $result = false; 
  1231.  
  1232. if ( $this->is_free ) { $result = true; } 
  1233. elseif ( 0 == (int) ($this->price * 100) ) { $result = true; } 
  1234.  
  1235. $result = apply_filters( 
  1236. 'ms_model_membership_is_free',  
  1237. $result,  
  1238. $this 
  1239. ); 
  1240.  
  1241. if ( $result && $this->is_free ) { 
  1242. $this->is_free = $result; 
  1243.  
  1244. return $result; 
  1245.  
  1246. /** 
  1247. * Returns true if this membership is eligable for trial period. 
  1248. * @since 1.0.1.0 
  1249. * @return bool 
  1250. */ 
  1251. public function has_trial() { 
  1252. $result = $this->trial_period_enabled; 
  1253.  
  1254. if ( $result ) { 
  1255. if ( ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_TRIAL ) ) { 
  1256. $result = false; 
  1257.  
  1258. return $result; 
  1259.  
  1260. /** 
  1261. * Returns the access flag, if a specific membership can subscribe to the 
  1262. * current membership. 
  1263. * A special value for $id is 'guest', which is used for all users without 
  1264. * a normal membership (is_system() type memberships are not normal) 
  1265. * @since 1.0.1.0 
  1266. * @param int|string $id A membership ID or the value 'guest'. 
  1267. * @return bool True if the specified membership can subscribe. 
  1268. */ 
  1269. public function update_allowed( $id ) { 
  1270. $denied = false; 
  1271.  
  1272. if ( isset( $this->update_denied[$id] ) ) { 
  1273. $denied = $this->update_denied[$id]; 
  1274.  
  1275. return ! $denied; 
  1276.  
  1277. /** 
  1278. * Returns the update-replacement flag, which defines if the OLD membership 
  1279. * should be cancelled during subscription. 
  1280. * This is used in cases where the new membership is an upgraded version of 
  1281. * the old membership and the user can only have one of both memberships. 
  1282. * @since 1.0.1.0 
  1283. * @param int|string $id A membership ID. 
  1284. * @return bool True if the specified membership should be cancelled. 
  1285. */ 
  1286. public function update_replaces( $id ) { 
  1287. $deny = false; 
  1288.  
  1289. if ( isset( $this->update_replace[$id] ) ) { 
  1290. $deny = $this->update_replace[$id]; 
  1291.  
  1292. return ! ! $deny; 
  1293.  
  1294. /** 
  1295. * Checks if a specific payment gateway is allowed for the current 
  1296. * membership. 
  1297. * @since 1.0.0 
  1298. * @param string $gateway_id The payment gateway ID. 
  1299. * @return bool 
  1300. */ 
  1301. public function can_use_gateway( $gateway_id ) { 
  1302. $result = true; 
  1303.  
  1304. $this->disabled_gateways = lib3()->array->get( $this->disabled_gateways ); 
  1305. if ( isset( $this->disabled_gateways[$gateway_id] ) ) { 
  1306. $state = $this->disabled_gateways[$gateway_id]; 
  1307. $result = ! lib3()->is_true( $state ); 
  1308.  
  1309. if ( $result ) { 
  1310. $gateway = MS_Model_Gateway::factory( $gateway_id ); 
  1311. $result = $gateway->payment_type_supported( $this ); 
  1312.  
  1313. $result = apply_filters( 
  1314. 'ms_model_membership_can_use_gateway',  
  1315. $result,  
  1316. $gateway_id,  
  1317. $this 
  1318. ); 
  1319. return $result; 
  1320.  
  1321. /** 
  1322. * Get protection Rule Model. 
  1323. * Note for network-wide mode: 
  1324. * In DB the rules for each site are stored in different objects. 
  1325. * When loading a membership we will always load 1 instance of each 
  1326. * rule_type, and this is the instance that belongs to the current site! 
  1327. * Instances for other sites are not accessible. 
  1328. * -> This is why we do not use/need a site_id or similar in this function. 
  1329. * @since 1.0.0 
  1330. * @api 
  1331. * @param string $rule_type The rule model type @see MS_Rule 
  1332. * @return MS_Rule The requested rule model. 
  1333. */ 
  1334. public function get_rule( $rule_type ) { 
  1335. if ( 'attachment' === $rule_type ) { 
  1336. $rule_type = MS_Rule_Media::RULE_ID; 
  1337.  
  1338. if ( ! isset( $this->_rules[ $rule_type ] ) 
  1339. || ! is_object( $this->_rules[ $rule_type ] ) // During plugin update. 
  1340. ) { 
  1341. // Create a new rule model object. 
  1342. $rule = MS_Rule::rule_factory( 
  1343. $rule_type,  
  1344. $this->id,  
  1345. $this->subscription_id 
  1346. ); 
  1347.  
  1348. $rule = apply_filters( 
  1349. 'ms_model_membership_get_rule',  
  1350. $rule,  
  1351. $rule_type,  
  1352. $this 
  1353. ); 
  1354.  
  1355. $this->_rules[ $rule_type ] = $rule; 
  1356. if ( ! is_array( $rule->rule_value ) ) { 
  1357. $rule->rule_value = array(); 
  1358.  
  1359. return $this->_rules[ $rule_type ]; 
  1360.  
  1361. /** 
  1362. * Set protection Rule Model. 
  1363. * Note for network-wide mode: 
  1364. * In DB the rules for each site are stored in different objects. 
  1365. * When loading a membership we will always load 1 instance of each 
  1366. * rule_type, and this is the instance that belongs to the current site! 
  1367. * Instances for other sites are not accessible. 
  1368. * -> This is why we do not use/need a site_id or similar in this function. 
  1369. * @since 1.0.0 
  1370. * @api 
  1371. * @param string The rule model type @see MS_Rule 
  1372. * @param MS_Rule $rule The protection rule to set. 
  1373. */ 
  1374. public function set_rule( $rule_type, $rule ) { 
  1375. $this->_rules[ $rule_type ] = apply_filters( 
  1376. 'ms_model_membership_set_rule',  
  1377. $rule,  
  1378. $rule_type,  
  1379. $this 
  1380. ); 
  1381.  
  1382. /** 
  1383. * Returns the unique HEX color for this membership. 
  1384. * The color is calculated from the membership-ID and therefore will never 
  1385. * change. 
  1386. * @since 1.0.0 
  1387. * @api 
  1388. * @return string Hex color, e.g. '#FFFFFF' 
  1389. */ 
  1390. public function get_color() { 
  1391. return MS_Helper_Utility::color_index( $this->type . $this->id ); 
  1392.  
  1393. /** 
  1394. * Returns a HTML tag that shows the membership name with the internal 
  1395. * membership color. 
  1396. * @since 1.0.0 
  1397. * @api 
  1398. */ 
  1399. public function get_name_tag() { 
  1400. $code = sprintf( 
  1401. '<span class="ms-membership" style="background:%2$s">%1$s</span>',  
  1402. esc_html( $this->name ),  
  1403. $this->get_color() 
  1404. ); 
  1405.  
  1406. return $code; 
  1407.  
  1408. /** 
  1409. * Echo a HTML tag that shows the membership name with the internal 
  1410. * membership color. 
  1411. * @since 1.0.0 
  1412. * @api 
  1413. */ 
  1414. public function name_tag() { 
  1415. echo $this->get_name_tag(); 
  1416.  
  1417. /** 
  1418. * Returns the parsed membership description for display. Shortcodes are 
  1419. * replaced and the content is filtered. 
  1420. * @since 1.0.1.2 
  1421. * @return string The parsed membership description. 
  1422. */ 
  1423. public function get_description() { 
  1424. $desc = apply_filters( 
  1425. 'ms_model_membership_get_description',  
  1426. $this->description,  
  1427. $this 
  1428. ); 
  1429.  
  1430. $desc = do_shortcode( wpautop( $desc ) ); 
  1431.  
  1432. return $desc; 
  1433.  
  1434. /** 
  1435. * Get current membership type description. 
  1436. * @since 1.0.0 
  1437. * @api 
  1438. * @return string The membership type description. 
  1439. */ 
  1440. public function get_type_description() { 
  1441. $types = self::get_types(); 
  1442. $desc = $types[ $this->type ]; 
  1443.  
  1444. return apply_filters( 
  1445. 'ms_model_membership_get_type_description',  
  1446. $desc,  
  1447. $this 
  1448. ); 
  1449.  
  1450. /** 
  1451. * Either creates or updates the value of a custom data field. 
  1452. * Note: Remember to prefix the $key with a unique string to prevent 
  1453. * conflicts with other plugins that also use this function. 
  1454. * @since 1.0.0 
  1455. * @api 
  1456. * @param string $key The field-key. 
  1457. * @param mixed $value The new value to assign to the field. 
  1458. */ 
  1459. public function set_custom_data( $key, $value ) { 
  1460. // Wrapper function, so this function shows up in API docs. 
  1461. parent::set_custom_data( $key, $value ); 
  1462.  
  1463. /** 
  1464. * Removes a custom data field from this object. 
  1465. * @since 1.0.0 
  1466. * @api 
  1467. * @param string $key The field-key. 
  1468. */ 
  1469. public function delete_custom_data( $key ) { 
  1470. // Wrapper function, so this function shows up in API docs. 
  1471. parent::delete_custom_data( $key ); 
  1472.  
  1473. /** 
  1474. * Returns the value of a custom data field. 
  1475. * @since 1.0.0 
  1476. * @api 
  1477. * @param string $key The field-key. 
  1478. * @return mixed The value that was previously assigned to the custom field 
  1479. * or false if no value was set for the field. 
  1480. */ 
  1481. public function get_custom_data( $key ) { 
  1482. // Wrapper function, so this function shows up in API docs. 
  1483. return parent::get_custom_data( $key ); 
  1484.  
  1485. /** 
  1486. * Merge Membership2 rules. 
  1487. * Merge every rule model with Membership2/visitor membership rules. 
  1488. * This ensure rules are consistent with Membership2 rules. 
  1489. * @since 1.0.0 
  1490. * @internal 
  1491. */ 
  1492. public function merge_protection_rules() { 
  1493. if ( $this->is_base() ) { 
  1494. // This is the visitor membership, no need to merge anything. 
  1495. return; 
  1496.  
  1497. $base_rules = self::get_base()->_rules; 
  1498.  
  1499. foreach ( $base_rules as $key => $base_rule ) { 
  1500. try { 
  1501. // Key could be "type" of "site:type" format. 
  1502. $rule_type = MS_Rule::rule_type( $key ); 
  1503.  
  1504. $rule = $this->get_rule( $rule_type ); 
  1505. $rule->protect_undefined_items( $base_rule, true ); 
  1506. $this->set_rule( $rule_type, $rule ); 
  1507. catch( Exception $e ) { 
  1508. MS_Helper_Debug::log( $e ); 
  1509.  
  1510. $this->_rules = apply_filters( 
  1511. 'ms_model_membership_merge_protection_rules',  
  1512. $this->_rules,  
  1513. $this 
  1514. ); 
  1515.  
  1516. /** 
  1517. * Get after membership expired options. 
  1518. * Memberships can be downgraded to the guest level protection. 
  1519. * @since 1.0.0 
  1520. * @api 
  1521. * @return array { 
  1522. * Returns array of $membership_id => $description. 
  1523. * @type int $membership_id The membership Id. 
  1524. * @type string $description The expired option description. 
  1525. * } 
  1526. */ 
  1527. public function get_after_ms_ends_options() { 
  1528. $options = array( 
  1529. 0 => __( 'Restrict access to Visitor-Level', 'membership2' ),  
  1530. ); 
  1531.  
  1532. $args = array( 
  1533. 'include_guest' => false,  
  1534. ); 
  1535. $options += $this->get_membership_names( $args ); 
  1536. unset( $options[$this->id] ); 
  1537.  
  1538. $label = __( 'Change to: %s', 'membership2' ); 
  1539. foreach ( $options as $id => $option ) { 
  1540. if ( $id > 0 ) { 
  1541. $options[$id] = sprintf( $label, $option ); 
  1542.  
  1543. return apply_filters( 
  1544. 'ms_model_membership_get_membership_names',  
  1545. $options,  
  1546. $this 
  1547. ); 
  1548.  
  1549. /** 
  1550. * Get a list of all subscriptions to this membership. 
  1551. * Note that this function will also return expired/cancelled subscriptions. 
  1552. * @since 1.0.1.0 
  1553. * @api 
  1554. * @return array All subscriptions. 
  1555. */ 
  1556. public function get_subscriptions() { 
  1557. $subscriptions = MS_Model_Relationship::get_subscriptions( 
  1558. array( 'membership_id' => $this->id ) 
  1559. ); 
  1560.  
  1561. return apply_filters( 
  1562. 'ms_model_membership_get_subscriptions',  
  1563. $subscriptions 
  1564. ); 
  1565.  
  1566. /** 
  1567. * Get members count of this membership. 
  1568. * This will also count members that have "cancelled" or "expired" 
  1569. * subscriptions but not "pending" or "deactivated". 
  1570. * To change this use the filter parameter: 
  1571. * $args = array( 'status' => 'all' ) 
  1572. * @since 1.0.0 
  1573. * @api 
  1574. * @param array $args The query post args 
  1575. * @return int The members count. 
  1576. */ 
  1577. public function get_members_count( $args = null ) { 
  1578. $args = wp_parse_args( 
  1579. array( 'membership_id' => $this->id ),  
  1580. $args 
  1581. ); 
  1582.  
  1583. $count = MS_Model_Relationship::get_subscription_count( $args ); 
  1584.  
  1585. return apply_filters( 
  1586. 'ms_model_membership_get_members_count',  
  1587. $count,  
  1588. $args,  
  1589. $this 
  1590. ); 
  1591.  
  1592. /** 
  1593. * Get members list of this membership. 
  1594. * This will also count members that have "cancelled" or "expired" 
  1595. * subscriptions but not "pending" or "deactivated". 
  1596. * To change this use the filter parameter: 
  1597. * $args = array( 'status' => 'all' ) 
  1598. * @since 1.0.1.0 
  1599. * @api 
  1600. * @param array $args The query post args 
  1601. * @return array List of members. 
  1602. */ 
  1603. public function get_members( $args = null ) { 
  1604. $args = wp_parse_args( 
  1605. array( 'membership_id' => $this->id ),  
  1606. $args 
  1607. ); 
  1608.  
  1609. // Get a list of subscriptions. 
  1610. $items = MS_Model_Relationship::get_subscriptions( $args ); 
  1611.  
  1612. // Get a list of members. 
  1613. $result = array(); 
  1614. foreach ( $items as $item ) { 
  1615. $result[$item->user_id] = $item->get_member(); 
  1616.  
  1617. return apply_filters( 
  1618. 'ms_model_membership_get_members',  
  1619. $result,  
  1620. $args,  
  1621. $this 
  1622. ); 
  1623.  
  1624. /** 
  1625. * Return membership has dripped content. 
  1626. * Verify post and page rules if there is a dripped content. 
  1627. * @since 1.0.0 
  1628. * @api 
  1629. * @return boolean 
  1630. */ 
  1631. public function has_dripped_content() { 
  1632. $has_dripped = false; 
  1633. $dripped = array( 'post', 'page' ); 
  1634.  
  1635. foreach ( $dripped as $rule_type ) { 
  1636. // using count() as !empty() never returned true 
  1637. if ( 0 < count( $this->get_rule( $rule_type )->dripped ) ) { 
  1638. $has_dripped = true; 
  1639.  
  1640. return apply_filters( 
  1641. 'ms_model_membership_has_dripped_content',  
  1642. $has_dripped,  
  1643. $this 
  1644. ); 
  1645.  
  1646. /** 
  1647. * Get protection rules sorted. 
  1648. * First one has priority over the last one. 
  1649. * These rules are used to determine access. 
  1650. * @since 1.0.0 
  1651. * @internal 
  1652. */ 
  1653. private function get_rules_hierarchy() { 
  1654. $rule_types = MS_Model_Rule::get_rule_types(); 
  1655. $rules = array(); 
  1656. $subscription = MS_Factory::load( 'MS_Model_Relationship', $this->subscription_id ); 
  1657.  
  1658. foreach ( $rule_types as $rule_type ) { 
  1659. $rule = $this->get_rule( $rule_type ); 
  1660.  
  1661. if ( $rule->rule_type != $rule_type ) { 
  1662. // This means that the $rule_type was not found... 
  1663. continue; 
  1664.  
  1665. // Sometimes the $subscription->id can be 0, which is intentional: 
  1666. // This is the case when the membership was auto-assigned to guest 
  1667. // or default membership. 
  1668. $rule->_subscription_id = $subscription->id; 
  1669.  
  1670. $rule->membership_id = $this->id; 
  1671. $rules[ $rule_type ] = $rule; 
  1672.  
  1673. return apply_filters( 
  1674. 'ms_model_membership_get_rules_hierarchy',  
  1675. $rules,  
  1676. $this 
  1677. ); 
  1678.  
  1679. /** 
  1680. * Mark membership setup as completed. 
  1681. * Only purpose of this flag is to display the correct update message to the 
  1682. * user: If setup_completed() returns true, then "Membership added" is 
  1683. * displayed, otherwise "Membership updated" 
  1684. * @since 1.0.0 
  1685. * @internal 
  1686. * @return bool $marked True in the first time setup is finished. 
  1687. */ 
  1688. public function setup_completed() { 
  1689. $marked = false; 
  1690.  
  1691. if ( ! $this->is_setup_completed ) { 
  1692. $this->is_setup_completed = true; 
  1693. $marked = true; 
  1694.  
  1695. return apply_filters( 
  1696. 'ms_model_membership_setup_completed',  
  1697. $marked,  
  1698. $this 
  1699. ); 
  1700.  
  1701. /** 
  1702. * Returns true if the membership the base membership. 
  1703. * @since 1.0.0 
  1704. * @see description of MS_Model_Membership::get_base() 
  1705. * @api 
  1706. * @return bool 
  1707. */ 
  1708. public function is_base( $type = null ) { 
  1709. if ( ! $type ) { $type = $this->type; } 
  1710. $res = $type == self::TYPE_BASE; 
  1711.  
  1712. return apply_filters( 
  1713. 'ms_model_membership_is_base',  
  1714. $res,  
  1715. $type 
  1716. ); 
  1717.  
  1718. /** 
  1719. * Returns true if the membership the guest membership. 
  1720. * @since 1.0.0 
  1721. * @see description of MS_Model_Membership::get_guest() 
  1722. * @api 
  1723. * @return bool 
  1724. */ 
  1725. public function is_guest( $type = null ) { 
  1726. if ( ! $type ) { $type = $this->type; } 
  1727. $res = $type == self::TYPE_GUEST; 
  1728.  
  1729. return apply_filters( 
  1730. 'ms_model_membership_is_guest',  
  1731. $res,  
  1732. $type 
  1733. ); 
  1734.  
  1735. /** 
  1736. * Returns true if the membership the user membership. 
  1737. * @since 1.0.0 
  1738. * @see description of MS_Model_Membership::get_user() 
  1739. * @api 
  1740. * @return bool 
  1741. */ 
  1742. public function is_user( $type = null ) { 
  1743. if ( ! $type ) { $type = $this->type; } 
  1744. $res = $type == self::TYPE_USER; 
  1745.  
  1746. return apply_filters( 
  1747. 'ms_model_membership_is_user',  
  1748. $res,  
  1749. $type 
  1750. ); 
  1751.  
  1752. /** 
  1753. * Returns true if the membership a dripped membership. 
  1754. * @since 1.0.0 
  1755. * @api 
  1756. * @return bool 
  1757. */ 
  1758. public function is_dripped( $type = null ) { 
  1759. if ( ! $type ) { $type = $this->type; } 
  1760. $res = $type == self::TYPE_DRIPPED; 
  1761.  
  1762. return apply_filters( 
  1763. 'ms_model_membership_is_dripped',  
  1764. $res,  
  1765. $type 
  1766. ); 
  1767.  
  1768. /** 
  1769. * Returns true if the membership the base or guest/user membership. 
  1770. * @since 1.0.0 
  1771. * @api 
  1772. * @return bool 
  1773. */ 
  1774. public function is_system( $type = null ) { 
  1775. if ( ! $type ) { $type = $this->type; } 
  1776.  
  1777. $res = false; 
  1778. if ( $this->is_base( $type ) ) { $res = true; } 
  1779. elseif ( $this->is_guest( $type ) ) { $res = true; } 
  1780. elseif ( $this->is_user( $type ) ) { $res = true; } 
  1781.  
  1782. return apply_filters( 
  1783. 'ms_model_membership_is_system',  
  1784. $res,  
  1785. $type 
  1786. ); 
  1787.  
  1788. /** 
  1789. * Can be used to validate if the current membership is actually loaded 
  1790. * from database. If this function returns false, then the specified 
  1791. * membership-ID does not exist in DB. 
  1792. * @since 1.0.0 
  1793. * @api 
  1794. * @return bool 
  1795. */ 
  1796. public function is_valid() { 
  1797. $res = ! empty( $this->id ); 
  1798.  
  1799. return apply_filters( 
  1800. 'ms_model_membership_is_valid',  
  1801. $res,  
  1802. $this 
  1803. ); 
  1804.  
  1805. /** 
  1806. * Verify access to current page. 
  1807. * Verify membership rules hierarchy for content accessed directly. 
  1808. * If 'has access' is found, it does have access. 
  1809. * Only for active memberships. 
  1810. * @since 1.0.0 
  1811. * @api 
  1812. * @param int $post_id 
  1813. * @return bool|null True if has access to current page. Default is false. 
  1814. * Null means: Rule not relevant for current page. 
  1815. */ 
  1816. public function has_access_to_current_page( $post_id = null ) { 
  1817. $has_access = null; 
  1818. $this->_access_reason = array(); 
  1819. $this->_deny_rule = array(); 
  1820. $this->_allow_rule = array(); 
  1821.  
  1822. // Only verify access if membership is Active. 
  1823. if ( $this->active ) { 
  1824.  
  1825. // If 'has access' is found in the hierarchy, it does have access. 
  1826. $rules = $this->get_rules_hierarchy(); 
  1827. foreach ( $rules as $rule ) { 
  1828. $rule_access = $rule->has_access( $post_id ); 
  1829.  
  1830. if ( null === $rule_access ) { 
  1831. $this->_access_reason[] = sprintf( 
  1832. __( 'Ignored: Rule "%s"', 'membership2' ),  
  1833. $rule->rule_type 
  1834. ); 
  1835. continue; 
  1836.  
  1837. $this->_access_reason[] = sprintf( 
  1838. __( '%s: Rule "%s"', 'membership2' ),  
  1839. $rule_access ? __( 'Allow', 'membership2' ) : __( 'Deny', 'membership2' ),  
  1840. $rule->rule_type 
  1841. ); 
  1842.  
  1843. if ( ! $rule_access ) { 
  1844. $this->_deny_rule[] = $rule->rule_type; 
  1845. } else { 
  1846. $this->_allow_rule[] = $rule->rule_type; 
  1847.  
  1848. // URL groups have final decission. 
  1849. if ( MS_Rule_Url::RULE_ID === $rule->rule_type ) { 
  1850. $has_access = $rule_access; 
  1851. break; 
  1852.  
  1853. // Special pages have final decission after URL groups. 
  1854. if ( MS_Rule_Special::RULE_ID === $rule->rule_type ) { 
  1855. $has_access = $rule_access; 
  1856. $this->_access_reason[] = $rule->matched_type; 
  1857. break; 
  1858.  
  1859. $has_access = ( $has_access || $rule_access ); 
  1860.  
  1861. if ( true === $has_access ) { 
  1862. break; 
  1863.  
  1864. return apply_filters( 
  1865. 'ms_model_membership_has_access_to_current_page',  
  1866. $has_access,  
  1867. $post_id,  
  1868. $this 
  1869. ); 
  1870.  
  1871. /** 
  1872. * Verify access to post. 
  1873. * Verify membership rules hierarchy for specific post or CPT. 
  1874. * @since 1.0.0 
  1875. * @api 
  1876. * @param int $post_id ID of specific post 
  1877. * @return boolean True if has access to current page. Default is false. 
  1878. */ 
  1879. public function has_access_to_post( $post_id ) { 
  1880. $has_access = null; 
  1881.  
  1882. if ( MS_Model_Member::is_normal_admin() ) { 
  1883. return true; 
  1884.  
  1885. if ( ! empty( $post_id ) ) { 
  1886. $post = get_post( $post_id ); 
  1887. if ( 'attachment' === $post->post_type ) { 
  1888. $post_id = get_post_field( 'post_parent', $post_id ); 
  1889.  
  1890. // If 'has access' is found in the hierarchy, it does have access. 
  1891. $rules = $this->get_rules_hierarchy(); 
  1892. foreach ( $rules as $rule ) { 
  1893. $rule->prepare_rule( $subscription ); 
  1894.  
  1895. // url groups have final decision 
  1896. if ( MS_Rule_Url::RULE_ID == $rule->rule_type 
  1897. && $rule->has_rule_for_post( $post_id ) 
  1898. ) { 
  1899. $has_access = $rule->has_access( $post_id ); 
  1900. break; 
  1901. } else { 
  1902. $rule_access = $rule->has_access( $post_id ); 
  1903. if ( null !== $rule_access ) { 
  1904. $has_access = $rule_access; 
  1905.  
  1906. if ( $has_access ) { 
  1907. break; 
  1908.  
  1909. if ( null === $has_access ) { 
  1910. // The post is not denied by any rule, so allow access. 
  1911. $has_access = true; 
  1912.  
  1913. return apply_filters( 
  1914. 'ms_model_membership_has_access_to_post',  
  1915. $has_access,  
  1916. $this 
  1917. ); 
  1918.  
  1919. /** 
  1920. * Set up the membership. This is always done, regardless if the user is 
  1921. * a normal user or an Admin user. 
  1922. * @since 1.0.0 
  1923. * @internal 
  1924. * @param MS_Model_Relationship $subscription The membership relationship. 
  1925. */ 
  1926. public function initialize( $subscription ) { 
  1927. do_action( 
  1928. 'ms_model_membership_initialize_before',  
  1929. $subscription,  
  1930. $this 
  1931. ); 
  1932.  
  1933. $this->subscription_id = $subscription->id; 
  1934. $rules = $this->get_rules_hierarchy(); 
  1935.  
  1936. // Apply protection settings of all rules (replace/hide contents, ...) 
  1937. foreach ( $rules as $rule ) { 
  1938. $rule->prepare_rule( $subscription ); 
  1939.  
  1940. do_action( 
  1941. 'ms_model_membership_initialize_after',  
  1942. $subscription,  
  1943. $this 
  1944. ); 
  1945.  
  1946. /** 
  1947. * Set initial protection for front-end. 
  1948. * This function is only executed when the current user is no Admin user. 
  1949. * Hide restricted content for this membership. 
  1950. * @since 1.0.0 
  1951. * @internal 
  1952. */ 
  1953. public function protect_content() { 
  1954. do_action( 
  1955. 'ms_model_membership_protect_content_before',  
  1956. $this 
  1957. ); 
  1958.  
  1959. $rules = $this->get_rules_hierarchy(); 
  1960.  
  1961. // Apply protection settings of all rules (replace/hide contents, ...) 
  1962. foreach ( $rules as $rule ) { 
  1963. $rule->protect_content(); 
  1964.  
  1965. do_action( 
  1966. 'ms_model_membership_protect_content_after',  
  1967. $this 
  1968. ); 
  1969.  
  1970. /** 
  1971. * Set initial protection for admin side. 
  1972. * Hide restricted content for this membership. 
  1973. * @since 1.0.0 
  1974. * @internal 
  1975. */ 
  1976. public function protect_admin_content() { 
  1977. do_action( 
  1978. 'ms_model_membership_protect_content_before',  
  1979. $this 
  1980. ); 
  1981.  
  1982. $rules = $this->get_rules_hierarchy(); 
  1983.  
  1984. foreach ( $rules as $rule ) { 
  1985. $rule->protect_admin_content(); 
  1986.  
  1987. do_action( 
  1988. 'ms_model_membership_protect_content_after',  
  1989. $this 
  1990. ); 
  1991.  
  1992. /** 
  1993. * Checks if the user is allowed to change the payment details for the 
  1994. * current membership. 
  1995. * Payment details can only be changed when 
  1996. * (A) no payment details were saved yet - OR - 
  1997. * (B) no members signed up for the memberships 
  1998. * @since 1.0.0 
  1999. * @api 
  2000. * @return bool 
  2001. */ 
  2002. public function can_change_payment() { 
  2003. // Allow if Membership is new/unsaved. 
  2004. if ( empty( $this->id ) ) { return true; } 
  2005.  
  2006. // Allow if no payment detail was entered yet (incomplete setup). 
  2007. if ( empty( $this->payment_type ) ) { return true; } 
  2008.  
  2009. // Allow if no members signed up yet. 
  2010. $members = MS_Model_Relationship::get_subscription_count( 
  2011. array( 'membership_id' => $this->id ) 
  2012. ); 
  2013. if ( empty( $members ) ) { return true; } 
  2014.  
  2015. // Otherwise payment details cannot be changed anymore. 
  2016. return false; 
  2017.  
  2018. /** 
  2019. * Returns property associated with the render. 
  2020. * @since 1.0.0 
  2021. * @internal 
  2022. * @param string $property The name of a property. 
  2023. * @return mixed Returns mixed value of a property or NULL if a property doesn't exist. 
  2024. */ 
  2025. public function __get( $property ) { 
  2026. $value = null; 
  2027.  
  2028. switch ( $property ) { 
  2029. case 'type': 
  2030. if ( ! self::is_valid_type( $this->type ) ) { 
  2031. $this->type = self::TYPE_STANDARD; 
  2032.  
  2033. $value = $this->type; 
  2034. break; 
  2035.  
  2036. case 'payment_type': 
  2037. $types = self::get_payment_types(); 
  2038. if ( ! array_key_exists( $this->payment_type, $types ) ) { 
  2039. $this->payment_type = self::PAYMENT_TYPE_PERMANENT; 
  2040. $value = $this->payment_type; 
  2041. break; 
  2042.  
  2043. case 'trial_period_enabled': 
  2044. case 'active': 
  2045. case 'private': 
  2046. case 'is_free': 
  2047. $value = lib3()->is_true( $this->$property ); 
  2048. break; 
  2049.  
  2050. case 'type_description': 
  2051. $value = $this->get_type_description(); 
  2052. break; 
  2053.  
  2054. case 'period_unit': 
  2055. $value = MS_Helper_Period::get_period_value( $this->period, 'period_unit' ); 
  2056. break; 
  2057.  
  2058. case 'period_type': 
  2059. $value = MS_Helper_Period::get_period_value( $this->period, 'period_type' ); 
  2060. break; 
  2061.  
  2062. case 'pay_cycle_period_unit': 
  2063. $value = MS_Helper_Period::get_period_value( $this->pay_cycle_period, 'period_unit' ); 
  2064. break; 
  2065.  
  2066. case 'pay_cycle_period_type': 
  2067. $value = MS_Helper_Period::get_period_value( $this->pay_cycle_period, 'period_type' ); 
  2068. break; 
  2069.  
  2070. case 'trial_period_unit': 
  2071. $value = MS_Helper_Period::get_period_value( $this->trial_period, 'period_unit' ); 
  2072. break; 
  2073.  
  2074. case 'trial_period_type': 
  2075. $value = MS_Helper_Period::get_period_value( $this->trial_period, 'period_type' ); 
  2076. break; 
  2077.  
  2078. case 'price': 
  2079. if ( $this->is_free() ) { 
  2080. $value = 0; 
  2081. } else { 
  2082. $value = $this->price; 
  2083. break; 
  2084.  
  2085. case 'total_price': 
  2086. if ( $this->is_free() ) { 
  2087. $value = 0; 
  2088. } else { 
  2089. $value = $this->price; 
  2090.  
  2091. $value = apply_filters( 
  2092. 'ms_apply_taxes',  
  2093. $value,  
  2094. $this 
  2095. ); 
  2096. break; 
  2097.  
  2098. case 'pay_cycle_repetitions': 
  2099. $value = absint( $this->pay_cycle_repetitions ); 
  2100. break; 
  2101.  
  2102. case 'disabled_gateways': 
  2103. $value = lib3()->array->get( $this->disabled_gateways ); 
  2104. break; 
  2105.  
  2106. case 'is_paid': 
  2107. $value = ! $this->is_free; 
  2108. break; 
  2109.  
  2110. case 'public': 
  2111. $value = ! $this->private; 
  2112. break; 
  2113.  
  2114. default: 
  2115. if ( property_exists( $this, $property ) ) { 
  2116. $value = $this->$property; 
  2117. break; 
  2118.  
  2119. return apply_filters( 
  2120. 'ms_model_membership__get',  
  2121. $value,  
  2122. $property,  
  2123. $this 
  2124. ); 
  2125.  
  2126. /** 
  2127. * Validate specific property before set. 
  2128. * @since 1.0.0 
  2129. * @internal 
  2130. * @param string $property The name of a property to associate. 
  2131. * @param mixed $value The value of a property. 
  2132. */ 
  2133. public function __set( $property, $value ) { 
  2134. if ( property_exists( $this, $property ) ) { 
  2135. switch ( $property ) { 
  2136. case 'name': 
  2137. case 'title': 
  2138. $this->$property = sanitize_text_field( $value ); 
  2139. break; 
  2140.  
  2141. case 'description': 
  2142. $this->$property = wp_kses( $value, 'post' ); 
  2143. break; 
  2144.  
  2145. case 'type': 
  2146. if ( ! self::is_valid_type( $value ) ) { 
  2147. $value = self::TYPE_STANDARD; 
  2148.  
  2149. if ( $this->is_system( $value ) ) { 
  2150. // Only one instance of these types can exist. 
  2151. $existing = $this->_get_system_membership( $value, false ); 
  2152.  
  2153. if ( $existing && $existing->id != $this->id ) { 
  2154. $value = self::TYPE_STANDARD; 
  2155. } else { 
  2156. $this->active = true; 
  2157. $this->private = true; 
  2158. $this->is_free = true; 
  2159. $this->price = 0; 
  2160. $this->post_name = sanitize_html_class( $this->title ); 
  2161. $this->payment_type = self::PAYMENT_TYPE_PERMANENT; 
  2162. $this->post_author = get_current_user_id(); 
  2163.  
  2164. $this->type = $value; 
  2165. break; 
  2166.  
  2167. case 'payment_type': 
  2168. $types = self::get_payment_types(); 
  2169. if ( array_key_exists( $value, $types ) ) { 
  2170. $this->payment_type = $value; 
  2171. } else { 
  2172. throw new Exception( 'Invalid membership type.' ); 
  2173. break; 
  2174.  
  2175. case 'trial_period_enabled': 
  2176. case 'active': 
  2177. case 'private': 
  2178. case 'is_free': 
  2179. $this->$property = lib3()->is_true( $value ); 
  2180. break; 
  2181.  
  2182. case 'price': 
  2183. case 'trial_price': 
  2184. $this->$property = floatval( $value ); 
  2185. break; 
  2186.  
  2187. case 'pay_cycle_repetitions': 
  2188. $this->$property = absint( $value ); 
  2189. break; 
  2190.  
  2191. case 'period': 
  2192. case 'pay_cycle_period': 
  2193. case 'trial_period': 
  2194. $this->$property = $this->validate_period( $value ); 
  2195. break; 
  2196.  
  2197. case 'period_date_start': 
  2198. case 'period_date_end': 
  2199. $this->$property = $this->validate_date( $value ); 
  2200. break; 
  2201.  
  2202. case 'on_end_membership_id': 
  2203. if ( 0 == $value ) { 
  2204. $this->$property = 0; 
  2205. } else if ( 0 < MS_Factory::load( 'MS_Model_Membership', $value )->id ) { 
  2206. $this->$property = $value; 
  2207. break; 
  2208.  
  2209. default: 
  2210. $this->$property = $value; 
  2211. break; 
  2212. } else { 
  2213. switch ( $property ) { 
  2214. case 'period_unit': 
  2215. $this->period['period_unit'] = $this->validate_period_unit( $value ); 
  2216. break; 
  2217.  
  2218. case 'period_type': 
  2219. $this->period['period_type'] = $this->validate_period_type( $value ); 
  2220. break; 
  2221.  
  2222. case 'pay_cycle_period_unit': 
  2223. $this->pay_cycle_period['period_unit'] = $this->validate_period_unit( $value ); 
  2224. break; 
  2225.  
  2226. case 'pay_cycle_period_type': 
  2227. $this->pay_cycle_period['period_type'] = $this->validate_period_type( $value ); 
  2228. break; 
  2229.  
  2230. case 'trial_period_unit': 
  2231. $this->trial_period['period_unit'] = $this->validate_period_unit( $value ); 
  2232. break; 
  2233.  
  2234. case 'trial_period_type': 
  2235. $this->trial_period['period_type'] = $this->validate_period_type( $value ); 
  2236. break; 
  2237.  
  2238. case 'public': 
  2239. $this->private = ! lib3()->is_true( $value ); 
  2240. break; 
  2241.  
  2242. case 'is_paid': 
  2243. $this->is_free = ! lib3()->is_true( $value ); 
  2244. break; 
  2245.  
  2246. case 'deny_update': 
  2247. foreach ( $value as $key => $state ) { 
  2248. $this->update_denied[$key] = lib3()->is_true( $state ); 
  2249. break; 
  2250.  
  2251. case 'replace_update': 
  2252. foreach ( $value as $key => $state ) { 
  2253. $this->update_replace[$key] = lib3()->is_true( $state ); 
  2254. break; 
  2255.  
  2256.  
  2257. do_action( 
  2258. 'ms_model_membership__set_after',  
  2259. $property,  
  2260. $value,  
  2261. $this 
  2262. ); 
  2263.