-
Notifications
You must be signed in to change notification settings - Fork 0
/
synced-text.php
78 lines (69 loc) · 1.94 KB
/
synced-text.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
<?php
/**
* Plugin Name: Synced Text
* Description: A block editor sidebar plugin text snippets that can be inserted inline into content and synchronised across the site.
* Version: 1.0.0
* Author: Human Made Limited
* Author URI: https://humanmade.com
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: synced-text
* Domain Path: /languages
*/
namespace SyncedText;
add_action( 'enqueue_block_editor_assets', function() : void {
$asset = require __DIR__ . '/build/index.asset.php';
wp_enqueue_script(
'synced-text',
plugins_url( 'build/index.js', __FILE__ ),
$asset['dependencies'],
$asset['version']
);
} );
/**
* Fires when preparing to serve a REST API request.
*/
add_action( 'rest_api_init', function () : void {
register_setting( 'writing', 'syncedText', [
'type' => 'object',
'label' => __( 'Synced Text', 'synced-text' ),
'description' => __( 'A set of inline text values that can be inserted into posts and kept in sync across the site', 'synced-text' ),
'sanitize_callback' => function ( $value ) {
if ( ! is_array( $value ) ) {
$value = [];
}
$value = array_map( 'sanitize_text_field', $value );
return $value;
},
'show_in_rest' => [
'schema' => [
'type' => 'object',
'patternProperties' => [
'^[a-z0-9_-]+$' => [
'type' => 'string',
],
],
]
],
] );
} );
/**
* Filters the content of a single block.
*
* @param string $block_content The block content.
* @return string The block content.
*/
add_filter( 'render_block', function( string $block_content ) : string {
$synced_text = get_option( 'syncedText', [] );
if ( ! is_array( $synced_text ) ) {
$synced_text = (array) $synced_text;
}
$block_content = str_replace(
array_map( function ( $key ) {
return sprintf( '{%s}', $key );
}, array_keys( $synced_text ) ),
$synced_text,
$block_content
);
return $block_content;
}, 10, 3 );