# \MS_Model_Member

Member model.

Defines several details about a WordPress user. The Member object allows us to quickly check if the user did subscribe to a certain membership, and other useful stuff.

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
can_register()
allow_registration()
is_admin_user()
is_normal_admin()
is_simulated_user()
is_normal_user()
get_username()
prepare_obj()
save()
signon_user()
set_custom_data()
delete_custom_data()
get_custom_data()
get_membership_ids()
add_membership()
drop_membership()
cancel_membership()
move_membership()
has_membership()
get_subscription()
can_subscribe_to()
cancel_ids_on_subscription()
get_user()
get_meta()
set_meta()
is_valid()
get_gateway_profile()
set_gateway_profile()
No public properties found
No constants found
get_active_memberships()
$subscriptions
$is_member
$username
$email
$name
$first_name
$last_name
$gateway_profiles
$custom_data
N/A
No private methods found
No private properties found
N/A

File
  model/class-ms-model-member.php
Package
  Membership2\Model
Class hierarchy
  \MS_Model
  \MS_Model_Member

## Tags

since |

## Properties

### $subscriptions

    $subscriptions : array

Member's active subscriptions.

Note: This field is populated by MS_Factory when the Member instance is created.

#### Type

array — { @type int $membership_id The membership ID. @type MS_Model_Relationship The membership relationship model object. }

#

## Tags

since |

### $is_member

    $is_member : boolean

Indicator if the user is an active M2 Member.

This is a convenience/redudant flag to speed up SQL queries. Actually everyone that has an active or trial status membership is considered an active member.

This flag is set when:

- In MS_Model_Relationship, when a payment is recorded via add_payment()

This flag is reset when:

- In MS_Model_Relationship, when a subscription is deactivated via check_membership_status()

#### Type

boolean

#

## Tags

since |

### $username

    $username : string

Member's username.

Mapped from wordpress $wp_user object.

#### Type

string

#

See also
  $user_login.

## Tags

since |

### $email

    $email : string

Member's email.

Mapped from wordpress $wp_user object.

#### Type

string

#

See also
  $user_email.

## Tags

since |

### $name

    $name : string

Member's name.

Mapped from wordpress $wp_user object.

#### Type

string

#

See also
  $user_nicename.

## Tags

since |

### $first_name

    $first_name : string

Member's first name.

Mapped from wordpress $wp_user object.

#### Type

string

#

See also
  $first_name

## Tags

since |

### $last_name

    $last_name : string

Member's last name.

Mapped from wordpress $wp_user object.

#### Type

string

#

See also
  $last_name.

## Tags

since |

### $gateway_profiles

    $gateway_profiles : array

Member's gateway profiles info.

Save gateway IDs.

#### Type

array — { Return structure: $gateway[ $field ] => $value; @type string $gateway_id The gateway id. @type string $field The field to store. @type mixed $value The field value to store. }

#

## Tags

since |

### $custom_data

    $custom_data : array

Custom data can be used by other plugins via the set_custom_data() and get_custom_data() functions.

This can be used to store additional information on user-level, e.g. settings needed by some Add-ons or even by other plugins.

#### Type

array

#

## Tags

since |

## Methods

### can_register()

    can_register() : boolean

Checks if user-signup is enabled for this site or not.

#### Returns

boolean

#

static

## Tags

since | 1.0.0
api |

### allow_registration()

    allow_registration() : boolean

Allows users to register for this site.

#### Returns

boolean

#

static

## Tags

since | 1.0.0
api |

### is_admin_user()

    is_admin_user(integer|false  $user_id = false) : boolean

Verify is user is Admin user.

#### Parameters

integer|false | $user_id | Optional. The user ID. Default to current user.

#### Returns

boolean —

True if user is admin.

#

static

## Tags

since | 1.0.0
api |

### is_normal_admin()

    is_normal_admin(integer|false  $user_id = false) : boolean

Verify is user is Admin user and simulation mode is deactivated.

#### Parameters

integer|false | $user_id | Optional. The user ID. Default to current user.

#### Returns

boolean

#

static

## Tags

since | 1.0.0
api |

### is_simulated_user()

    is_simulated_user(integer|false  $user_id = false) : boolean

Verify is user is Admin user and simulation mode is active.

#### Parameters

integer|false | $user_id | Optional. The user ID. Default to current user.

#### Returns

boolean

#

static

## Tags

since | 1.0.0
api |

### is_normal_user()

    is_normal_user(integer|false  $user_id = false) : boolean

Verify is user is not Admin user and simulation mode is deactivated.

#### Parameters

integer|false | $user_id | Optional. The user ID. Default to current user.

#### Returns

boolean

#

static

## Tags

since | 1.0.0
api |

### get_username()

    get_username(integer  $user_id) : string

Get username from user_id.

#### Parameters

integer | $user_id | The user ID to get username.

#### Returns

string —

The username.

#

static

## Tags

since | 1.0.0
api |

### prepare_obj()

    prepare_obj()

Validates the object right after it was loaded/initialized.

We ensure that the custom_data field is an array.

#

## Tags

since | 1.0.0

### save()

    save() : \MS_Model_Member

Save member.

Create a new user is id is empty. Save member fields to wp_user and wp_usermeta tables. Set cache for further use in MSFactory::load. The usermeta are prefixed with 'ms'.

#### Returns

\MS_Model_Member —

The saved member object.

#

## Tags

since | 1.0.0
api |

### signon_user()

    signon_user()

Sign on user.

#

## 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_membership_ids()

    get_membership_ids() : array

Returns a list of all membership IDs of the current user.

#### Returns

array

#

## Tags

since | 1.0.0
api |

### add_membership()

    add_membership(integer  $membership_id, string  $gateway_id = 'admin', integer|string  $move_from_id) : object|null

Add a new membership.

If multiple membership is disabled, may move existing membership.

Only add a membership if a user is not already a member.

#### Parameters

integer | $membership_id | The membership id to add to.
string | $gateway_id | Optional. The gateway used to add the membership.
integer|string | $move_from_id | Optional. The membership id(s) to cancel.

#### Returns

object|null —

$subscription

#

## Tags

since | 1.0.0
api |

### drop_membership()

    drop_membership(integer  $membership_id)

Drop a membership.

Only update the status to deactivated.

#### Parameters

integer | $membership_id | The membership id to drop.

#

## Tags

since | 1.0.0
api |

### cancel_membership()

    cancel_membership(integer  $membership_id)

Cancel a membership.

The membership remains valid until expiration date.

#### Parameters

integer | $membership_id | The membership id to drop.

#

## Tags

since | 1.0.0
api |

### move_membership()

    move_membership(integer  $old_membership_id, integer  $mew_membership_id)

Move a membership.

#### Parameters

integer | $old_membership_id | The membership id to move from.
integer | $mew_membership_id | The membership id to move to.

#

## Tags

since | 1.0.0
api |

### has_membership()

    has_membership(integer  $membership_id) : boolean

Check membership relationship status.

Canceled status is allowed until it expires.

#### Parameters

integer | $membership_id | Optional. The specific membership to verify. If empty, verify against all memberships.

#### Returns

boolean —

True if has a valid membership.

#

## Tags

since | 1.0.0
api |

### get_subscription()

    get_subscription(integer|string  $membership_id,   $key = -1) : \MS_Model_Relationship

Return the subscription object for the specified membership.

#### Parameters

integer|string | $membership_id | The specific membership to return. Value 'priority' will return the subcription with lowest priority.
| $key |

#### Returns

\MS_Model_Relationship —

The subscription object.

#

## Tags

since | 1.0.0
api |

### can_subscribe_to()

    can_subscribe_to(integer  $membership_id) : boolean

Checks if the current user is allowed to subscribe to the specified membership.

#### Parameters

integer | $membership_id | A membership_id.

#### Returns

boolean —

Whether subscription is allowed or not.

#

## Tags

since | 1.0.1.0
api |

### cancel_ids_on_subscription()

    cancel_ids_on_subscription(integer  $membership_id) : array

Returns an array of existing subscriptions that should be cancelled when the user signs up to the specified membership.

#### Parameters

integer | $membership_id | A membership ID.

#### Returns

array —

Might be an empty array or a list of membership IDs.

#

## Tags

since | 1.0.1.0

### get_user()

    get_user() : \WP_User

Returns the WP_User object that is linked to the current member

#### Returns

\WP_User

#

## Tags

since | 1.0.0
api |

### get_meta()

    get_meta(string  $key) : mixed

Returns a value from the user-meta table.

#### Parameters

string | $key | The meta-key.

#### Returns

mixed —

The meta-value.

#

## Tags

since | 1.0.1.0
api |

### set_meta()

    set_meta(string  $key, mixed  $value)

Updates a value in the user-meta table.

#### Parameters

string | $key | The meta-key.
mixed | $value | The new meta-value.

#

## Tags

since | 1.0.1.0
api |

### is_valid()

    is_valid() : boolean

Verify if current object is valid.

#### Returns

boolean —

True if is valid.

#

## Tags

since | 1.0.0
api |

### get_gateway_profile()

    get_gateway_profile(string  $gateway, string  $field = null) : mixed

Get gateway profile.

#### Parameters

string | $gateway | The gateway ID.
string | $field | Optional. The field to retrive. Default to null, returning all profile info.

#### Returns

mixed —

The gateway profile info.

#

## Tags

since | 1.0.0
api |

### set_gateway_profile()

    set_gateway_profile(string  $gateway, string  $field, mixed  $value)

Set gateway profile.

#### Parameters

string | $gateway | The gateway ID.
string | $field | The field name to save.
mixed | $value | The field value to save.

#

## Tags

since | 1.0.0
api |

### get_active_memberships()

    get_active_memberships() : array

Returns a list of memberships for all active subscriptions of the member.

#### Returns

array

#

## Tags

since | 1.0.1.0

### class-ms-model-member.php