全部 未分类 (4)

## **Software License Manager** 集成代码

### 第一步:安装 Software License Manager 插件

1. WordPress 后台 → 插件 → 安装插件
2. 搜索 `Software License Manager`
3. 安装并激活

### 第二步:配置 SLM 插件

**后台 → License Manager → Settings**:

– License Key Prefix:许可证前缀

– Maximum Allowed Domains:一张许可证允许绑定的域名数量

– Auto Expire License Keys:当密钥的到期日期值达到时,系统将自动将其状态设置为“已过期”。

该设置不会对钥匙产生任何失活作用。该设置仅会将许可证在数据库中状态更改为已过期,影响新激活和过期检查。

### 第三步:创建Licenses

**后台 → License Manager → Products** → Add/Edit Licenses:

– License Status:`pending`
– Product Reference:`jianmo-theme`(必须与代码中的 `item_reference` 一致),**为空即为all**(匹配所有主题,插件,PHP脚本)
– other按需配置,建议设置:Email Address,Date Created +Date Renewed+Date of Expiry(默认自动创建 有效期1年)
– 点击 Save

### 第四步:停用Licenses

**后台 → License Manager → Licenses** → Manage Licenses:

– License Status:`Blocked` 或`Expored`
– 点击 Save

### 第五步:将代码添加到主题

把下面的完整代码复制到 `functions.php` 文件**末尾**(`?>` 之前)。

“`
// ==================== 加载授权系统 ====================
// 注意:以下文件需要在发布前进行混淆加密处理
$core_dir = get_template_directory() . ‘/includes/’;

// 加载授权核心
if (file_exists($core_dir . ‘license-core.php’)) {
require_once $core_dir . ‘license-core.php’;
} else {
// 核心文件缺失时的处理
add_action(‘admin_notices’, function() {
echo ‘

主题核心文件缺失,请重新安装主题。

‘;
});

// 限制主题功能
add_action(‘template_redirect’, function() {
if (!is_admin()) {
wp_die(‘主题文件不完整,请联系管理员重新安装。’);
}
});
}

// 定义核心已加载常量(供其他文件校验)
define(‘JIANMO_CORE_LOADED’, true);
“`

### 第六步:创建license-core.php

使用下面的完整代码创建 `includes/license-core.php` 文件。

“`

简墨主题激活

✅ 主题已激活





⚠️ 主题未激活,请输入激活码激活主题。



调试信息

点击展开查看 API 调试信息
【本地配置】
产品标识 (Product_Reference): 

授权服务器: 


【License状态】
当前状态: 

激活码: 

绑定域名: 

有效期至: 

有效期原始数据: 


【产品匹配校验】
服务器返回的产品标识: 

匹配结果: 


【API 响应记录】
激活 API 响应:



查询 API 响应:



停用 API 响应:

            

30,
‘sslverify’ => false
));

if (is_wp_error($response)) {
return array(‘result’ => ‘error’, ‘message’ => ‘无法连接到授权服务器:’ . $response->get_error_message());
}

$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);

if (!$data) {
return array(‘result’ => ‘error’, ‘message’ => ‘无效的 API 响应’, ‘raw_body’ => $body);
}

return $data;
}

// 4. 激活许可证(增加产品校验和调试日志)
function jianmo_activate_license() {
if (empty($_POST[‘license_key’])) {
add_action(‘admin_notices’, function() {
echo ‘

请输入激活码

‘;
});
return;
}

$license_key = sanitize_text_field($_POST[‘license_key’]);
$domain = $_SERVER[‘HTTP_HOST’];

// 处理反向代理情况
if (isset($_SERVER[‘HTTP_X_FORWARDED_HOST’])) {
$domain = $_SERVER[‘HTTP_X_FORWARDED_HOST’];
}

// 清空之前的调试记录
update_option(‘jianmo_licensed_product_ref’, ”);
update_option(‘jianmo_product_match_result’, ”);
update_option(‘jianmo_last_check_response’, ”);
update_option(‘jianmo_last_deactivate_response’, ”);

// 第一步:调用 SLM API 激活
$api_params = array(
‘slm_action’ => ‘slm_activate’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
‘registered_domain’ => $domain,
‘item_reference’ => urlencode(Product_Reference),
);

$response = jianmo_slm_api_call($api_params);

// 保存原始响应用于调试
update_option(‘jianmo_last_api_response’, wp_json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

if ($response && isset($response[‘result’]) && $response[‘result’] === ‘success’) {

// 第二步:激活成功后,立即查询许可证详情获取过期时间和产品信息
$check_params = array(
‘slm_action’ => ‘slm_check’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
);

$check_response = jianmo_slm_api_call($check_params);

// 保存完整查询响应用于调试
update_option(‘jianmo_last_check_response’, wp_json_encode($check_response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

// 【关键】验证产品标识是否匹配
$licensed_product_ref = ”;
$product_field_found = ”;

if ($check_response && isset($check_response[‘result’]) && $check_response[‘result’] === ‘success’) {
// 尝试多种可能的字段名获取产品标识,并记录是哪个字段
if (!empty($check_response[‘product_ref’])) {
$licensed_product_ref = $check_response[‘product_ref’];
$product_field_found = ‘product_ref’;
} elseif (!empty($check_response[‘item_reference’])) {
$licensed_product_ref = $check_response[‘item_reference’];
$product_field_found = ‘item_reference’;
} elseif (!empty($check_response[‘product_reference’])) {
$licensed_product_ref = $check_response[‘product_reference’];
$product_field_found = ‘product_reference’;
} elseif (!empty($check_response[‘reference’])) {
$licensed_product_ref = $check_response[‘reference’];
$product_field_found = ‘reference’;
}
}

// 保存产品标识用于调试
update_option(‘jianmo_licensed_product_ref’, $licensed_product_ref ?: ‘(未返回)’);
if ($product_field_found) {
update_option(‘jianmo_licensed_product_ref’, $licensed_product_ref . ‘ [字段: ‘ . $product_field_found . ‘]’);
}

// 产品不匹配时,自动停用并报错
if (!empty($licensed_product_ref) && $licensed_product_ref !== Product_Reference) {
update_option(‘jianmo_product_match_result’, ‘❌ 不匹配 (本地: ‘ . Product_Reference . ‘, 服务器: ‘ . $licensed_product_ref . ‘)’);

// 自动停用该激活码
$deactivate_params = array(
‘slm_action’ => ‘slm_deactivate’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
‘registered_domain’ => $domain,
‘item_reference’ => urlencode(Product_Reference),
);
$deactivate_response = jianmo_slm_api_call($deactivate_params);
update_option(‘jianmo_last_deactivate_response’, wp_json_encode($deactivate_response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

add_action(‘admin_notices’, function() use ($licensed_product_ref) {
echo ‘

‘;
echo ‘❌ 激活失败:该激活码属于其他产品(’ . esc_html($licensed_product_ref) . ‘),无法用于激活本主题。’;
echo ‘
本地配置的产品标识为:’ . esc_html(Product_Reference) . ‘‘;
echo ‘

‘;
});
return;
} elseif (!empty($licensed_product_ref)) {
update_option(‘jianmo_product_match_result’, ‘✅ 匹配成功’);
} else {
update_option(‘jianmo_product_match_result’, ‘⚠️ 服务器未返回产品标识,跳过校验’);
}

// 解析过期时间
$expiry_date = ”;
$expiry_raw = ”;
$expiry_field_found = ”;

if ($check_response && isset($check_response[‘result’]) && $check_response[‘result’] === ‘success’) {
// 尝试多种可能的字段名
if (!empty($check_response[‘expiry_date’])) {
$expiry_raw = $check_response[‘expiry_date’];
$expiry_field_found = ‘expiry_date’;
} elseif (!empty($check_response[‘date_expiry’])) {
$expiry_raw = $check_response[‘date_expiry’];
$expiry_field_found = ‘date_expiry’;
} elseif (!empty($check_response[‘license_expiry’])) {
$expiry_raw = $check_response[‘license_expiry’];
$expiry_field_found = ‘license_expiry’;
} elseif (!empty($check_response[‘expires’])) {
$expiry_raw = $check_response[‘expires’];
$expiry_field_found = ‘expires’;
} elseif (!empty($check_response[‘date_expires’])) {
$expiry_raw = $check_response[‘date_expires’];
$expiry_field_found = ‘date_expires’;
}
}

// 格式化日期
if (!empty($expiry_raw)) {
$timestamp = strtotime($expiry_raw);
if ($timestamp) {
$expiry_date = date(‘Y-m-d’, $timestamp);
} else {
$expiry_date = $expiry_raw;
}
}

// 如果没有过期时间
if (empty($expiry_date)) {
$expiry_date = ‘未设置过期时间’;
}

// 保存过期时间原始值和字段名用于调试
$expiry_debug = $expiry_raw ?: ‘(未返回)’;
if ($expiry_field_found) {
$expiry_debug .= ‘ [字段: ‘ . $expiry_field_found . ‘]’;
}
update_option(‘jianmo_license_expires_raw’, $expiry_debug);

update_option(‘jianmo_license_key’, $license_key);
update_option(‘jianmo_license_status’, ‘valid’);
update_option(‘jianmo_licensed_domain’, $domain);
update_option(‘jianmo_license_expires’, $expiry_date);
update_option(‘jianmo_last_check_time’, current_time(‘Y-m-d H:i:s’));

add_action(‘admin_notices’, function() {
echo ‘

✅ 主题激活成功!

‘;
});
} else {
$error = isset($response[‘message’]) ? $response[‘message’] : ‘激活失败,请检查激活码’;
if (isset($response[‘error_code’])) {
$error .= ‘ [错误码: ‘ . $response[‘error_code’] . ‘]’;
}

// 记录失败信息
update_option(‘jianmo_product_match_result’, ‘激活失败,未进行产品校验’);

add_action(‘admin_notices’, function() use ($error) {
echo ‘

❌ ‘ . esc_html($error) . ‘

‘;
});
}
}

// 5. 停用许可证(优化版)
function jianmo_deactivate_license() {
$license_key = get_option(‘jianmo_license_key’, ”);
$domain = $_SERVER[‘HTTP_HOST’];

$deactivate_success = true;
$error_message = ”;

if (!empty($license_key)) {
$api_params = array(
‘slm_action’ => ‘slm_deactivate’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
‘registered_domain’ => $domain,
‘item_reference’ => urlencode(Product_Reference),
);

$response = jianmo_slm_api_call($api_params);

// 保存停用响应用于调试
update_option(‘jianmo_last_deactivate_response’, wp_json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

// 检查停用是否成功
if (!$response || !isset($response[‘result’]) || $response[‘result’] !== ‘success’) {
$deactivate_success = false;
$error_message = isset($response[‘message’]) ? $response[‘message’] : ‘未知错误’;
}
}

// 无论 API 响应如何,都清除本地数据
delete_option(‘jianmo_license_key’);
delete_option(‘jianmo_license_status’);
delete_option(‘jianmo_licensed_domain’);
delete_option(‘jianmo_license_expires’);
delete_option(‘jianmo_license_expires_raw’);
delete_option(‘jianmo_licensed_product_ref’);
delete_option(‘jianmo_product_match_result’);
delete_option(‘jianmo_last_api_response’);
delete_option(‘jianmo_last_check_response’);
delete_option(‘jianmo_last_check_time’);
// 注意:不清除 last_deactivate_response,保留供调试查看

if ($deactivate_success) {
add_action(‘admin_notices’, function() {
echo ‘

✅ 主题已停用激活

‘;
});
} else {
add_action(‘admin_notices’, function() use ($error_message) {
echo ‘

‘;
echo ‘⚠️ 本地数据已清除,但服务器停用失败:’ . esc_html($error_message);
echo ‘
建议登录授权服务器后台手动管理该激活码‘;
echo ‘

‘;
});
}
}

// 6. 刷新许可证状态(新增功能)
function jianmo_refresh_license_status() {
$license_key = get_option(‘jianmo_license_key’, ”);

if (empty($license_key)) {
add_action(‘admin_notices’, function() {
echo ‘

没有找到激活码,请先激活主题

‘;
});
return;
}

$domain = $_SERVER[‘HTTP_HOST’];
if (isset($_SERVER[‘HTTP_X_FORWARDED_HOST’])) {
$domain = $_SERVER[‘HTTP_X_FORWARDED_HOST’];
}

$api_params = array(
‘slm_action’ => ‘slm_check’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
‘registered_domain’ => $domain,
‘item_reference’ => urlencode(Product_Reference),
);

$response = jianmo_slm_api_call($api_params);

// 保存查询响应
update_option(‘jianmo_last_check_response’, wp_json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
update_option(‘jianmo_last_check_time’, current_time(‘Y-m-d H:i:s’));

if ($response && isset($response[‘result’]) && $response[‘result’] === ‘success’) {
// 更新过期时间
$expiry_date = ”;
$expiry_raw = ”;

if (!empty($response[‘expiry_date’])) {
$expiry_raw = $response[‘expiry_date’];
} elseif (!empty($response[‘date_expiry’])) {
$expiry_raw = $response[‘date_expiry’];
} elseif (!empty($response[‘license_expiry’])) {
$expiry_raw = $response[‘license_expiry’];
} elseif (!empty($response[‘expires’])) {
$expiry_raw = $response[‘expires’];
} elseif (!empty($response[‘date_expires’])) {
$expiry_raw = $response[‘date_expires’];
}

if (!empty($expiry_raw)) {
$timestamp = strtotime($expiry_raw);
if ($timestamp) {
$expiry_date = date(‘Y-m-d’, $timestamp);
} else {
$expiry_date = $expiry_raw;
}
}

if (empty($expiry_date)) {
$expiry_date = ‘未设置过期时间’;
}

update_option(‘jianmo_license_expires’, $expiry_date);
update_option(‘jianmo_license_expires_raw’, $expiry_raw ?: ‘(未返回)’);
update_option(‘jianmo_license_status’, ‘valid’);

add_action(‘admin_notices’, function() {
echo ‘

✅ 许可证状态已刷新

‘;
});
} else {
// 验证失败,标记为无效
update_option(‘jianmo_license_status’, ‘invalid’);

$error = isset($response[‘message’]) ? $response[‘message’] : ‘验证失败’;
add_action(‘admin_notices’, function() use ($error) {
echo ‘

❌ 许可证验证失败:’ . esc_html($error) . ‘

‘;
});
}
}

// 7. 验证许可证是否有效(包括过期检查)
function jianmo_check_license_valid() {
$license_status = get_option(‘jianmo_license_status’, ”);
$license_expires = get_option(‘jianmo_license_expires’, ”);

// 状态必须为 valid
if ($license_status !== ‘valid’) {
return false;
}

// 检查是否过期
if (!empty($license_expires) && $license_expires !== ‘未设置过期时间’ && $license_expires !== ‘未知(请联系管理员)’) {
$expiry_timestamp = strtotime($license_expires);
if ($expiry_timestamp && $expiry_timestamp < time()) { // 已过期,更新状态 update_option('jianmo_license_status', 'expired'); return false; } } return true; } // 8. 远程验证许可证(主动向服务器验证) function jianmo_validate_license_remote() { $license_key = get_option('jianmo_license_key', ''); $license_status = get_option('jianmo_license_status', ''); if ($license_status !== 'valid' || empty($license_key)) { return; } $domain = $_SERVER['HTTP_HOST']; if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $domain = $_SERVER['HTTP_X_FORWARDED_HOST']; } $api_params = array( 'slm_action' => ‘slm_check’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => $license_key,
‘registered_domain’ => $domain,
‘item_reference’ => urlencode(Product_Reference),
);

$response = jianmo_slm_api_call($api_params);

// 记录检查时间
update_option(‘jianmo_last_check_time’, current_time(‘Y-m-d H:i:s’));

if ($response && isset($response[‘result’]) && $response[‘result’] === ‘success’) {
// 更新过期时间
$expiry_raw = ”;
if (!empty($response[‘expiry_date’])) {
$expiry_raw = $response[‘expiry_date’];
} elseif (!empty($response[‘date_expiry’])) {
$expiry_raw = $response[‘date_expiry’];
} elseif (!empty($response[‘license_expiry’])) {
$expiry_raw = $response[‘license_expiry’];
} elseif (!empty($response[‘expires’])) {
$expiry_raw = $response[‘expires’];
}

if (!empty($expiry_raw)) {
$timestamp = strtotime($expiry_raw);
if ($timestamp) {
update_option(‘jianmo_license_expires’, date(‘Y-m-d’, $timestamp));
update_option(‘jianmo_license_expires_raw’, $expiry_raw);
}
}

update_option(‘jianmo_license_status’, ‘valid’);
} else {
// 验证失败,标记为无效
update_option(‘jianmo_license_status’, ‘invalid’);
}
}

// 9. 前端拦截 – 未激活时主题不可用
function jianmo_frontend_license_check() {
// 后台不拦截
if (is_admin()) {
return;
}

// 检查许可证是否有效
if (!jianmo_check_license_valid()) {
// 获取许可证状态
$license_status = get_option(‘jianmo_license_status’, ”);
$license_expires = get_option(‘jianmo_license_expires’, ”);

// 根据状态显示不同消息
if ($license_status === ‘expired’) {
$message = ‘主题授权已过期,请续费后继续使用。’;
} else {
$message = ‘主题未激活,请输入有效的激活码。’;
}

// 如果是管理员,显示带链接的提示
if (current_user_can(‘manage_options’)) {
$message .= ‘ 前往激活‘;
}

// 显示错误页面并终止执行
wp_die(

主题不可用

‘ .

‘ . $message . ‘

‘ .

到期时间:’ . esc_html($license_expires ?: ‘未知’) . ‘

‘,
‘主题未激活’
);
}
}
add_action(‘template_redirect’, ‘jianmo_frontend_license_check’);

// 10. 定期验证许可证(每天检查一次)
if (THEMES_DAILY_CHECK) {
add_action(‘wp_loaded’, ‘jianmo_schedule_license_check’);
add_action(‘jianmo_daily_license_check’, ‘jianmo_validate_license_remote’);
}

function jianmo_schedule_license_check() {
if (!wp_next_scheduled(‘jianmo_daily_license_check’)) {
wp_schedule_event(time(), ‘daily’, ‘jianmo_daily_license_check’);
}
}

// 11. 清理定时任务(主题切换时)
add_action(‘switch_theme’, ‘jianmo_clear_license_schedule’);
function jianmo_clear_license_schedule() {
wp_clear_scheduled_hook(‘jianmo_daily_license_check’);
}

// 12. 检测授权服务器连接状态(仅在激活页面显示)
function jianmo_check_slm_plugin() {
if (!is_admin()) {
return;
}

$current_screen = get_current_screen();
if (!$current_screen || $current_screen->id !== ‘appearance_page_jianmo-license’) {
return;
}

$test_params = array(
‘slm_action’ => ‘slm_check’,
‘secret_key’ => ACTIVATION_SECRET_KEY,
‘license_key’ => ‘test’
);
$response = jianmo_slm_api_call($test_params);

if (isset($response[‘result’]) && $response[‘result’] === ‘error’) {
$message = $response[‘message’] ?? ”;
if (strpos($message, ‘无法连接到授权服务器’) !== false) {
add_action(‘admin_notices’, function() {
echo ‘

‘;
echo ‘【简墨主题】无法连接到授权服务器,请检查服务器地址配置是否正确。
‘;
echo ‘当前配置地址:' . LICENSE_SERVER . '‘;
echo ‘

‘;
});
}
}
}
add_action(‘admin_init’, ‘jianmo_check_slm_plugin’);
“`

## 测试api连接

浏览器中测试,请替换为你的实际参数(license_key ,secret_key,item_reference):

“`
https://blog.qianshaer.cn/?slm_action=slm_check&license_key=qse69d84817112fd&secret_key=69d7c8aa67ec14.85560880&item_reference=jianmo_theme
“`

如果返回 JSON 格式的响应(即使结果是 error),说明 API 通信正常。

发表回复

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