forked from dmtrmrv/intro-to-block-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro-to-block-filters.php
79 lines (66 loc) · 1.9 KB
/
intro-to-block-filters.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
<?php
/**
* Plugin Name: Intro To Block Filters
* Description: Demo plugin for CSS-tricks article about WordPress block filters
* Author: Dmitry Mayorov
* Author URI: https://dmtrmrv.com
* Version: 0.1.0
*
* @package IntroToBlockFilters
*/
namespace IntroToBlockFilters;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
};
function frontend_assets() {
wp_enqueue_style(
'intro-to-block-filters-frontend-style',
plugin_dir_url( __FILE__ ) . 'assets/frontend.css',
[],
'0.1.0'
);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\frontend_assets' );
function editor_assets() {
wp_enqueue_script(
'intro-to-block-filters-editor-script',
plugin_dir_url( __FILE__ ) . 'build/index.js',
[ 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ],
'0.1.0'
);
wp_enqueue_style(
'intro-to-block-filters-editor-style',
plugin_dir_url( __FILE__ ) . 'assets/editor.css',
[],
'0.1.0'
);
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\editor_assets' );
/**
* Add button size class.
*
* A PHP approach of filtering block markup.
*
* @param string $block_content Block content to be rendered.
* @param array $block Block attributes.
* @return string
*/
function add_button_size_class( $block_content = '', $block = [] ) {
if ( isset( $block['blockName'] ) && 'core/button' === $block['blockName'] ) {
$defaults = [
'size' => 'regular'
];
$args = wp_parse_args( $block['attrs'], $defaults );
$html = str_replace(
'<div class="wp-block-button',
'<div class="wp-block-button has-size-' . esc_attr( $args['size']) . ' ',
$block_content
);
return $html;
}
return $block_content;
}
// Uncomment the following line to test this approach. Also don't forget to
// comment out the blocks.getSaveContent.extraProps filter in button-size.js
// add_filter( 'render_block', __NAMESPACE__ . '\add_button_size_class', 10, 2 );