Kadence 教学:在 WooCommerce 中销售 Kadence Pattern Hub 访问密钥

在使用 Kadence Pattern Hub 时,可以为设计库生成访问密钥,并为他人提供访问权限。若打算通过 WooCommerce 销售这些访问密钥,需要使用更强大的工具来生成和管理密钥。WooCommerce 软件许可证插件和 License Manager for WooCommerce 插件是理想的解决方案。本文将介绍这两种插件的基本设置方法,以及如何与 Kadence Pattern Hub 集成,进行访问密钥销售。

第一步:创建 Pattern Hub 访问产品

首先,在 WooCommerce 中创建一个新产品,该产品将用于销售访问密钥。设置产品为虚拟产品,不用发货流程。

创建虚拟产品
进入 WordPress 后台,选择 WooCommerce -> 产品 -> 添加新产品,并设置为虚拟产品。

配置许可证
根据所使用的插件(WooCommerce Software License 或 License Manager for WooCommerce),设置产品的许可证配置,并将其与访问密钥生成器关联。

WooCommerce 软件许可证(WooCommerce Software License)
WooCommerce 许可证管理器(License Manager for WooCommerce)

第二步:连接 Kadence Pattern Hub 到许可证验证

WooCommerce 中销售访问密钥后,需要确保 Kadence Pattern Hub 能验证许可证的有效性。为此,需要添加自定义 PHP 过滤器。

WooCommerce Software License 过滤器设置

以下代码段将在请求时检查许可证是否有效,且是否已为请求的域名激活。如果密钥有效并已激活,则返回允许访问。

/**
* 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 Pattern Hub. 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 );

限制访问到特定模式库集合

如果你希望限制某个访问密钥只能访问特定的模式库集合,可以通过以下代码实现:

/**
* Set access to a specific pattern hub 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 );

销售多个集合的访问密钥

如果你有多个模式集合需要销售,可以通过为每个集合设置不同的许可证前缀来实现:

/**
* 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 Pattern Hub. Let that pass for testing purposes.
if ( $access ) {
return $access;
}
// Make sure WooCommerce Software License exists.
global $WOO_SL_API;
if ( $WOO_SL_API ) {
if ( substr( $key, 0, strlen( ‘col_one’ ) ) === ‘col_one’ ) {
$product_id = ‘PRODUCT_UNIQUE_ID’;
} else {
$product_id = ‘PRODUCT_UNIQUE_ID_TWO’;
}
$site = preg_replace(‘(^https?://)’, ”, $request->get_param( ‘site’ ) );
$args = array(
‘licence_key’ => $key,
‘domain’ => $site,
‘woo_sl_action’ => ‘status-check’,
‘product_unique_id’ => $product_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 );

License Manager for WooCommerce 过滤器设置

如果你使用的是 License Manager for WooCommerce 插件,以下 PHP 代码将帮助你验证许可证的有效性:

/**
* 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 );

总结

结合 WooCommerce 软件许可证插件或 License Manager for WooCommerce 插件,Kadence Pattern Hub 能提供强大的访问密钥管理功能。这些插件能够帮助你轻松管理访问密钥,设置精确的权限控制,并支持多个集合的销售,极大地方便了内容销售和管理。

Leave a Reply

您的电子邮箱地址不会被公开。 必填项已用 * 标注