RCP_Levels

The RCP_Levels class is for interacting with the subscription level database table. It can be used for getting, inserting, and updating levels, or adding meta data to a level.

This class can be used with the $rcp_levels_db global variable or by creating a new instance of the class directly.

get_level( $level_id = 0 )

This method retrieves a specific level from the database. Example:

$levels = new RCP_Levels();
$level  = $levels->get_level( 1 );

The $level object would look something like this if printed out:

stdClass Object
(
    [id] => 8
    [name] => Silver
    [description] => Silver level description
    [duration] => 1
    [duration_unit] => month
    [price] => 30
    [fee] => 0
    [list_order] => 12
    [level] => 1
    [status] => active
    [role] => subscriber
    [trial_duration] => 0
    [trial_duration_unit] => day
)

get_level_by( $field = 'name', $value = '' )

This method can be used to retrieve a subscription level by a specific field/value. This can be used instead of get_level() if you need to retrieve a level by something other than its ID.

Example:

$levels = new RCP_Levels();
$level  = $levels->get_level_by( 'name', 'Silver' );

get_levels( $args = array() )

This method returns an array of subscription levels based on the provided arguments. It can also return false if no levels are found.

Default arguments:

  • status (string) - all ( active | inactive | all )
  • limit (null|int) - null (enter an integer to limit results, otherwise all results are returned)
  • orderby (string) - list_order ( any column name )

Example:

$levels_db = new RCP_Levels();
$levels    = $levels_db->get_levels( array( 'status' => 'active' ) );

if ( ! empty( $levels ) ) {
    foreach ( $levels as $level ) {
        echo $level->name;
    }
}

insert( $args = array() )

Used for inserting a new subscription level. The available arguments are:

  • name (string) - Name of the level
  • description (string) - Level description
  • duration (int|string) - Duration of the level. Either an integer or 'unlimited'.
  • duration_unit (string) - day | month | year
  • trial_duration (int) - Duration of the trial. Set to 0 for no trial.
  • trial_duration_unit (string) - day | month | year
  • price (int|float) - Price of the subscription level.
  • fee (int|float) - Signup fee, or 0 to disable.
  • list_order (int) - Where the subscription level should appear in the list (lower = first; higher = last).
  • level (int) - Access level granted.
  • status (string) - active | inactive
  • role (string) - User role that is granted.

update( $level_id = 0, $args = array() )

Used for updating a specific subscription level. This method takes the same arguments as the insert() method.

remove( $level_id = 0 )

Used for deleting a subscription level.

Retrieving information about a level

A few methods are available for getting specific details about a subscription level.

  • get_level_field( $level_id = 0, $field = '' ) - Get a sepcific field value for a subscription level.
  • has_trial( $level_id = 0 ) - Determines if a specific subscription level has a trial option.
  • trial_duration( $level_id = 0 ) - Retrieves the trial duration for a subscription level.
  • trial_duration_unit( $level_id = 0 ) - Retrieves the trial duration unit for a subscription level.

Getting, setting, and deleting level meta

Subscription levels have a meta table, just like user meta or post meta. You can use the following methods to interact with the meta table:

  • get_meta( $level_id = 0, $meta_key = '', $single = false ) - Get the meta value for a level ID and key.
  • add_meta( $level_id = 0, $meta_key = '', $meta_value, $unique = false ) - Add meta data to a subscription level.
  • update_meta( $level_id = 0, $meta_key = '', $meta_value, $prev_value = '' ) - Update an existing meta value. If the field does not yet exist, it will be created.
  • delete_meta( $level_id = 0, $meta_key = '', $meta_value = '' ) - Deletes the matching meta for a subscription level.
  • remove_all_meta_for_level_id( $level_id = 0 ) - Removes all meta data associated with a subscription level.