Skip to content

Commit

Permalink
1.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
websevendev committed Jul 8, 2022
1 parent 695c8a7 commit f991357
Show file tree
Hide file tree
Showing 10 changed files with 15,425 additions and 19,488 deletions.
22 changes: 18 additions & 4 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.4
* Version: 1.0.5
* Author: websevendev
* Author URI: https://chap.website/author/websevendev
*/
Expand All @@ -13,9 +13,8 @@
defined('ABSPATH') || exit;

function_exists('get_plugin_data') || require_once ABSPATH . 'wp-admin/includes/plugin.php';
$plugin = get_plugin_data(__FILE__, false, false);

define('WSD_ANFB_VER', $plugin['Version']);
$anfb_plugin = get_plugin_data(__FILE__, false, false);
define('WSD_ANFB_VER', $anfb_plugin['Version']);
define('WSD_ANFB_FILE', __FILE__);
define('WSD_ANFB_DIR', dirname(__FILE__));
define('WSD_ANFB_AOS_HANDLE', 'animate-on-scroll');
Expand All @@ -31,3 +30,18 @@ function_exists('get_plugin_data') || require_once ABSPATH . 'wp-admin/includes/
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.
*
* @param array $plugin_meta
* @param string $plugin_file
* @return array
*/
function github_link($plugin_meta, $plugin_file) {
if($plugin_file === plugin_basename(WSD_ANFB_FILE)) {
$plugin_meta[] = '<a href="https://github.com/websevendev/animations-for-blocks" target="_blank" rel="noopener noreferrer">GitHub</a>';
}
return $plugin_meta;
}
add_filter('plugin_row_meta', __NAMESPACE__ . '\\github_link', 10, 2);
3 changes: 1 addition & 2 deletions bin/build-plugin-zip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ zip -r animations-for-blocks.zip \
animations-for-blocks.php \
readme.txt \
build/* \
includes/* \
src/*
includes/*

unzip animations-for-blocks.zip -d animations-for-blocks
rm animations-for-blocks.zip
Expand Down
48 changes: 17 additions & 31 deletions includes/anfb-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,42 @@ function get_animation_attributes($args = []) {
return $attributes;
}

/**
* Animation.
*/
/** Animation. */
$attributes['data-aos'] = $args['animation'] === $args['variation']
? $args['animation']
: $args['animation'] . '-' . $args['variation'];

/**
* Delay.
*/
/** Delay. */
if(is_numeric($args['delay']) && (int)$args['delay'] !== 0) {
$attributes['data-aos-delay'] = (int)$args['delay'];
}

/**
* Duration.
*/
/** Duration. */
if(is_numeric($args['duration']) && (int)$args['duration'] !== 400) {
$attributes['data-aos-duration'] = (int)$args['duration'];
}

/**
* Once.
*/
/** Once. */
if($args['once'] === 'true') {
$attributes['data-aos-once'] = 'true';
}

/**
* Mirror.
*/
/** Mirror. */
if($args['mirror'] === 'true') {
$attributes['data-aos-mirror'] = 'true';
}

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

/**
* Offset.
*/
/** Offset. */
if(is_numeric($args['offset']) && (int)$args['offset'] !== 120) {
$attributes['data-aos-offset'] = (int)$args['offset'];
}

/**
* Anchor placement.
*/
/** Anchor placement. */
if(!empty($args['anchorPlacement']) && $args['anchorPlacement'] !== 'top-bottom') {
$attributes['data-aos-anchor-placement'] = $args['anchorPlacement'];
}
Expand All @@ -97,34 +81,36 @@ function get_animation_attributes($args = []) {
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.
*/

/** 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);
$node->parentNode->removeChild($node); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
}
$body->appendChild($container);
}
/**
* Add attributes.
*/

/** 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) {
$root->setAttribute($key, esc_attr($value));
}
}
}

return get_body($dom->saveHTML());
}
8 changes: 2 additions & 6 deletions includes/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,11 @@ function editor_assets() {
*/
function front_end_assets() {
if(apply_filters('anfb_load_styles', true)) {
/**
* Load Animate on Scroll styles.
*/
/** Load Animate on Scroll styles. */
wp_enqueue_style(WSD_ANFB_AOS_HANDLE);
}
if(apply_filters('anfb_load_scripts', true)) {
/**
* Initialize Animate on Scroll library.
*/
/** Initialize Animate on Scroll library. */
$asset = include WSD_ANFB_DIR . '/build/init.asset.php';
wp_enqueue_script(
'animations-for-blocks',
Expand Down
8 changes: 2 additions & 6 deletions includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ function block_args($args, $name) {
$args['attributes'] = [];
}

/**
* Register ANFB attribute.
*/
/** Register ANFB attribute. */
$args['attributes']['animationsForBlocks'] = [
'type' => 'object',
'default' => [],
];

/**
* Override `render_callback` to add animation attributes.
*/
/** 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) {
Expand Down
Loading

0 comments on commit f991357

Please sign in to comment.