Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
websevendev committed Mar 13, 2023
1 parent b046845 commit b6cdd7e
Show file tree
Hide file tree
Showing 27 changed files with 4,578 additions and 4,230 deletions.
9 changes: 1 addition & 8 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.0.6
* Version: 1.1.0
* Author: websevendev
* Author URI: https://chap.website/author/websevendev
*/
Expand All @@ -24,13 +24,6 @@ function_exists('get_plugin_data') || require_once ABSPATH . 'wp-admin/includes/
require_once WSD_ANFB_DIR . '/includes/anfb-attributes.php';
require_once WSD_ANFB_DIR . '/includes/blocks.php';

add_action('init', __NAMESPACE__ . '\\register_aos');
add_action('enqueue_block_editor_assets', __NAMESPACE__ . '\\editor_assets', 5);
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\front_end_assets', 500);
add_filter('anfb_load_styles', __NAMESPACE__ . '\\disable_on_amp');
add_filter('anfb_load_scripts', __NAMESPACE__ . '\\disable_on_amp');
add_filter('register_block_type_args', __NAMESPACE__ . '\\block_args', 10, 2);

/**
* Add GitHub link on the plugins page.
*
Expand Down
19 changes: 9 additions & 10 deletions includes/anfb-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @return array Attributes to add to root element.
*/
function get_animation_attributes($args = []) {

$args = wp_parse_args($args, [
'animation' => 'none',
'variation' => '',
Expand Down Expand Up @@ -42,6 +43,11 @@ function get_animation_attributes($args = []) {
$attributes['data-aos-duration'] = (int)$args['duration'];
}

/** Easing. */
if(!empty($args['easing']) && $args['easing'] !== 'ease') {
$attributes['data-aos-easing'] = $args['easing'];
}

/** Once. */
if($args['once'] === 'true' || $args['once'] === true) {
$attributes['data-aos-once'] = 'true';
Expand All @@ -52,11 +58,6 @@ function get_animation_attributes($args = []) {
$attributes['data-aos-mirror'] = 'true';
}

/** Easing. */
if(!empty($args['easing']) && $args['easing'] !== 'ease') {
$attributes['data-aos-easing'] = $args['easing'];
}

/** Offset. */
if(is_numeric($args['offset']) && (int)$args['offset'] !== 120) {
$attributes['data-aos-offset'] = (int)$args['offset'];
Expand All @@ -67,7 +68,7 @@ function get_animation_attributes($args = []) {
$attributes['data-aos-anchor-placement'] = $args['anchorPlacement'];
}

return $attributes;
return apply_filters('anfb_aos_attributes', $attributes, $args);
}

/**
Expand All @@ -79,31 +80,29 @@ function get_animation_attributes($args = []) {
* @return string Block HTML with animation attributes.
*/
function add_animation_attributes($args, $html, $name = '') {

$dom = get_dom($html);
$body = $dom->getElementsByTagName('body')->item(0);

/** Wrap elements when there are more than 1. */
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
if($body->childNodes->length > 1) {
$container = $dom->createElement('div');
$container->setAttribute('class', 'anfb-animation-container');
$container->setAttribute('data-block', esc_attr($name));
$remove = [];
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach($body->childNodes as $node) {
if(is_dom_element($node)) {
$container->appendChild($node->cloneNode(true));
$remove[] = $node;
}
}
foreach($remove as $node) {
$node->parentNode->removeChild($node); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$node->parentNode->removeChild($node);
}
$body->appendChild($container);
}

/** Add attributes. */
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach($body->childNodes as $root) {
if(method_exists($root, 'setAttribute')) {
foreach(get_animation_attributes($args) as $key => $value) {
Expand Down
22 changes: 16 additions & 6 deletions includes/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @see https://github.com/michalsnik/aos
*/
function register_aos() {

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

wp_register_style(
Expand All @@ -28,34 +29,35 @@ function register_aos() {
true
);
}
add_action('init', __NAMESPACE__ . '\\register_aos');

/**
* Enqueue editor assets.
*/
function editor_assets() {

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

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

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

wp_localize_script(
'animations-for-blocks',
'anfbData',
[
'unsupportedBlocks' => include WSD_ANFB_DIR . '/includes/unsupported-blocks.php',
]
['unsupportedBlocks' => include WSD_ANFB_DIR . '/includes/unsupported-blocks.php']
);

if(function_exists('wp_set_script_translations')) {
Expand All @@ -65,15 +67,18 @@ function editor_assets() {
);
}
}
add_action('enqueue_block_editor_assets', __NAMESPACE__ . '\\editor_assets', 5);

/**
* Front end assets.
*/
function front_end_assets() {

if(apply_filters('anfb_load_styles', true)) {
/** Load Animate on Scroll styles. */
wp_enqueue_style(WSD_ANFB_AOS_HANDLE);
}

if(apply_filters('anfb_load_scripts', true)) {
/** Initialize Animate on Scroll library. */
$asset = include WSD_ANFB_DIR . '/build/init.asset.php';
Expand All @@ -92,6 +97,7 @@ function front_end_assets() {
);
}
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\front_end_assets', 500);

/**
* AMP behavior.
Expand All @@ -100,8 +106,12 @@ function front_end_assets() {
* @return bool
*/
function disable_on_amp($load) {

if(function_exists('is_amp_endpoint') && is_amp_endpoint()) {
return false;
}

return $load;
}
add_filter('anfb_load_styles', __NAMESPACE__ . '\\disable_on_amp');
add_filter('anfb_load_scripts', __NAMESPACE__ . '\\disable_on_amp');
73 changes: 45 additions & 28 deletions includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
defined('ABSPATH') || exit;

/**
* When registering a block, add ANFB argument and wrap `render_callback`.
*
* @param array $args
* @param string $name
* @return array
*/
function block_args($args, $name) {

static $not_supported;
if(!is_array($not_supported)) {
$not_supported = include WSD_ANFB_DIR . '/includes/unsupported-blocks.php';
}

if(in_array($name, $not_supported)) {
return $args;
}
Expand All @@ -24,36 +24,53 @@ function block_args($args, $name) {
$args['attributes'] = [];
}

/** Register ANFB attribute. */
/** Register ANFB attribute, this is necessary for `/wp-json/wp/v2/block-renderer` REST endpoint to not throw `rest_additional_properties_forbidden`. */
$args['attributes']['animationsForBlocks'] = [
'type' => 'object',
'default' => [],
];

/** Override `render_callback` to add animation attributes. */
if(isset($args['render_callback']) && is_callable($args['render_callback'])) {
$cb = $args['render_callback'];
$args['render_callback'] = function($attributes, $content, $block = null) use ($cb, $name) {
$rendered = call_user_func($cb, $attributes, $content, $block);
if(
!isset($attributes['animationsForBlocks'])
|| !is_array($attributes['animationsForBlocks'])
) {
return $rendered;
}
if(
isset($attributes['animationsForBlocks']['animation'])
&& $attributes['animationsForBlocks']['animation'] !== 'none'
) {
return add_animation_attributes(
$attributes['animationsForBlocks'],
$rendered,
$name
);
}
return $rendered;
};
return $args;
}
add_filter('register_block_type_args', __NAMESPACE__ . '\\block_args', 10, 2);

/**
* Add animation attributes to blocks' root HTML element when applicable.
*
* @param string $block_content Rendered block.
* @param string $block Parsed array representation of block.
* @return string
*/
function animate_block($block_content, $block) {

static $not_supported;
if(!is_array($not_supported)) {
$not_supported = include WSD_ANFB_DIR . '/includes/unsupported-blocks.php';
}

return $args;
if(
!in_array($block['blockName'], $not_supported, true)
&& isset($block['attrs']['animationsForBlocks']['animation'])
&& !empty($block['attrs']['animationsForBlocks']['animation'])
&& $block['attrs']['animationsForBlocks']['animation'] !== 'none'
) {

if(
defined('REST_REQUEST')
&& REST_REQUEST
&& isset($_REQUEST['context'])
&& $_REQUEST['context'] === 'edit'
) {
/** Don't animate server-side rendered blocks. */
return $block_content;
}

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

return $block_content;
}
add_filter('render_block', __NAMESPACE__ . '\\animate_block', 10, 2);
Loading

0 comments on commit b6cdd7e

Please sign in to comment.