Skip to content

Commit

Permalink
1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
websevendev committed Jul 16, 2023
1 parent 9e9dceb commit 1d3303d
Show file tree
Hide file tree
Showing 13 changed files with 4,937 additions and 3,722 deletions.
125 changes: 101 additions & 24 deletions animations-for-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Animations for Blocks
* Plugin URI: https://wordpress.org/plugins/animations-for-blocks
* Description: Allows to add animations to Gutenberg blocks on scroll.
* Version: 1.1.1
* Version: 1.1.2
* Author: websevendev
* Author URI: https://chap.website/author/websevendev
*/
Expand All @@ -21,6 +21,55 @@ function_exists('get_plugin_data') || require_once ABSPATH . 'wp-admin/includes/
define('WSD_ANFB_DIR', dirname(__FILE__));
define('WSD_ANFB_AOS_HANDLE', 'animate-on-scroll');

/**
* Register global plugin options.
*/
function register_settings() {

$default_settings = [
'animateInEditor' => true,
'lazyloadAssets' => false,
];

$sanitize_callback = function($settings) use ($default_settings) {

if(!is_array($settings)) {
return $default_settings;
}

return [
'animateInEditor' => (bool)($settings['animateInEditor'] ?? $default_settings['animateInEditor']),
'lazyloadAssets' => (bool)($settings['lazyloadAssets'] ?? $default_settings['lazyloadAssets']),
];
};

register_setting(
'animations-for-blocks',
'animations-for-blocks',
[
'description' => __('Animations for Blocks settings', 'animations-for-blocks'),
'show_in_rest' => [
'schema' => [
'type' => 'object',
'properties' => [
'animateInEditor' => [
'type' => 'boolean',
'default' => $default_settings['animateInEditor'],
],
'lazyloadAssets' => [
'type' => 'boolean',
'default' => $default_settings['lazyloadAssets'],
],
],
'sanitize_callback' => $sanitize_callback,
],
],
'default' => $default_settings,
]
);
}
add_action('init', __NAMESPACE__ . '\\register_settings');


/**
* Blocks known to not work properly with Animations for Blocks.
Expand All @@ -38,11 +87,11 @@ function get_unsupported_blocks() {


/**
* Register Animate on Scroll library.
* Register plugin assets.
*
* @see https://github.com/michalsnik/aos
*/
function register_aos() {
function register_assets() {

$asset = include WSD_ANFB_DIR . '/build/index.asset.php';

Expand All @@ -61,8 +110,23 @@ function register_aos() {
$asset['version'], // 3.0.0-beta.6
true
);

$asset = include WSD_ANFB_DIR . '/build/init.asset.php';
wp_register_script(
'animations-for-blocks',
plugins_url('build/init.js', WSD_ANFB_FILE),
array_merge(
array_diff(
$asset['dependencies'],
apply_filters('anfb_init_exclude_deps', ['wp-polyfill']) // Shouldn't need polyfill.
),
[apply_filters('anfb_aos_handle', WSD_ANFB_AOS_HANDLE)]
),
$asset['version'],
true
);
}
add_action('init', __NAMESPACE__ . '\\register_aos');
add_action('init', __NAMESPACE__ . '\\register_assets');


/**
Expand All @@ -73,25 +137,28 @@ function editor_assets() {
$asset = include WSD_ANFB_DIR . '/build/index.asset.php';

wp_enqueue_style(
'animations-for-blocks',
'animations-for-blocks-admin',
plugins_url('build/style-index.css', WSD_ANFB_FILE),
[],
$asset['version'],
'all'
);

wp_enqueue_script(
'animations-for-blocks',
'animations-for-blocks-admin',
plugins_url('build/index.js', WSD_ANFB_FILE),
$asset['dependencies'],
$asset['version'],
false
);

wp_localize_script(
'animations-for-blocks',
'animations-for-blocks-admin',
'anfbData',
['unsupportedBlocks' => get_unsupported_blocks()]
[
'unsupportedBlocks' => get_unsupported_blocks(),
'settings' => get_option('animations-for-blocks'),
]
);

if(function_exists('wp_set_script_translations')) {
Expand All @@ -102,9 +169,9 @@ function editor_assets() {


/**
* Enqueue front end assets.
* Enqueues front end assets.
*/
function front_end_assets() {
function enqueue_front_end_assets() {

if(apply_filters('anfb_load_styles', true)) {
/** Load Animate on Scroll styles. */
Expand All @@ -113,21 +180,21 @@ function front_end_assets() {

if(apply_filters('anfb_load_scripts', true)) {
/** Initialize Animate on Scroll library. */
$asset = include WSD_ANFB_DIR . '/build/init.asset.php';
wp_enqueue_script(
'animations-for-blocks',
plugins_url('build/init.js', WSD_ANFB_FILE),
array_merge(
array_diff(
$asset['dependencies'],
apply_filters('anfb_init_exclude_deps', ['wp-polyfill']) // Shouldn't need polyfill.
),
[apply_filters('anfb_aos_handle', WSD_ANFB_AOS_HANDLE)]
),
$asset['version'],
true
);
wp_enqueue_script('animations-for-blocks');
}
}

/**
* Enqueue front end assets in head.
*/
function front_end_assets() {

$options = get_option('animations-for-blocks');
if($options['lazyloadAssets']) {
return;
}

enqueue_front_end_assets();
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\front_end_assets', 500);

Expand Down Expand Up @@ -308,6 +375,16 @@ function animate_block($block_content, $block) {
return $block_content;
}

/** Lazyload assets. */
static $lazyloaded = false;
if(!$lazyloaded) {
$options = get_option('animations-for-blocks');
if($options['lazyloadAssets']) {
enqueue_front_end_assets();
}
$lazyloaded = true;
}

return add_animation_attributes($block_content, $block['attrs']['animationsForBlocks']);
}

Expand Down
Loading

0 comments on commit 1d3303d

Please sign in to comment.