PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / trunk
Block Animations, Motion & Scroll Effects – Ghost Kit vtrunk
3.7.2 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 trunk, at class-ghost-kit.php

511 lines 14.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Ghost Kit
4 * Description: Animate WordPress blocks with reveal, scroll, mouse and loop effects.
5 * Version: 3.7.2
6 * Plugin URI: https://www.ghostkit.io/?utm_source=wordpress.org&utm_medium=readme&utm_campaign=byline
7 * Author: Ghost Kit Team
8 * Author URI: https://www.ghostkit.io/?utm_source=wordpress.org&utm_medium=readme&utm_campaign=byline
9 * License: GPLv2 or later
10 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
11 * Text Domain: ghostkit
12 *
13 * @package ghostkit
14 */
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /*
21 * Ghost Kit Pro carries its own copy of this core, so only one of the two may run.
22 * Which copy PHP reaches first depends on the order WordPress includes plugins in,
23 * and that order is not ours to pick: network-activated plugins come before
24 * site-activated ones, and a third party can reorder `active_plugins`. So the
25 * standalone plugin steps aside on its own whenever the Pro plugin is active,
26 * before it defines anything at all.
27 *
28 * Only the activated plugin steps aside. The copy inside the Pro plugin, and any
29 * copy embedded in a theme or another plugin, is not in the list below and keeps
30 * loading as before.
31 */
32 $gkt_active_plugins = (array) get_option( 'active_plugins', array() );
33
34 if ( is_multisite() ) {
35 $gkt_active_plugins = array_merge(
36 $gkt_active_plugins,
37 array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) )
38 );
39 }
40
41 if (
42 in_array( plugin_basename( __FILE__ ), $gkt_active_plugins, true ) &&
43 in_array( 'ghostkit-pro/class-ghost-kit-pro.php', $gkt_active_plugins, true ) &&
44 // `active_plugins` goes on naming a plugin whose directory was removed by hand
45 // and WordPress simply skips it, so stepping aside for one of those would leave
46 // the site with neither plugin.
47 file_exists( WP_PLUGIN_DIR . '/ghostkit-pro/class-ghost-kit-pro.php' )
48 ) {
49 unset( $gkt_active_plugins );
50 return;
51 }
52
53 unset( $gkt_active_plugins );
54
55 if ( ! defined( 'GHOSTKIT_VERSION' ) ) {
56 define( 'GHOSTKIT_VERSION', '3.7.2' );
57 }
58
59 if ( ! class_exists( 'GhostKit' ) ) :
60 /**
61 * GhostKit Class
62 */
63 class GhostKit {
64
65 /**
66 * The single class instance.
67 *
68 * @var $instance
69 */
70 private static $instance = null;
71
72 /**
73 * Path to the plugin directory
74 *
75 * @var $plugin_path
76 */
77 public $plugin_path;
78
79 /**
80 * URL to the plugin directory
81 *
82 * @var $plugin_url
83 */
84 public $plugin_url;
85
86 /**
87 * Plugin name
88 *
89 * @var $plugin_name
90 */
91 public $plugin_name;
92
93 /**
94 * Plugin version
95 *
96 * @var $plugin_version
97 */
98 public $plugin_version;
99
100 /**
101 * Plugin slug
102 *
103 * @var $plugin_slug
104 */
105 public $plugin_slug;
106
107 /**
108 * Plugin name sanitized
109 *
110 * @var $plugin_name_sanitized
111 */
112 public $plugin_name_sanitized;
113
114 /**
115 * GhostKit constructor.
116 */
117 public function __construct() {
118 /* We do nothing here! */
119 }
120
121 /**
122 * Main Instance
123 * Ensures only one instance of this class exists in memory at any one time.
124 */
125 public static function instance() {
126 if ( is_null( self::$instance ) ) {
127 self::$instance = new self();
128 self::$instance->init_options();
129 self::$instance->init_hooks();
130 }
131 return self::$instance;
132 }
133
134 /**
135 * Init options
136 */
137 public function init_options() {
138 $this->plugin_path = plugin_dir_path( __FILE__ );
139 $this->plugin_url = plugin_dir_url( __FILE__ );
140
141 require_once $this->plugin_path . 'settings/index.php';
142 require_once $this->plugin_path . 'gutenberg/index.php';
143
144 require_once $this->plugin_path . 'classes/class-rest.php';
145 require_once $this->plugin_path . 'classes/class-parse-blocks.php';
146 require_once $this->plugin_path . 'classes/class-assets-detector.php';
147 require_once $this->plugin_path . 'classes/class-assets.php';
148 require_once $this->plugin_path . 'classes/class-reusable-widget.php';
149 require_once $this->plugin_path . 'classes/class-icons.php';
150 require_once $this->plugin_path . 'classes/class-shapes.php';
151 require_once $this->plugin_path . 'classes/class-typography.php';
152 require_once $this->plugin_path . 'classes/class-fonts.php';
153 require_once $this->plugin_path . 'classes/class-templates.php';
154 require_once $this->plugin_path . 'classes/class-breakpoints.php';
155 require_once $this->plugin_path . 'classes/class-ask-review.php';
156 require_once $this->plugin_path . 'classes/class-deactivate-duplicate-plugin.php';
157
158 require_once $this->plugin_path . 'gutenberg/utils/encode-decode/index.php';
159 require_once $this->plugin_path . 'gutenberg/extend/index.php';
160
161 // 3rd.
162 require_once $this->plugin_path . 'classes/3rd/class-astra.php';
163 require_once $this->plugin_path . 'classes/3rd/class-page-builder-framework.php';
164 require_once $this->plugin_path . 'classes/3rd/class-blocksy.php';
165 require_once $this->plugin_path . 'classes/3rd/class-rank-math.php';
166
167 // Migration.
168 require_once $this->plugin_path . 'classes/class-migration.php';
169 }
170
171 /**
172 * Init hooks
173 */
174 public function init_hooks() {
175 add_action( 'admin_init', array( $this, 'admin_init' ) );
176
177 add_action( 'init', array( $this, 'init_hook' ) );
178
179 add_action( 'init', array( $this, 'add_custom_fields_support' ), 100 );
180
181 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_go_pro_link_plugins_page' ) );
182
183 add_action( 'wp_enqueue_scripts', array( $this, 'js_translation' ), 11 );
184 add_action( 'enqueue_block_editor_assets', array( $this, 'js_translation_editor' ) );
185
186 // add Ghost Kit blocks category.
187 add_filter( 'block_categories_all', array( $this, 'block_categories_all' ), 9999 );
188
189 // we need to enqueue the main script earlier to let 3rd-party plugins add custom styles support.
190 add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_assets' ), 9 );
191
192 // add support for excerpts to some blocks.
193 add_filter( 'excerpt_allowed_blocks', array( $this, 'excerpt_allowed_blocks' ) );
194
195 // add support for additional mimes.
196 add_filter( 'upload_mimes', array( $this, 'upload_mimes' ), 100 );
197 add_filter( 'wp_check_filetype_and_ext', array( $this, 'wp_check_filetype_and_ext' ), 100, 3 );
198 }
199
200 /**
201 * JS translation.
202 */
203 public function js_translation() {
204 if ( ! function_exists( 'wp_set_script_translations' ) ) {
205 return;
206 }
207
208 wp_set_script_translations( 'ghostkit-block-countdown', 'ghostkit', plugin_dir_path( __FILE__ ) . '/languages' );
209 }
210
211 /**
212 * JS translation for editor.
213 */
214 public function js_translation_editor() {
215 if ( ! function_exists( 'wp_set_script_translations' ) ) {
216 return;
217 }
218
219 wp_set_script_translations( 'ghostkit-editor', 'ghostkit', plugin_dir_path( __FILE__ ) . '/languages' );
220 wp_set_script_translations( 'ghostkit-settings', 'ghostkit', plugin_dir_path( __FILE__ ) . '/languages' );
221 }
222
223 /**
224 * Register Ghost Kit blocks category
225 *
226 * @param array $categories - available categories.
227 * @return array
228 */
229 public function block_categories_all( $categories ) {
230 return array_merge(
231 array(
232 array(
233 'slug' => 'ghostkit',
234 'title' => __( 'Ghost Kit', 'ghostkit' ),
235 ),
236 ),
237 $categories
238 );
239 }
240
241 /**
242 * Enqueue editor assets
243 */
244 public function enqueue_block_assets() {
245 if ( ! is_admin() ) {
246 return;
247 }
248
249 global $current_screen;
250
251 $css_deps = array();
252 $js_deps = array( 'ghostkit-helper' );
253
254 // Ivent.
255 if ( apply_filters( 'gkt_enqueue_plugin_ivent', true ) ) {
256 $js_deps[] = 'ivent';
257 }
258
259 // Motion.
260 if ( apply_filters( 'gkt_enqueue_plugin_motion', true ) ) {
261 $js_deps[] = 'motion';
262 }
263
264 // Jarallax.
265 if ( apply_filters( 'gkt_enqueue_plugin_jarallax', true ) ) {
266 $js_deps[] = 'jarallax';
267 $js_deps[] = 'jarallax-video';
268 }
269
270 // Luxon.
271 if ( apply_filters( 'gkt_enqueue_plugin_luxon', true ) ) {
272 $js_deps[] = 'luxon';
273 }
274
275 // Lottie Player.
276 if ( apply_filters( 'gkt_enqueue_plugin_lottie_player', true ) ) {
277 $js_deps[] = 'lottie-player';
278 }
279
280 // GistEmbed.
281 if ( apply_filters( 'gkt_enqueue_plugin_gist_simple', true ) ) {
282 $css_deps[] = 'gist-simple';
283 $js_deps[] = 'gist-simple';
284 }
285
286 // In block metadata, many Ghost Kit blocks declare `style: ["ghostkit", ...]`.
287 // Because of this, WordPress auto-enqueues the `ghostkit` style handle for those blocks
288 // in every context where blocks are rendered, including the editor.
289 //
290 // `ghostkit` is our frontend stylesheet (`build/gutenberg/style.css`), while editor UI
291 // is styled by `ghostkit-editor` (`build/gutenberg/editor.css`). Loading both in editor
292 // causes conflicts (for example, frontend Display extension rules can hide editor blocks).
293 //
294 // To keep block dependency chains intact without editing all block.json files, we override
295 // only the `ghostkit` handle in admin and make it an alias that depends on `ghostkit-editor`.
296 // Result:
297 // - frontend keeps using real `ghostkit` styles;
298 // - editor resolves `ghostkit` dependencies to editor styles, avoiding CSS conflicts.
299 if ( wp_style_is( 'ghostkit', 'registered' ) ) {
300 wp_deregister_style( 'ghostkit' );
301 }
302 wp_register_style( 'ghostkit', false, array( 'ghostkit-editor' ), GHOSTKIT_VERSION );
303
304 // Ghost Kit.
305 GhostKit_Assets::enqueue_style(
306 'ghostkit-editor',
307 'build/gutenberg/editor',
308 $css_deps,
309 filemtime( plugin_dir_path( __FILE__ ) . 'build/gutenberg/editor.css' )
310 );
311 wp_style_add_data( 'ghostkit-editor', 'rtl', 'replace' );
312
313 // Since WordPress 7.1 the post editor canvas is always iframed, and core collects
314 // the iframe assets by running `enqueue_block_assets` a second time with
315 // `should_load_block_editor_scripts_and_styles` forced to false.
316 // The editor bundles belong to the editor chrome only - loading them in the canvas
317 // would boot a second copy of the block editor inside the iframe. The vendor
318 // libraries, on the other hand, are needed inside the canvas to render block previews
319 // (for example the `lottie-player` custom element, which is registered per window).
320 if ( ! wp_should_load_block_editor_scripts_and_styles() ) {
321 foreach ( $js_deps as $dep ) {
322 wp_enqueue_script( $dep );
323 }
324
325 return;
326 }
327
328 GhostKit_Assets::enqueue_script(
329 'ghostkit-editor',
330 'build/gutenberg/index',
331 $js_deps
332 );
333
334 // Load plugins in editor only (skip legacy Widgets screen).
335 if ( ! isset( $current_screen->id ) || 'widgets' !== $current_screen->id ) {
336 GhostKit_Assets::enqueue_script(
337 'ghostkit-editor-plugins',
338 'build/gutenberg/plugins',
339 array( 'ghostkit-editor' )
340 );
341 }
342 }
343
344 /**
345 * Excerpt allowed wrapper blocks.
346 *
347 * @param array $blocks - allowed blocks.
348 * @return array
349 */
350 public function excerpt_allowed_blocks( $blocks ) {
351 return array_merge(
352 $blocks,
353 array(
354 'ghostkit/accordion',
355 'ghostkit/accordion-item',
356 'ghostkit/alert',
357 'ghostkit/button',
358 'ghostkit/carousel',
359 'ghostkit/carousel-single',
360 'ghostkit/changelog',
361 'ghostkit/counter-box',
362 'ghostkit/grid',
363 'ghostkit/grid-column',
364 'ghostkit/icon-box',
365 'ghostkit/pricing-table',
366 'ghostkit/pricing-table-item',
367 'ghostkit/tabs-v2',
368 'ghostkit/tabs-tab-v2',
369 'ghostkit/testimonial',
370 )
371 );
372 }
373
374 /**
375 * Allow JSON uploads
376 *
377 * @param array $mimes supported mimes.
378 *
379 * @return array
380 */
381 public function upload_mimes( $mimes ) {
382 if ( ! isset( $mimes['json'] ) ) {
383 $mimes['json'] = 'application/json';
384 }
385
386 return $mimes;
387 }
388
389 /**
390 * Allow JSON file uploads
391 *
392 * @param array $data File data.
393 * @param array $file File object.
394 * @param string $filename File name.
395 *
396 * @return array
397 */
398 public function wp_check_filetype_and_ext( $data, $file, $filename ) {
399 $ext = isset( $data['ext'] ) ? $data['ext'] : '';
400
401 if ( ! $ext ) {
402 $exploded = explode( '.', $filename );
403 $ext = strtolower( end( $exploded ) );
404 }
405
406 if ( 'json' === $ext ) {
407 $data['type'] = 'application/json';
408 $data['ext'] = 'json';
409 }
410
411 return $data;
412 }
413
414 /**
415 * Init variables
416 */
417 public function admin_init() {
418 // get current plugin data.
419 $data = get_plugin_data( __FILE__ );
420 $this->plugin_name = $data['Name'];
421 $this->plugin_version = $data['Version'];
422 $this->plugin_slug = plugin_basename( __FILE__ );
423 $this->plugin_name_sanitized = basename( __FILE__, '.php' );
424 }
425
426 /**
427 * Init hook
428 */
429 public function init_hook() {
430 load_plugin_textdomain( 'ghostkit', false, plugin_dir_path( __FILE__ ) . '/languages' );
431 }
432
433 /**
434 * Add support for 'custom-fields' for all post types.
435 * Required by Customizer and Custom CSS blocks.
436 */
437 public function add_custom_fields_support() {
438 $available_post_types = get_post_types(
439 array(
440 'show_ui' => true,
441 ),
442 'object'
443 );
444
445 foreach ( $available_post_types as $post_type ) {
446 if ( 'attachment' !== $post_type->name ) {
447 add_post_type_support( $post_type->name, 'custom-fields' );
448 }
449 }
450 }
451
452 /**
453 * Equivalent of GHOSTKIT.replaceVars method.
454 *
455 * @param string $str styles string.
456 * @return string string with replaced vars.
457 */
458 public function replace_vars( $str ) {
459 $breakpoints = GhostKit_Breakpoints::get_breakpoints();
460 // TODO: Due to different formats in scss and assets there is an offset.
461 $vars = array(
462 'media_sm' => '(max-width: ' . $breakpoints['xs'] . 'px)',
463 'media_md' => '(max-width: ' . $breakpoints['sm'] . 'px)',
464 'media_lg' => '(max-width: ' . $breakpoints['md'] . 'px)',
465 'media_xl' => '(max-width: ' . $breakpoints['lg'] . 'px)',
466 );
467
468 foreach ( $vars as $k => $var ) {
469 $str = preg_replace( "/#{ghostkitvar:$k}/", $var, $str );
470 }
471
472 return $str;
473 }
474
475 /**
476 * Add Go Pro link to plugins page.
477 *
478 * @param array $links - available links.
479 *
480 * @return array
481 */
482 public function add_go_pro_link_plugins_page( $links ) {
483 return array_merge(
484 $links,
485 array(
486 '<a target="_blank" href="admin.php?page=ghostkit_go_pro&utm_medium=plugins_list">' . esc_html__( 'Go Pro', 'ghostkit' ) . '</a>',
487 )
488 );
489 }
490
491 /**
492 * Get Go Pro link
493 *
494 * @return string
495 */
496 public function go_pro_link() {
497 return 'https://www.ghostkit.io/pricing/';
498 }
499 }
500
501 /**
502 * Function works with the GhostKit class instance
503 *
504 * @return object GhostKit
505 */
506 function ghostkit() {
507 return GhostKit::instance();
508 }
509 add_action( 'plugins_loaded', 'ghostkit' );
510 endif;
511