PluginProbe
GutSlider – All in One Slider and Carousel Blocks for Gutenberg / 2.13.1
GutSlider – All in One Slider and Carousel Blocks for Gutenberg v2.13.1
3.1.0 3.0.0 2.13.2 2.13.1 2.13.0 trunk 1.0.0 2.1.0 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.1 2.5.3 2.5.4 2.5.5 2.6.1 All 56 releases
slider-blocks / includes / Assets / EnqueueAssets.php

EnqueueAssets.php in GutSlider – All in One Slider and Carousel Blocks for Gutenberg 2.13.1, at includes/Assets/EnqueueAssets.php

189 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
64
65 /**
66 * Enqueue front-end and shared assets.
67 *
68 * Registers Swiper library assets and the lightbox script
69 * for use by individual blocks on the front-end.
70 *
71 * @since 3.0.0
72 *
73 * @return void
74 */
75 public function enqueue_both_assets(): void {
76 wp_register_style(
77 'gutslider-swiper-style',
78 GUTSLIDER_URL . 'assets/css/swiper-bundle.min.css',
79 array(),
80 GUTSLIDER_VERSION
81 );
82
83 wp_register_script(
84 'gutslider-swiper-script',
85 GUTSLIDER_URL . 'assets/js/swiper-bundle.min.js',
86 array(),
87 GUTSLIDER_VERSION,
88 true
89 );
90
91 wp_localize_script(
92 'gutslider-swiper-script',
93 'gutslider_swiper',
94 array(
95 'placeholder' => esc_url( GUTSLIDER_URL . 'assets/images/placeholder.svg' ),
96 )
97 );
98
99 if ( ! is_admin() ) {
100 wp_register_script(
101 'gutslider-fslightbox',
102 GUTSLIDER_URL . 'assets/js/fslightbox.js',
103 array(),
104 GUTSLIDER_VERSION,
105 true
106 );
107 }
108 }
109
110 /**
111 * Enqueue or register a build asset by name.
112 *
113 * Loads the asset's dependency file and uses it to register or
114 * enqueue the corresponding JavaScript and optionally CSS file.
115 *
116 * @since 3.0.0
117 *
118 * @param string $name The asset name matching the build directory.
119 * @param bool $enqueue_style Whether to also enqueue the CSS file.
120 * @param bool $register_mode Whether to register only (true) or enqueue (false).
121 * @return void
122 */
123 private function enqueue_asset( string $name, bool $enqueue_style = true, bool $register_mode = false ): void {
124 $asset_path = GUTSLIDER_DIR_PATH . "build/{$name}/{$name}.asset.php";
125
126 if ( file_exists( $asset_path ) ) {
127 $dependency_file = include $asset_path;
128
129 if ( is_array( $dependency_file ) && ! empty( $dependency_file ) ) {
130 $handle = "gutslider-blocks-{$name}-script";
131 $src = GUTSLIDER_URL . "build/{$name}/{$name}.js";
132 $dependencies = $dependency_file['dependencies'] ?? array();
133 $version = $dependency_file['version'] ?? GUTSLIDER_VERSION;
134 $in_footer = ( 'global' === $name );
135
136 if ( $register_mode ) {
137 wp_register_script( $handle, $src, $dependencies, $version, $in_footer );
138 } else {
139 wp_enqueue_script( $handle, $src, $dependencies, $version, $in_footer );
140 }
141 }
142 }
143
144 if ( $enqueue_style ) {
145 $style_handle = "gutslider-blocks-{$name}-style";
146 $style_src = GUTSLIDER_URL . "build/{$name}/{$name}.css";
147
148 if ( $register_mode ) {
149 wp_register_style( $style_handle, $style_src, array(), GUTSLIDER_VERSION );
150 } else {
151 wp_enqueue_style( $style_handle, $style_src, array(), GUTSLIDER_VERSION );
152 }
153 }
154 }
155
156 /**
157 * Localize preview image URLs for the block editor.
158 *
159 * Passes preview image URLs and pro status to the modules script
160 * for use in block preview rendering.
161 *
162 * @since 3.0.0
163 *
164 * @return void
165 */
166 private function localize_preview_images(): void {
167 $preview_urls = array();
168 foreach ( self::PREVIEW_IMAGES as $key => $image ) {
169 $preview_urls[ $key ] = esc_url( GUTSLIDER_URL . "assets/images/{$image}" );
170 }
171
172 $preview_urls['is_pro'] = defined( 'GUTSLIDER_PRO_VERSION' );
173
174 wp_localize_script(
175 'gutslider-blocks-modules-script',
176 'gutsliderData',
177 array(
178 'hasPro' => defined( 'GUTSLIDER_PRO_VERSION' ),
179 )
180 );
181
182 wp_localize_script(
183 'gutslider-blocks-modules-script',
184 'gutslider_preview',
185 $preview_urls
186 );
187 }
188 }
189