- phpcbf 自动修复 3893 处格式问题 - 添加 phpcs.xml 排除不适用的规则 - 安全相关规则降级为 warning 保持可见 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
235 lines
6.7 KiB
PHP
235 lines
6.7 KiB
PHP
<?php
|
|
/**
|
|
* EasyPay Blocks Manager
|
|
*
|
|
* Manages WooCommerce Blocks integration for EasyPay payment methods.
|
|
*
|
|
* @package WenPaiEPay
|
|
* @since 1.0.1
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
|
|
|
|
/**
|
|
* EasyPay Blocks Manager class
|
|
*
|
|
* This class manages the integration between EasyPay payment methods and WooCommerce Blocks.
|
|
* It handles registration of payment method blocks and manages block-related assets.
|
|
*
|
|
* @since 1.0.1
|
|
*/
|
|
class EasyPay_Blocks_Manager {
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* Initializes the blocks manager and sets up necessary hooks for
|
|
* WooCommerce Blocks integration.
|
|
*
|
|
* @since 1.0.1
|
|
*/
|
|
public function __construct() {
|
|
// Only initialize if WooCommerce is available
|
|
if ( ! class_exists( 'WooCommerce' ) ) {
|
|
return;
|
|
}
|
|
|
|
add_action( 'woocommerce_blocks_loaded', array( $this, 'register_payment_methods' ) );
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_block_styles' ) );
|
|
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_styles' ) );
|
|
}
|
|
|
|
/**
|
|
* Register payment methods with WooCommerce Blocks
|
|
*
|
|
* This method loads all EasyPay payment method block classes and registers
|
|
* them with the WooCommerce Blocks payment method registry.
|
|
*
|
|
* @since 1.0.1
|
|
* @return void
|
|
*/
|
|
public function register_payment_methods() {
|
|
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
|
|
error_log( 'EasyPay Blocks: AbstractPaymentMethodType class not found.' );
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// Include payment method classes with error checking
|
|
$block_files = array(
|
|
'class-easypay-wx-blocks.php',
|
|
'class-easypay-zfb-blocks.php',
|
|
'class-easypay-qq-blocks.php',
|
|
'class-easypay-usdt-blocks.php',
|
|
);
|
|
|
|
$loaded_classes = array();
|
|
|
|
foreach ( $block_files as $file ) {
|
|
$file_path = plugin_dir_path( __FILE__ ) . $file;
|
|
if ( file_exists( $file_path ) ) {
|
|
require_once $file_path;
|
|
$loaded_classes[] = $file;
|
|
} else {
|
|
error_log( 'EasyPay Blocks: Block file not found: ' . $file_path );
|
|
}
|
|
}
|
|
|
|
// Register payment methods with error handling
|
|
add_action(
|
|
'woocommerce_blocks_payment_method_type_registration',
|
|
function ( $payment_method_registry ) use ( $loaded_classes ) {
|
|
$payment_methods = array(
|
|
'Easypay_Wx_Blocks',
|
|
'Easypay_Zfb_Blocks',
|
|
'Easypay_Qq_Blocks',
|
|
'Easypay_Usdt_Blocks',
|
|
);
|
|
|
|
foreach ( $payment_methods as $class_name ) {
|
|
if ( class_exists( $class_name ) ) {
|
|
try {
|
|
$payment_method_registry->register( new $class_name() );
|
|
} catch ( Exception $e ) {
|
|
error_log( 'EasyPay Blocks: Failed to register ' . $class_name . ': ' . $e->getMessage() );
|
|
}
|
|
} else {
|
|
error_log( 'EasyPay Blocks: Class not found: ' . $class_name );
|
|
}
|
|
}
|
|
}
|
|
);
|
|
|
|
} catch ( Exception $e ) {
|
|
error_log( 'EasyPay Blocks: Error in register_payment_methods: ' . $e->getMessage() );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Enqueue block styles and scripts
|
|
*/
|
|
public function enqueue_block_styles() {
|
|
// Load on frontend pages and in block editor
|
|
if ( is_admin() && ! $this->is_block_editor() ) {
|
|
return;
|
|
}
|
|
|
|
// Enqueue block styles
|
|
wp_enqueue_style(
|
|
'easypay-blocks-style',
|
|
plugin_dir_url( __FILE__ ) . '../../assets/css/easypay-blocks.css',
|
|
array(),
|
|
'1.0.1'
|
|
);
|
|
|
|
// Enqueue block scripts with correct dependencies
|
|
wp_enqueue_script(
|
|
'easypay-blocks-script',
|
|
plugin_dir_url( __FILE__ ) . '../../assets/js/easypay-blocks.js',
|
|
array( 'wc-blocks-registry', 'wp-element', 'wp-i18n', 'wc-settings' ),
|
|
'1.0.1',
|
|
true
|
|
);
|
|
|
|
// Get payment gateway instances and prepare data for JavaScript
|
|
$payment_gateways = WC()->payment_gateways()->payment_gateways();
|
|
$gateway_data = array();
|
|
|
|
$gateway_mapping = array(
|
|
'easypay_zfb' => 'easypay_zfb_data',
|
|
'easypay_wx' => 'easypay_wx_data',
|
|
'easypay_qq' => 'easypay_qq_data',
|
|
'easypay_usdt' => 'easypay_usdt_data',
|
|
);
|
|
|
|
foreach ( $gateway_mapping as $gateway_id => $data_key ) {
|
|
if ( isset( $payment_gateways[ $gateway_id ] ) && $payment_gateways[ $gateway_id ]->enabled === 'yes' ) {
|
|
$gateway = $payment_gateways[ $gateway_id ];
|
|
$gateway_data[ $data_key ] = array(
|
|
'name' => $gateway_id,
|
|
'title' => $gateway->get_title(),
|
|
'description' => $gateway->get_description(),
|
|
'logo_url' => $this->get_gateway_logo_url( $gateway_id ),
|
|
'supports' => array( 'products' ),
|
|
);
|
|
}
|
|
}
|
|
|
|
// Add payment method data to wcSettings using the correct method
|
|
foreach ( $gateway_data as $key => $data ) {
|
|
// Use wp_add_inline_script to add data to wcSettings.allSettings
|
|
wp_add_inline_script(
|
|
'easypay-blocks-script',
|
|
sprintf(
|
|
'window.wc = window.wc || {}; window.wc.wcSettings = window.wc.wcSettings || {}; window.wc.wcSettings.allSettings = window.wc.wcSettings.allSettings || {}; window.wc.wcSettings.allSettings["%s"] = %s;',
|
|
esc_js( $key ),
|
|
wp_json_encode( $data )
|
|
),
|
|
'before'
|
|
);
|
|
}
|
|
|
|
// Localize script with additional params
|
|
wp_localize_script(
|
|
'easypay-blocks-script',
|
|
'easypay_blocks_params',
|
|
array(
|
|
'ajax_url' => admin_url( 'admin-ajax.php' ),
|
|
'nonce' => wp_create_nonce( 'easypay_blocks_nonce' ),
|
|
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
|
|
'gateway_count' => count( $gateway_data ),
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get gateway logo URL
|
|
*/
|
|
private function get_gateway_logo_url( $gateway_id ) {
|
|
$logo_mapping = array(
|
|
'easypay_zfb' => 'zfb-logo.svg',
|
|
'easypay_wx' => 'wx-logo.svg',
|
|
'easypay_qq' => 'qq-logo.svg',
|
|
'easypay_usdt' => 'usdt-logo.svg',
|
|
);
|
|
|
|
if ( isset( $logo_mapping[ $gateway_id ] ) ) {
|
|
$logo_path = plugin_dir_url( __FILE__ ) . '../../assets/logo/' . $logo_mapping[ $gateway_id ];
|
|
// Check if file exists, return URL or empty string
|
|
$file_path = plugin_dir_path( __FILE__ ) . '../../assets/logo/' . $logo_mapping[ $gateway_id ];
|
|
return file_exists( $file_path ) ? $logo_path : '';
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Check if we're in the block editor
|
|
*/
|
|
private function is_block_editor() {
|
|
// Check if we're in the block editor
|
|
if ( function_exists( 'get_current_screen' ) ) {
|
|
$screen = get_current_screen();
|
|
if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Check for block editor specific parameters
|
|
if ( isset( $_GET['action'] ) && $_GET['action'] === 'edit' && isset( $_GET['post'] ) ) {
|
|
return true;
|
|
}
|
|
|
|
// Check for REST API requests from block editor
|
|
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Initialize the blocks manager
|
|
new EasyPay_Blocks_Manager();
|