| 1 |
<?php |
| 2 |
/** |
| 3 |
* Disco |
| 4 |
* |
| 5 |
* @package Disco |
| 6 |
* @author Ohidul Islam <wahid0003@gmail.com> |
| 7 |
* @copyright 2022 WebAppick |
| 8 |
* @license GPL 2.0+ |
| 9 |
* @link http://domain.tld |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\Frontend; |
| 13 |
|
| 14 |
use Disco\Engine\Base; |
| 15 |
use Inpsyde\Assets\Asset; |
| 16 |
use Inpsyde\Assets\AssetManager; |
| 17 |
use Inpsyde\Assets\Script; |
| 18 |
use Inpsyde\Assets\Style; |
| 19 |
|
| 20 |
/** |
| 21 |
* Enqueue stuff on the frontend |
| 22 |
*/ |
| 23 |
class Enqueue extends Base { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize the class. |
| 27 |
* |
| 28 |
* @return void|bool |
| 29 |
*/ |
| 30 |
public function initialize() { |
| 31 |
parent::initialize(); |
| 32 |
|
| 33 |
\add_action( AssetManager::ACTION_SETUP, array( $this, 'enqueue_assets' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Enqueue assets with Inpyside library https://inpsyde.github.io/assets |
| 38 |
* |
| 39 |
* @param \Inpsyde\Assets\AssetManager $asset_manager The class. |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function enqueue_assets( AssetManager $asset_manager ) { |
| 43 |
// Load public-facing style sheet and JavaScript. |
| 44 |
$assets = $this->enqueue_styles(); |
| 45 |
|
| 46 |
if ( ! empty( $assets ) ) { |
| 47 |
foreach ( $assets as $asset ) { |
| 48 |
$asset_manager->register( $asset ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$assets = $this->enqueue_scripts(); |
| 53 |
|
| 54 |
if ( empty( $assets ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ( $assets as $asset ) { |
| 59 |
$asset_manager->register( $asset ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register and enqueue public-facing style sheet. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function enqueue_styles() { |
| 70 |
$styles = array(); |
| 71 |
$styles[0] = new Style( DISCO_TEXTDOMAIN . '-plugin-styles', \plugins_url( 'assets/build/plugin-public.css', DISCO_PLUGIN_ABSOLUTE ) ); |
| 72 |
$styles[0]->forLocation( Asset::FRONTEND )->useAsyncFilter()->withVersion( DISCO_VERSION ); |
| 73 |
$styles[0]->dependencies(); |
| 74 |
|
| 75 |
return $styles; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register and enqueues public-facing JavaScript files. |
| 80 |
* |
| 81 |
* @since 1.0.0 |
| 82 |
* @return array |
| 83 |
*/ |
| 84 |
public static function enqueue_scripts() { |
| 85 |
$scripts = array(); |
| 86 |
$scripts[0] = new Script( DISCO_TEXTDOMAIN . '-plugin-script', \plugins_url( 'assets/build/plugin-public.js', DISCO_PLUGIN_ABSOLUTE ) ); |
| 87 |
$scripts[0]->forLocation( Asset::FRONTEND )->useAsyncFilter()->withVersion( DISCO_VERSION ); |
| 88 |
$scripts[0]->dependencies(); |
| 89 |
$scripts[0]->withLocalize( |
| 90 |
'exampleDemo', |
| 91 |
array( |
| 92 |
'alert' => \__( 'Error!', 'disco' ), |
| 93 |
'nonce' => \wp_create_nonce( 'demo_example' ), |
| 94 |
'wp_rest' => \wp_create_nonce( 'wp_rest' ), |
| 95 |
) |
| 96 |
); |
| 97 |
|
| 98 |
return $scripts; |
| 99 |
} |
| 100 |
|
| 101 |
} |
| 102 |
|