-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhello-dolly-block.php
116 lines (107 loc) · 3.36 KB
/
hello-dolly-block.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
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Plugin Name: Hello Dolly Block
* Description: The original Hello Dolly plugin, but in block form.
* Requires at least: 5.9
* Requires PHP: 7.0
* Version: 0.1.0
* Author: Nick Diego
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: hello-dolly-block
*
* @package create-block
*/
/**
* 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_hello_dolly_block_block_init() {
register_block_type(
__DIR__ . '/build',
array(
'render_callback' => 'create_block_hello_dolly_block_render_callback'
)
);
}
add_action( 'init', 'create_block_hello_dolly_block_block_init' );
/**
* Renders the Holl Dolly Block on the frontend.
*
* @param array $attributes The block attributes.
* @param string $content The block content.
* @param WP_Block $block Block instance.
*/
function create_block_hello_dolly_block_render_callback( $attributes, $content, $block ) {
$text_align = $attributes[ 'textAlign' ];
$wrapper_attributes = get_block_wrapper_attributes();
if ( $text_align ) {
$wrapper_attributes = get_block_wrapper_attributes(
array( 'class' => 'has-text-align-' . $text_align )
);
}
ob_start();
require plugin_dir_path( __FILE__ ) . 'build/template.php';
return ob_get_clean();
}
/**
* The original lyric function from Hello Dolly by Matt Mullenweg.
*
* @see https://plugins.trac.wordpress.org/browser/hello-dolly/trunk/hello.php
*/
function hello_dolly_block_get_lyric() {
/** These are the lyrics to Hello Dolly */
$lyrics = "Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, take her wrap, fellas
Dolly, never go away again
Hello, Dolly
Well, hello, Dolly
It's so nice to have you back where you belong
You're lookin' swell, Dolly
I can tell, Dolly
You're still glowin', you're still crowin'
You're still goin' strong
I feel the room swayin'
While the band's playin'
One of our old favorite songs from way back when
So, golly, gee, fellas
Have a little faith in me, fellas
Dolly, never go away
Promise, you'll never go away
Dolly'll never go away again";
// Here we split it into lines.
$lyrics = explode( "\n", $lyrics );
// And then randomly choose a line.
return wptexturize( $lyrics[ mt_rand( 0, count( $lyrics ) - 1 ) ] );
}
/**
* The original function from Hello Dolly by Matt Mullenweg. To work within a
* block context, printf was changed to sprintf.
*
* @see https://plugins.trac.wordpress.org/browser/hello-dolly/trunk/hello.php
*/
function hello_dolly_block_render_lyric() {
$chosen = hello_dolly_block_get_lyric();
$lang = '';
if ( 'en_' !== substr( get_user_locale(), 0, 3 ) ) {
$lang = ' lang="en"';
}
return sprintf(
'<span class="screen-reader-text">%s </span><span dir="ltr"%s>%s</span>',
__( 'Quote from Hello Dolly song, by Jerry Herman:', 'hello-dolly' ),
$lang,
$chosen
);
}