mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-08 21:52:55 +08:00
189 lines
5.2 KiB
PHP
189 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* The Settings module.
|
|
*
|
|
* @package WooCommerce\PayPalCommerce\Settings
|
|
*/
|
|
|
|
declare( strict_types = 1 );
|
|
|
|
namespace WooCommerce\PayPalCommerce\Settings;
|
|
|
|
use WooCommerce\PayPalCommerce\Settings\Endpoint\ConnectManualRestEndpoint;
|
|
use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint;
|
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ExecutableModule;
|
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ModuleClassNameIdTrait;
|
|
use WooCommerce\PayPalCommerce\Vendor\Inpsyde\Modularity\Module\ServiceModule;
|
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
|
|
|
/**
|
|
* Class SettingsModule
|
|
*/
|
|
class SettingsModule implements ServiceModule, ExecutableModule {
|
|
use ModuleClassNameIdTrait;
|
|
|
|
/**
|
|
* Returns whether the old settings UI should be loaded.
|
|
*/
|
|
public static function should_use_the_old_ui(): bool {
|
|
return apply_filters( 'woocommerce_paypal_payments_should_use_the_old_ui', false );
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function services() : array {
|
|
return require __DIR__ . '/../services.php';
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc}
|
|
*/
|
|
public function run( ContainerInterface $container ) : bool {
|
|
if ( self::should_use_the_old_ui() ) {
|
|
add_action(
|
|
'woocommerce_paypal_payments_inside_settings_page_header',
|
|
static function () : void {
|
|
echo sprintf(
|
|
'<a href="#" class="button" onclick="javascript:void(0);">%s</a>',
|
|
esc_html__( 'Switch to new settings UI', 'woocommerce-paypal-payments' )
|
|
);
|
|
}
|
|
);
|
|
|
|
add_action(
|
|
'admin_enqueue_scripts',
|
|
static function () use ( $container ) {
|
|
$module_url = $container->get('settings.url');
|
|
$script_asset_file = require dirname( realpath( __FILE__ ) ?: '', 2 ) . '/assets/switchSettingsUi.asset.php';
|
|
|
|
wp_register_script(
|
|
'ppcp-switch-settings-ui',
|
|
untrailingslashit( $module_url ) . '/assets/switchSettingsUi.js',
|
|
$script_asset_file['dependencies'],
|
|
$script_asset_file['version'],
|
|
true
|
|
);
|
|
|
|
wp_localize_script(
|
|
'ppcp-switch-settings-ui',
|
|
'PayPalCommerceGatewayOrderTrackingInfo',
|
|
array()
|
|
);
|
|
|
|
wp_enqueue_script( 'ppcp-switch-settings-ui' );
|
|
}
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
add_action(
|
|
'admin_enqueue_scripts',
|
|
/**
|
|
* Param types removed to avoid third-party issues.
|
|
*
|
|
* @psalm-suppress MissingClosureParamType
|
|
*/
|
|
static function ( $hook_suffix ) use ( $container ) {
|
|
if ( 'woocommerce_page_wc-settings' !== $hook_suffix ) {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Require resolves.
|
|
*
|
|
* @psalm-suppress UnresolvableInclude
|
|
*/
|
|
$script_asset_file = require dirname( realpath( __FILE__ ) ?: '', 2 ) . '/assets/index.asset.php';
|
|
|
|
$module_url = $container->get( 'settings.url' );
|
|
|
|
wp_register_script(
|
|
'ppcp-admin-settings',
|
|
$module_url . '/assets/index.js',
|
|
$script_asset_file['dependencies'],
|
|
$script_asset_file['version'],
|
|
true
|
|
);
|
|
|
|
wp_enqueue_script( 'ppcp-admin-settings' );
|
|
|
|
/**
|
|
* Require resolves.
|
|
*
|
|
* @psalm-suppress UnresolvableInclude
|
|
*/
|
|
$style_asset_file = require dirname( realpath( __FILE__ ) ?: '', 2 ) . '/assets/style.asset.php';
|
|
|
|
wp_register_style(
|
|
'ppcp-admin-settings',
|
|
$module_url . '/assets/style-style.css',
|
|
$style_asset_file['dependencies'],
|
|
$style_asset_file['version']
|
|
);
|
|
|
|
wp_enqueue_style( 'ppcp-admin-settings' );
|
|
|
|
wp_enqueue_style( 'ppcp-admin-settings-font', 'https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap', array(), $style_asset_file['version'] );
|
|
wp_localize_script(
|
|
'ppcp-admin-settings',
|
|
'ppcpSettings',
|
|
array(
|
|
'assets' => array(
|
|
'imagesUrl' => $module_url . '/images/',
|
|
),
|
|
'wcPaymentsTabUrl' => admin_url( 'admin.php?page=wc-settings&tab=checkout' ),
|
|
'debug' => defined( 'WP_DEBUG' ) && WP_DEBUG,
|
|
)
|
|
);
|
|
}
|
|
);
|
|
|
|
add_action(
|
|
'woocommerce_paypal_payments_gateway_admin_options_wrapper',
|
|
function () : void {
|
|
global $hide_save_button;
|
|
$hide_save_button = true;
|
|
|
|
$this->render_header();
|
|
$this->render_content();
|
|
}
|
|
);
|
|
|
|
add_action(
|
|
'rest_api_init',
|
|
static function () use ( $container ) : void {
|
|
$onboarding_endpoint = $container->get( 'settings.rest.onboarding' );
|
|
assert( $onboarding_endpoint instanceof OnboardingRestEndpoint );
|
|
$onboarding_endpoint->register_routes();
|
|
|
|
$connect_manual_endpoint = $container->get( 'settings.rest.connect_manual' );
|
|
assert( $connect_manual_endpoint instanceof ConnectManualRestEndpoint );
|
|
$connect_manual_endpoint->register_routes();
|
|
}
|
|
);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Outputs the settings page header (title and back-link).
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function render_header() : void {
|
|
echo '<h2>' . esc_html__( 'PayPal', 'woocommerce-paypal-payments' );
|
|
wc_back_link( __( 'Return to payments', 'woocommerce-paypal-payments' ), admin_url( 'admin.php?page=wc-settings&tab=checkout' ) );
|
|
echo '</h2>';
|
|
}
|
|
|
|
/**
|
|
* Renders the container for the React app.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected function render_content() : void {
|
|
echo '<div id="ppcp-settings-container"></div>';
|
|
}
|
|
}
|