-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.php
164 lines (144 loc) · 3.76 KB
/
index.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* Plugin Name: Unadorned Announcement Bar
* Plugin URI: https://github.com/wptrainingteam/unadorned-announcement-bar
* Description: A companion plugin for a WordPress Developer Blog article.
* Version: 1.0.1
* Requires at least: 6.1
* Requires PHP: 7.4
* Author: Róbert Mészáros
* Author URI: https://www.meszarosrob.com/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: unadorned-announcement-bar
*
* @package unadorned-announcement-bar
*/
declare( strict_types=1 );
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Registers the options page.
*/
function unadorned_announcement_bar_settings_page() {
add_options_page(
__( 'Unadorned Announcement Bar', 'unadorned-announcement-bar' ),
__( 'Unadorned Announcement Bar', 'unadorned-announcement-bar' ),
'manage_options',
'unadorned-announcement-bar',
'unadorned_announcement_bar_settings_page_html'
);
}
add_action( 'admin_menu', 'unadorned_announcement_bar_settings_page' );
/**
* Outputs the root element for the main React component.
*/
function unadorned_announcement_bar_settings_page_html() {
printf(
'<div class="wrap" id="unadorned-announcement-bar-settings">%s</div>',
esc_html__( 'Loading…', 'unadorned-announcement-bar' )
);
}
/**
* Enqueues the necessary styles and script only on the options page.
*
* @param string $admin_page The current admin page.
*/
function unadorned_announcement_bar_settings_page_enqueue_style_script( $admin_page ) {
if ( 'settings_page_unadorned-announcement-bar' !== $admin_page ) {
return;
}
$asset_file = plugin_dir_path( __FILE__ ) . 'build/index.asset.php';
if ( ! file_exists( $asset_file ) ) {
return;
}
$asset = include $asset_file;
wp_enqueue_script(
'unadorned-announcement-bar-script',
plugins_url( 'build/index.js', __FILE__ ),
$asset['dependencies'],
$asset['version'],
array(
'in_footer' => true,
)
);
wp_enqueue_style(
'unadorned-announcement-bar-style',
plugins_url( 'build/index.css', __FILE__ ),
array_filter(
$asset['dependencies'],
function ( $style ) {
return wp_style_is( $style, 'registered' );
}
),
$asset['version'],
);
}
add_action( 'admin_enqueue_scripts', 'unadorned_announcement_bar_settings_page_enqueue_style_script' );
/**
* Registers the setting and defines its type and default value.
*/
function unadorned_announcement_bar_settings() {
$default = array(
'message' => __( 'Hello, World!', 'unadorned-announcement-bar' ),
'display' => true,
'size' => 'medium',
);
$schema = array(
'type' => 'object',
'properties' => array(
'message' => array(
'type' => 'string',
),
'display' => array(
'type' => 'boolean',
),
'size' => array(
'type' => 'string',
'enum' => array(
'small',
'medium',
'large',
'x-large',
),
),
),
);
register_setting(
'options',
'unadorned_announcement_bar',
array(
'type' => 'object',
'default' => $default,
'show_in_rest' => array(
'schema' => $schema,
),
)
);
}
add_action( 'init', 'unadorned_announcement_bar_settings' );
/**
* Displays the announcement bar on the front-end.
*/
function unadorned_announcement_bar_front_page() {
$options = get_option( 'unadorned_announcement_bar' );
if ( ! $options['display'] ) {
return;
}
$css = WP_Style_Engine::compile_css(
array(
'background' => 'var(--wp--preset--color--vivid-purple, #9b51e0)',
'color' => 'var(--wp--preset--color--white, #ffffff)',
'padding' => 'var(--wp--preset--spacing--20, 1.5rem)',
'font-size' => $options['size'],
),
''
);
printf(
'<div style="%s">%s</div>',
esc_attr( $css ),
esc_html( $options['message'] )
);
}
add_action( 'wp_body_open', 'unadorned_announcement_bar_front_page' );