2020-08-31 09:37:15 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Registers and enqueues the assets for the Onboarding process.
|
|
|
|
*
|
2020-09-11 14:11:10 +03:00
|
|
|
* @package WooCommerce\PayPalCommerce\Onboarding\Assets
|
2020-08-31 09:37:15 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
namespace WooCommerce\PayPalCommerce\Onboarding\Assets;
|
2020-08-31 09:37:15 +03:00
|
|
|
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\Onboarding\Endpoint\LoginSellerEndpoint;
|
2022-02-08 10:25:01 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
2020-09-11 14:11:10 +03:00
|
|
|
use WooCommerce\PayPalCommerce\Onboarding\State;
|
2020-08-31 09:37:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class OnboardingAssets
|
|
|
|
*/
|
|
|
|
class OnboardingAssets {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The URL to the module.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $module_url;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The State.
|
|
|
|
*
|
|
|
|
* @var State
|
|
|
|
*/
|
|
|
|
private $state;
|
|
|
|
|
2022-02-08 10:25:01 +02:00
|
|
|
/**
|
|
|
|
* The Environment.
|
|
|
|
*
|
|
|
|
* @var Environment
|
|
|
|
*/
|
|
|
|
private $environment;
|
|
|
|
|
2020-08-31 09:37:15 +03:00
|
|
|
/**
|
|
|
|
* The LoginSeller Endpoint.
|
|
|
|
*
|
|
|
|
* @var LoginSellerEndpoint
|
|
|
|
*/
|
|
|
|
private $login_seller_endpoint;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OnboardingAssets constructor.
|
|
|
|
*
|
|
|
|
* @param string $module_url The URL to the module.
|
|
|
|
* @param State $state The State object.
|
2022-02-08 10:25:01 +02:00
|
|
|
* @param Environment $environment The Environment.
|
2020-08-31 09:37:15 +03:00
|
|
|
* @param LoginSellerEndpoint $login_seller_endpoint The LoginSeller endpoint.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
string $module_url,
|
|
|
|
State $state,
|
2022-02-08 10:25:01 +02:00
|
|
|
Environment $environment,
|
2020-08-31 09:37:15 +03:00
|
|
|
LoginSellerEndpoint $login_seller_endpoint
|
|
|
|
) {
|
|
|
|
|
2021-01-27 14:53:45 -05:00
|
|
|
$this->module_url = untrailingslashit( $module_url );
|
2020-08-31 09:37:15 +03:00
|
|
|
$this->state = $state;
|
2022-02-08 10:25:01 +02:00
|
|
|
$this->environment = $environment;
|
2020-08-31 09:37:15 +03:00
|
|
|
$this->login_seller_endpoint = $login_seller_endpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers the scripts.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function register(): bool {
|
|
|
|
|
2022-01-14 14:34:54 +02:00
|
|
|
$url = untrailingslashit( $this->module_url ) . '/assets/css/onboarding.css';
|
2020-08-31 09:37:15 +03:00
|
|
|
wp_register_style(
|
|
|
|
'ppcp-onboarding',
|
|
|
|
$url,
|
|
|
|
array(),
|
|
|
|
1
|
|
|
|
);
|
2022-01-14 14:34:54 +02:00
|
|
|
$url = untrailingslashit( $this->module_url ) . '/assets/js/settings.js';
|
2020-08-31 09:37:15 +03:00
|
|
|
wp_register_script(
|
|
|
|
'ppcp-settings',
|
|
|
|
$url,
|
|
|
|
array(),
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2022-01-14 14:34:54 +02:00
|
|
|
$url = untrailingslashit( $this->module_url ) . '/assets/js/onboarding.js';
|
2020-08-31 09:37:15 +03:00
|
|
|
wp_register_script(
|
|
|
|
'ppcp-onboarding',
|
|
|
|
$url,
|
|
|
|
array( 'jquery' ),
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
wp_localize_script(
|
|
|
|
'ppcp-onboarding',
|
|
|
|
'PayPalCommerceGatewayOnboarding',
|
2021-01-27 14:56:42 -05:00
|
|
|
$this->get_script_data()
|
2020-08-31 09:37:15 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-27 14:56:42 -05:00
|
|
|
/**
|
|
|
|
* Returns the data associated to the onboarding script.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_script_data() {
|
|
|
|
return array(
|
2022-02-04 10:50:54 +02:00
|
|
|
'endpoint' => home_url( \WC_AJAX::get_endpoint( LoginSellerEndpoint::ENDPOINT ) ),
|
|
|
|
'nonce' => wp_create_nonce( $this->login_seller_endpoint::nonce() ),
|
|
|
|
'paypal_js_url' => 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js',
|
|
|
|
'sandbox_state' => State::get_state_name( $this->state->sandbox_state() ),
|
|
|
|
'production_state' => State::get_state_name( $this->state->production_state() ),
|
|
|
|
'current_state' => State::get_state_name( $this->state->current_state() ),
|
2022-02-08 10:25:01 +02:00
|
|
|
'current_env' => $this->environment->current_environment(),
|
2022-02-07 09:05:12 +02:00
|
|
|
'error_messages' => array(
|
|
|
|
'no_credentials' => __( 'Enter the credentials.', 'woocommerce-paypal-payments' ),
|
|
|
|
),
|
2021-01-27 14:56:42 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:37:15 +03:00
|
|
|
/**
|
|
|
|
* Enqueues the necessary scripts.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function enqueue(): bool {
|
|
|
|
wp_enqueue_style( 'ppcp-onboarding' );
|
|
|
|
wp_enqueue_script( 'ppcp-settings' );
|
|
|
|
if ( ! $this->should_render_onboarding_script() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_enqueue_script( 'ppcp-onboarding' );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the onboarding script should be rendered or not.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function should_render_onboarding_script(): bool {
|
|
|
|
global $current_section;
|
2020-10-01 09:00:09 +03:00
|
|
|
return 'ppcp-gateway' === $current_section;
|
2020-08-31 09:37:15 +03:00
|
|
|
}
|
|
|
|
}
|