| 1 |
<?php |
| 2 |
declare( strict_types=1 ); |
| 3 |
|
| 4 |
namespace GutSlider\Assets; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Handles enqueuing of editor and front-end assets. |
| 12 |
* |
| 13 |
* Registers and enqueues JavaScript and CSS assets required by the |
| 14 |
* plugin in both the block editor and the front-end. |
| 15 |
* |
| 16 |
* @package GutSlider\Assets |
| 17 |
* @since 3.0.0 |
| 18 |
*/ |
| 19 |
final class EnqueueAssets { |
| 20 |
|
| 21 |
/** |
| 22 |
* Preview images for blocks. |
| 23 |
* |
| 24 |
* Maps block identifiers to their preview image filenames. |
| 25 |
* |
| 26 |
* @since 3.0.0 |
| 27 |
* @var array<string, string> |
| 28 |
*/ |
| 29 |
private const PREVIEW_IMAGES = array( |
| 30 |
'content' => 'content.svg', |
| 31 |
'photo_carousel' => 'photo.svg', |
| 32 |
'testimonial_slider' => 'testimonial.svg', |
| 33 |
'before_after' => 'ba.svg', |
| 34 |
'placeholder' => 'placeholder.svg', |
| 35 |
); |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor. |
| 39 |
* |
| 40 |
* Registers WordPress hooks for asset enqueuing. |
| 41 |
* |
| 42 |
* @since 3.0.0 |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ), 2 ); |
| 46 |
add_action( 'enqueue_block_assets', array( $this, 'enqueue_both_assets' ) ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Enqueue editor-specific assets. |
| 51 |
* |
| 52 |
* Loads JavaScript and CSS required only within the block editor, |
| 53 |
* and localizes preview image URLs for block previews. |
| 54 |
* |
| 55 |
* @since 3.0.0 |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function enqueue_editor_assets(): void { |
| 60 |
$this->enqueue_asset( 'global', true, true ); |
| 61 |
$this->enqueue_asset( 'modules', false ); |
| 62 |
$this->localize_preview_images(); |
| 63 |
$this->localize_editor_settings(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Expose editor-facing settings to the global editor bundle. |
| 68 |
* |
| 69 |
* Printed as an inline script rather than via wp_localize_script so the |
| 70 |
* values keep their real types — wp_localize_script casts top level |
| 71 |
* scalars to strings, which would turn `false` into a truthy "". |
| 72 |
* |
| 73 |
* @since 3.0.0 |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
private function localize_editor_settings(): void { |
| 78 |
$settings = get_option( 'gutslider_settings', array() ); |
| 79 |
|
| 80 |
$pattern_library = ! is_array( $settings ) || ! array_key_exists( 'pattern_library', $settings ) |
| 81 |
? true |
| 82 |
: (bool) $settings['pattern_library']; |
| 83 |
|
| 84 |
wp_add_inline_script( |
| 85 |
'gutslider-blocks-global-script', |
| 86 |
'window.gutsliderEditor = ' . wp_json_encode( |
| 87 |
array( |
| 88 |
'patternLibrary' => $pattern_library, |
| 89 |
) |
| 90 |
) . ';', |
| 91 |
'before' |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Enqueue front-end and shared assets. |
| 97 |
* |
| 98 |
* Registers Swiper library assets and the lightbox script |
| 99 |
* for use by individual blocks on the front-end. |
| 100 |
* |
| 101 |
* @since 3.0.0 |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function enqueue_both_assets(): void { |
| 106 |
wp_register_style( |
| 107 |
'gutslider-swiper-style', |
| 108 |
GUTSLIDER_URL . 'assets/css/swiper-bundle.min.css', |
| 109 |
array(), |
| 110 |
GUTSLIDER_VERSION |
| 111 |
); |
| 112 |
|
| 113 |
wp_register_script( |
| 114 |
'gutslider-swiper-script', |
| 115 |
GUTSLIDER_URL . 'assets/js/swiper-bundle.min.js', |
| 116 |
array(), |
| 117 |
GUTSLIDER_VERSION, |
| 118 |
true |
| 119 |
); |
| 120 |
|
| 121 |
wp_localize_script( |
| 122 |
'gutslider-swiper-script', |
| 123 |
'gutslider_swiper', |
| 124 |
array( |
| 125 |
'placeholder' => esc_url( GUTSLIDER_URL . 'assets/images/placeholder.svg' ), |
| 126 |
) |
| 127 |
); |
| 128 |
|
| 129 |
if ( ! is_admin() ) { |
| 130 |
wp_register_script( |
| 131 |
'gutslider-fslightbox', |
| 132 |
GUTSLIDER_URL . 'assets/js/fslightbox.js', |
| 133 |
array(), |
| 134 |
GUTSLIDER_VERSION, |
| 135 |
true |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Enqueue or register a build asset by name. |
| 142 |
* |
| 143 |
* Loads the asset's dependency file and uses it to register or |
| 144 |
* enqueue the corresponding JavaScript and optionally CSS file. |
| 145 |
* |
| 146 |
* @since 3.0.0 |
| 147 |
* |
| 148 |
* @param string $name The asset name matching the build directory. |
| 149 |
* @param bool $enqueue_style Whether to also enqueue the CSS file. |
| 150 |
* @param bool $register_mode Whether to register only (true) or enqueue (false). |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
private function enqueue_asset( string $name, bool $enqueue_style = true, bool $register_mode = false ): void { |
| 154 |
$asset_path = GUTSLIDER_DIR_PATH . "build/{$name}/{$name}.asset.php"; |
| 155 |
|
| 156 |
if ( file_exists( $asset_path ) ) { |
| 157 |
$dependency_file = include $asset_path; |
| 158 |
|
| 159 |
if ( is_array( $dependency_file ) && ! empty( $dependency_file ) ) { |
| 160 |
$handle = "gutslider-blocks-{$name}-script"; |
| 161 |
$src = GUTSLIDER_URL . "build/{$name}/{$name}.js"; |
| 162 |
$dependencies = $dependency_file['dependencies'] ?? array(); |
| 163 |
$version = $dependency_file['version'] ?? GUTSLIDER_VERSION; |
| 164 |
$in_footer = ( 'global' === $name ); |
| 165 |
|
| 166 |
if ( $register_mode ) { |
| 167 |
wp_register_script( $handle, $src, $dependencies, $version, $in_footer ); |
| 168 |
} else { |
| 169 |
wp_enqueue_script( $handle, $src, $dependencies, $version, $in_footer ); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if ( $enqueue_style ) { |
| 175 |
$style_handle = "gutslider-blocks-{$name}-style"; |
| 176 |
$style_src = GUTSLIDER_URL . "build/{$name}/{$name}.css"; |
| 177 |
|
| 178 |
if ( $register_mode ) { |
| 179 |
wp_register_style( $style_handle, $style_src, array(), GUTSLIDER_VERSION ); |
| 180 |
} else { |
| 181 |
wp_enqueue_style( $style_handle, $style_src, array(), GUTSLIDER_VERSION ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Localize preview image URLs for the block editor. |
| 188 |
* |
| 189 |
* Passes preview image URLs and pro status to the modules script |
| 190 |
* for use in block preview rendering. |
| 191 |
* |
| 192 |
* @since 3.0.0 |
| 193 |
* |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
private function localize_preview_images(): void { |
| 197 |
$preview_urls = array(); |
| 198 |
foreach ( self::PREVIEW_IMAGES as $key => $image ) { |
| 199 |
$preview_urls[ $key ] = esc_url( GUTSLIDER_URL . "assets/images/{$image}" ); |
| 200 |
} |
| 201 |
|
| 202 |
$preview_urls['is_pro'] = defined( 'GUTSLIDER_PRO_VERSION' ); |
| 203 |
|
| 204 |
wp_localize_script( |
| 205 |
'gutslider-blocks-modules-script', |
| 206 |
'gutsliderData', |
| 207 |
array( |
| 208 |
'hasPro' => defined( 'GUTSLIDER_PRO_VERSION' ), |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
wp_localize_script( |
| 213 |
'gutslider-blocks-modules-script', |
| 214 |
'gutslider_preview', |
| 215 |
$preview_urls |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|