PluginProbe
SimpleTOC – Table of Contents Block / 7.4.0
SimpleTOC – Table of Contents Block v7.4.0
7.4.0 7.3.1 7.2.0 7.3.0 7.1.1 7.1.0 7.0.10 7.0.9 7.0.8 7.0.7 7.0.6 7.0.4 7.0.5 5.0.21.1 5.0.22 5.0.23 5.0.24 5.0.25 5.0.25.1 5.0.27 5.0.28 5.0.29 5.0.3 5.0.30 5.0.31 All 161 releases
simpletoc / simpletoc-admin-settings.php

simpletoc-admin-settings.php in SimpleTOC – Table of Contents Block 7.4.0, at simpletoc-admin-settings.php

233 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * SimpleTOC admin settings page.
4 *
5 * @package simpletoc
6 */
7
8 namespace MToensing\SimpleTOC;
9
10 /**
11 * Whether scroll highlighting is enforced globally.
12 *
13 * @return bool
14 */
15 function simpletoc_scroll_spy_enabled() {
16 return (bool) apply_filters( 'simpletoc_scroll_spy_enabled', (bool) get_option( 'simpletoc_scroll_spy_enabled', false ) );
17 }
18
19 /**
20 * Add SimpleTOC global settings page.
21 */
22 function simpletoc_add_settings_page() {
23 add_options_page(
24 __( 'SimpleTOC Settings', 'simpletoc' ),
25 __( 'SimpleTOC', 'simpletoc' ),
26 'manage_options',
27 'simpletoc',
28 __NAMESPACE__ . '\simpletoc_settings_page'
29 );
30 }
31 add_action( 'admin_menu', __NAMESPACE__ . '\simpletoc_add_settings_page' );
32
33 /**
34 * SimpleTOC settings page content.
35 */
36 function simpletoc_settings_page() {
37 if ( ! current_user_can( 'manage_options' ) ) {
38 return;
39 }
40
41 ?>
42 <div class="wrap">
43 <h1><?php _e( 'SimpleTOC Settings', 'simpletoc' ); ?></h1>
44 <form method="post" action="options.php">
45 <?php
46 settings_fields( 'simpletoc_settings' );
47 do_settings_sections( 'simpletoc' );
48 submit_button();
49 ?>
50 </form>
51 </div>
52 <?php
53 }
54
55 /**
56 * Register SimpleTOC settings.
57 */
58 function simpletoc_register_settings() {
59 // Register settings and filters for existing features.
60 $accordion_enabled_filter = apply_filters( 'simpletoc_accordion_enabled', null );
61 $smooth_enabled_filter = apply_filters( 'simpletoc_smooth_enabled', null );
62 $absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null );
63 $autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null );
64 $box_style_enabled_filter = apply_filters( 'simpletoc_box_style_enabled', null );
65
66 if ( null === $accordion_enabled_filter ) {
67 register_setting( 'simpletoc_settings', 'simpletoc_accordion_enabled' );
68 }
69
70 if ( null === $smooth_enabled_filter ) {
71 register_setting( 'simpletoc_settings', 'simpletoc_smooth_enabled' );
72 }
73
74 if ( null === $absolute_urls_enabled_filter ) {
75 register_setting( 'simpletoc_settings', 'simpletoc_absolute_urls_enabled' );
76 }
77
78 if ( null === $autoupdate_enabled_filter ) {
79 register_setting( 'simpletoc_settings', 'simpletoc_autoupdate_enabled', array( 'show_in_rest' => true ) );
80 }
81
82 if ( null === $box_style_enabled_filter ) {
83 register_setting( 'simpletoc_settings', 'simpletoc_box_style_enabled' );
84 }
85
86 if ( null === apply_filters( 'simpletoc_scroll_spy_enabled', null ) ) {
87 register_setting(
88 'simpletoc_settings',
89 'simpletoc_scroll_spy_enabled',
90 array(
91 'type' => 'boolean',
92 'sanitize_callback' => 'rest_sanitize_boolean',
93 'default' => false,
94 )
95 );
96 }
97
98 // Add settings sections and fields.
99 add_settings_section(
100 'simpletoc_wrapper_section',
101 esc_html__( 'Global settings', 'simpletoc' ),
102 __NAMESPACE__ . '\simpletoc_wrapper_section_callback',
103 'simpletoc'
104 );
105
106 add_settings_field(
107 'simpletoc_accordion_enabled',
108 esc_html__( 'Force accordion menu', 'simpletoc' ),
109 __NAMESPACE__ . '\simpletoc_accordion_enabled_callback',
110 'simpletoc',
111 'simpletoc_wrapper_section'
112 );
113
114 add_settings_field(
115 'simpletoc_smooth_enabled',
116 esc_html__( 'Force smooth scrolling', 'simpletoc' ),
117 __NAMESPACE__ . '\simpletoc_smooth_enabled_callback',
118 'simpletoc',
119 'simpletoc_wrapper_section'
120 );
121
122 add_settings_field(
123 'simpletoc_absolute_urls_enabled',
124 esc_html__( 'Force absolute urls', 'simpletoc' ),
125 __NAMESPACE__ . '\simpletoc_absolute_urls_enabled_callback',
126 'simpletoc',
127 'simpletoc_wrapper_section'
128 );
129
130 // Add the autoupdate settings field.
131 add_settings_field(
132 'simpletoc_autoupdate_enabled',
133 esc_html__( 'Force no auto refresh', 'simpletoc' ),
134 __NAMESPACE__ . '\simpletoc_autoupdate_enabled_callback',
135 'simpletoc',
136 'simpletoc_wrapper_section'
137 );
138
139 add_settings_field(
140 'simpletoc_scroll_spy_enabled',
141 esc_html__( 'Force highlight current section', 'simpletoc' ),
142 __NAMESPACE__ . '\simpletoc_scroll_spy_enabled_callback',
143 'simpletoc',
144 'simpletoc_wrapper_section'
145 );
146
147 add_settings_field(
148 'simpletoc_box_style_enabled',
149 esc_html__( 'Force box style', 'simpletoc' ),
150 __NAMESPACE__ . '\simpletoc_box_style_enabled_callback',
151 'simpletoc',
152 'simpletoc_wrapper_section'
153 );
154 }
155
156 add_action( 'admin_init', __NAMESPACE__ . '\simpletoc_register_settings' );
157
158 /**
159 * SimpleTOC wrapper section callback.
160 */
161 function simpletoc_wrapper_section_callback() {
162 $donatelink = '<a href="https://marc.tv/out/donate">' . esc_html__( 'Donate here!', 'simpletoc' ) . '</a>';
163
164 echo '<p>' .
165 esc_html__( 'Enforce these settings globally, ignoring any block-level configurations.', 'simpletoc' ) . '</p><p>' .
166 esc_html__( 'Think about making a donation if you use any of these features.', 'simpletoc' ) . ' ' .
167 $donatelink .
168 '</p>';
169 }
170
171 /**
172 * SimpleTOC accordion enabled callback.
173 */
174 function simpletoc_accordion_enabled_callback() {
175 $accordion_enabled = get_option( 'simpletoc_accordion_enabled', false );
176 echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked( 1, $accordion_enabled, false ) . ' />';
177 echo '<label for="simpletoc_accordion_enabled" class="description">' . esc_html__( 'Adds minimal JavaScript and css styles.', 'simpletoc' ) . '</label>';
178 }
179
180 /**
181 * SimpleTOC smooth enabled callback.
182 */
183 function simpletoc_smooth_enabled_callback() {
184 $smooth_enabled = get_option( 'simpletoc_smooth_enabled', false );
185 echo '<input type="checkbox" name="simpletoc_smooth_enabled" id="simpletoc_smooth_enabled" value="1" ' . checked( 1, $smooth_enabled, false ) . ' />';
186 echo '<label for="simpletoc_smooth_enabled" class="description">' . esc_html__( 'Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc' ) . '</label>';
187 }
188
189 /**
190 * SimpleTOC absolute urls enabled callback.
191 */
192 function simpletoc_absolute_urls_enabled_callback() {
193 $absolute_urls_enabled = get_option( 'simpletoc_absolute_urls_enabled', false );
194 echo '<input type="checkbox" name="simpletoc_absolute_urls_enabled" id="simpletoc_absolute_urls_enabled" value="1" ' . checked( 1, $absolute_urls_enabled, false ) . ' />';
195 echo '<label for="simpletoc_absolute_urls_enabled" class="description">' . esc_html__( 'Adds the permalink url to the fragment.', 'simpletoc' ) . '</label>';
196 }
197
198 /**
199 * SimpleTOC autoupdate enabled callback.
200 */
201 function simpletoc_autoupdate_enabled_callback() {
202 $autoupdate_enabled = get_option( 'simpletoc_autoupdate_enabled', false );
203 echo '<input type="checkbox" name="simpletoc_autoupdate_enabled" id="simpletoc_autoupdate_enabled" value="1" ' . checked( 1, $autoupdate_enabled, false ) . ' />';
204 echo '<label for="simpletoc_autoupdate_enabled" class="description">' . esc_html__( 'Deactivate the automatic table of contents refresh feature.', 'simpletoc' ) . '</label>';
205 }
206
207 /**
208 * SimpleTOC box style enabled callback.
209 */
210 function simpletoc_box_style_enabled_callback() {
211 $box_style_enabled = get_option( 'simpletoc_box_style_enabled', false );
212
213 if ( has_filter( 'simpletoc_box_style_enabled' ) ) {
214 echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" checked="checked" disabled="disabled" />';
215 echo '<label for="simpletoc_box_style_enabled" class="description">' . esc_html__( 'Setting controlled by "simpletoc_box_style_enabled" filter. Remove filter to adjust setting.', 'simpletoc' ) . '</label>';
216 } else {
217 echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" ' . checked( 1, $box_style_enabled, false ) . ' />';
218 echo '<label for="simpletoc_box_style_enabled" class="description">' . esc_html__( 'Applies the Box style with the default gray background to all SimpleTOC blocks.', 'simpletoc' ) . '</label>';
219 }
220 }
221
222 /**
223 * Scroll highlighting setting.
224 */
225 function simpletoc_scroll_spy_enabled_callback() {
226 $controlled = null !== apply_filters( 'simpletoc_scroll_spy_enabled', null );
227 echo '<input type="checkbox" name="simpletoc_scroll_spy_enabled" id="simpletoc_scroll_spy_enabled" value="1" ' . checked( true, simpletoc_scroll_spy_enabled(), false ) . disabled( $controlled, true, false ) . ' />';
228 echo '<label for="simpletoc_scroll_spy_enabled" class="description">' . esc_html__( 'Underlines the current section link in all SimpleTOC blocks in supported browsers. No JavaScript fallback.', 'simpletoc' ) . '</label>';
229 if ( $controlled ) {
230 echo '<p class="description">' . esc_html__( 'Setting controlled by the simpletoc_scroll_spy_enabled filter.', 'simpletoc' ) . '</p>';
231 }
232 }
233