PluginProbe
Getwid – Gutenberg Blocks / 1.8.7
Getwid – Gutenberg Blocks v1.8.7
3.0.2 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 1.8.7, at includes/blocks/section.php

233 lines 5.6 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_script(
44 'draggabilly',
45 getwid_get_plugin_url( 'vendors/draggabilly/draggabilly.pkgd.min.js' ),
46 [ 'jquery' ],
47 '2.2.0',
48 true
49 );
50
51 wp_register_style(
52 'animate',
53 getwid_get_plugin_url( 'vendors/animate.css/animate.min.css' ),
54 [],
55 '3.7.0'
56 );
57
58 wp_register_style(
59 'slick',
60 getwid_get_plugin_url( 'vendors/slick/slick/slick.min.css' ),
61 [],
62 '1.9.0'
63 );
64
65 wp_register_style(
66 'slick-theme',
67 getwid_get_plugin_url( 'vendors/slick/slick/slick-theme.min.css' ),
68 [],
69 '1.9.0'
70 );
71 }
72 }
73
74 public function getLabel() {
75 return __('Section', 'getwid');
76 }
77
78 public function block_frontend_styles($styles) {
79
80 //fontawesome
81 $styles = getwid()->fontIconsManager()->enqueueFonts( $styles );
82
83 //animate.min.css
84 if ( is_admin() && ! in_array( 'animate', $styles ) ) {
85 array_push( $styles, 'animate' );
86 }
87
88 //slick.min.css
89 if ( ! in_array( 'slick', $styles ) ) {
90 array_push( $styles, 'slick' );
91 }
92
93 //slick-theme.min.css
94 if ( ! in_array( 'slick-theme', $styles ) ) {
95 array_push( $styles, 'slick-theme' );
96 }
97
98 return $styles;
99 }
100
101 public function block_editor_scripts($scripts) {
102
103 //wow.min.js
104 if ( ! in_array( 'wow', $scripts ) ) {
105 array_push( $scripts, 'wow' );
106 }
107
108 //wow.min.js
109 if ( ! in_array( 'slick', $scripts ) ) {
110 array_push( $scripts, 'slick' );
111 }
112
113 //draggabilly.pkgd.min.js
114 if ( ! in_array( 'draggabilly', $scripts ) ) {
115 array_push( $scripts, 'draggabilly' );
116 }
117
118 return $scripts;
119 }
120
121 public function block_frontend_assets( $attributes = [], $content = '' ) {
122
123 if ( is_admin() ) {
124 return;
125 }
126
127 if ( ! empty( $attributes['entranceAnimation'] ) ) {
128 //wow.min.js
129 if ( ! wp_script_is( 'wow', 'enqueued' ) ){
130 wp_enqueue_script('wow');
131 }
132
133 //animate.min.css
134 if ( ! wp_style_is( 'animate', 'enqueued' ) ) {
135 wp_enqueue_style( 'animate' );
136 }
137 }
138
139 $has_background_slider = false !== strpos( $content, 'wp-block-getwid-section__background-slider-item' );
140 //slick.min.js
141 if ( $has_background_slider && ! wp_script_is( 'slick', 'enqueued' ) ) {
142 wp_enqueue_script('slick');
143 }
144
145 //imagesloaded.min.js
146 if ( $has_background_slider && ! wp_script_is( 'imagesloaded', 'enqueued' ) ) {
147 wp_enqueue_script('imagesloaded');
148 }
149
150 /* optimization */
151 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
152 return;
153 }
154
155 $deps_css = [
156 'slick', 'slick-theme'
157 ];
158
159 $deps_js = [ 'jquery', 'imagesloaded' ];
160
161 if ( $has_background_slider && ! wp_script_is( 'slick', 'enqueued' ) ) {
162 $deps_js[] = 'slick';
163 }
164
165 if ( ! empty( $attributes['entranceAnimation'] ) ) {
166 $deps_css[] = 'animate';
167 $deps_js[] = 'wow';
168 }
169
170 //fontawesome
171 $deps_css = getwid()->fontIconsManager()->enqueueFonts( $deps_css );
172
173 add_filter( 'getwid/optimize/assets',
174 function ( $assets ) {
175 $assets[] = 'slick';
176 $assets[] = 'slick-theme';
177 $assets[] = getwid()->settings()->getPrefix() . '-blocks-common';
178
179 return $assets;
180 }
181 );
182
183 add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
184
185 $rtl = is_rtl() ? '.rtl' : '';
186
187 wp_enqueue_style(
188 self::$blockName,
189 getwid_get_plugin_url( 'assets/blocks/section/style' . $rtl . '.css' ),
190 $deps_css,
191 getwid()->settings()->getVersion()
192 );
193
194 // ensure that inline styles are enqueued only once
195 if ( !$this->assetsAlreadyEnqueued ) {
196 wp_add_inline_style( self::$blockName, getwid_generate_section_content_width_css() . getwid_generate_smooth_animation_css() );
197 }
198
199 wp_enqueue_script(
200 self::$blockName,
201 getwid_get_plugin_url( 'assets/blocks/section/frontend.js' ),
202 $deps_js,
203 getwid()->settings()->getVersion(),
204 true
205 );
206
207 if ( !$this->assetsAlreadyEnqueued ) {
208 $inline_script =
209 'var Getwid = Getwid || {};' .
210 'Getwid["isRTL"] = ' . json_encode( is_rtl() ) . ';';
211
212 wp_add_inline_script(
213 self::$blockName,
214 $inline_script,
215 'before'
216 );
217 }
218
219 $this->assetsAlreadyEnqueued = true;
220 }
221
222 public function render_callback( $attributes, $content ) {
223
224 $this->block_frontend_assets( $attributes, $content );
225
226 return $content;
227 }
228 }
229
230 getwid()->blocksManager()->addBlock(
231 new \Getwid\Blocks\Section()
232 );
233