PluginProbe
Getwid – Gutenberg Blocks / 2.1.3
Getwid – Gutenberg Blocks v2.1.3
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / section.php

section.php in Getwid – Gutenberg Blocks 2.1.3, at includes/blocks/section.php

215 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 class Section extends \Getwid\Blocks\AbstractBlock {
6
7 protected static $blockName = 'getwid/section';
8 private $assetsAlreadyEnqueued = false;
9
10 public function __construct() {
11
12 parent::__construct( self::$blockName );
13
14 register_block_type(
15 'getwid/section',
16 array(
17 'render_callback' => [ $this, 'render_callback' ]
18 )
19 );
20
21 if ( $this->isEnabled() ) {
22
23 add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts'] );
24 add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] );
25
26 //Register JS/CSS assets
27 wp_register_script(
28 'wow',
29 getwid_get_plugin_url( 'vendors/wow.js/dist/wow.min.js' ),
30 [ 'jquery' ],
31 '1.2.1',
32 true
33 );
34
35 wp_register_script(
36 'slick',
37 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.js' ),
38 [ 'jquery' ],
39 '1.9.0',
40 true
41 );
42
43 wp_register_style(
44 'animate',
45 getwid_get_plugin_url( 'vendors/animate.css/animate.min.css' ),
46 [],
47 '3.7.0'
48 );
49
50 wp_register_style(
51 'slick',
52 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.css' ),
53 [],
54 '1.9.0'
55 );
56
57 wp_register_style(
58 'slick-theme',
59 getwid_get_plugin_url( 'vendors/slick/slick/slick-theme.min.css' ),
60 [],
61 '1.9.0'
62 );
63 }
64 }
65
66 public function getLabel() {
67 return __('Section', 'getwid');
68 }
69
70 public function block_frontend_styles($styles) {
71
72 //fontawesome
73 $styles = getwid()->fontIconsManager()->enqueueFonts( $styles );
74
75 //animate.min.css
76 if ( is_admin() && ! in_array( 'animate', $styles ) ) {
77 array_push( $styles, 'animate' );
78 }
79
80 //slick.min.css
81 if ( ! in_array( 'slick', $styles ) ) {
82 array_push( $styles, 'slick' );
83 }
84
85 //slick-theme.min.css
86 if ( ! in_array( 'slick-theme', $styles ) ) {
87 array_push( $styles, 'slick-theme' );
88 }
89
90 return $styles;
91 }
92
93 public function block_editor_scripts($scripts) {
94
95 //slick.min.js
96 if ( ! in_array( 'slick', $scripts ) ) {
97 array_push( $scripts, 'slick' );
98 }
99
100 return $scripts;
101 }
102
103 public function block_frontend_assets( $attributes = [], $content = '' ) {
104
105 if ( is_admin() ) {
106 return;
107 }
108
109 if ( ! empty( $attributes['entranceAnimation'] ) ) {
110 //wow.min.js
111 if ( ! wp_script_is( 'wow', 'enqueued' ) ){
112 wp_enqueue_script('wow');
113 }
114
115 //animate.min.css
116 if ( ! wp_style_is( 'animate', 'enqueued' ) ) {
117 wp_enqueue_style( 'animate' );
118 }
119 }
120
121 $has_background_slider = false !== strpos( $content, 'wp-block-getwid-section__background-slider-item' );
122 //slick.min.js
123 if ( $has_background_slider && ! wp_script_is( 'slick', 'enqueued' ) ) {
124 wp_enqueue_script('slick');
125 }
126
127 //imagesloaded.min.js
128 if ( $has_background_slider && ! wp_script_is( 'imagesloaded', 'enqueued' ) ) {
129 wp_enqueue_script('imagesloaded');
130 }
131
132 /* optimization */
133 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
134 return;
135 }
136
137 $deps_css = [
138 'slick', 'slick-theme'
139 ];
140
141 $deps_js = [ 'jquery', 'imagesloaded' ];
142
143 if ( $has_background_slider && ! wp_script_is( 'slick', 'enqueued' ) ) {
144 $deps_js[] = 'slick';
145 }
146
147 if ( ! empty( $attributes['entranceAnimation'] ) ) {
148 $deps_css[] = 'animate';
149 $deps_js[] = 'wow';
150 }
151
152 //fontawesome
153 $deps_css = getwid()->fontIconsManager()->enqueueFonts( $deps_css );
154
155 add_filter( 'getwid/optimize/assets',
156 function ( $assets ) {
157 $assets[] = 'slick';
158 $assets[] = 'slick-theme';
159 $assets[] = getwid()->settings()->getPrefix() . '-blocks-common';
160
161 return $assets;
162 }
163 );
164
165 add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
166
167 $rtl = is_rtl() ? '.rtl' : '';
168
169 wp_enqueue_style(
170 self::$blockName,
171 getwid_get_plugin_url( 'assets/blocks/section/style' . $rtl . '.css' ),
172 $deps_css,
173 getwid()->settings()->getVersion()
174 );
175
176 // ensure that inline styles are enqueued only once
177 if ( !$this->assetsAlreadyEnqueued ) {
178 wp_add_inline_style( self::$blockName, getwid_generate_section_content_width_css() . getwid_generate_smooth_animation_css() );
179 }
180
181 wp_enqueue_script(
182 self::$blockName,
183 getwid_get_plugin_url( 'assets/blocks/section/frontend.js' ),
184 $deps_js,
185 getwid()->settings()->getVersion(),
186 true
187 );
188
189 if ( !$this->assetsAlreadyEnqueued ) {
190 $inline_script =
191 'var Getwid = Getwid || {};' .
192 'Getwid["isRTL"] = ' . json_encode( is_rtl() ) . ';';
193
194 wp_add_inline_script(
195 self::$blockName,
196 $inline_script,
197 'before'
198 );
199 }
200
201 $this->assetsAlreadyEnqueued = true;
202 }
203
204 public function render_callback( $attributes, $content ) {
205
206 $this->block_frontend_assets( $attributes, $content );
207
208 return $content;
209 }
210 }
211
212 getwid()->blocksManager()->addBlock(
213 new \Getwid\Blocks\Section()
214 );
215