Kadence 教學(xué):在 WooCommerce 中銷售 Kadence Pattern Hub 訪問(wèn)密鑰

在使用 Kadence Pattern Hub 時(shí),可以為設(shè)計(jì)庫(kù)生成訪問(wèn)密鑰,并為他人提供訪問(wèn)權(quán)限。若打算通過(guò) WooCommerce 銷售這些訪問(wèn)密鑰,需要使用更強(qiáng)大的工具來(lái)生成和管理密鑰。WooCommerce 軟件許可證插件和 License Manager for WooCommerce 插件是理想的解決方案。本文將介紹這兩種插件的基本設(shè)置方法,以及如何與 Kadence Pattern Hub 集成,進(jìn)行訪問(wèn)密鑰銷售。

圖片[1]-在 WooCommerce 中銷售 Kadence Pattern Hub 訪問(wèn)密鑰的完整指南

第一步:創(chuàng)建 Pattern Hub 訪問(wèn)產(chǎn)品

首先,在 WooCommerce 中創(chuàng)建一個(gè)新產(chǎn)品,該產(chǎn)品將用于銷售訪問(wèn)密鑰。設(shè)置產(chǎn)品為虛擬產(chǎn)品,不用發(fā)貨流程。

  • 創(chuàng)建虛擬產(chǎn)品
    進(jìn)入 WordPress 后臺(tái),選擇 WooCommerce -> 產(chǎn)品 -> 添加新產(chǎn)品,并設(shè)置為虛擬產(chǎn)品。
圖片[2]-在 WooCommerce 中銷售 Kadence Pattern Hub 訪問(wèn)密鑰的完整指南
  • 配置許可證
    根據(jù)所使用的插件(WooCommerce Software License 或 License Manager for WooCommerce),設(shè)置產(chǎn)品的許可證配置,并將其與訪問(wèn)密鑰生成器關(guān)聯(lián)。
圖片[3]-在 WooCommerce 中銷售 Kadence Pattern Hub 訪問(wèn)密鑰的完整指南
WooCommerce 軟件許可證(WooCommerce Software License)
圖片[4]-在 WooCommerce 中銷售 Kadence Pattern Hub 訪問(wèn)密鑰的完整指南
WooCommerce 許可證管理器(License Manager for WooCommerce)

第二步:連接 Kadence Pattern Hub 到許可證驗(yàn)證

WooCommerce 中銷售訪問(wèn)密鑰后,需要確保 Kadence Pattern Hub 能驗(yàn)證許可證的有效性。為此,需要添加自定義 PHP 過(guò)濾器。

WooCommerce Software License 過(guò)濾器設(shè)置

以下代碼段將在請(qǐng)求時(shí)檢查許可證是否有效,且是否已為請(qǐng)求的域名激活。如果密鑰有效并已激活,則返回允許訪問(wèn)。

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

限制訪問(wèn)到特定模式庫(kù)集合

如果你希望限制某個(gè)訪問(wèn)密鑰只能訪問(wèn)特定的模式庫(kù)集合,可以通過(guò)以下代碼實(shí)現(xiàn):

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

銷售多個(gè)集合的訪問(wèn)密鑰

如果你有多個(gè)模式集合需要銷售,可以通過(guò)為每個(gè)集合設(shè)置不同的許可證前綴來(lái)實(shí)現(xiàn):

/**
 * 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 過(guò)濾器設(shè)置

如果你使用的是 License Manager for WooCommerce 插件,以下 PHP 代碼將幫助你驗(yàn)證許可證的有效性:

/**
 * 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( 'LicenseManagerForWooCommerce\Repositories\Resources\License' ) ) {
		$license = \LicenseManagerForWooCommerce\Repositories\Resources\License::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 = \LicenseManagerForWooCommerce\Repositories\Resources\License::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 );

總結(jié)

結(jié)合 WooCommerce 軟件許可證插件或 License Manager for WooCommerce 插件,Kadence Pattern Hub 能提供強(qiáng)大的訪問(wèn)密鑰管理功能。這些插件能夠幫助你輕松管理訪問(wèn)密鑰,設(shè)置精確的權(quán)限控制,并支持多個(gè)集合的銷售,極大地方便了內(nèi)容銷售和管理。


聯(lián)系我們
教程看不懂?聯(lián)系我們?yōu)槟赓M(fèi)解答!免費(fèi)助力個(gè)人,小企站點(diǎn)!
客服微信
客服微信
電話:020-2206-9892
QQ咨詢:1025174874
郵件:info@361sale.com
工作時(shí)間:周一至周五,9:30-18:30,節(jié)假日休息
? 轉(zhuǎn)載聲明
本文作者:賊將鼠膽
THE END
喜歡就支持一下吧
點(diǎn)贊464 分享
評(píng)論 搶沙發(fā)

請(qǐng)登錄后發(fā)表評(píng)論

    暫無(wú)評(píng)論內(nèi)容