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