wp-woocommerce-pay/includes/class-easypay-checkout-editor.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

407 lines
11 KiB
PHP
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
/**
* EasyPay Checkout Editor Class
*
* Handles checkout page block editing functionality for custom form layouts
*
* @package WenPaiEPay
* @since 1.0.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* EasyPay Checkout Editor Class
*
* Manages checkout page block editing features and custom layout options
*/
class EasyPay_Checkout_Editor {
/**
* Available checkout layouts
*
* @var array
*/
private $available_layouts = array(
'default' => '默认布局',
'compact' => '紧凑布局',
'detailed' => '详细布局',
'grid' => '网格布局',
);
/**
* Constructor
*/
public function __construct() {
$this->init_hooks();
}
/**
* Initialize hooks
*/
private function init_hooks() {
// 添加区块编辑器支持
add_action( 'init', array( $this, 'register_block_editor_assets' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
// 添加自定义设置到WooCommerce设置页面
add_filter( 'woocommerce_get_settings_checkout', array( $this, 'add_checkout_layout_settings' ), 10, 2 );
add_action( 'woocommerce_update_options_checkout', array( $this, 'save_checkout_layout_settings' ) );
// 添加区块编辑器预览支持
add_action( 'wp_ajax_easypay_preview_layout', array( $this, 'ajax_preview_layout' ) );
add_action( 'wp_ajax_nopriv_easypay_preview_layout', array( $this, 'ajax_preview_layout' ) );
add_action( 'wp_ajax_easypay_save_layout_settings', array( $this, 'ajax_save_layout_settings' ) );
// 在前端应用选择的布局
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_layout_styles' ) );
add_filter( 'body_class', array( $this, 'add_layout_body_class' ) );
}
/**
* AJAX handler for saving layout settings
*/
public function ajax_save_layout_settings() {
// 验证nonce
if ( ! wp_verify_nonce( $_POST['nonce'], 'easypay_editor_nonce' ) ) {
wp_die( '安全验证失败' );
}
// 验证用户权限
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( '权限不足' );
}
$layout = sanitize_text_field( $_POST['layout'] );
$show_icons = isset( $_POST['showIcons'] ) ? 'yes' : 'no';
$enable_animations = isset( $_POST['enableAnimations'] ) ? 'yes' : 'no';
$custom_class = sanitize_text_field( $_POST['customClass'] );
// 验证布局类型
if ( ! array_key_exists( $layout, $this->available_layouts ) ) {
wp_send_json_error( '无效的布局类型' );
}
// 保存设置
update_option( 'easypay_checkout_layout', $layout );
update_option( 'easypay_show_icons', $show_icons );
update_option( 'easypay_enable_animations', $enable_animations );
update_option( 'easypay_custom_css_class', $custom_class );
wp_send_json_success(
array(
'message' => '设置已保存',
'layout' => $layout,
)
);
}
/**
* Register block editor assets
*/
public function register_block_editor_assets() {
// 注册编辑器专用的JavaScript
wp_register_script(
'easypay-checkout-editor',
WENPAIEPAY_PLUGIN_URL . 'assets/js/easypay-checkout-editor.js',
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ),
WENPAIEPAY_VERSION,
true
);
// 注册编辑器专用的CSS
wp_register_style(
'easypay-checkout-editor',
WENPAIEPAY_PLUGIN_URL . 'assets/css/easypay-checkout-editor.css',
array( 'wp-edit-blocks' ),
WENPAIEPAY_VERSION
);
// 本地化脚本数据
wp_localize_script(
'easypay-checkout-editor',
'easypayEditor',
array(
'layouts' => $this->available_layouts,
'currentLayout' => get_option( 'easypay_checkout_layout', 'default' ),
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'easypay_editor_nonce' ),
'strings' => array(
'layoutSettings' => '布局设置',
'selectLayout' => '选择结账布局',
'previewLayout' => '预览布局',
'saveSettings' => '保存设置',
),
)
);
}
/**
* Enqueue editor assets
*/
public function enqueue_editor_assets() {
// 只在结账页面编辑时加载
global $post;
if ( $post && has_block( 'woocommerce/checkout', $post->post_content ) ) {
wp_enqueue_script( 'easypay-checkout-editor' );
wp_enqueue_style( 'easypay-checkout-editor' );
}
}
/**
* Add checkout layout settings to WooCommerce settings
*
* @param array $settings Current settings
* @param string $current_section Current section
* @return array Modified settings
*/
public function add_checkout_layout_settings( $settings, $current_section ) {
// 只在结账设置页面添加
if ( $current_section === '' ) {
$layout_settings = array(
array(
'title' => 'EasyPay 结账布局设置',
'type' => 'title',
'desc' => '自定义EasyPay支付方法在结账页面的显示布局',
'id' => 'easypay_checkout_layout_options',
),
array(
'title' => '结账布局',
'desc' => '选择EasyPay支付方法的显示布局',
'id' => 'easypay_checkout_layout',
'type' => 'select',
'options' => $this->available_layouts,
'default' => 'default',
'desc_tip' => true,
),
array(
'title' => '启用动画效果',
'desc' => '为支付方法切换添加动画效果',
'id' => 'easypay_enable_animations',
'type' => 'checkbox',
'default' => 'yes',
'desc_tip' => true,
),
array(
'title' => '显示支付图标',
'desc' => '在支付方法标签中显示图标',
'id' => 'easypay_show_icons',
'type' => 'checkbox',
'default' => 'yes',
'desc_tip' => true,
),
array(
'title' => '自定义CSS类',
'desc' => '为结账表单添加自定义CSS类可选',
'id' => 'easypay_custom_css_class',
'type' => 'text',
'default' => '',
'desc_tip' => true,
),
array(
'type' => 'sectionend',
'id' => 'easypay_checkout_layout_options',
),
);
// 在结账设置的末尾添加我们的设置
$settings = array_merge( $settings, $layout_settings );
}
return $settings;
}
/**
* Save checkout layout settings
*/
public function save_checkout_layout_settings() {
woocommerce_update_options( $this->get_settings() );
}
/**
* Get settings array
*
* @return array Settings array
*/
private function get_settings() {
return array(
array(
'id' => 'easypay_checkout_layout',
'type' => 'select',
),
array(
'id' => 'easypay_enable_animations',
'type' => 'checkbox',
),
array(
'id' => 'easypay_show_icons',
'type' => 'checkbox',
),
array(
'id' => 'easypay_custom_css_class',
'type' => 'text',
),
);
}
/**
* AJAX handler for layout preview
*/
public function ajax_preview_layout() {
// 验证nonce
if ( ! wp_verify_nonce( $_POST['nonce'], 'easypay_editor_nonce' ) ) {
wp_die( '安全验证失败' );
}
$layout = sanitize_text_field( $_POST['layout'] );
if ( ! array_key_exists( $layout, $this->available_layouts ) ) {
wp_send_json_error( '无效的布局类型' );
}
// 生成预览HTML
$preview_html = $this->generate_layout_preview( $layout );
wp_send_json_success(
array(
'html' => $preview_html,
'layout' => $layout,
'name' => $this->available_layouts[ $layout ],
)
);
}
/**
* Generate layout preview HTML
*
* @param string $layout Layout type
* @return string Preview HTML
*/
private function generate_layout_preview( $layout ) {
$preview_methods = array(
'easypay_zfb' => array(
'title' => '支付宝',
'description' => '使用支付宝安全快捷支付',
'logo' => WENPAIEPAY_PLUGIN_URL . 'assets/images/alipay.png',
),
'easypay_wx' => array(
'title' => '微信支付',
'description' => '使用微信支付便捷付款',
'logo' => WENPAIEPAY_PLUGIN_URL . 'assets/images/wechat.png',
),
);
$html = '<div class="easypay-layout-' . esc_attr( $layout ) . ' easypay-preview">';
foreach ( $preview_methods as $method_id => $method_data ) {
$html .= $this->render_payment_method_preview( $method_id, $method_data, $layout );
}
$html .= '</div>';
return $html;
}
/**
* Render payment method preview
*
* @param string $method_id Method ID
* @param array $method_data Method data
* @param string $layout Layout type
* @return string Method HTML
*/
private function render_payment_method_preview( $method_id, $method_data, $layout ) {
$class_suffix = ( $layout !== 'default' ) ? '-' . $layout : '';
$html = '<div class="wc-block-components-radio-control-accordion-option">';
$html .= '<div class="easypay-payment-label' . $class_suffix . '">';
if ( get_option( 'easypay_show_icons', 'yes' ) === 'yes' ) {
$html .= '<img src="' . esc_url( $method_data['logo'] ) . '" alt="' . esc_attr( $method_data['title'] ) . '" />';
}
$html .= '<span>' . esc_html( $method_data['title'] ) . '</span>';
$html .= '</div>';
$html .= '<div class="easypay-payment-content' . $class_suffix . '">';
$html .= '<div>' . esc_html( $method_data['description'] ) . '</div>';
$html .= '</div>';
$html .= '</div>';
return $html;
}
/**
* Enqueue frontend layout styles
*/
public function enqueue_frontend_layout_styles() {
if ( is_checkout() || is_cart() ) {
$layout = get_option( 'easypay_checkout_layout', 'default' );
$enable_animations = get_option( 'easypay_enable_animations', 'yes' );
// 添加内联样式来应用当前布局
$custom_css = "
.wc-block-checkout__payment-method .easypay-layout-{$layout} {
/* 应用选择的布局样式 */
}
";
if ( $enable_animations === 'no' ) {
$custom_css .= '
.easypay-payment-label,
.easypay-payment-content {
transition: none !important;
animation: none !important;
}
';
}
wp_add_inline_style( 'easypay-blocks', $custom_css );
}
}
/**
* Add layout class to body
*
* @param array $classes Body classes
* @return array Modified classes
*/
public function add_layout_body_class( $classes ) {
if ( is_checkout() || is_cart() ) {
$layout = get_option( 'easypay_checkout_layout', 'default' );
$custom_class = get_option( 'easypay_custom_css_class', '' );
$classes[] = 'easypay-layout-' . $layout;
if ( ! empty( $custom_class ) ) {
$classes[] = sanitize_html_class( $custom_class );
}
}
return $classes;
}
/**
* Get current layout
*
* @return string Current layout
*/
public function get_current_layout() {
return get_option( 'easypay_checkout_layout', 'default' );
}
/**
* Get available layouts
*
* @return array Available layouts
*/
public function get_available_layouts() {
return $this->available_layouts;
}
}
// 初始化类
new EasyPay_Checkout_Editor();