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

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