- phpcbf 自动修复 3893 处格式问题 - 添加 phpcs.xml 排除不适用的规则 - 安全相关规则降级为 warning 保持可见 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
355 lines
9.1 KiB
PHP
355 lines
9.1 KiB
PHP
<?php
|
||
/**
|
||
* EasyPay HPOS (High-Performance Order Storage) Support
|
||
*
|
||
* 为易支付插件提供WooCommerce高性能订单存储系统支持
|
||
* 确保与传统订单存储和新的HPOS系统兼容
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit; // Exit if accessed directly
|
||
}
|
||
|
||
class EasyPay_HPOS_Support {
|
||
|
||
/**
|
||
* 构造函数
|
||
*/
|
||
public function __construct() {
|
||
$this->init_hooks();
|
||
}
|
||
|
||
/**
|
||
* 初始化钩子
|
||
*/
|
||
private function init_hooks() {
|
||
// 声明HPOS兼容性
|
||
add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
|
||
|
||
// 添加订单元数据处理钩子
|
||
add_action( 'woocommerce_new_order', array( $this, 'handle_new_order' ), 10, 1 );
|
||
add_action( 'woocommerce_order_status_changed', array( $this, 'handle_order_status_change' ), 10, 4 );
|
||
|
||
// 添加管理界面支持
|
||
add_filter( 'woocommerce_order_data_store_cpt_get_orders_query', array( $this, 'handle_custom_query_var' ), 10, 2 );
|
||
|
||
// 确保支付网关数据正确存储
|
||
add_action( 'woocommerce_checkout_order_processed', array( $this, 'store_payment_gateway_data' ), 10, 3 );
|
||
}
|
||
|
||
/**
|
||
* 声明HPOS兼容性
|
||
*/
|
||
public function declare_hpos_compatibility() {
|
||
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
|
||
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
|
||
'custom_order_tables',
|
||
WC_Freepay_FILE,
|
||
true
|
||
);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 检查是否启用了HPOS
|
||
*
|
||
* @return bool
|
||
*/
|
||
public function is_hpos_enabled() {
|
||
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
|
||
return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled();
|
||
}
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* 获取订单元数据(兼容HPOS和传统存储)
|
||
*
|
||
* @param int|WC_Order $order 订单ID或订单对象
|
||
* @param string $meta_key 元数据键
|
||
* @param bool $single 是否返回单个值
|
||
* @return mixed
|
||
*/
|
||
public function get_order_meta( $order, $meta_key, $single = true ) {
|
||
if ( ! $order instanceof WC_Order ) {
|
||
$order = wc_get_order( $order );
|
||
}
|
||
|
||
if ( ! $order ) {
|
||
return false;
|
||
}
|
||
|
||
// 使用WooCommerce的标准方法,自动处理HPOS兼容性
|
||
return $order->get_meta( $meta_key, $single );
|
||
}
|
||
|
||
/**
|
||
* 更新订单元数据(兼容HPOS和传统存储)
|
||
*
|
||
* @param int|WC_Order $order 订单ID或订单对象
|
||
* @param string $meta_key 元数据键
|
||
* @param mixed $meta_value 元数据值
|
||
* @return bool
|
||
*/
|
||
public function update_order_meta( $order, $meta_key, $meta_value ) {
|
||
if ( ! $order instanceof WC_Order ) {
|
||
$order = wc_get_order( $order );
|
||
}
|
||
|
||
if ( ! $order ) {
|
||
return false;
|
||
}
|
||
|
||
// 使用WooCommerce的标准方法,自动处理HPOS兼容性
|
||
$order->update_meta_data( $meta_key, $meta_value );
|
||
$order->save();
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 删除订单元数据(兼容HPOS和传统存储)
|
||
*
|
||
* @param int|WC_Order $order 订单ID或订单对象
|
||
* @param string $meta_key 元数据键
|
||
* @return bool
|
||
*/
|
||
public function delete_order_meta( $order, $meta_key ) {
|
||
if ( ! $order instanceof WC_Order ) {
|
||
$order = wc_get_order( $order );
|
||
}
|
||
|
||
if ( ! $order ) {
|
||
return false;
|
||
}
|
||
|
||
// 使用WooCommerce的标准方法,自动处理HPOS兼容性
|
||
$order->delete_meta_data( $meta_key );
|
||
$order->save();
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 处理新订单创建
|
||
*
|
||
* @param int $order_id 订单ID
|
||
*/
|
||
public function handle_new_order( $order_id ) {
|
||
$order = wc_get_order( $order_id );
|
||
if ( ! $order ) {
|
||
return;
|
||
}
|
||
|
||
// 为易支付订单添加标识
|
||
if ( $this->is_easypay_order( $order ) ) {
|
||
$this->update_order_meta( $order, '_easypay_order', 'yes' );
|
||
$this->update_order_meta( $order, '_easypay_created_time', current_time( 'timestamp' ) );
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理订单状态变更
|
||
*
|
||
* @param int $order_id 订单ID
|
||
* @param string $old_status 旧状态
|
||
* @param string $new_status 新状态
|
||
* @param WC_Order $order 订单对象
|
||
*/
|
||
public function handle_order_status_change( $order_id, $old_status, $new_status, $order ) {
|
||
if ( ! $this->is_easypay_order( $order ) ) {
|
||
return;
|
||
}
|
||
|
||
// 记录状态变更历史
|
||
$status_history = $this->get_order_meta( $order, '_easypay_status_history', false );
|
||
if ( ! is_array( $status_history ) ) {
|
||
$status_history = array();
|
||
}
|
||
|
||
$status_history[] = array(
|
||
'from' => $old_status,
|
||
'to' => $new_status,
|
||
'timestamp' => current_time( 'timestamp' ),
|
||
'datetime' => current_time( 'mysql' ),
|
||
);
|
||
|
||
$this->update_order_meta( $order, '_easypay_status_history', $status_history );
|
||
|
||
// 处理支付完成状态
|
||
if ( $new_status === 'processing' || $new_status === 'completed' ) {
|
||
$this->update_order_meta( $order, '_easypay_payment_completed_time', current_time( 'timestamp' ) );
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理自定义查询变量
|
||
*
|
||
* @param array $query 查询参数
|
||
* @param array $query_vars 查询变量
|
||
* @return array
|
||
*/
|
||
public function handle_custom_query_var( $query, $query_vars ) {
|
||
// 支持按易支付订单筛选
|
||
if ( ! empty( $query_vars['easypay_order'] ) ) {
|
||
$query['meta_query'][] = array(
|
||
'key' => '_easypay_order',
|
||
'value' => 'yes',
|
||
'compare' => '=',
|
||
);
|
||
}
|
||
|
||
// 支持按支付方式筛选
|
||
if ( ! empty( $query_vars['easypay_payment_method'] ) ) {
|
||
$query['meta_query'][] = array(
|
||
'key' => '_payment_method',
|
||
'value' => $query_vars['easypay_payment_method'],
|
||
'compare' => '=',
|
||
);
|
||
}
|
||
|
||
return $query;
|
||
}
|
||
|
||
/**
|
||
* 存储支付网关数据
|
||
*
|
||
* @param int $order_id 订单ID
|
||
* @param array $posted_data 提交的数据
|
||
* @param WC_Order $order 订单对象
|
||
*/
|
||
public function store_payment_gateway_data( $order_id, $posted_data, $order ) {
|
||
if ( ! $this->is_easypay_order( $order ) ) {
|
||
return;
|
||
}
|
||
|
||
// 存储易支付相关的额外数据
|
||
$payment_method = $order->get_payment_method();
|
||
$this->update_order_meta( $order, '_easypay_payment_method', $payment_method );
|
||
|
||
// 存储客户端信息
|
||
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ) : '';
|
||
$this->update_order_meta( $order, '_easypay_user_agent', $user_agent );
|
||
|
||
// 存储IP地址
|
||
$ip_address = WC_Geolocation::get_ip_address();
|
||
$this->update_order_meta( $order, '_easypay_customer_ip', $ip_address );
|
||
}
|
||
|
||
/**
|
||
* 检查是否为易支付订单
|
||
*
|
||
* @param WC_Order $order 订单对象
|
||
* @return bool
|
||
*/
|
||
private function is_easypay_order( $order ) {
|
||
$payment_method = $order->get_payment_method();
|
||
$easypay_methods = array( 'easypay_zfb', 'easypay_wx', 'easypay_qq', 'easypay_usdt' );
|
||
|
||
return in_array( $payment_method, $easypay_methods );
|
||
}
|
||
|
||
/**
|
||
* 获取易支付订单统计信息
|
||
*
|
||
* @param array $args 查询参数
|
||
* @return array
|
||
*/
|
||
public function get_easypay_order_stats( $args = array() ) {
|
||
$default_args = array(
|
||
'status' => array( 'wc-processing', 'wc-completed' ),
|
||
'limit' => -1,
|
||
'easypay_order' => 'yes',
|
||
);
|
||
|
||
$args = wp_parse_args( $args, $default_args );
|
||
|
||
// 使用HPOS兼容的查询方法
|
||
if ( $this->is_hpos_enabled() && class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) ) {
|
||
$orders = wc_get_orders( $args );
|
||
} else {
|
||
// 传统查询方法
|
||
$orders = wc_get_orders( $args );
|
||
}
|
||
|
||
$stats = array(
|
||
'total_orders' => count( $orders ),
|
||
'total_amount' => 0,
|
||
'by_method' => array(),
|
||
);
|
||
|
||
foreach ( $orders as $order ) {
|
||
$stats['total_amount'] += $order->get_total();
|
||
|
||
$payment_method = $order->get_payment_method();
|
||
if ( ! isset( $stats['by_method'][ $payment_method ] ) ) {
|
||
$stats['by_method'][ $payment_method ] = array(
|
||
'count' => 0,
|
||
'amount' => 0,
|
||
);
|
||
}
|
||
|
||
++$stats['by_method'][ $payment_method ]['count'];
|
||
$stats['by_method'][ $payment_method ]['amount'] += $order->get_total();
|
||
}
|
||
|
||
return $stats;
|
||
}
|
||
|
||
/**
|
||
* 批量更新订单状态(HPOS优化)
|
||
*
|
||
* @param array $order_ids 订单ID数组
|
||
* @param string $status 新状态
|
||
* @return bool
|
||
*/
|
||
public function bulk_update_order_status( $order_ids, $status ) {
|
||
if ( empty( $order_ids ) || ! is_array( $order_ids ) ) {
|
||
return false;
|
||
}
|
||
|
||
foreach ( $order_ids as $order_id ) {
|
||
$order = wc_get_order( $order_id );
|
||
if ( $order && $this->is_easypay_order( $order ) ) {
|
||
$order->update_status( $status );
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* 清理过期的订单数据
|
||
*
|
||
* @param int $days 保留天数
|
||
* @return int 清理的订单数量
|
||
*/
|
||
public function cleanup_expired_orders( $days = 30 ) {
|
||
$cutoff_date = date( 'Y-m-d H:i:s', strtotime( "-{$days} days" ) );
|
||
|
||
$args = array(
|
||
'status' => array( 'wc-cancelled', 'wc-failed' ),
|
||
'date_created' => '<' . $cutoff_date,
|
||
'easypay_order' => 'yes',
|
||
'limit' => -1,
|
||
);
|
||
|
||
$orders = wc_get_orders( $args );
|
||
$cleaned_count = 0;
|
||
|
||
foreach ( $orders as $order ) {
|
||
// 删除易支付相关的元数据
|
||
$this->delete_order_meta( $order, '_easypay_order' );
|
||
$this->delete_order_meta( $order, '_easypay_status_history' );
|
||
$this->delete_order_meta( $order, '_easypay_created_time' );
|
||
$this->delete_order_meta( $order, '_easypay_payment_completed_time' );
|
||
|
||
++$cleaned_count;
|
||
}
|
||
|
||
return $cleaned_count;
|
||
}
|
||
}
|
||
|
||
// 初始化HPOS支持
|
||
new EasyPay_HPOS_Support();
|