PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 1.6.3
Block Animations, Motion & Scroll Effects – Ghost Kit v1.6.3
3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / class-ghost-kit.php

class-ghost-kit.php in Block Animations, Motion & Scroll Effects – Ghost Kit 1.6.3, at class-ghost-kit.php

492 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: GhostKit
4 * Description: Blocks collection and extensions for Gutenberg
5 * Version: 1.6.3
6 * Author: nK
7 * Author URI: https://nkdev.info
8 * License: GPLv2 or later
9 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
10 * Text Domain: ghostkit
11 *
12 * @package ghostkit
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * GhostKit Class
21 */
22 class GhostKit {
23
24 /**
25 * The single class instance.
26 *
27 * @var $_instance
28 */
29 private static $_instance = null;
30
31 /**
32 * Path to the plugin directory
33 *
34 * @var $plugin_path
35 */
36 public $plugin_path;
37
38 /**
39 * URL to the plugin directory
40 *
41 * @var $plugin_url
42 */
43 public $plugin_url;
44
45 /**
46 * Plugin name
47 *
48 * @var $plugin_name
49 */
50 public $plugin_name;
51
52 /**
53 * Plugin version
54 *
55 * @var $plugin_version
56 */
57 public $plugin_version;
58
59 /**
60 * Plugin slug
61 *
62 * @var $plugin_slug
63 */
64 public $plugin_slug;
65
66 /**
67 * Plugin name sanitized
68 *
69 * @var $plugin_name_sanitized
70 */
71 public $plugin_name_sanitized;
72
73 /**
74 * GhostKit constructor.
75 */
76 public function __construct() {
77 /* We do nothing here! */
78 }
79
80 /**
81 * Main Instance
82 * Ensures only one instance of this class exists in memory at any one time.
83 */
84 public static function instance() {
85 if ( is_null( self::$_instance ) ) {
86 self::$_instance = new self();
87 self::$_instance->init_options();
88 self::$_instance->init_hooks();
89 }
90 return self::$_instance;
91 }
92
93 /**
94 * Init options
95 */
96 public function init_options() {
97 $this->plugin_path = plugin_dir_path( __FILE__ );
98 $this->plugin_url = plugin_dir_url( __FILE__ );
99
100 // load textdomain.
101 load_plugin_textdomain( 'ghostkit', false, basename( dirname( __FILE__ ) ) . '/languages' );
102
103 // additional blocks php.
104 require_once( $this->plugin_path . 'blocks/index.php' );
105
106 // rest.
107 require_once( $this->plugin_path . 'classes/class-rest.php' );
108
109 // reusable widget.
110 require_once( $this->plugin_path . 'classes/class-reusable-widget.php' );
111 }
112
113
114 /**
115 * Init hooks
116 */
117 public function init_hooks() {
118 add_action( 'admin_init', array( $this, 'admin_init' ) );
119
120 add_action( 'init', array( $this, 'add_custom_fields_support' ), 100 );
121
122 add_action( 'save_post', array( $this, 'parse_styles_from_blocks' ) );
123 add_action( 'wp_enqueue_scripts', array( $this, 'add_styles_from_blocks' ), 100 );
124
125 // include blocks.
126 // work only if Gutenberg available.
127 if ( function_exists( 'register_block_type' ) ) {
128 add_action( 'init', array( $this, 'register_scripts' ) );
129
130 // add GhostKit blocks category.
131 add_filter( 'block_categories', array( $this, 'block_categories' ), 9 );
132
133 // we need to enqueue the main script earlier to let 3rd-party plugins add custom styles support.
134 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 9 );
135 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_block_assets' ) );
136 }
137 }
138
139 /**
140 * Register GhostKit blocks category
141 *
142 * @param array $categories - available categories.
143 * @return array
144 */
145 public function block_categories( $categories ) {
146 return array_merge(
147 $categories,
148 array(
149 array(
150 'slug' => 'ghostkit',
151 'title' => __( 'GhostKit', 'ghostkit' ),
152 ),
153 )
154 );
155 }
156
157 /**
158 * Register scripts.
159 */
160 public function register_scripts() {
161 // FontAwesome.
162 if ( apply_filters( 'gkt_enqueue_plugin_font_awesome', true ) ) {
163 wp_register_script( 'font-awesome-v4-shims', plugins_url( 'assets/vendor/font-awesome/v4-shims.min.js', __FILE__ ), array(), '5.2.0' );
164 wp_register_script( 'font-awesome', plugins_url( 'assets/vendor/font-awesome/all.min.js', __FILE__ ), array( 'font-awesome-v4-shims' ), '5.2.0' );
165 }
166
167 // VideoWorker.
168 if ( apply_filters( 'gkt_enqueue_plugin_video_worker', true ) ) {
169 wp_register_script( 'video-worker', plugins_url( 'assets/vendor/video-worker/dist/video-worker.js', __FILE__ ), array(), '1.1.2' );
170 }
171
172 // Object Fit Images.
173 if ( apply_filters( 'gkt_enqueue_plugin_object_fit_images', true ) ) {
174 wp_register_script( 'object-fit-images', plugins_url( 'assets/vendor/object-fit-images/ofi.min.js', __FILE__ ), array(), '3.2.3', true );
175 }
176
177 // Swiper.
178 if ( apply_filters( 'gkt_enqueue_plugin_swiper', true ) ) {
179 wp_register_style( 'swiper', plugins_url( 'assets/vendor/swiper/css/swiper.css', __FILE__ ), array(), '4.3.3' );
180 wp_register_script( 'swiper', plugins_url( 'assets/vendor/swiper/js/swiper.min.js', __FILE__ ), array(), '4.3.3', true );
181 }
182
183 // GistEmbed.
184 if ( apply_filters( 'gkt_enqueue_plugin_gist_embed', true ) ) {
185 wp_register_script( 'gist-embed', plugins_url( 'assets/vendor/gist-embed/gist-embed.min.js', __FILE__ ), array( 'jquery' ), '2.7.1', true );
186 }
187
188 // ScrollReveal.
189 if ( apply_filters( 'gkt_enqueue_plugin_scrollreveal', true ) ) {
190 wp_register_script( 'scrollreveal', plugins_url( 'assets/vendor/scrollreveal/scrollreveal.min.js', __FILE__ ), array(), '4.0.2', true );
191 }
192
193 // Get all sidebars.
194 $sidebars = false;
195 if ( ! empty( $GLOBALS['wp_registered_sidebars'] ) ) {
196 foreach ( $GLOBALS['wp_registered_sidebars'] as $k => $sidebar ) {
197 $sidebars[ $k ] = array(
198 'id' => $sidebar['id'],
199 'name' => $sidebar['name'],
200 );
201 }
202 }
203
204 // helper script.
205 wp_register_script(
206 'ghostkit-helper',
207 plugins_url( 'assets/js/helper.min.js', __FILE__ ),
208 array( 'jquery' ),
209 filemtime( plugin_dir_path( __FILE__ ) . 'assets/js/helper.min.js' )
210 );
211 $default_variant = array(
212 'default' => array(
213 'title' => esc_html__( 'Default', 'ghostkit' ),
214 ),
215 );
216
217 // Google Maps prepare localization as in WordPress settings.
218 $gmaps_locale = get_locale();
219 $gmaps_suffix = '.com';
220 switch ( $gmaps_locale ) {
221 case 'he_IL':
222 // Hebrew correction.
223 $gmaps_locale = 'iw';
224 break;
225 case 'zh_CN':
226 // Chinese integration.
227 $gmaps_suffix = '.cn';
228 break;
229 }
230 $gmaps_locale = substr( $gmaps_locale, 0, 2 );
231
232 wp_localize_script( 'ghostkit-helper', 'ghostkitVariables', array(
233 // TODO: Move this to plugin options (part 1).
234 'media_sizes' => array(
235 'sm' => 576,
236 'md' => 768,
237 'lg' => 992,
238 'xl' => 1200,
239 ),
240 'googleMapsAPIKey' => get_option( 'ghostkit_google_maps_api_key' ),
241 'googleMapsAPIUrl' => 'https://maps.googleapis' . $gmaps_suffix . '/maps/api/js?v=3.exp&language=' . esc_attr( $gmaps_locale ),
242 'googleMapsLibrary' => apply_filters( 'gkt_enqueue_plugin_gmaps', true ) ? array(
243 'url' => plugins_url( 'assets/vendor/gmaps/gmaps.min.js', __FILE__ ) . '?ver=0.4.25',
244 ) : false,
245 'sidebars' => $sidebars,
246 'variants' => array(
247 'accordion' => array_merge( $default_variant, apply_filters( 'gkt_accordion_variants', array() ) ),
248 'accordion_item' => array_merge( $default_variant, apply_filters( 'gkt_accordion_item_variants', array() ) ),
249 'alert' => array_merge( $default_variant, apply_filters( 'gkt_alert_variants', array() ) ),
250 'button_wrapper' => array_merge( $default_variant, apply_filters( 'gkt_button_wrapper_variants', array() ) ),
251 'button' => array_merge( $default_variant, apply_filters( 'gkt_button_variants', array() ) ),
252 'carousel' => array_merge( $default_variant, apply_filters( 'gkt_carousel_variants', array() ) ),
253 'carousel_slide' => array_merge( $default_variant, apply_filters( 'gkt_carousel_slide_variants', array() ) ),
254 'changelog' => array_merge( $default_variant, apply_filters( 'gkt_changelog_variants', array() ) ),
255 'counter_box' => array_merge( $default_variant, apply_filters( 'gkt_counter_box_variants', array() ) ),
256 'divider' => array_merge( $default_variant, apply_filters( 'gkt_divider_variants', array() ) ),
257 'gist' => array_merge( $default_variant, apply_filters( 'gkt_gist_variants', array() ) ),
258 'google_maps' => array_merge( $default_variant, apply_filters( 'gkt_google_maps_variants', array() ) ),
259 'grid' => array_merge( $default_variant, apply_filters( 'gkt_grid_variants', array() ) ),
260 'grid_column' => array_merge( $default_variant, apply_filters( 'gkt_grid_column_variants', array() ) ),
261 'icon_box' => array_merge( $default_variant, apply_filters( 'gkt_icon_box_variants', array() ) ),
262 'instagram' => array_merge( $default_variant, apply_filters( 'gkt_instagram_variants', array() ) ),
263 'pricing_table' => array_merge( $default_variant, apply_filters( 'gkt_pricing_table_variants', array() ) ),
264 'pricing_table_item' => array_merge( $default_variant, apply_filters( 'gkt_pricing_table_item_variants', array() ) ),
265 'progress' => array_merge( $default_variant, apply_filters( 'gkt_progress_variants', array() ) ),
266 'tabs' => array_merge( $default_variant, apply_filters( 'gkt_tabs_variants', array() ) ),
267 'tabs_tab' => array_merge( $default_variant, apply_filters( 'gkt_tabs_tab_variants', array() ) ),
268 'testimonial' => array_merge( $default_variant, apply_filters( 'gkt_testimonial_variants', array() ) ),
269 'twitter' => array_merge( $default_variant, apply_filters( 'gkt_twitter_variants', array() ) ),
270 'video' => array_merge( $default_variant, apply_filters( 'gkt_video_variants', array() ) ),
271 ),
272 'admin_url' => admin_url(),
273 ) );
274 }
275
276 /**
277 * Enqueue editor assets
278 */
279 public function enqueue_block_editor_assets() {
280 $css_deps = array();
281 $js_deps = array( 'ghostkit-helper', 'wp-editor', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-edit-post', 'wp-compose', 'underscore', 'wp-components' );
282
283 // FontAwesome.
284 if ( apply_filters( 'gkt_enqueue_plugin_font_awesome', true ) ) {
285 $js_deps[] = 'font-awesome';
286 }
287
288 // VideoWorker.
289 if ( apply_filters( 'gkt_enqueue_plugin_video_worker', true ) ) {
290 $js_deps[] = 'video-worker';
291 }
292
293 // GistEmbed.
294 if ( apply_filters( 'gkt_enqueue_plugin_gist_embed', true ) ) {
295 $js_deps[] = 'gist-embed';
296 }
297
298 // GhostKit.
299 wp_enqueue_style(
300 'ghostkit-editor',
301 plugins_url( 'assets/admin/css/style.min.css', __FILE__ ),
302 $css_deps,
303 filemtime( plugin_dir_path( __FILE__ ) . 'assets/admin/css/style.min.css' )
304 );
305 wp_enqueue_script(
306 'ghostkit-editor',
307 plugins_url( 'blocks/index.min.js', __FILE__ ),
308 $js_deps,
309 filemtime( plugin_dir_path( __FILE__ ) . 'blocks/index.min.js' )
310 );
311 }
312
313 /**
314 * Enqueue editor frontend assets
315 */
316 public function enqueue_block_assets() {
317 $css_deps = array();
318 $js_deps = array( 'jquery', 'ghostkit-helper' );
319
320 // FontAwesome.
321 if ( apply_filters( 'gkt_enqueue_plugin_font_awesome', true ) ) {
322 $js_deps[] = 'font-awesome';
323 }
324
325 // VideoWorker.
326 if ( apply_filters( 'gkt_enqueue_plugin_video_worker', true ) ) {
327 $js_deps[] = 'video-worker';
328 }
329
330 // Object Fit Images.
331 if ( apply_filters( 'gkt_enqueue_plugin_object_fit_images', true ) ) {
332 $js_deps[] = 'object-fit-images';
333 }
334
335 // Swiper.
336 if ( apply_filters( 'gkt_enqueue_plugin_swiper', true ) ) {
337 $css_deps[] = 'swiper';
338 $js_deps[] = 'swiper';
339 }
340
341 // GistEmbed.
342 if ( apply_filters( 'gkt_enqueue_plugin_gist_embed', true ) ) {
343 $js_deps[] = 'gist-embed';
344 }
345
346 // ScrollReveal.
347 if ( apply_filters( 'gkt_enqueue_plugin_scrollreveal', true ) ) {
348 $js_deps[] = 'scrollreveal';
349 }
350
351 // GhostKit.
352 wp_enqueue_style(
353 'ghostkit',
354 plugins_url( 'blocks/style.min.css', __FILE__ ),
355 $css_deps,
356 filemtime( plugin_dir_path( __FILE__ ) . 'blocks/style.min.css' )
357 );
358 wp_enqueue_script(
359 'ghostkit',
360 plugins_url( 'assets/js/script.min.js', __FILE__ ),
361 $js_deps,
362 filemtime( plugin_dir_path( __FILE__ ) . 'assets/js/script.min.js' )
363 );
364 }
365
366 /**
367 * Init variables
368 */
369 public function admin_init() {
370 // get current plugin data.
371 $data = get_plugin_data( __FILE__ );
372 $this->plugin_name = $data['Name'];
373 $this->plugin_version = $data['Version'];
374 $this->plugin_slug = plugin_basename( __FILE__, '.php' );
375 $this->plugin_name_sanitized = basename( __FILE__, '.php' );
376 }
377
378 /**
379 * Add support for 'custom-fields' for all post types.
380 * Required by Customizer and Custom CSS blocks.
381 */
382 public function add_custom_fields_support() {
383 $available_post_types = get_post_types( array(
384 'show_ui' => true,
385 ), 'object' );
386
387 foreach ( $available_post_types as $post_type ) {
388 if ( 'attachment' !== $post_type->name ) {
389 add_post_type_support( $post_type->name, 'custom-fields' );
390 }
391 }
392 }
393
394 /**
395 * Parse styles from blocks and save it to the post meta.
396 *
397 * @param int $post_id - current post id.
398 */
399 public function parse_styles_from_blocks( $post_id ) {
400 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
401 return;
402 }
403 if ( ! current_user_can( 'edit_post', $post_id ) ) {
404 return;
405 }
406
407 $post = get_post( $post_id );
408
409 if ( ! $post->post_content ) {
410 return;
411 }
412
413 if ( class_exists( 'DOMDocument' ) ) {
414 $css = '';
415 $dom = new DOMDocument();
416 @$dom->loadHTML( $post->post_content );
417 foreach ( $dom->getElementsByTagName( '*' ) as $node ) {
418 $styles = $node->getAttribute( 'data-ghostkit-styles' );
419 if ( $styles ) {
420 $css .= ' ' . $styles;
421 }
422 }
423
424 if ( empty( $css ) || ! $css ) {
425 delete_post_meta( $post_id, '_ghostkit_blocks_custom_css' );
426 } else {
427 // TODO: Move this to plugin options (part 2).
428 $vars = array(
429 'media_sm' => '(max-width: 576px)',
430 'media_md' => '(max-width: 768px)',
431 'media_lg' => '(max-width: 992px)',
432 'media_xl' => '(max-width: 1200px)',
433 );
434
435 foreach ( $vars as $k => $var ) {
436 $css = preg_replace( "/#{ghostkitvar:$k}/", $var, $css );
437 }
438
439 update_post_meta( $post_id, '_ghostkit_blocks_custom_css', $css );
440 }
441 }
442 }
443
444 /**
445 * Add styles from blocks to the head section.
446 *
447 * @param int $post_id - current post id.
448 */
449 public function add_styles_from_blocks( $post_id ) {
450 if ( ! is_singular() ) {
451 return;
452 }
453 if ( ! $post_id ) {
454 $post_id = get_the_ID();
455 }
456
457 if ( $post_id ) {
458 $css = get_post_meta( $post_id, '_ghostkit_blocks_custom_css', true );
459 $custom_css = get_post_meta( $post_id, 'ghostkit_custom_css', true );
460
461 if ( ! empty( $css ) ) {
462 $custom_css_handle = 'ghostkit-blocks-custom-css';
463 $css = wp_kses( $css, array( '\'', '\"' ) );
464 $css = str_replace( '&gt;', '>', $css );
465
466 wp_register_style( $custom_css_handle, false );
467 wp_enqueue_style( $custom_css_handle );
468 wp_add_inline_style( $custom_css_handle, $css );
469 }
470 if ( ! empty( $custom_css ) ) {
471 $custom_css_handle = 'ghostkit-custom-css';
472 $css = wp_kses( $custom_css, array( '\'', '\"' ) );
473 $css = str_replace( '&gt;', '>', $css );
474
475 wp_register_style( $custom_css_handle, false );
476 wp_enqueue_style( $custom_css_handle );
477 wp_add_inline_style( $custom_css_handle, $css );
478 }
479 }
480 }
481 }
482
483 /**
484 * Function works with the GhostKit class instance
485 *
486 * @return object GhostKit
487 */
488 function ghostkit() {
489 return GhostKit::instance();
490 }
491 add_action( 'plugins_loaded', 'ghostkit' );
492