class-wc-payments-subscriptions-empty-state-manager.php
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Class WC_Payments_Subscriptions_Empty_State.
*
* @package WooCommerce\Payments
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class for loading WooCommerce Payments Subscription empty state screen.
*/
class WC_Payments_Subscriptions_Empty_State_Manager {
use WC_Payments_Subscriptions_Utilities;
/**
* WC_Payments_Account instance to get information about the account.
*
* @var WC_Payments_Account
*/
private $account;
/**
* WC_Payments_Subscriptions_Empty_State Constructor
*
* @param WC_Payments_Account $account Account class instance.
*/
public function __construct( WC_Payments_Account $account ) {
$this->account = $account;
if ( ! $this->is_subscriptions_plugin_active() ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts_and_styles' ] );
add_filter( 'woocommerce_subscriptions_not_found_label', [ $this, 'replace_subscriptions_empty_state' ] );
}
}
/**
* Enqueues the WCPay Subscription empty state scripts and styles.
*/
public function enqueue_scripts_and_styles() {
$screen = get_current_screen();
// Only enqueue the scripts on the admin subscriptions screen.
if ( ! $screen || 'edit-shop_subscription' !== $screen->id || wcs_do_subscriptions_exist() ) {
return;
}
$script_src_url = plugins_url( 'dist/subscriptions-empty-state.js', WCPAY_PLUGIN_FILE );
$script_asset_path = WCPAY_ABSPATH . 'dist/subscriptions-empty-state.asset.php';
$script_asset = file_exists( $script_asset_path ) ? require $script_asset_path : [ 'dependencies' => [] ];
$wcpay_settings = [
'connectUrl' => WC_Payments_Account::get_connect_url( 'WC_SUBSCRIPTIONS_TABLE' ),
'isConnected' => $this->account->is_stripe_connected(),
'newProductUrl' => WC_Subscriptions_Admin::add_subscription_url(),
];
wp_register_script(
'WCPAY_SUBSCRIPTIONS_EMPTY_STATE',
$script_src_url,
$script_asset['dependencies'],
WC_Payments::get_file_version( 'dist/subscriptions-empty-state.js' ),
true
);
wp_localize_script(
'WCPAY_SUBSCRIPTIONS_EMPTY_STATE',
'wcpay',
$wcpay_settings
);
wp_register_style(
'WCPAY_SUBSCRIPTIONS_EMPTY_STATE',
plugins_url( 'dist/subscriptions-empty-state.css', WCPAY_PLUGIN_FILE ),
[],
WC_Payments::get_file_version( 'dist/subscriptions-empty-state.css' )
);
wp_enqueue_script( 'WCPAY_SUBSCRIPTIONS_EMPTY_STATE' );
wp_enqueue_style( 'WCPAY_SUBSCRIPTIONS_EMPTY_STATE' );
}
/**
* Replaces the default empty subscriptions state HTML with a wrapper for our content to be placed into.
*
* @param string $default_empty_state_html The default Subscriptions empty state HTML.
* @return string The empty subscriptions sate wrapper.
*/
public function replace_subscriptions_empty_state( $default_empty_state_html ) {
if ( wcs_do_subscriptions_exist() ) {
return $default_empty_state_html;
}
return '<div id="wcpay_subscriptions_empty_state"></div>';
}
}