PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.11.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.11.2
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Assets.php

Assets.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.11.2, at includes/Assets.php

162 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription;
4
5 /**
6 * Scripts and Styles Class
7 */
8 class Assets {
9
10 /**
11 * Assets constructor.
12 */
13 public function __construct() {
14 if ( is_admin() ) {
15 add_action( 'admin_enqueue_scripts', array( $this, 'register' ), 5 );
16 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_components' ), 6 );
17 } else {
18 add_action( 'wp_enqueue_scripts', array( $this, 'register' ), 5 );
19 }
20 }
21
22 /**
23 * Register our app scripts and styles
24 *
25 * @return void
26 */
27 public function register() {
28 $this->register_scripts( $this->get_scripts() );
29 $this->register_styles( $this->get_styles() );
30 }
31
32 /**
33 * Register scripts
34 *
35 * @param array $scripts scripts.
36 *
37 * @return void
38 */
39 private function register_scripts( $scripts ) {
40 foreach ( $scripts as $handle => $script ) {
41 $deps = isset( $script['deps'] ) ? $script['deps'] : false;
42 $in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false;
43 $version = isset( $script['version'] ) ? $script['version'] : SUBSCRPT_VERSION;
44
45 wp_register_script( $handle, $script['src'], $deps, $version, $in_footer );
46
47 // Register script translations for scripts that use wp-i18n (e.g., block assets).
48 if ( function_exists( 'wp_set_script_translations' ) && 'sdevs_subscrpt_cart_block' === $handle ) {
49 wp_set_script_translations( $handle, 'subscription', SUBSCRPT_PATH . '/languages' );
50 }
51 }
52 }
53
54 /**
55 * Register styles
56 *
57 * @param array $styles styles.
58 *
59 * @return void
60 */
61 public function register_styles( $styles ) {
62 foreach ( $styles as $handle => $style ) {
63 $deps = isset( $style['deps'] ) ? $style['deps'] : false;
64
65 wp_register_style( $handle, $style['src'], $deps, SUBSCRPT_VERSION );
66 }
67 }
68
69 /**
70 * Get all registered scripts
71 *
72 * @return array
73 */
74 public function get_scripts() {
75 $plugin_js_assets_path = SUBSCRPT_ASSETS . '/js/';
76
77 $block_script_asset_path = SUBSCRPT_PATH . '/build/index.asset.php';
78 $block_script_asset = file_exists( $block_script_asset_path )
79 ? require $block_script_asset_path
80 : array(
81 'dependencies' => false,
82 'version' => SUBSCRPT_VERSION,
83 );
84
85 $scripts = array(
86 'sdevs_subscription_admin' => array(
87 'src' => $plugin_js_assets_path . 'admin.js',
88 'deps' => array( 'jquery' ),
89 'in_footer' => true,
90 ),
91 'subscrpt_admin_components' => array(
92 'src' => $plugin_js_assets_path . 'admin-components.js',
93 'deps' => array( 'wp-i18n' ),
94 'in_footer' => true,
95 ),
96 'sdevs_installer' => array(
97 'src' => $plugin_js_assets_path . 'installer.js',
98 'deps' => array( 'jquery' ),
99 'in_footer' => true,
100 ),
101 'sdevs_subscrpt_cart_block' => array(
102 'src' => SUBSCRPT_URL . '/build/index.js',
103 'deps' => $block_script_asset['dependencies'],
104 'version' => $block_script_asset['version'],
105 'in_footer' => true,
106 ),
107 );
108
109 return $scripts;
110 }
111
112 /**
113 * Get registered styles
114 *
115 * @return array
116 */
117 public function get_styles() {
118 $plugin_css_assets_path = SUBSCRPT_ASSETS . '/css/';
119
120 $styles = array(
121 'subscrpt_admin_css' => array(
122 'src' => $plugin_css_assets_path . 'admin.css',
123 ),
124 'subscrpt_admin_components' => array(
125 'src' => $plugin_css_assets_path . 'admin-components.css',
126 ),
127 'subscrpt_status_css' => array(
128 'src' => $plugin_css_assets_path . 'status.css',
129 ),
130 'sdevs_installer' => array(
131 'src' => $plugin_css_assets_path . 'installer.css',
132 ),
133 );
134
135 return $styles;
136 }
137
138 /**
139 * Enqueue admin component styles on all WPSubscription admin pages.
140 *
141 * @param string $hook The current admin page hook.
142 */
143 public function enqueue_admin_components( $hook ) {
144 $is_main_page = 'toplevel_page_wp-subscription' === $hook;
145 $is_subs_page = str_starts_with( $hook, 'wpsubscription_page' ) || str_starts_with( $hook, 'wp-subscription_page' );
146
147 if ( $is_main_page || $is_subs_page ) {
148 wp_enqueue_style( 'subscrpt_admin_components' );
149 wp_enqueue_script( 'subscrpt_admin_components' );
150 }
151 }
152
153 /**
154 * Enqueue chart.js where this method is called.
155 *
156 * Source: https://cdn.jsdelivr.net/npm/chart.js
157 */
158 public static function enqueue_chart_js() {
159 wp_enqueue_script( 'subscrpt-ext-lib-chartjs', SUBSCRPT_ASSETS . '/js/chart.js', [], SUBSCRPT_VERSION, true );
160 }
161 }
162