Sell Access to your Kadence Cloud

Sell access to your WordPress designs in the Kadence Pattern Hub

You want to sell access to your own WordPress Design Library in the Kadence Pattern Hub (formerly Kadence Cloud)? Then we set up the automatic assignment of license keys together. This sounds complicated at first, but it is not. In addition, the complete software for this promotion is available free of charge. At least if you already own the Kadence Pattern Hub.

Before that, you should read how to create Pro Templates for your Pattern Hub.

What do I need for this guide?

The following video shows you how to sell access to your WordPress designs and blocks:

Step 1 – Create your Cloud Access product

In this tutorial, we will not cover the use of WooCommerce subscriptions, but the process is very similar.

Create a new WooCommerce product. Create it as a virtual product and set up the license configuration depending on the plugin.

This is a screenshot of the paid WooCommerce Software License Plugin:

WooCommerce Software License Plugin
WooCommerce Software License Plugin

Here is a screenshot of the free License Manager for WooCommerce plugin:

License Manager for WooCommerce
License Manager for WooCommerce Plugin

Step 2 – Connect Kadence Pattern Hub with License Check

For this step, you need to add a custom PHP filter depending on which license manager plugin you are using. For this I recommend you to use a code snippet plugin to add the PHP filter.

WooCommerce Software License Filter Code:

The PHP filter below checks if the license is valid and if it is enabled for the domain of the request page. If enabled, true is returned. If it has not yet been activated for the domain, an attempt is made to activate it. If the key is not valid or is already in use on the maximum number of sites, access is prevented.

*Note: Make sure that you change the PRODUCT_UNIQUE_ID with the Unique ID that you set when creating your product in the license configuration (line 22).

/**
 * Validates license with WooCommerce Software License.
 *
 * @param Boolean $access true or false based on access.
 * @param String $key the access key.
 * @param WP_REST_Request $request full details about the request.
 * @return Boolean based on if access should be granted.
 */
function custom_check_cloud_access( $access, $key, $request ) {
	If true the key matches with settings in Kadence Cloud. Let that pass for testing purposes.
	if ( $access ) {
		return $access;
	}
	Make sure WooCommerce Software License exists.
	global $WOO_SL_API;
	if ( $WOO_SL_API ) {
		$site = preg_replace('(^https?:/ /)', '', $request->get_param( 'site' ) );
		$args = array(
			'licence_key' => $key,
			'domain' => $site,
			'woo_sl_action' => 'status-check',
			'product_unique_id' => 'PRODUCT_UNIQUE_ID',
		);
		$response = $WOO_SL_API >API_call( $args );
		$response = json_decode( $response );
		end( $response );
		$response_data = current( $response );
		if ( is_object( $response_data ) && 'success' === $response_data->status ) {
			Lets activate it for this domain if it's not.
			if ( $response_data->status_code && 's203' === $response_data->status_code ) {
				$args ['woo_sl_action'] = 'activate';
				$response = $WOO_SL_API >API_call( $args );
			}
			return true;
		} else if ( is_object( $response_data ) && 'error' === $response_data->status ) {
			Lets activate it for this domain if possible.
			if ( $response_data->status_code && 'e204' === $response_data->status_code ) {
				$args ['woo_sl_action'] = 'activate';
				$response = $WOO_SL_API >API_call( $args );
				$response = json_decode( $response );
				end( $response );
				$response_data = current( $response );
				if ( is_object( $response_data ) && 'success' === $response_data->status ) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	return $access;
}
add_filter( 'kadence_cloud_rest_request_access', 'custom_check_cloud_access', 10, 3 );

If you want to limit your WooCommerce software license access keys to a specific collection in your cloud, you can do so with the filter below. All you have to do is replace the"COLLECTION_SLUG"with the slug you want to send.

/**
 * Set access to a specific cloud library collection.
 *
 * @param array $args the query args for retrieving items.
 * @param string $key the access key.
 * @param array $request_extras the extra args for the request.
 * @return array with updated query args.
 */
function custom_kadence_cloud_query_args( $args, $key, $request_extras ) {
	if ( ! isset( $args ['tax_query'] ) ) {
		$args = ['tax_query'] array(
			array(
				'taxonomy' => 'kadence-cloud-collections',
				'field' => 'slug',
				'terms' => array( 'COLLECTION_SLUG'),
			),
		);
	}
	return $args;
}
add_filter( 'kadence_cloud_template_query_args', 'custom_kadence_cloud_query_args', 10, 3 );

License Manager for WooCommerce filter Code

The PHP filter below checks if the license is valid.

/**
 * Validates license with license manager for woocommerce.
 *
 * @param Boolean $access true or false based on access.
 * @param String $key the access key.
 * @param WP_REST_Request $request full details about the request.
 * @return Boolean based on if access should be granted.
 */
function custom_check_cloud_access( $access, $key, $request ) {
	If true the key matches with settings in Kadence Cloud.
	if ( $access ) {
		return $access;
	}
	Make sure license manager for woocommerce exists.
	if ( class_exists( 'LicenseManagerForWooCommerceRepositoriesResourcesLicense' ) ) {
		$license = LicenseManagerForWooCommerceRepositoriesResourcesLicense::instance()->findBy(
			array( 'hash' => apply_filters( 'lmfwc_hash', $key ) )
		);
		if ( ! $license ) {
			No license was found.
			return false;
		} else {
			Check if expired.
			$expiresAt = $license->getExpiresAt();
			$dateExpiresAt = new DateTime($expiresAt);
			$dateNow = new DateTime('now', new DateTimeZone('UTC'));
			if ( $dateNow < $dateExpiresAt ) {
				return false;
			}
			// Make sure it shows "activated".
			if ( intval( $license->getTimesActivated() ) < 1 ) {
				$timesActivatedNew = 1;
				$updatedLicense = LicenseManagerForWooCommerceRepositoriesResourcesLicense::instance()-> update(
					$license >getId(),
					array(
						'times_activated' => $timesActivatedNew
					)
				);
			}
			We have success lets return true.
			return true;
		}
	}
	return $access;
}
add_filter( 'kadence_cloud_rest_request_access', 'custom_check_cloud_access', 10, 3 );

If you have problems with the code, you can find more information directly from the developer.

Kadence Theme

Create stunning websites in minutes with this sleek and fast WordPress theme. Advanced web design!

Kadence Blocks

Kadence Blocks provides tools that allow you to create unique and meaningful content more easily in the native WordPress editor.

Conclusion

It’s already crass what the Kadence Pattern Hub and the complete Kadence WP Suite offers for possibilities. What started with the Kadence Theme is now a game changer for agencies and freelancers. With a little luck, a Kadence Discount is also active.

Leave a Reply

Your email address will not be published. Required fields are marked *