PluginProbe
Light Modal Block / trunk
Light Modal Block vtrunk
1.9.2 trunk 1.0.0 1.0.1 1.1.0 1.1.1 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.8.0 1.9.0 1.9.1
light-modal-block / light-modal-block.php

light-modal-block.php in Light Modal Block trunk, at light-modal-block.php

104 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Light Modal Block
4 * Description: Lightweight, customizable modal block for the WordPress block editor
5 * Requires at least: 6.6
6 * Requires PHP: 7.0
7 * Version: 1.9.2
8 * Author: CloudCatch LLC
9 * Author URI: https://cloudcatch.io
10 * License: GPL-2.0-or-later
11 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
12 * Text Domain: light-modal-block
13 *
14 * @package CloudCatch\LightModalBlock
15 */
16
17 /**
18 * Register the block.
19 *
20 * @return void
21 */
22 function cloudcatch_light_modal_block_block_init() {
23 register_block_type( __DIR__ . '/build' );
24 }
25 add_action( 'init', 'cloudcatch_light_modal_block_block_init' );
26
27 /**
28 * Modifies the post template block, filtering the modal ID to include the current query ID and post ID.
29 *
30 * @param string $block_content The block content.
31 * @param array $block The block.
32 * @param WP_Block $instance The block instance.
33 * @return string The rendered block.
34 */
35 function cloudcatch_light_modal_block_post_template( $block_content, $block, $instance ) {
36 $query_id = (int) ( $instance->context['queryId'] ?? 1 );
37
38 $tags = new WP_HTML_Tag_Processor( $block_content );
39
40 $current_post_id = null;
41
42 while ( $tags->next_tag() ) {
43
44 if ( $tags->has_class( 'wp-block-post' ) ) {
45 $matches = null;
46
47 $post_class = $tags->get_attribute( 'class' );
48
49 preg_match( '/post-(\d+)/', $post_class, $matches );
50
51 if ( isset( $matches[1] ) ) {
52 $current_post_id = (int) $matches[1];
53 }
54
55 if ( ! $current_post_id ) {
56 continue;
57 }
58 }
59
60 if ( $current_post_id ) {
61 if ( $tags->get_attribute( 'data-trigger-modal' ) ) {
62 $modal_id = $tags->get_attribute( 'data-trigger-modal' );
63
64 $tags->set_attribute( 'data-trigger-modal', $modal_id . '-q' . $query_id . '-p' . $current_post_id );
65 }
66
67 if ( $tags->get_attribute( 'data-modal-id' ) ) {
68 $modal_id = $tags->get_attribute( 'data-modal-id' );
69
70 $tags->set_attribute( 'data-modal-id', $modal_id . '-q' . $query_id . '-p' . $current_post_id );
71 }
72 }
73 }
74
75 $block_content = $tags->get_updated_html();
76
77 return $block_content;
78 }
79 add_filter( 'render_block_core/post-template', 'cloudcatch_light_modal_block_post_template', 10, 3 );
80
81 /**
82 * Accessibility improvements for buttons that trigger modals by changing
83 * the element from an anchor tag to a button tag.
84 *
85 * @param string $block_content The block content.
86 * @return string
87 */
88 function cloudcatch_light_modal_block_accessible_buttons( $block_content ) {
89 $tags = new WP_HTML_Tag_Processor( $block_content );
90
91 $has_modal = (bool) ( $tags->next_tag() && $tags->get_attribute( 'data-trigger-modal' ) );
92
93 if ( ! $has_modal ) {
94 return $block_content;
95 }
96
97 // Replace <a> with <button> if it has a modal trigger.
98 $block_content = str_replace( '<a ', '<button ', $block_content );
99 $block_content = str_replace( '</a>', '</button>', $block_content );
100
101 return $block_content;
102 }
103 add_filter( 'render_block_core/button', 'cloudcatch_light_modal_block_accessible_buttons' );
104