| 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 |
function __construct() { |
| 14 |
if ( is_admin() ) { |
| 15 |
add_action( 'admin_enqueue_scripts', array( $this, 'register' ), 5 ); |
| 16 |
} else { |
| 17 |
add_action( 'wp_enqueue_scripts', array( $this, 'register' ), 5 ); |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register our app scripts and styles |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function register() { |
| 27 |
$this->register_scripts( $this->get_scripts() ); |
| 28 |
$this->register_styles( $this->get_styles() ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Register scripts |
| 33 |
* |
| 34 |
* @param array $scripts |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
private function register_scripts( $scripts ) { |
| 39 |
foreach ( $scripts as $handle => $script ) { |
| 40 |
$deps = isset( $script['deps'] ) ? $script['deps'] : false; |
| 41 |
$in_footer = isset( $script['in_footer'] ) ? $script['in_footer'] : false; |
| 42 |
$version = isset( $script['version'] ) ? $script['version'] : WP_SUBSCRIPTION_VERSION; |
| 43 |
|
| 44 |
wp_register_script( $handle, $script['src'], $deps, $version, $in_footer ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Register styles |
| 50 |
* |
| 51 |
* @param array $styles |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function register_styles( $styles ) { |
| 56 |
foreach ( $styles as $handle => $style ) { |
| 57 |
$deps = isset( $style['deps'] ) ? $style['deps'] : false; |
| 58 |
|
| 59 |
wp_register_style( $handle, $style['src'], $deps, WP_SUBSCRIPTION_VERSION ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Get all registered scripts |
| 65 |
* |
| 66 |
* @return array |
| 67 |
*/ |
| 68 |
public function get_scripts() { |
| 69 |
$plugin_js_assets_path = WP_SUBSCRIPTION_ASSETS . '/js/'; |
| 70 |
|
| 71 |
$block_script_asset_path = WP_SUBSCRIPTION_PATH . '/build/index.asset.php'; |
| 72 |
$block_script_asset = file_exists( $block_script_asset_path ) |
| 73 |
? require $block_script_asset_path |
| 74 |
: array( |
| 75 |
'dependencies' => false, |
| 76 |
'version' => WP_SUBSCRIPTION_VERSION, |
| 77 |
); |
| 78 |
|
| 79 |
$scripts = array( |
| 80 |
'sdevs_subscription_admin' => array( |
| 81 |
'src' => $plugin_js_assets_path . 'admin.js', |
| 82 |
'deps' => array( 'jquery' ), |
| 83 |
'in_footer' => true, |
| 84 |
), |
| 85 |
'sdevs_installer' => array( |
| 86 |
'src' => $plugin_js_assets_path . 'installer.js', |
| 87 |
'deps' => array( 'jquery' ), |
| 88 |
'in_footer' => true, |
| 89 |
), |
| 90 |
'sdevs_subscrpt_cart_block' => array( |
| 91 |
'src' => WP_SUBSCRIPTION_URL . '/build/index.js', |
| 92 |
'deps' => $block_script_asset['dependencies'], |
| 93 |
'version' => $block_script_asset['version'], |
| 94 |
'in_footer' => true, |
| 95 |
), |
| 96 |
); |
| 97 |
|
| 98 |
return $scripts; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get registered styles |
| 103 |
* |
| 104 |
* @return array |
| 105 |
*/ |
| 106 |
public function get_styles() { |
| 107 |
$plugin_css_assets_path = WP_SUBSCRIPTION_ASSETS . '/css/'; |
| 108 |
|
| 109 |
$styles = array( |
| 110 |
'subscrpt_admin_css' => array( |
| 111 |
'src' => $plugin_css_assets_path . 'admin.css', |
| 112 |
), |
| 113 |
'subscrpt_status_css' => array( |
| 114 |
'src' => $plugin_css_assets_path . 'status.css', |
| 115 |
), |
| 116 |
'sdevs_installer' => array( |
| 117 |
'src' => $plugin_css_assets_path . 'installer.css', |
| 118 |
), |
| 119 |
); |
| 120 |
|
| 121 |
return $styles; |
| 122 |
} |
| 123 |
} |
| 124 |
|