forked from mtoensing/simpletoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletoc-admin-settings.php
More file actions
194 lines (169 loc) · 7 KB
/
simpletoc-admin-settings.php
File metadata and controls
194 lines (169 loc) · 7 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
<?php
/**
* SimpleTOC admin settings page.
*
* @package simpletoc
*/
namespace MToensing\SimpleTOC;
/**
* 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 _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() {
// Register settings and filters for existing features.
$wrapper_enabled_filter = apply_filters( 'simpletoc_wrapper_enabled', null );
$accordion_enabled_filter = apply_filters( 'simpletoc_accordion_enabled', null );
$smooth_enabled_filter = apply_filters( 'simpletoc_smooth_enabled', null );
$absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null );
$autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null );
if ( null === $wrapper_enabled_filter ) {
register_setting( 'simpletoc_settings', 'simpletoc_wrapper_enabled' );
}
if ( null === $accordion_enabled_filter ) {
register_setting( 'simpletoc_settings', 'simpletoc_accordion_enabled' );
}
if ( null === $smooth_enabled_filter ) {
register_setting( 'simpletoc_settings', 'simpletoc_smooth_enabled' );
}
if ( null === $absolute_urls_enabled_filter ) {
register_setting( 'simpletoc_settings', 'simpletoc_absolute_urls_enabled' );
}
if ( null === $autoupdate_enabled_filter ) {
register_setting( 'simpletoc_settings', 'simpletoc_autoupdate_enabled', array( 'show_in_rest' => true ) );
}
// 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_wrapper_enabled',
esc_html__( 'Force wrapper div', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_wrapper_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 no auto refresh', 'simpletoc' ),
__NAMESPACE__ . '\simpletoc_autoupdate_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>';
}
/**
* SimpleTOC wrapper enabled callback.
*/
function simpletoc_wrapper_enabled_callback() {
$wrapper_enabled = get_option( 'simpletoc_wrapper_enabled', false );
if ( has_filter( 'simpletoc_wrapper_enabled' ) ) {
echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" checked="checked" disabled="disabled" />';
echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Setting controlled by "simpletoc_wrapper_enabled" filter. Remove filter to adjust setting.', 'simpletoc' ) . '</label>';
} else {
echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" ' . checked( 1, $wrapper_enabled, false ) . ' />';
echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Additionally adds the role "navigation" and ARIA attributes.', 'simpletoc' ) . '</label>';
}
}
/**
* SimpleTOC accordion enabled callback.
*/
function simpletoc_accordion_enabled_callback() {
$accordion_enabled = get_option( 'simpletoc_accordion_enabled', false );
if ( $accordion_enabled ) {
update_option( 'simpletoc_wrapper_enabled', true );
}
echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked( 1, $accordion_enabled, false ) . ' />';
echo '<label for="simpletoc_accordion_enabled" class="description">' . esc_html__( 'Adds minimal JavaScript and css styles.', 'simpletoc' ) . ' <strong>' . esc_html__( 'Notice:', 'simpletoc' ) . '</strong> ' . esc_html__( 'This will automatically enable the wrapper div.', 'simpletoc' ) . '</label>';
}
/**
* SimpleTOC smooth enabled callback.
*/
function simpletoc_smooth_enabled_callback() {
$smooth_enabled = get_option( 'simpletoc_smooth_enabled', false );
echo '<input type="checkbox" name="simpletoc_smooth_enabled" id="simpletoc_smooth_enabled" value="1" ' . checked( 1, $smooth_enabled, false ) . ' />';
echo '<label for="simpletoc_smooth_enabled" class="description">' . esc_html__( 'Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc' ) . '</label>';
}
/**
* SimpleTOC absolute urls enabled callback.
*/
function simpletoc_absolute_urls_enabled_callback() {
$absolute_urls_enabled = get_option( 'simpletoc_absolute_urls_enabled', false );
echo '<input type="checkbox" name="simpletoc_absolute_urls_enabled" id="simpletoc_absolute_urls_enabled" value="1" ' . checked( 1, $absolute_urls_enabled, false ) . ' />';
echo '<label for="simpletoc_absolute_urls_enabled" class="description">' . esc_html__( 'Adds the permalink url to the fragment.', 'simpletoc' ) . '</label>';
}
/**
* SimpleTOC autoupdate enabled callback.
*/
function simpletoc_autoupdate_enabled_callback() {
$autoupdate_enabled = get_option( 'simpletoc_autoupdate_enabled', false );
echo '<input type="checkbox" name="simpletoc_autoupdate_enabled" id="simpletoc_autoupdate_enabled" value="1" ' . checked( 1, $autoupdate_enabled, false ) . ' />';
echo '<label for="simpletoc_autoupdate_enabled" class="description">' . esc_html__( 'Deactivate the automatic table of contents refresh feature.', 'simpletoc' ) . '</label>';
}