| Server IP : 43.141.49.107 / Your IP : 113.219.202.173 Web Server : Apache System : Linux VM-8-5-opencloudos 6.6.34-9.oc9.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Jun 19 19:35:45 CST 2024 x86_64 User : www ( 1000) PHP Version : 8.1.27 Disable Function : passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /www/wwwroot/www.ucppt.com/wp-content/plugins/20251214021435751/ |
Upload File : |
<?php
/**
* Plugin Name: WPCOM Member Custom API
* Plugin URI: https://www.ucppt.com
* Description: REST API endpoints for WPCOM Member Pro integration with Next.js frontend
* Version: 1.0.0
* Author: UCPPT Team
* Author URI: https://www.ucppt.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wpcom-custom-api
* Requires at least: 5.0
* Requires PHP: 7.2
*
* 功能:
* - 提供 WPCOM Member Pro 会员信息 REST API
* - 支持 JWT Token 认证(需配合 Simple JWT Login 插件)
* - 暴露会员等级、订单、钱包等数据给前端应用
*
* API 端点:
* - GET /wp-json/custom/v1/user-membership/{user_id} - 获取指定用户会员信息
* - GET /wp-json/custom/v1/my-membership - 获取当前用户会员信息
* - GET /wp-json/custom/v1/user-orders/{user_id} - 获取用户订单
* - GET /wp-json/custom/v1/user-wallet/{user_id} - 获取用户钱包
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
// 定义插件常量
define('WPCOM_CUSTOM_API_VERSION', '1.0.0');
define('WPCOM_CUSTOM_API_FILE', __FILE__);
define('WPCOM_CUSTOM_API_PATH', plugin_dir_path(__FILE__));
define('WPCOM_CUSTOM_API_URL', plugin_dir_url(__FILE__));
// 注册自定义 REST API 端点
add_action('rest_api_init', 'wpcom_custom_api_register_routes');
function wpcom_custom_api_register_routes() {
// 获取用户会员信息
register_rest_route('custom/v1', '/user-membership/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'wpcom_get_user_membership_info',
'permission_callback' => function() {
return current_user_can('read');
},
'args' => array(
'id' => array(
'validate_callback' => function($param) {
return is_numeric($param);
}
)
)
));
// 获取当前用户会员信息(无需user_id)
register_rest_route('custom/v1', '/my-membership', array(
'methods' => 'GET',
'callback' => 'wpcom_get_current_user_membership',
'permission_callback' => function() {
return is_user_logged_in();
}
));
// 获取用户订单列表
register_rest_route('custom/v1', '/user-orders/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'wpcom_get_user_orders_info',
'permission_callback' => function() {
return current_user_can('read');
},
'args' => array(
'id' => array(
'validate_callback' => function($param) {
return is_numeric($param);
}
)
)
));
// 获取用户钱包信息
register_rest_route('custom/v1', '/user-wallet/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'wpcom_get_user_wallet_info',
'permission_callback' => function() {
return current_user_can('read');
},
'args' => array(
'id' => array(
'validate_callback' => function($param) {
return is_numeric($param);
}
)
)
));
}
/**
* 获取用户会员信息
*/
function wpcom_get_user_membership_info($request) {
global $wpdb;
$user_id = $request['id'];
$result = array(
'user_id' => $user_id,
'membership' => null,
'orders' => array(),
'meta' => array()
);
// 1. 查询用户元数据中的会员信息
$vip_level = get_user_meta($user_id, 'vip_level', true);
$vip_expire = get_user_meta($user_id, 'vip_expire', true);
$vip_status = get_user_meta($user_id, 'vip_status', true);
if ($vip_level || $vip_expire || $vip_status) {
$result['membership'] = array(
'level' => $vip_level,
'expire_date' => $vip_expire,
'status' => $vip_status,
'is_active' => $vip_status === 'active' && time() < strtotime($vip_expire)
);
}
// 2. 查询 WPCOM 订单表(如果存在)
$table_name = $wpdb->prefix . 'wpcom_orders';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
$orders = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 10",
$user_id
), ARRAY_A);
$result['orders'] = $orders;
}
// 3. 查询 WooCommerce Memberships(如果使用)
if (function_exists('wc_memberships_get_user_memberships')) {
$wc_memberships = wc_memberships_get_user_memberships($user_id);
$result['wc_memberships'] = array();
foreach ($wc_memberships as $membership) {
$result['wc_memberships'][] = array(
'id' => $membership->get_id(),
'plan_id' => $membership->get_plan_id(),
'plan_name' => $membership->get_plan()->get_name(),
'status' => $membership->get_status(),
'start_date' => $membership->get_start_date('Y-m-d H:i:s'),
'end_date' => $membership->get_end_date('Y-m-d H:i:s'),
'is_active' => $membership->is_active()
);
}
}
// 4. 收集所有会员相关的 meta 字段
$all_meta = get_user_meta($user_id);
foreach ($all_meta as $key => $value) {
if (preg_match('/(vip|member|wallet|point|commission)/i', $key)) {
$result['meta'][$key] = maybe_unserialize($value[0]);
}
}
return new WP_REST_Response($result, 200);
}
/**
* 获取当前用户会员信息
*/
function wpcom_get_current_user_membership($request) {
$user_id = get_current_user_id();
$request['id'] = $user_id;
return wpcom_get_user_membership_info($request);
}
/**
* 获取用户订单信息
*/
function wpcom_get_user_orders_info($request) {
global $wpdb;
$user_id = $request['id'];
$result = array(
'user_id' => $user_id,
'wpcom_orders' => array(),
'wc_orders' => array()
);
// 1. 查询 WPCOM 订单
$table_name = $wpdb->prefix . 'wpcom_orders';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
$orders = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 50",
$user_id
), ARRAY_A);
$result['wpcom_orders'] = $orders;
}
// 2. 查询 WooCommerce 订单
if (function_exists('wc_get_orders')) {
$wc_orders = wc_get_orders(array(
'customer_id' => $user_id,
'limit' => 50,
'orderby' => 'date',
'order' => 'DESC'
));
foreach ($wc_orders as $order) {
$result['wc_orders'][] = array(
'id' => $order->get_id(),
'status' => $order->get_status(),
'total' => $order->get_total(),
'currency' => $order->get_currency(),
'date_created' => $order->get_date_created()->date('Y-m-d H:i:s'),
'payment_method' => $order->get_payment_method(),
'items' => array_map(function($item) {
return array(
'name' => $item->get_name(),
'quantity' => $item->get_quantity(),
'total' => $item->get_total()
);
}, $order->get_items())
);
}
}
return new WP_REST_Response($result, 200);
}
/**
* 获取用户钱包信息
*/
function wpcom_get_user_wallet_info($request) {
global $wpdb;
$user_id = $request['id'];
$result = array(
'user_id' => $user_id,
'balance' => 0,
'frozen' => 0,
'records' => array()
);
// 查询钱包余额(meta)
$balance = get_user_meta($user_id, 'wallet_balance', true);
$frozen = get_user_meta($user_id, 'wallet_frozen', true);
$result['balance'] = $balance ? floatval($balance) : 0;
$result['frozen'] = $frozen ? floatval($frozen) : 0;
$result['total'] = $result['balance'] + $result['frozen'];
// 查询钱包交易记录
$table_name = $wpdb->prefix . 'wpcom_wallet_records';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
$records = $wpdb->get_results($wpdb->prepare(
"SELECT * FROM $table_name WHERE user_id = %d ORDER BY created_at DESC LIMIT 100",
$user_id
), ARRAY_A);
$result['records'] = $records;
}
return new WP_REST_Response($result, 200);
}
/**
* 插件激活时的钩子
*/
register_activation_hook(__FILE__, 'wpcom_custom_api_activate');
function wpcom_custom_api_activate() {
// 刷新 WordPress 重写规则
flush_rewrite_rules();
}
/**
* 插件停用时的钩子
*/
register_deactivation_hook(__FILE__, 'wpcom_custom_api_deactivate');
function wpcom_custom_api_deactivate() {
// 刷新 WordPress 重写规则
flush_rewrite_rules();
}