Repository navigation
Expand file tree
/
Copy pathsimpletoc-admin-settings.php
More file actions
242 lines (215 loc) · 7.16 KB
/
Copy pathsimpletoc-admin-settings.php
File metadata and controls
242 lines (215 loc) · 7.16 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* SimpleTOC admin settings page.
*
* @package simpletoc
*/
namespace MToensing\SimpleTOC;
/**
* Returns the effective global setting. Filters override saved options.
* A false global setting still allows individual blocks to opt in.
*
* @param string $option Setting name.
* @return bool
*/
function simpletoc_setting_enabled( $option ) {
return (bool) apply_filters( $option, (bool) get_option( $option, false ) );
}
/**
* Whether scroll highlighting is enforced globally.
*
* @return bool
*/
function simpletoc_scroll_spy_enabled() {
return simpletoc_setting_enabled( 'simpletoc_scroll_spy_enabled' );
}
/**
* Global values shared by the block editor settings and script fallback.
*
* @return array
*/
function simpletoc_get_editor_settings() {
return array(
'settingsUrl' => admin_url( 'options-general.php?page=simpletoc' ),
'scrollSpyEnabled' => simpletoc_scroll_spy_enabled(),
'autoRefreshDisabled' => simpletoc_setting_enabled( 'simpletoc_autoupdate_enabled' ),
'accordionEnabled' => simpletoc_setting_enabled( 'simpletoc_accordion_enabled' ),
'smoothEnabled' => simpletoc_setting_enabled( 'simpletoc_smooth_enabled' ),
'absoluteUrlsEnabled' => simpletoc_setting_enabled( 'simpletoc_absolute_urls_enabled' ),
'boxStyleEnabled' => simpletoc_setting_enabled( 'simpletoc_box_style_enabled' ),
);
}
/**
* Add SimpleTOC global settings page.
*/
function simpletoc_add_settings_page() {
add_options_page(
__( 'SimpleTOC Settings', 'simpletoc' ),
__( 'SimpleTOC', 'simpletoc' ),
'manage_options',
'simpletoc',
__NAMESPACE__ . '\simpletoc_settings_page'
);
}
add_action( 'admin_menu', __NAMESPACE__ . '\simpletoc_add_settings_page' );
/**
* SimpleTOC settings page content.
*/
function simpletoc_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e( 'SimpleTOC Settings', 'simpletoc' ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'simpletoc_settings' );
do_settings_sections( 'simpletoc' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register SimpleTOC settings.
*/
function simpletoc_register_settings() {
$options = array(
'simpletoc_accordion_enabled',
'simpletoc_smooth_enabled',
'simpletoc_absolute_urls_enabled',
'simpletoc_autoupdate_enabled',
'simpletoc_box_style_enabled',
'simpletoc_scroll_spy_enabled',
);
foreach ( $options as $option ) {
if ( null !== apply_filters( $option, null ) ) {
continue;
}
register_setting(
'simpletoc_settings',
$option,
array(
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'default' => false,
'show_in_rest' => 'simpletoc_autoupdate_enabled' === $option,
)
);
}
// Add settings sections and fields.
add_settings_section(
'simpletoc_wrapper_section',
esc_html__( 'Global settings', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_wrapper_section_callback',
'simpletoc'
);
add_settings_field(
'simpletoc_accordion_enabled',
esc_html__( 'Force accordion menu', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_accordion_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
add_settings_field(
'simpletoc_smooth_enabled',
esc_html__( 'Force smooth scrolling', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_smooth_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
add_settings_field(
'simpletoc_absolute_urls_enabled',
esc_html__( 'Force absolute urls', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_absolute_urls_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
// Add the autoupdate settings field.
add_settings_field(
'simpletoc_autoupdate_enabled',
esc_html__( 'Force manual refresh', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_autoupdate_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
add_settings_field(
'simpletoc_scroll_spy_enabled',
esc_html__( 'Force highlight current section', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_scroll_spy_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
add_settings_field(
'simpletoc_box_style_enabled',
esc_html__( 'Force box style', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_box_style_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
}
add_action( 'admin_init', __NAMESPACE__ . '\simpletoc_register_settings' );
/**
* SimpleTOC wrapper section callback.
*/
function simpletoc_wrapper_section_callback() {
$donatelink = '<a href="https://marc.tv/out/donate">' . esc_html__( 'Donate here!', 'simpletoc' ) . '</a>';
echo '<p>' .
esc_html__( 'Enforce these settings globally, ignoring any block-level configurations.', 'simpletoc' ) . '</p><p>' .
esc_html__( 'Think about making a donation if you use any of these features.', 'simpletoc' ) . ' ' .
$donatelink .
'</p>';
}
/**
* Outputs a global checkbox with its effective value and filter ownership.
*
* @param string $option Setting name.
* @param string $description Help text.
*/
function simpletoc_setting_checkbox( $option, $description ) {
$controlled = null !== apply_filters( $option, null );
echo '<input type="checkbox" name="' . esc_attr( $option ) . '" id="' . esc_attr( $option ) . '" value="1" ' . checked( true, simpletoc_setting_enabled( $option ), false ) . disabled( $controlled, true, false ) . ' />';
echo '<label for="' . esc_attr( $option ) . '" class="description">' . esc_html( $description ) . '</label>';
if ( $controlled ) {
/* translators: %s: WordPress filter name. */
echo '<p class="description">' . esc_html( sprintf( __( 'Setting controlled by the %s filter.', 'simpletoc' ), $option ) ) . '</p>';
}
}
/**
* Outputs the accordion setting.
*/
function simpletoc_accordion_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_accordion_enabled', __( 'Collapses the table of contents by default. Click the heading to expand it. Adds minimal JavaScript and CSS.', 'simpletoc' ) );
}
/**
* Outputs the smooth setting.
*/
function simpletoc_smooth_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_smooth_enabled', __( 'Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc' ) );
}
/**
* Outputs the absolute urls setting.
*/
function simpletoc_absolute_urls_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_absolute_urls_enabled', __( 'Adds the permalink url to the fragment.', 'simpletoc' ) );
}
/**
* Outputs the autoupdate setting.
*/
function simpletoc_autoupdate_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_autoupdate_enabled', __( 'Deactivate the automatic table of contents refresh feature.', 'simpletoc' ) );
}
/**
* Outputs the box style setting.
*/
function simpletoc_box_style_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_box_style_enabled', __( 'Applies the Box style with the default gray background to all SimpleTOC blocks.', 'simpletoc' ) );
}
/**
* Outputs the scroll spy setting.
*/
function simpletoc_scroll_spy_enabled_callback() {
simpletoc_setting_checkbox( 'simpletoc_scroll_spy_enabled', __( 'Underlines the current section link in all SimpleTOC blocks in supported browsers. No JavaScript fallback.', 'simpletoc' ) );
}