-
Notifications
You must be signed in to change notification settings - Fork 2
/
animated-timeline.php
106 lines (91 loc) · 2.89 KB
/
animated-timeline.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Plugin Name: Animated Timeline
* Description: Extends the Group block for an animating timeline effect.
* Requires at least: 6.5
* Requires PHP: 7.4
* Version: 1.0.0
* Author: Damon Cook
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: animated-timeline
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Registers the custom script and style for the timeline plugin.
*/
function animated_timeline_register_scripts() {
// Register the custom script to be used later.
wp_register_script(
'animated-timeline-script',
plugin_dir_url( __FILE__ ) . '/assets/scripts/core-blocks/group--animated-timeline.js',
array(),
'1.0.0',
true
);
// Register the custom style to be used later.
wp_register_style(
'animated-timeline-style',
plugin_dir_url( __FILE__ ) . '/assets/styles/core-blocks/group--animated-timeline.css',
array(),
'1.0.0',
);
}
add_action( 'wp_enqueue_scripts', 'animated_timeline_register_scripts' );
/**
* Modify the core Group block.
*
* @param string $block_content The block content about to be rendered.
*
* @return string The maybe modified block content.
*/
function animated_timeline_filter_group_content( $block_content ) {
$processor = new WP_HTML_Tag_Processor( $block_content );
$counter = 0;
// Check for the presence of the 'timeline' class.
if ( ! $processor->next_tag( array( 'class_name' => 'animated-timeline' ) ) ) {
return $block_content;
}
// Loop through each child block with the class name 'wp-block-column'.
while ( $processor->next_tag( array( 'class_name' => 'wp-block-column' ) ) ) {
$processor->add_class( 'animated__item' );
++$counter;
switch ( $counter ) {
case 1:
$processor->add_class( 'animated__item--first' );
break;
case 2:
$processor->add_class( 'animated__item--line' );
break;
case 3:
$processor->add_class( 'animated__item--last' );
$counter = 0;
break;
}
}
$block_content = $processor->get_updated_html();
// Enqueue the custom script and style.
wp_enqueue_script( 'animated-timeline-script' );
wp_enqueue_style( 'animated-timeline-style' );
// Return the maybe modified block content.
return $block_content;
}
add_filter( 'render_block_core/group', 'animated_timeline_filter_group_content', 10 );
/**
* Registers a block pattern for the timeline plugin.
*
* This function registers a block pattern for the timeline plugin. It checks if the pattern file exists and then registers the pattern using the `register_block_pattern` function.
*/
function animated_timeline_register_block_pattern() {
$pattern_file = plugin_dir_path( __FILE__ ) . '/patterns/animated-timeline.php';
if ( ! file_exists( $pattern_file ) ) {
return;
}
register_block_pattern(
'animated-timeline/animated-timeline',
require $pattern_file
);
}
add_action( 'init', 'animated_timeline_register_block_pattern' );