wp-woocommerce-pay/wp-woocommerce-pay.php
feibisi daf7932d59
Some checks are pending
gitleaks 密钥泄露扫描 / gitleaks (push) Waiting to run
feicode/ai-security No obvious risky pattern in latest diff
WordPress 插件 CI / ci (push) Successful in -8h1m15s
fix: phpcbf 自动修复 + phpcs.xml 代码规范配置
- phpcbf 自动修复 3893 处格式问题
- 添加 phpcs.xml 排除不适用的规则
- 安全相关规则降级为 warning 保持可见

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-18 15:19:03 +08:00

342 lines
10 KiB
PHP
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* Plugin Name: WenPaiEPay
* Plugin URI: https://feicode.com/WenPai-org/wp-woocommerce-pay
* Description: 文派易付适用于WooCommerce商店的免签约易支付集成支持支付宝、微信、QQ钱包、USDT等多种支付方式
* Version: 1.0.1
* Author: 文派开源
* Author URI: https://wenpai.org/
* Text Domain: wenpaiepay
* Domain Path: /languages
* Update URI: https://updates.wenpai.net
* Requires at least: 5.0
* Tested up to: 6.4
* Requires PHP: 7.4
* WC requires at least: 4.0
* WC tested up to: 8.5
* Network: false
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package WenPaiEPay
* @author 文派开源
* @since 1.0.1
*
* 环境要求:
* - WordPress 5.0+
* - WooCommerce 4.0+
* - PHP 7.4+
* - MySQL 5.6+ 或 MariaDB 10.1+
* - HTTPS (生产环境推荐)
*
* 依赖插件:
* - WooCommerce (必需)
* - WooCommerce Blocks (可选,用于区块编辑器支持)
*/
// 防止该文件直接被访问
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// 定义插件常量
define( 'WENPAIEPAY_VERSION', '1.0.1' );
define( 'WENPAIEPAY_PLUGIN_FILE', __FILE__ );
define( 'WENPAIEPAY_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WENPAIEPAY_PLUGIN_URL', plugins_url( '', __FILE__ ) );
define( 'WENPAIEPAY_MIN_WP_VERSION', '5.0' );
define( 'WENPAIEPAY_MIN_WC_VERSION', '4.0' );
define( 'WENPAIEPAY_MIN_PHP_VERSION', '7.4' );
// 向后兼容
define( 'WC_Freepay_URL', WENPAIEPAY_PLUGIN_URL );
define( 'WC_Freepay_FILE', WENPAIEPAY_PLUGIN_FILE );
// 环境检查
register_activation_hook( __FILE__, 'wenpaiepay_check_environment' );
add_action( 'admin_init', 'wenpaiepay_check_environment' );
add_action( 'plugins_loaded', 'wenpaiepay_init_plugin' );
/**
* 检查插件运行环境
*/
function wenpaiepay_check_environment() {
$errors = array();
// 检查PHP版本
if ( version_compare( PHP_VERSION, WENPAIEPAY_MIN_PHP_VERSION, '<' ) ) {
$errors[] = sprintf(
__( 'WenPaiEPay 需要 PHP %1$s 或更高版本,当前版本:%2$s', 'wenpaiepay' ),
WENPAIEPAY_MIN_PHP_VERSION,
PHP_VERSION
);
}
// 检查WordPress版本
global $wp_version;
if ( version_compare( $wp_version, WENPAIEPAY_MIN_WP_VERSION, '<' ) ) {
$errors[] = sprintf(
__( 'WenPaiEPay 需要 WordPress %1$s 或更高版本,当前版本:%2$s', 'wenpaiepay' ),
WENPAIEPAY_MIN_WP_VERSION,
$wp_version
);
}
// 检查WooCommerce是否激活
if ( ! class_exists( 'WooCommerce' ) ) {
$errors[] = __( 'WenPaiEPay 需要安装并激活 WooCommerce 插件', 'wenpaiepay' );
} else {
// 检查WooCommerce版本
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, WENPAIEPAY_MIN_WC_VERSION, '<' ) ) {
$errors[] = sprintf(
__( 'WenPaiEPay 需要 WooCommerce %1$s 或更高版本,当前版本:%2$s', 'wenpaiepay' ),
WENPAIEPAY_MIN_WC_VERSION,
WC_VERSION
);
}
}
// 如果有错误,显示通知并停用插件
if ( ! empty( $errors ) ) {
add_action(
'admin_notices',
function () use ( $errors ) {
echo '<div class="notice notice-error"><p>';
echo '<strong>' . __( 'WenPaiEPay 插件无法激活:', 'wenpaiepay' ) . '</strong><br>';
foreach ( $errors as $error ) {
echo '• ' . esc_html( $error ) . '<br>';
}
echo '</p></div>';
}
);
// 如果是激活时检查,停用插件
if ( current_filter() === 'register_activation_hook' ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
return;
}
// 如果是运行时检查,阻止插件初始化
remove_action( 'plugins_loaded', 'wenpaiepay_init_plugin' );
return;
}
}
/**
* 初始化插件
*/
function wenpaiepay_init_plugin() {
$updater_file = WENPAIEPAY_PLUGIN_DIR . 'includes/class-wenpaiepay-updater.php';
if ( file_exists( $updater_file ) ) {
require_once $updater_file;
}
if ( class_exists( 'WenPaiEPay_Updater' ) ) {
static $updater = null;
if ( null === $updater ) {
$updater = new WenPaiEPay_Updater( plugin_basename( __FILE__ ), WENPAIEPAY_VERSION );
}
}
// 确保WooCommerce已加载
if ( ! class_exists( 'WooCommerce' ) ) {
return;
}
// 加载文本域
load_plugin_textdomain( 'wenpaiepay', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// 初始化支付网关
init_freepay_gateway_class();
}
/**
* 插件卸载时的清理工作
*/
register_uninstall_hook( __FILE__, 'wenpaiepay_uninstall' );
function wenpaiepay_uninstall() {
// 删除插件相关的选项
delete_option( 'easypay_address' );
delete_option( 'easypay_app_id' );
delete_option( 'easypay_app_key' );
delete_option( 'easypay_return_url' );
// 清理临时数据
delete_transient( 'wenpaiepay_version_check' );
// 删除配置文件(如果存在)
$config_file = plugin_dir_path( __FILE__ ) . 'inc/easypay.config.php';
if ( file_exists( $config_file ) ) {
wp_delete_file( $config_file );
}
}
/**
* 插件激活时的初始化工作
*/
register_activation_hook( __FILE__, 'wenpaiepay_activate' );
function wenpaiepay_activate() {
// 检查环境
wenpaiepay_check_environment();
// 设置默认选项
if ( ! get_option( 'easypay_address' ) ) {
update_option( 'easypay_address', 'https://payments.weithemes.com/' );
}
// 刷新重写规则
flush_rewrite_rules();
}
/**
* 插件停用时的清理工作
*/
register_deactivation_hook( __FILE__, 'wenpaiepay_deactivate' );
function wenpaiepay_deactivate() {
// 清理计划任务
wp_clear_scheduled_hook( 'wenpaiepay_cleanup_logs' );
// 刷新重写规则
flush_rewrite_rules();
}
// Initialize blocks support when WooCommerce Blocks is loaded
add_action( 'woocommerce_blocks_loaded', 'init_easypay_blocks_support' );
function init_freepay_gateway_class() {
require_once 'class-wc-gateway-easypay-zfb.php';
require_once 'class-wc-gateway-easypay-qq.php';
require_once 'class-wc-gateway-easypay-wx.php';
require_once 'class-wc-gateway-easypay-usdt.php';
require_once 'class-wc-gateway-easypay-notify.php';
global $wc_gateway_easypay_zfb;
global $wc_gateway_easypay_qq;
global $wc_gateway_easypay_wx;
global $wc_gateway_easypay_usdt;
$wc_gateway_easypay_zfb = new WC_Gateway_Easypay_zfb();
$wc_gateway_easypay_qq = new WC_Gateway_Easypay_qq();
$wc_gateway_easypay_wx = new WC_Gateway_Easypay_wx();
$wc_gateway_easypay_usdt = new WC_Gateway_Easypay_usdt();
}
add_filter( 'woocommerce_payment_gateways', 'add_easypay_zfb_gateway_class' );
function add_easypay_zfb_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_Easypay_zfb';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_easypay_qq_gateway_class' );
function add_easypay_qq_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_Easypay_qq';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_easypay_usdt_gateway_class' );
function add_easypay_usdt_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_Easypay_usdt';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_easypay_wx_gateway_class' );
function add_easypay_wx_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_Easypay_wx';
return $methods;
}
add_action( 'admin_menu', 'create_menu_page' );
function create_menu_page() {
add_menu_page( ( '免签约支付' ), _( '支付设置' ), 'administrator', 'wenpaiepay_gateway', 'menuPage', plugins_url( 'wp-woocommerce-pay/assets/logo/logo.png' ) );
}
function menuPage() {
require_once 'inc/show.php';
if ( isset( $_POST['easypay_app_id'] ) && isset( $_POST['easypay_app_key'] ) ) {
// 网关地址固定为 https://payments.weithemes.com/,不允许修改
update_option( 'easypay_address', 'https://payments.weithemes.com/' );
update_option( 'easypay_app_id', $_POST['easypay_app_id'] );
update_option( 'easypay_app_key', $_POST['easypay_app_key'] );
update_option( 'easypay_return_url', $_POST['easypay_return_url'] );
$path = plugin_dir_path( __FILE__ ) . 'inc/easypay.config.php';
$arr = array(
'1' => '$alipay_config["partner"] = "' . get_option( 'easypay_app_id' ) . '";',
'2' => '$alipay_config["key"] = "' . get_option( 'easypay_app_key' ) . '";',
'12' => '$alipay_config["apiurl"] = "https://payments.weithemes.com/";',
'13' => '$return_url = "' . get_option( 'easypay_return_url' ) . '";',
);
homeagain( $path, $arr );
// 修改支付logo图标
}
}
/**
* Initialize WooCommerce Blocks support
*
* This function loads the necessary block classes for WooCommerce Blocks integration.
* It ensures that all payment method blocks are properly registered and available
* for use in the block-based checkout experience.
*
* @since 1.0.1
* @return void
*/
function init_easypay_blocks_support() {
// Check if WooCommerce is available and blocks are supported
if ( ! class_exists( 'WooCommerce' ) ) {
error_log( 'EasyPay: WooCommerce not found, blocks support disabled.' );
return;
}
// Check if WooCommerce Blocks is available
if ( ! class_exists( 'Automattic\\WooCommerce\\Blocks\\Payments\\Integrations\\AbstractPaymentMethodType' ) ) {
error_log( 'EasyPay: WooCommerce Blocks not found, blocks support disabled.' );
return;
}
try {
// 包含必要的类文件
require_once WENPAIEPAY_PLUGIN_DIR . 'includes/blocks/class-easypay-blocks-manager.php';
// 包含结账编辑器类
require_once WENPAIEPAY_PLUGIN_DIR . 'includes/class-easypay-checkout-editor.php';
// 包含HPOS支持类
require_once WENPAIEPAY_PLUGIN_DIR . 'class-easypay-hpos-support.php';
// Load block manager
$manager_file = plugin_dir_path( __FILE__ ) . 'includes/blocks/class-easypay-blocks-manager.php';
if ( file_exists( $manager_file ) ) {
require_once $manager_file;
} else {
error_log( 'EasyPay: Block manager file not found: ' . $manager_file );
return;
}
// Load individual payment method blocks with error checking
$block_files = array(
'includes/blocks/class-easypay-wx-blocks.php',
'includes/blocks/class-easypay-zfb-blocks.php',
'includes/blocks/class-easypay-qq-blocks.php',
'includes/blocks/class-easypay-usdt-blocks.php',
);
foreach ( $block_files as $file ) {
$file_path = plugin_dir_path( __FILE__ ) . $file;
if ( file_exists( $file_path ) ) {
require_once $file_path;
} else {
error_log( 'EasyPay: Block file not found: ' . $file_path );
}
}
} catch ( Exception $e ) {
error_log( 'EasyPay: Error initializing blocks support: ' . $e->getMessage() );
}
// Initialize the blocks manager
if ( class_exists( 'EasyPay_Blocks_Manager' ) ) {
new EasyPay_Blocks_Manager();
}
}