PluginProbe
Gutenify – Visual Site Builder Blocks & Starter Templates / 1.0.2
Gutenify – Visual Site Builder Blocks & Starter Templates v1.0.2
1.9.0 1.8.0 1.7.0 1.6.6 1.6.5 1.6.4 trunk 0.0.1 0.0.2 0.0.3 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 All 74 releases
gutenify / inc / class-block-assets.php

class-block-assets.php in Gutenify – Visual Site Builder Blocks & Starter Templates 1.0.2, at inc/class-block-assets.php

488 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Load assets for our blocks.
4 *
5 * @package Gutenify
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Load general assets for our blocks.
15 *
16 * @since 1.0.0
17 */
18 class Gutenify_Block_Assets {
19 /**
20 * This plugin's instance.
21 *
22 * @var Gutenify_Block_Assets
23 */
24 private static $instance;
25
26 /**
27 * Registers the plugin.
28 *
29 * @return Gutenify_Block_Assets
30 */
31 public static function register() {
32 if ( null === self::$instance ) {
33 self::$instance = new Gutenify_Block_Assets();
34 }
35
36 return self::$instance;
37 }
38
39 /**
40 * The Constructor.
41 */
42 public function __construct() {
43 add_action( 'enqueue_block_assets', array( $this, 'block_assets' ) );
44 add_action( 'init', array( $this, 'editor_assets' ) );
45 add_action( 'wp_enqueue_scripts', array( $this, 'add_block_inline_css' ), 200 );
46 add_action( 'admin_enqueue_scripts', array( $this, 'add_admin_global_inline_css' ), 200 );
47 add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ), 9 );
48 add_action( 'enqueue_block_editor_assets', array( $this, 'editor_scripts' ) );
49 add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
50 }
51
52 /**
53 * Loads the asset file for the given script or style.
54 * Returns a default if the asset file is not found.
55 *
56 * @param string $filepath The name of the file without the extension.
57 *
58 * @return array The asset file contents.
59 */
60 public function get_asset_file( $filepath ) {
61 $asset_path = GUTENIFY_PLUGIN_DIR . $filepath . '.asset.php';
62
63 return file_exists( $asset_path )
64 ? include $asset_path
65 : array(
66 'dependencies' => array(),
67 'version' => GUTENIFY_VERSION,
68 );
69 }
70
71 /**
72 * Enqueue block assets for use within Gutenberg.
73 *
74 * @access public
75 */
76 public function block_assets() {
77 if ( is_admin() ) {
78 return false;
79 }
80 global $post;
81
82 // Only load the front end CSS if a Gutenify is in use.
83 $has_gutenify = ! is_singular();
84
85 if ( ! is_admin() && is_singular() ) {
86 $wp_post = get_post( $post );
87
88 // This is similar to has_block() in core, but will match anything
89 // in the gutenify/* namespace.
90 if ( $wp_post instanceof WP_Post ) {
91 $has_gutenify = ! empty(
92 array_filter(
93 array(
94 false !== strpos( $wp_post->post_content, '<!-- wp:gutenify/' ),
95 has_block( 'core/block', $wp_post ),
96 has_block( 'core/button', $wp_post ),
97 has_block( 'core/cover', $wp_post ),
98 has_block( 'core/heading', $wp_post ),
99 has_block( 'core/image', $wp_post ),
100 has_block( 'core/gallery', $wp_post ),
101 has_block( 'core/list', $wp_post ),
102 has_block( 'core/paragraph', $wp_post ),
103 has_block( 'core/pullquote', $wp_post ),
104 has_block( 'core/quote', $wp_post ),
105 )
106 )
107 );
108 }
109 }
110
111 if ( ! $has_gutenify && ! $this->is_page_gutenberg() ) {
112 return;
113 }
114
115 wp_enqueue_style( 'gutenify-fonts', gutenify_fonts_url(), array(), null );
116
117 // Styles.
118 $name = 'gutenify-style';
119 $filepath = 'dist/' . $name;
120 $asset_file = $this->get_asset_file( $filepath );
121 $rtl = ! is_rtl() ? '' : '-rtl';
122
123 wp_enqueue_style(
124 'gutenify-frontend',
125 GUTENIFY_PLUGIN_URL . $filepath . $rtl . '.css',
126 array( 'gutenify-fontawesome-style', 'gutenify-inline-handle' ),
127 $asset_file['version']
128 );
129 }
130
131 /**
132 * Enqueue block assets for use within Gutenberg.
133 *
134 * @access public
135 */
136 public function editor_assets() {
137 // Styles.
138 $name = 'gutenify-editor';
139 $filepath = 'dist/' . $name;
140 $asset_file = $this->get_asset_file( $filepath );
141 $rtl = ! is_rtl() ? '' : '-rtl';
142
143 wp_register_style(
144 'gutenify-fontawesome-style',
145 GUTENIFY_PLUGIN_URL . '/assets/fontawesome/css/all.css',
146 array(),
147 'v4'
148 );
149
150 wp_register_style(
151 'gutenify-editor',
152 GUTENIFY_PLUGIN_URL . $filepath . $rtl . '.css',
153 array( 'gutenify-fontawesome-style' ),
154 $asset_file['version']
155 );
156
157 // Scripts.
158 $name = 'gutenify'; // gutenify.js.
159 $filepath = 'dist/' . $name;
160 $asset_file = $this->get_asset_file( $filepath );
161
162 wp_register_script(
163 'gutenify-editor',
164 GUTENIFY_PLUGIN_URL . $filepath . '.js',
165 array_merge( $asset_file['dependencies'], array( 'wp-api', 'gutenify-global' ) ),
166 $asset_file['version'],
167 true
168 );
169
170 $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
171
172 wp_localize_script(
173 'gutenify-editor',
174 'gutenify_block_data',
175 array(
176 'plugin_directory_url' => esc_url( GUTENIFY_PLUGIN_URL ),
177 'site_url' => esc_url( site_url() ),
178 'settings_url' => esc_url( admin_url( 'edit.php?post_type=gutenify_template&page=gutenify-settings' ) ),
179 'add_template_url' => esc_url( admin_url( 'post-new.php?post_type=gutenify_template' ) ),
180 )
181 );
182
183 }
184
185 /**
186 * Enqueue front-end assets for blocks.
187 *
188 * @access public
189 * @since 1.9.5
190 */
191 public function frontend_scripts() {
192
193 // Scripts.
194 $name = 'swiper-bundle'; // swiper.js.
195 $filepath = 'assets/js/lib/' . $name;
196
197 wp_register_script(
198 'gutenify-swiper',
199 GUTENIFY_PLUGIN_URL . $filepath . '.js',
200 array( 'jquery' ),
201 '6.8.2',
202 false
203 );
204
205 // Scripts.
206 $name = 'gutenify-frontend'; // gutenify.js.
207 $filepath = 'dist/js/' . $name;
208 $asset_file = $this->get_asset_file( $filepath );
209
210 // Enqueue for gutenify animations.
211 wp_enqueue_script(
212 'gutenify-frontend',
213 GUTENIFY_PLUGIN_URL . $filepath . '.js',
214 array( 'jquery', 'gutenify-swiper' ),
215 GUTENIFY_VERSION,
216 false
217 );
218
219 // Styles.
220 $name = 'swiper-bundle.min'; // swiper.js.
221 $filepath = 'assets/css/lib/' . $name;
222 $asset_file = $this->get_asset_file( $filepath );
223 wp_enqueue_style(
224 'gutenify-swiper-style',
225 GUTENIFY_PLUGIN_URL . $filepath . '.css',
226 array(),
227 '6.8.2'
228 );
229
230 }
231
232 /**
233 * Enqueue editor scripts for blocks.
234 *
235 * @access public
236 * @since 1.9.5
237 */
238 public function editor_scripts() {
239 }
240
241 /**
242 * Return whether a post type should display the Block Editor.
243 *
244 * @param string $post_type The post_type slug to check.
245 */
246 protected function is_post_type_gutenberg( $post_type ) {
247 return use_block_editor_for_post_type( $post_type );
248 }
249
250 /**
251 * Return whether the page we are on is loading the Block Editor.
252 */
253 protected function is_page_gutenberg() {
254 if ( ! is_admin() ) {
255 return false;
256 }
257
258 $admin_page = wp_basename( esc_url( $_SERVER['REQUEST_URI'] ) );
259
260 if ( false !== strpos( $admin_page, 'post-new.php' ) && empty( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
261 return true;
262 }
263
264 if ( false !== strpos( $admin_page, 'post-new.php' ) && isset( $_GET['post_type'] ) && $this->is_post_type_gutenberg( $_GET['post_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
265 return true;
266 }
267
268 if ( false !== strpos( $admin_page, 'post.php' ) ) {
269 $wp_post = get_post( $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
270 if ( isset( $wp_post ) && isset( $wp_post->post_type ) && $this->is_post_type_gutenberg( $wp_post->post_type ) ) {
271 return true;
272 }
273 }
274
275 if ( false !== strpos( $admin_page, 'revision.php' ) ) {
276 $wp_post = get_post( $_GET['revision'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
277 $post_parent = get_post( $wp_post->post_parent );
278 if ( isset( $post_parent ) && isset( $post_parent->post_type ) && $this->is_post_type_gutenberg( $post_parent->post_type ) ) {
279 return true;
280 }
281 }
282 return false;
283 }
284
285 /**
286 * Add inline css
287 *
288 * @return void
289 */
290 public static function add_block_inline_css() {
291 if ( is_singular() ) {
292 global $post;
293 // $style_file = trailingslashit( wp_upload_dir()['basedir'] ) . 'gutenify/styles/style-' . 'post-' . $post->ID . '.css';
294
295 // if ( file_exists( $style_file ) ) {
296 // $style_file_url = trailingslashit( wp_upload_dir()['baseurl'] ) . 'gutenify/styles/style-' . 'post-' . $post->ID . '.css';
297
298 // wp_enqueue_style( 'gutenify-frontend-post', $style_file_url, array( 'gutenify-frontend' ), GUTENIFY_VERSION );
299 // } else {
300 // }
301 $post_meta = get_post_meta( $post->ID );
302 $gutenify_custom_css = ! empty( $post_meta['gutenify_custom_css'][0] ) ? $post_meta['gutenify_custom_css'][0] : '';
303 wp_add_inline_style( 'gutenify-frontend', $gutenify_custom_css );
304 }
305 $gutenify_global_style = get_option( 'gutenify_global_style' );
306
307 wp_register_style( 'gutenify-inline-handle', false );
308 wp_enqueue_style( 'gutenify-inline-handle' );
309
310 if ( $gutenify_global_style ) {
311 global $wp_styles;
312 $handle = 'gutenify-inline-handle';
313 if ( in_array( 'global-styles', $wp_styles->queue ) ) {
314 $handle = 'global-styles';
315 }
316 wp_add_inline_style( $handle, $gutenify_global_style );
317 }
318 }
319
320 /**
321 * Add Global inline css
322 *
323 * @return void
324 */
325 public static function add_admin_global_inline_css() {
326
327 $gutenify_admin_global_style = get_option( 'gutenify_admin_global_style' );
328
329 wp_register_style( 'gutenify-inline-handle', false );
330 wp_enqueue_style( 'gutenify-inline-handle' );
331
332 if ( $gutenify_admin_global_style ) {
333 wp_add_inline_style( 'gutenify-inline-handle', $gutenify_admin_global_style );
334 }
335 }
336
337 /**
338 * Admin scripts.
339 *
340 * @return mixed
341 */
342 public function admin_scripts() {
343 // Scripts.
344 $name = 'gutenify-global'; // gutenify.js.
345 $filepath = 'dist/' . $name;
346 $asset_file = $this->get_asset_file( $filepath );
347
348 // Enqueue for gutenify animations.
349 wp_register_script(
350 $name,
351 GUTENIFY_PLUGIN_URL . $filepath . '.js',
352 $asset_file['dependencies'],
353 $asset_file['version'],
354 true
355 );
356
357
358 // Site options
359 if ( ( ! empty( $_GET['page'] ) && 'gutenify-site-options' === $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
360 // Scripts.
361 $name = 'gutenify-site-options-admin'; // gutenify.js.
362 $filepath = 'dist/' . $name;
363 $asset_file = $this->get_asset_file( $filepath );
364
365 // Enqueue for gutenify animations.
366 wp_enqueue_script(
367 'gutenify-site-options-admin',
368 GUTENIFY_PLUGIN_URL . $filepath . '.js',
369 $asset_file['dependencies'],
370 $asset_file['version'],
371 true
372 );
373
374 // Styles.
375 $name = 'gutenify-site-options-admin-style';
376 $filepath = 'dist/' . $name;
377 $asset_file = $this->get_asset_file( $filepath );
378 $rtl = ! is_rtl() ? '' : '-rtl';
379
380 wp_enqueue_style(
381 $name,
382 GUTENIFY_PLUGIN_URL . $filepath . $rtl . '.css',
383 array( 'gutenify-fontawesome-style', 'wp-components' ),
384 $asset_file['version']
385 );
386
387 wp_localize_script(
388 'gutenify-site-options-admin',
389 '_gutenify_site_options',
390 array(
391 'site_url' => esc_url( site_url() ),
392 'gutenify_com_server_url' => defined( 'GUTENIFY_COM_SERVER_URL' ) ? trailingslashit( GUTENIFY_COM_SERVER_URL ) : trailingslashit( 'https://api.gutenify.com/' ),
393 'is_block_theme' => function_exists( 'wp_get_theme' ) && ! empty( wp_get_theme()->is_block_theme() ),
394 'pro_license_status' => apply_filters( 'gutenify_pro_license_status', false ),
395 )
396 );
397 }
398
399 // if ( ! empty( $_GET['post_type'] ) && 'gutenify_kit' === $_GET['post_type'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
400 if ( ! empty( $_GET['post_type'] ) && 'gutenify_template' === $_GET['post_type'] && ( ! empty( $_GET['page'] ) && 'gutenify-template-kits' === $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
401 // Scripts.
402 $name = 'gutenify-kit-admin'; // gutenify.js.
403 $filepath = 'dist/' . $name;
404 $asset_file = $this->get_asset_file( $filepath );
405
406 // Enqueue for gutenify animations.
407 wp_enqueue_script(
408 'gutenify-kit-admin',
409 GUTENIFY_PLUGIN_URL . $filepath . '.js',
410 $asset_file['dependencies'],
411 $asset_file['version'],
412 true
413 );
414
415 // Styles.
416 $name = 'gutenify-kit-admin-style';
417 $filepath = 'dist/' . $name;
418 $asset_file = $this->get_asset_file( $filepath );
419 $rtl = ! is_rtl() ? '' : '-rtl';
420
421 wp_enqueue_style(
422 $name,
423 GUTENIFY_PLUGIN_URL . $filepath . $rtl . '.css',
424 array( 'gutenify-fontawesome-style', 'wp-components' ),
425 $asset_file['version']
426 );
427
428 wp_localize_script(
429 'gutenify-kit-admin',
430 '_gutenify_kit',
431 array(
432 'plugin_directory_url' => esc_url( GUTENIFY_PLUGIN_URL ),
433 'site_url' => esc_url( site_url() ),
434 'gutenify_com_server_url' => defined( 'GUTENIFY_COM_SERVER_URL' ) ? trailingslashit( GUTENIFY_COM_SERVER_URL ) : trailingslashit( 'https://api.gutenify.com/' ),
435 'is_block_theme' => function_exists( 'wp_get_theme' ) && ! empty( wp_get_theme()->is_block_theme() ),
436 'pro_license_status' => apply_filters( 'gutenify_pro_license_status', false ),
437 )
438 );
439 }
440
441 if ( empty( $_GET['post_type'] ) || empty( 'page' ) || 'gutenify_template' !== $_GET['post_type'] || ( ! empty( $_GET['page'] ) && 'gutenify-settings' !== $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
442 return false;
443 }
444 // Scripts.
445 $name = 'gutenify-admin-settings'; // gutenify.js.
446 $filepath = 'dist/' . $name;
447 $asset_file = $this->get_asset_file( $filepath );
448
449 $asset_file['dependencies'][] = 'gutenify-global';
450
451 // Enqueue for gutenify animations.
452 wp_enqueue_script(
453 'gutenify-admin-settings',
454 GUTENIFY_PLUGIN_URL . $filepath . '.js',
455 $asset_file['dependencies'],
456 $asset_file['version'],
457 true
458 );
459
460 wp_localize_script(
461 'gutenify-admin-settings',
462 '_gutenify',
463 array(
464 'site_url' => esc_url( site_url() ),
465 'pro_account_url' => esc_url( admin_url('edit.php?post_type=gutenify_template&page=gutenify-pro-license') ),
466 'gutenify_com_server_url' => defined( 'GUTENIFY_COM_SERVER_URL' ) ? trailingslashit( GUTENIFY_COM_SERVER_URL ) : trailingslashit( 'https://api.gutenify.com/' ),
467 'pro_license_status' => apply_filters( 'gutenify_pro_license_status', false ),
468 'font_families' =>gutenify_font_families(),
469 )
470 );
471
472 // Styles.
473 $name = 'gutenify-admin-settings-style';
474 $filepath = 'dist/' . $name;
475 $asset_file = $this->get_asset_file( $filepath );
476 $rtl = ! is_rtl() ? '' : '-rtl';
477
478 wp_enqueue_style(
479 $name,
480 GUTENIFY_PLUGIN_URL . $filepath . $rtl . '.css',
481 array( 'gutenify-fontawesome-style', 'wp-components' ),
482 $asset_file['version']
483 );
484 }
485 }
486
487 Gutenify_Block_Assets::register();
488