| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Disco |
| 5 |
* |
| 6 |
* @package Disco |
| 7 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 8 |
* @link http://domain.tld |
| 9 |
* @license GPL 2.0+ |
| 10 |
* @copyright 2022 WebAppick |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Disco\Backend; |
| 14 |
|
| 15 |
use Disco\Engine\Base; |
| 16 |
use Inpsyde\Assets\Asset; |
| 17 |
use Inpsyde\Assets\AssetManager; |
| 18 |
use Inpsyde\Assets\Script; |
| 19 |
use Inpsyde\Assets\Style; |
| 20 |
|
| 21 |
/** |
| 22 |
* This class contain the Enqueue stuff for the backend |
| 23 |
*/ |
| 24 |
class Enqueue extends Base { |
| 25 |
|
| 26 |
/** |
| 27 |
* Initialize the class. |
| 28 |
* |
| 29 |
* @return void|bool |
| 30 |
*/ |
| 31 |
public function initialize() { |
| 32 |
if ( ! parent::initialize() ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
\add_action( AssetManager::ACTION_SETUP, array( $this, 'enqueue_assets' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Enqueue assets with Inpyside library https://inpsyde.github.io/assets |
| 41 |
* |
| 42 |
* @param \Inpsyde\Assets\AssetManager $asset_manager The class. |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public function enqueue_assets( AssetManager $asset_manager ) { |
| 46 |
// Load admin style sheet and JavaScript. |
| 47 |
$assets = $this->enqueue_admin_styles(); |
| 48 |
|
| 49 |
if ( ! empty( $assets ) ) { |
| 50 |
foreach ( $assets as $asset ) { |
| 51 |
$asset_manager->register( $asset ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
$assets = $this->enqueue_admin_scripts(); |
| 56 |
|
| 57 |
if ( empty( $assets ) ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
foreach ( $assets as $asset ) { |
| 62 |
$asset_manager->register( $asset ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register and enqueue admin-specific style sheet. |
| 68 |
* |
| 69 |
* @return array |
| 70 |
* @since 1.0.0 |
| 71 |
*/ |
| 72 |
public function enqueue_admin_styles() { |
| 73 |
$admin_page = \get_current_screen(); |
| 74 |
$styles = array(); |
| 75 |
|
| 76 |
$styles[0] = new Style( DISCO_TEXTDOMAIN . '-admin-style', \plugins_url( 'assets/build/plugin-admin.css', DISCO_PLUGIN_ABSOLUTE ) ); |
| 77 |
$styles[0]->forLocation( Asset::BACKEND )->withVersion( DISCO_VERSION ); |
| 78 |
$styles[0]->withDependencies( 'dashicons' ); |
| 79 |
|
| 80 |
if ( ! \is_null( $admin_page ) && 'toplevel_page_disco-create-discount' === $admin_page->id ) { |
| 81 |
$styles[1] = new Style( DISCO_TEXTDOMAIN . '-admin-tailwind-style', \plugins_url( 'backend/views/asset/tailwind.css', DISCO_PLUGIN_ABSOLUTE ) ); |
| 82 |
$styles[1]->forLocation( Asset::BACKEND )->withVersion( DISCO_VERSION ); |
| 83 |
$styles[1]->withDependencies( 'dashicons' ); |
| 84 |
} |
| 85 |
|
| 86 |
return $styles; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Register and enqueue admin-specific JavaScript. |
| 91 |
* |
| 92 |
* @return array |
| 93 |
* @since 1.0.0 |
| 94 |
*/ |
| 95 |
public function enqueue_admin_scripts() { |
| 96 |
$admin_page = \get_current_screen(); |
| 97 |
$scripts = array(); |
| 98 |
|
| 99 |
if ( ! \is_null( $admin_page ) && 'toplevel_page_disco-create-discount' === $admin_page->id ) { |
| 100 |
$scripts[0] = new Script( DISCO_TEXTDOMAIN . '-settings-admin', \plugins_url( 'assets/build/plugin-admin.js', DISCO_PLUGIN_ABSOLUTE ) ); |
| 101 |
$scripts[0]->forLocation( Asset::BACKEND )->withVersion( DISCO_VERSION ); |
| 102 |
$scripts[0]->dependencies(); |
| 103 |
$scripts[0]->withLocalize( |
| 104 |
'DISCO', |
| 105 |
array( |
| 106 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 107 |
'json_url' => esc_url_raw( rest_url( 'disco/v1' ) ), |
| 108 |
'is_pretty_url' => (bool) get_option('permalink_structure'), |
| 109 |
'rest_nonce' => wp_create_nonce( 'wp_rest' ), |
| 110 |
'admin_url' => admin_url( 'admin.php' ), |
| 111 |
'site_url' => site_url(), |
| 112 |
'TEXTDOMAIN' => DISCO_TEXTDOMAIN, |
| 113 |
'is_pro_active' => is_plugin_active( 'disco-pro/disco-pro.php' ), |
| 114 |
) |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
$scripts[1] = new Script( DISCO_TEXTDOMAIN . '-admin-tailwind-script', \plugins_url( 'assets/build/plugin-notice.js', DISCO_PLUGIN_ABSOLUTE ) ); |
| 119 |
$scripts[1]->forLocation( Asset::BACKEND )->withVersion( DISCO_VERSION ); |
| 120 |
$scripts[1]->dependencies(); |
| 121 |
$scripts[1]->withLocalize( |
| 122 |
'DISCO_NOTICE', |
| 123 |
array( |
| 124 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 125 |
'nonce' => wp_create_nonce( 'disco_dismiss_notice' ), |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
return $scripts; |
| 130 |
} |
| 131 |
|
| 132 |
} |
| 133 |
|