RCP_Levels::insert PHP Method

insert() public method

Insert a subscription level into the database
Since: 1.5
public insert ( $args = [] )
    public function insert($args = array())
    {
        global $wpdb;
        $defaults = array('name' => '', 'description' => '', 'duration' => 'unlimited', 'duration_unit' => 'month', 'price' => '0', 'fee' => '0', 'list_order' => '0', 'level' => '0', 'status' => 'inactive', 'role' => 'subscriber');
        $args = wp_parse_args($args, $defaults);
        do_action('rcp_pre_add_subscription', $args);
        $args = apply_filters('rcp_add_subscription_args', $args);
        foreach (array('price', 'fee') as $key) {
            if (empty($args[$key])) {
                $args[$key] = '0';
            }
            $args[$key] = str_replace(',', '', $args[$key]);
        }
        // Validate price value
        if (false === $this->valid_amount($args['price']) || $args['price'] < 0) {
            return false;
        }
        // Validate fee value
        if (false === $this->valid_amount($args['fee'])) {
            return false;
        }
        $add = $wpdb->query($wpdb->prepare("INSERT INTO {$this->db_name} SET\n\t\t\t\t\t`name`          = '%s',\n\t\t\t\t\t`description`   = '%s',\n\t\t\t\t\t`duration`      = '%d',\n\t\t\t\t\t`duration_unit` = '%s',\n\t\t\t\t\t`price`         = '%s',\n\t\t\t\t\t`fee`           = '%s',\n\t\t\t\t\t`list_order`    = '0',\n\t\t\t\t\t`level`         = '%d',\n\t\t\t\t\t`status`        = '%s',\n\t\t\t\t\t`role`          = '%s'\n\t\t\t\t;", sanitize_text_field($args['name']), sanitize_text_field($args['description']), sanitize_text_field($args['duration']), sanitize_text_field($args['duration_unit']), sanitize_text_field($args['price']), sanitize_text_field($args['fee']), absint($args['level']), sanitize_text_field($args['status']), sanitize_text_field($args['role'])));
        if ($add) {
            $args = array('status' => 'all', 'limit' => null, 'orderby' => 'list_order');
            $cache_key = md5(implode('|', $args));
            wp_cache_delete($cache_key, 'rcp');
            do_action('rcp_add_subscription', $wpdb->insert_id, $args);
            return $wpdb->insert_id;
        }
        return false;
    }

Usage Example

	public function setUp() {
		parent::setUp();

		$user = wp_insert_user( array(
			'user_login' => 'test',
			'user_pass'       => 'pass',
			'first_name' => 'Tester',
			'user_email' => '*****@*****.**'
		) );

		$this->member = new RCP_Member( $user );

		$levels = new RCP_Levels;

		$this->level_id = $levels->insert( array(
			'name'          => 'Gold',
			'duration'      => 1,
			'duration_unit' => 'month',
			'level'         => 1,
			'status'        => 'active'
		) );

		$this->level_id_2 = $levels->insert( array(
			'name'          => 'Silver',
			'duration'      => 1,
			'duration_unit' => 'month',
			'status'        => 'active',
			'level'         => 3
		) );
	}
All Usage Examples Of RCP_Levels::insert