-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlineup.php
55 lines (49 loc) · 1.69 KB
/
lineup.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
<?php
/**
* Plugin Name: Lineup
* Description: Inline block examples.
* Requires at least: 5.9
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: lineup
*
* @package s8-lineup
*/
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_lineup_block_init() {
register_block_type( __DIR__ . '/build/lineup', [
'render_callback' => 'create_block_render_lineup'
] );
register_block_type( __DIR__ . '/build/text' );
register_block_type( __DIR__ . '/build/ip', [
'render_callback' => 'create_block_render_ip'
] );
register_block_type( __DIR__ . '/build/special' );
}
add_action( 'init', 'create_block_lineup_block_init' );
/**
* Renders Lineup block, removing whitespace from between tags.
*/
function create_block_render_lineup( $attributes, $content ) {
return trim(preg_replace('/>\s+</', '><', $content));
}
function create_block_render_ip() {
return "<span class=\"wp-block-s8-ip\">{$_SERVER['REMOTE_ADDR']}</span>";
}
function create_block_print_ip_to_js(){
$ns = '"s8/"';
return "if ( ! ($ns in window ) ) window[$ns] = { ip: '{$_SERVER['REMOTE_ADDR']}' };";
}
add_action( 'enqueue_block_editor_assets', function() {
$handle = 's8-ip-editor-script';
wp_add_inline_script( $handle, create_block_print_ip_to_js(), 'before' );
} );