# \MS_Model_Membership

Membership model.

A membership defines payment options, access rules and similar details. It is not related to a specific user!

Note that all properties are declared protected but they can be access directly (e.g. `$membership->type` to get the type value). There are magic methods __get() and __set() that do some validation before accessing the properties.

## Summary

Methods
Properties
Constants
get_post_type()
sort_by_priority()
is_valid_membership()
get_base()
get_guest()
get_user()
is_valid_type()
save()
delete()
get_payment_type_desc()
is_free()
has_trial()
update_allowed()
update_replaces()
can_use_gateway()
get_rule()
set_rule()
get_color()
get_name_tag()
name_tag()
get_type_description()
set_custom_data()
delete_custom_data()
get_custom_data()
get_after_ms_ends_options()
get_subscriptions()
get_members_count()
get_members()
has_dripped_content()
is_base()
is_guest()
is_user()
is_dripped()
is_system()
is_valid()
has_access_to_current_page()
has_access_to_post()
can_change_payment()
No public properties found
TYPE_STANDARD
TYPE_DRIPPED
TYPE_BASE
TYPE_GUEST
TYPE_USER
PAYMENT_TYPE_PERMANENT
PAYMENT_TYPE_FINITE
PAYMENT_TYPE_DATE_RANGE
PAYMENT_TYPE_RECURRING
No protected methods found
$type
$payment_type
$active
$private
$priority
$is_free
$price
$disabled_gateways
$period
$pay_cycle_period
$pay_cycle_repetitions
$period_date_start
$period_date_end
$trial_period_enabled
$trial_period
$on_end_membership_id
$subscription_id
N/A
No private methods found
No private properties found
N/A

File
  model/class-ms-model-membership.php
Package
  Membership2\Model
Class hierarchy
  \MS_Model_CustomPostType
  \MS_Model_Membership

## Tags

since |

## Constants

### TYPE_STANDARD

    TYPE_STANDARD

Membership type constant.

#

See also
  $type property.

## Tags

since |

### TYPE_DRIPPED

    TYPE_DRIPPED

Membership type constant.

#

See also
  $type property.

## Tags

since |

### TYPE_BASE

    TYPE_BASE

Membership type constant.

System membership, hidden, created automatically.

#

See also
  $type property.

## Tags

since |

### TYPE_GUEST

    TYPE_GUEST

Membership type constant.

Guest membership, only one membership possible.

#

See also
  $type property.

## Tags

since |

### TYPE_USER

    TYPE_USER

Membership type constant.

User membership, only one membership possible.

#

See also
  $type property.

## Tags

since |

### PAYMENT_TYPE_PERMANENT

    PAYMENT_TYPE_PERMANENT

Membership payment type constants.

#

See also
  $payment_type property.

## Tags

since |

### PAYMENT_TYPE_FINITE

    PAYMENT_TYPE_FINITE

Membership payment type constants.

#

See also
  $payment_type property.

## Tags

since |

### PAYMENT_TYPE_DATE_RANGE

    PAYMENT_TYPE_DATE_RANGE

Membership payment type constants.

#

See also
  $payment_type property.

## Tags

since |

### PAYMENT_TYPE_RECURRING

    PAYMENT_TYPE_RECURRING

Membership payment type constants.

The only type that auto-renews without asking the user!

#

See also
  $payment_type property.

## Tags

since |

## Properties

### $type

    $type : string

Membership type.

Default is TYPE_STANDARD.

#### Type

string

#

## Tags

since |

### $payment_type

    $payment_type : string

Membership payment type.

Default is PAYMENT_TYPE_PERMANENT.

#### Type

string

#

## Tags

since |

### $active

    $active : boolean

Membership active status.

By default a new membership is active.

#### Type

boolean

#

## Tags

since |

### $private

    $private : boolean

Membership private status.

Private means that the membership will not be displayed on the default registration page and membership list. Users can still stubscribe to the membership via the shortcode [ms-membership-buy] or by otherwise reaching the subscription URL.

#### Type

boolean

#

## Tags

since |

### $priority

    $priority : integer

A priority value that is used to determine the effective override settings if a user has multiple memberships.

#### Type

integer

#

## Tags

since |

### $is_free

    $is_free : boolean

Membership free status.

#### Type

boolean

#

## Tags

since |

### $price

    $price : float

Membership price.

#### Type

float

#

## Tags

since |

### $disabled_gateways

    $disabled_gateways : array

A list of disabled gateways.

#### Type

array

#

## Tags

since |

### $period

    $period : array

Membership period for finite access.

Used for payment_type PAYMENT_TYPE_FINITE.

#### Type

array — { @type int $period_unit The period of time quantity. @type string $period_type The period type (days, weeks, months, years). }

#

See also
  $payment_type property.

## Tags

since |

### $pay_cycle_period

    $pay_cycle_period : array

Membership payment recurring period cycle.

Used for the payment_type PAYMENT_TYPE_RECURRING.

#### Type

array — {@see $period $period property}.

#

See also
  $payment_type property.

## Tags

since |

### $pay_cycle_repetitions

    $pay_cycle_repetitions : integer

Defines how many payments are made before the membership ends.

Used for the payment_type PAYMENT_TYPE_RECURRING.

#### Type

integer

#

See also
  $payment_type property.

## Tags

since |

### $period_date_start

    $period_date_start : string

Membership start date for date range payment type.

Used for the payment_type PAYMENT_TYPE_DATE_RANGE.

#### Type

string — The membership start date.

#

See also
  $payment_type property.

## Tags

since |

### $period_date_end

    $period_date_end : string

Membership end date for date range payment type.

Used for the payment_type PAYMENT_TYPE_DATE_RANGE.

#### Type

string — The membership end date.

#

See also
  $payment_type property.

## Tags

since |

### $trial_period_enabled

    $trial_period_enabled : boolean

Membership trial period enabled indicator.

Requires the Trial Period Add-on to work.

#### Type

boolean

#

## Tags

since |

### $trial_period

    $trial_period : array

Membership trial period.

#### Type

array — {@see $period $period property}.

#

## Tags

since |

### $on_end_membership_id

    $on_end_membership_id : integer

Move to Membership when the current one expires.

After current membership expire move to the indicated membership_id. This membership is assigned when the current membership expires.

#### Type

integer

#

See also

## Tags

since |

### $subscription_id

    $subscription_id : integer

Stores the subscription-ID of the parent object.

This value will only have a value when the Membership is loaded within the context of a subscription.

#### Type

integer

#

## Tags

since |

## Methods

### get_post_type()

    get_post_type() : string

Returns the post-type of the current object.

#### Returns

string —

The post-type name.

#

static

## Tags

since | 1.0.0
api |

### sort_by_priority()

    sort_by_priority(\MS_Model_Membership  $a, \MS_Model_Membership  $b) : integer

Sort function used as second param by `uasort()` to sort a membership list by priority.

Memberships with equal priority are sorted alphabeically.

#### Parameters

\MS_Model_Membership | $a |
\MS_Model_Membership | $b |

#### Returns

integer —

-1: a < b | 0: a = b | +1: a > b

#

static

## Tags

since | 1.0.1.0

### is_valid_membership()

    is_valid_membership(integer  $membership_id) : boolean

Verify if membership is valid.

Verify if membership was not deleted, trying to load from DB.

#### Parameters

integer | $membership_id | The membership id to verify.

#### Returns

boolean —

True if is valid.

#

static

## Tags

since | 1.0.0
api |

### get_base()

    get_base() : \MS_Model_Membership

Get Membership2 base membership.

Create a new membership if membership does not exist. This is an internal membership which is never displayed anywhere.

#### Returns

\MS_Model_Membership —

The base membership.

#

static

## Tags

since | 1.0.0
api |

### get_guest()

    get_guest() : \MS_Model_Membership

Get special membership that is assigned to all guests.

Create a new membership if membership does not exist.

#### Returns

\MS_Model_Membership —

The guest membership.

#

static

## Tags

since | 1.0.0
api |

### get_user()

    get_user() : \MS_Model_Membership

Get default membership for all logged-in users that did not yet subscribe to any membership.

Create a new membership if membership does not exist.

#### Returns

\MS_Model_Membership —

The guest membership.

#

static

## Tags

since | 1.0.0
api |

### is_valid_type()

    is_valid_type(string  $type) : boolean

Checks if the specified string is a valid Membership-Type identifier.

#### Parameters

string | $type | A string to check against all known membership types.

#### Returns

boolean —

True if the string is a valid type.

#

static

## Tags

since | 1.0.1.0

### save()

    save()

Save model and move the object to the singleton cache if required.

#

## Tags

since | 1.0.0

### delete()

    delete() : boolean

Permanently delete the membership.

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### get_payment_type_desc()

    get_payment_type_desc() : string

Get current payment type description.

Description to show in the admin list table.

#### Returns

string —

The current payment type description.

#

## Tags

since | 1.0.0
api |

### is_free()

    is_free() : boolean

Returns true if the current membership is free.

A membership is free when... ... it is explicitely marked as "free" ... the price is 0.00 ... it is a parent membership that cannot be signed up for

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### has_trial()

    has_trial() : boolean

Returns true if this membership is eligable for trial period.

#### Returns

boolean

#

## Tags

since | 1.0.1.0

### update_allowed()

    update_allowed(integer|string  $id) : boolean

Returns the access flag, if a specific membership can subscribe to the current membership.

A special value for $id is 'guest', which is used for all users without a normal membership (is_system() type memberships are not normal)

#### Parameters

integer|string | $id | A membership ID or the value 'guest'.

#### Returns

boolean —

True if the specified membership can subscribe.

#

## Tags

since | 1.0.1.0

### update_replaces()

    update_replaces(integer|string  $id) : boolean

Returns the update-replacement flag, which defines if the OLD membership should be cancelled during subscription.

This is used in cases where the new membership is an upgraded version of the old membership and the user can only have one of both memberships.

#### Parameters

integer|string | $id | A membership ID.

#### Returns

boolean —

True if the specified membership should be cancelled.

#

## Tags

since | 1.0.1.0

### can_use_gateway()

    can_use_gateway(string  $gateway_id) : boolean

Checks if a specific payment gateway is allowed for the current membership.

#### Parameters

string | $gateway_id | The payment gateway ID.

#### Returns

boolean

#

## Tags

since | 1.0.0

### get_rule()

    get_rule(string  $rule_type) : \MS_Rule

Get protection Rule Model.

Note for network-wide mode: In DB the rules for each site are stored in different objects. When loading a membership we will always load 1 instance of each rule_type, and this is the instance that belongs to the current site! Instances for other sites are not accessible. -> This is why we do not use/need a site_id or similar in this function.

#### Parameters

string | $rule_type | The rule model type @see MS_Rule

#### Returns

\MS_Rule —

The requested rule model.

#

## Tags

since | 1.0.0
api |

### set_rule()

    set_rule(  $rule_type, \MS_Rule  $rule)

Set protection Rule Model.

Note for network-wide mode: In DB the rules for each site are stored in different objects. When loading a membership we will always load 1 instance of each rule_type, and this is the instance that belongs to the current site! Instances for other sites are not accessible. -> This is why we do not use/need a site_id or similar in this function.

#### Parameters

| $rule_type |
\MS_Rule | $rule | The protection rule to set.

#

## Tags

since | 1.0.0
api |

### get_color()

    get_color() : string

Returns the unique HEX color for this membership.

The color is calculated from the membership-ID and therefore will never change.

#### Returns

string —

Hex color, e.g. '#FFFFFF'

#

## Tags

since | 1.0.0
api |

### get_name_tag()

    get_name_tag()

Returns a HTML tag that shows the membership name with the internal membership color.

#

## Tags

since | 1.0.0
api |

### name_tag()

    name_tag()

Echo a HTML tag that shows the membership name with the internal membership color.

#

## Tags

since | 1.0.0
api |

### get_type_description()

    get_type_description() : string

Get current membership type description.

#### Returns

string —

The membership type description.

#

## Tags

since | 1.0.0
api |

### set_custom_data()

    set_custom_data(string  $key, mixed  $value)

Either creates or updates the value of a custom data field.

Note: Remember to prefix the $key with a unique string to prevent conflicts with other plugins that also use this function.

#### Parameters

string | $key | The field-key.
mixed | $value | The new value to assign to the field.

#

## Tags

since | 1.0.0
api |

### delete_custom_data()

    delete_custom_data(string  $key)

Removes a custom data field from this object.

#### Parameters

string | $key | The field-key.

#

## Tags

since | 1.0.0
api |

### get_custom_data()

    get_custom_data(string  $key) : mixed

Returns the value of a custom data field.

#### Parameters

string | $key | The field-key.

#### Returns

mixed —

The value that was previously assigned to the custom field or false if no value was set for the field.

#

## Tags

since | 1.0.0
api |

### get_after_ms_ends_options()

    get_after_ms_ends_options() : array

Get after membership expired options.

Memberships can be downgraded to the guest level protection.

#### Returns

array —

{ Returns array of $membership_id => $description. @type int $membership_id The membership Id. @type string $description The expired option description. }

#

## Tags

since | 1.0.0
api |

### get_subscriptions()

    get_subscriptions() : array

Get a list of all subscriptions to this membership.

Note that this function will also return expired/cancelled subscriptions.

#### Returns

array —

All subscriptions.

#

## Tags

since | 1.0.1.0
api |

### get_members_count()

    get_members_count(array  $args = null) : integer

Get members count of this membership.

This will also count members that have "cancelled" or "expired" subscriptions but not "pending" or "deactivated".

To change this use the filter parameter: $args = array( 'status' => 'all' )

#### Parameters

array | $args | The query post args

#### Returns

integer —

The members count.

#

## Tags

since | 1.0.0
api |

### get_members()

    get_members(array  $args = null) : array

Get members list of this membership.

This will also count members that have "cancelled" or "expired" subscriptions but not "pending" or "deactivated".

To change this use the filter parameter: $args = array( 'status' => 'all' )

#### Parameters

array | $args | The query post args

#### Returns

array —

List of members.

#

## Tags

since | 1.0.1.0
api |

### has_dripped_content()

    has_dripped_content() : boolean

Return membership has dripped content.

Verify post and page rules if there is a dripped content.

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### is_base()

    is_base(  $type = null) : boolean

Returns true if the membership the base membership.

#### Parameters

| $type |

#### Returns

boolean

#

See also
  of MS_Model_Membership::get_base()

## Tags

since | 1.0.0
api |

### is_guest()

    is_guest(  $type = null) : boolean

Returns true if the membership the guest membership.

#### Parameters

| $type |

#### Returns

boolean

#

See also
  of MS_Model_Membership::get_guest()

## Tags

since | 1.0.0
api |

### is_user()

    is_user(  $type = null) : boolean

Returns true if the membership the user membership.

#### Parameters

| $type |

#### Returns

boolean

#

See also
  of MS_Model_Membership::get_user()

## Tags

since | 1.0.0
api |

### is_dripped()

    is_dripped(  $type = null) : boolean

Returns true if the membership a dripped membership.

#### Parameters

| $type |

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### is_system()

    is_system(  $type = null) : boolean

Returns true if the membership the base or guest/user membership.

#### Parameters

| $type |

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### is_valid()

    is_valid() : boolean

Can be used to validate if the current membership is actually loaded from database. If this function returns false, then the specified membership-ID does not exist in DB.

#### Returns

boolean

#

## Tags

since | 1.0.0
api |

### has_access_to_current_page()

    has_access_to_current_page(integer  $post_id = null) : boolean|null

Verify access to current page.

Verify membership rules hierarchy for content accessed directly. If 'has access' is found, it does have access. Only for active memberships.

#### Parameters

integer | $post_id |

#### Returns

boolean|null —

True if has access to current page. Default is false. Null means: Rule not relevant for current page.

#

## Tags

since | 1.0.0
api |

### has_access_to_post()

    has_access_to_post(integer  $post_id) : boolean

Verify access to post.

Verify membership rules hierarchy for specific post or CPT.

#### Parameters

integer | $post_id | ID of specific post

#### Returns

boolean —

True if has access to current page. Default is false.

#

## Tags

since | 1.0.0
api |

### can_change_payment()

    can_change_payment()

#

## Tags

None found

### class-ms-model-membership.php