-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathplugin.php
More file actions
190 lines (162 loc) · 4.35 KB
/
Copy pathplugin.php
File metadata and controls
190 lines (162 loc) · 4.35 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
* Registers JS and CSS assets.
*
* @package insert-special-characters
*/
namespace InsertSpecialCharacters;
/**
* Registers JS and CSS assets.
*/
function register_assets() {
$asset_data_file = trailingslashit( ISC_PLUGIN_PATH ) . 'build/index.asset.php';
if ( ! file_exists( $asset_data_file ) ) {
return;
}
$script_data = include $asset_data_file;
wp_register_script(
'insert-special-characters',
ISC_PLUGIN_URL . 'build/index.js',
$script_data['dependencies'],
$script_data['version'],
true
);
wp_register_style(
'insert-special-characters-css',
ISC_PLUGIN_URL . 'build/index.css',
array(),
$script_data['version']
);
wp_set_script_translations( 'insert-special-characters', 'insert-special-characters', ISC_PLUGIN_PATH . 'languages' );
}
add_action( 'init', __NAMESPACE__ . '\register_assets' );
/**
* Enqueue the admin JavaScript assets.
*/
function gcm_block_enqueue_scripts() {
wp_enqueue_script( 'insert-special-characters' );
wp_enqueue_style( 'insert-special-characters-css' );
wp_add_inline_script(
'insert-special-characters',
sprintf(
'var tenupIscVars = window.tenupIscVars || {}; tenupIscVars = %1$s',
wp_json_encode(
array(
'most_read_palette' => get_most_used_palette_setting(),
)
)
)
);
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\gcm_block_enqueue_scripts' );
/**
* Registers settings fields.
*/
function register_settings_fields() {
register_setting(
'writing',
'tenup_isc_most_read_palette',
array(
'type' => 'boolean',
'show_in_rest' => true,
'default' => false,
'sanitize_callback' => function( $input ) {
// Accept only true, 'on', or 1
if ( $input === 'on' || $input === true || $input === 1 || $input === '1' ) {
return true;
}
// If input is invalid, add an admin error message
add_settings_error(
'tenup_isc_most_read_palette',
esc_attr( 'invalid_input' ),
__( 'Invalid value for Most Used Palette setting.', 'insert-special-characters' ),
'error'
);
return false;
},
)
);
add_settings_section(
'tenup_isc_writing_section',
esc_html__( 'Insert Special Characters', 'insert-special-characters' ),
null,
'writing'
);
add_settings_field(
'tenup_isc_most_read_palette',
esc_html__( 'Most used characters palette', 'insert-special-characters' ),
__NAMESPACE__ . '\render_isc_writing_setting',
'writing',
'tenup_isc_writing_section',
array(
'label_for' => 'tenup_isc_most_read_palette',
)
);
}
add_action( 'admin_init', __NAMESPACE__ . '\register_settings_fields' );
/**
* Renders settings fields.
*/
function render_isc_writing_setting() {
$option = get_most_used_palette_setting();
?>
<p>
<label for="tenup_isc_most_read_palette">
<input
type="checkbox"
name="tenup_isc_most_read_palette"
id="tenup_isc_most_read_palette"
<?php checked( $option, true, true ); ?>
>
<?php esc_html_e( 'Check this to enable the most used character palette.', 'insert-special-characters' ); ?>
</label>
</p>
<?php if ( $option ) : ?>
<p>
<button class="button secondary" id="isc_reset_palette" type="button" style="margin-top: 16px;">
<?php esc_html_e( 'Clear palette', 'insert-special-characters' ); ?>
</button>
<?php esc_html_e( 'Press this to clear palette data.', 'insert-special-characters' ); ?>
</p>
<?php
endif;
}
/**
* Helper function to get most used character palette setting.
*
* @return boolean
*/
function get_most_used_palette_setting() {
return 'on' === get_option( 'tenup_isc_most_read_palette', 'on' );
}
/**
* Loads admin scripts.
*
* @param string $hook The current admin page.
*/
function load_admin_scripts( $hook ) {
if ( 'options-writing.php' !== $hook ) {
return;
}
$asset_data_file = trailingslashit( ISC_PLUGIN_PATH ) . 'build/admin.asset.php';
if ( ! file_exists( $asset_data_file ) ) {
return;
}
$script_data = include $asset_data_file;
wp_enqueue_script(
'insert-special-characters-admin-js',
ISC_PLUGIN_URL . 'build/admin.js',
$script_data['dependencies'],
$script_data['version'],
true
);
wp_localize_script(
'insert-special-characters-admin-js',
'tenupIscAdminVars',
array(
'palette_deleted_message' => __( 'Palette cleared', 'insert-special-characters' ),
)
);
}
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\load_admin_scripts' );