PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.10.4
Subscriptions for WooCommerce with Stripe Recurring Payments v1.10.4
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 1.5.2 All 60 releases
subscription / includes / Assets.php

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

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