How to offer free items and show premium items

If you are selling access to your cloud you may want to offer some limited free access. One option is to create one simple access key in the Kadence Cloud settings and give that out to anyone that wants free access. Then you can target that key with a filter and make sure it blocks access to all the library items of a specific “pro” category.

We created a video tutorial covering the steps.

Step 1. Create a “Pro” category and assign items in your library to it.

To assign one of your categories with a pro label you can use the following filter. This example is assuming you have a category with the slug “pro”.

/**
 * Apply a pro tag based on the cloud library category.
 *
 * @param boolean $enabled true or false based on if pro.
 * @param object  $post the current cloud library item post object.
 * @param array   $request_extras an array of extra information.
 * @return Boolean based on if access should be labeled pro.
 */
function custom_kadence_cloud_add_pro_tag( $enabled, $post, $request_extras ) {
	if ( has_term( 'pro', 'kadence-cloud-categories', $post ) ) {
		return true;
	}
	return $enabled;
}
add_filter( 'kadence_cloud_post_is_pro', 'custom_kadence_cloud_add_pro_tag', 10, 3 );

Step 2. Filter the request to see if the key has access to pro items.

Next we want to add a tag we can reference to see if the given request is for our free key and if it is make sure to lock down the pro category items. The code below assumes your free access key is “free”.

/**
 * Checks if key is "free" and adds request extra specifying.
 *
 * @param array           $args true or false based on access.
 * @param WP_REST_Request $request full details about the request.
 * @return array with variables for request.
 */
function custom_kadence_cloud_add_extra_args( $args, $request ) {
	$key = $request->get_param( 'key' );
	$args['pro_access'] = true;
	if ( 'free' === $key ) {
		$args['pro_access'] = false;
	}
	return $args;
}
add_filter( 'kadence_cloud_rest_request_extras', 'custom_kadence_cloud_add_extra_args', 20, 2 );

Step 3. Filter access to pro items

Using the “pro_access” argument we filtered in above we will check that on a per item bases to see if we need to lock the item from the user.

/**
 * Lock access to specific cloud library items.
 *
 * @param boolean $enabled true or false based on if pro.
 * @param object  $post the current cloud library item post object.
 * @param array   $request_extras an array of extra information.
 * @return Boolean based on if access should be labeled pro.
 */
function custom_kadence_cloud_lock_pro_tag( $enabled, $post, $request_extras ) {
	if ( has_term( 'pro', 'kadence-cloud-categories', $post ) && ! $request_extras['pro_access'] ) {
		return true;
	}
	return $enabled;
}
add_filter( 'kadence_cloud_post_is_locked', 'custom_kadence_cloud_lock_pro_tag', 10, 3 );