|
1 | 1 | <?php |
| 2 | +/** |
| 3 | + * SimpleTOC admin settings page. |
| 4 | + * |
| 5 | + * @package simpletoc |
| 6 | + */ |
2 | 7 |
|
3 | | -// Add SimpleTOC global settings page |
| 8 | +namespace MToensing\SimpleTOC; |
| 9 | + |
| 10 | +/** |
| 11 | + * Add SimpleTOC global settings page. |
| 12 | + */ |
4 | 13 | function simpletoc_add_settings_page() { |
5 | | - add_options_page( |
6 | | - __('SimpleTOC Settings', 'simpletoc'), |
7 | | - __('SimpleTOC', 'simpletoc'), |
8 | | - 'manage_options', |
9 | | - 'simpletoc', |
10 | | - 'simpletoc_settings_page' |
11 | | - ); |
| 14 | + add_options_page( |
| 15 | + __( 'SimpleTOC Settings', 'simpletoc' ), |
| 16 | + __( 'SimpleTOC', 'simpletoc' ), |
| 17 | + 'manage_options', |
| 18 | + 'simpletoc', |
| 19 | + 'simpletoc_settings_page' |
| 20 | + ); |
12 | 21 | } |
13 | | -add_action('admin_menu', 'simpletoc_add_settings_page'); |
| 22 | +add_action( 'admin_menu', __NAMESPACE__ . '\simpletoc_add_settings_page' ); |
14 | 23 |
|
15 | | -// SimpleTOC settings page content |
| 24 | +/** |
| 25 | + * SimpleTOC settings page content. |
| 26 | + */ |
16 | 27 | function simpletoc_settings_page() { |
17 | | - if (!current_user_can('manage_options')) { |
18 | | - return; |
19 | | - } |
20 | | - |
21 | | - ?> |
22 | | - <div class="wrap"> |
23 | | - <h1><?php _e('SimpleTOC Settings', 'simpletoc'); ?></h1> |
24 | | - <form method="post" action="options.php"> |
25 | | - <?php |
26 | | - settings_fields('simpletoc_settings'); |
27 | | - do_settings_sections('simpletoc'); |
28 | | - submit_button(); |
29 | | - ?> |
30 | | - </form> |
31 | | - </div> |
32 | | - <?php |
| 28 | + if ( ! current_user_can( 'manage_options' ) ) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + ?> |
| 33 | + <div class="wrap"> |
| 34 | + <h1><?php _e( 'SimpleTOC Settings', 'simpletoc' ); ?></h1> |
| 35 | + <form method="post" action="options.php"> |
| 36 | + <?php |
| 37 | + settings_fields( 'simpletoc_settings' ); |
| 38 | + do_settings_sections( 'simpletoc' ); |
| 39 | + submit_button(); |
| 40 | + ?> |
| 41 | + </form> |
| 42 | + </div> |
| 43 | + <?php |
33 | 44 | } |
34 | 45 |
|
35 | | -// Register SimpleTOC settings |
| 46 | +/** |
| 47 | + * Register SimpleTOC settings. |
| 48 | + */ |
36 | 49 | function simpletoc_register_settings() { |
37 | | - // Register settings and filters for existing features |
38 | | - $wrapper_enabled_filter = apply_filters('simpletoc_wrapper_enabled', null); |
39 | | - $accordion_enabled_filter = apply_filters('simpletoc_accordion_enabled', null); |
40 | | - $smooth_enabled_filter = apply_filters('simpletoc_smooth_enabled', null); |
41 | | - $absolute_urls_enabled_filter = apply_filters('simpletoc_absolute_urls_enabled', null); |
42 | | - $autoupdate_enabled_filter = apply_filters('simpletoc_autoupdate_enabled', null); |
43 | | - |
44 | | - if ($wrapper_enabled_filter === null) { |
45 | | - register_setting('simpletoc_settings', 'simpletoc_wrapper_enabled'); |
46 | | - } |
47 | | - |
48 | | - if ($accordion_enabled_filter === null) { |
49 | | - register_setting('simpletoc_settings', 'simpletoc_accordion_enabled'); |
50 | | - } |
51 | | - |
52 | | - if ($smooth_enabled_filter === null) { |
53 | | - register_setting('simpletoc_settings', 'simpletoc_smooth_enabled'); |
54 | | - } |
55 | | - |
56 | | - if ($absolute_urls_enabled_filter === null) { |
57 | | - register_setting('simpletoc_settings', 'simpletoc_absolute_urls_enabled'); |
58 | | - } |
59 | | - |
60 | | - if ($autoupdate_enabled_filter === null) { |
61 | | - register_setting('simpletoc_settings', 'simpletoc_autoupdate_enabled', array( 'show_in_rest' => true ) ); |
62 | | - } |
63 | | - |
64 | | - // Add settings sections and fields |
65 | | - add_settings_section( |
66 | | - 'simpletoc_wrapper_section', |
67 | | - __('Global settings', 'simpletoc'), |
68 | | - 'simpletoc_wrapper_section_callback', |
69 | | - 'simpletoc' |
70 | | - ); |
71 | | - |
72 | | - add_settings_field( |
73 | | - 'simpletoc_accordion_enabled', |
74 | | - __('Force accordion menu', 'simpletoc'), |
75 | | - 'simpletoc_accordion_enabled_callback', |
76 | | - 'simpletoc', |
77 | | - 'simpletoc_wrapper_section' |
78 | | - ); |
79 | | - |
80 | | - add_settings_field( |
81 | | - 'simpletoc_wrapper_enabled', |
82 | | - __('Force wrapper div', 'simpletoc'), |
83 | | - 'simpletoc_wrapper_enabled_callback', |
84 | | - 'simpletoc', |
85 | | - 'simpletoc_wrapper_section' |
86 | | - ); |
87 | | - |
88 | | - add_settings_field( |
89 | | - 'simpletoc_smooth_enabled', |
90 | | - __('Force smooth scrolling', 'simpletoc'), |
91 | | - 'simpletoc_smooth_enabled_callback', |
92 | | - 'simpletoc', |
93 | | - 'simpletoc_wrapper_section' |
94 | | - ); |
95 | | - |
96 | | - add_settings_field( |
97 | | - 'simpletoc_absolute_urls_enabled', |
98 | | - __('Force absolute urls', 'simpletoc'), |
99 | | - 'simpletoc_absolute_urls_enabled_callback', |
100 | | - 'simpletoc', |
101 | | - 'simpletoc_wrapper_section' |
102 | | - ); |
103 | | - |
104 | | - // Add the autoupdate settings field |
105 | | - add_settings_field( |
106 | | - 'simpletoc_autoupdate_enabled', |
107 | | - __('Force no auto refresh', 'simpletoc'), |
108 | | - 'simpletoc_autoupdate_enabled_callback', |
109 | | - 'simpletoc', |
110 | | - 'simpletoc_wrapper_section' |
111 | | - ); |
| 50 | + // Register settings and filters for existing features. |
| 51 | + $wrapper_enabled_filter = apply_filters( 'simpletoc_wrapper_enabled', null ); |
| 52 | + $accordion_enabled_filter = apply_filters( 'simpletoc_accordion_enabled', null ); |
| 53 | + $smooth_enabled_filter = apply_filters( 'simpletoc_smooth_enabled', null ); |
| 54 | + $absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null ); |
| 55 | + $autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null ); |
| 56 | + |
| 57 | + if ( null === $wrapper_enabled_filter ) { |
| 58 | + register_setting( 'simpletoc_settings', 'simpletoc_wrapper_enabled' ); |
| 59 | + } |
| 60 | + |
| 61 | + if ( null === $accordion_enabled_filter ) { |
| 62 | + register_setting( 'simpletoc_settings', 'simpletoc_accordion_enabled' ); |
| 63 | + } |
| 64 | + |
| 65 | + if ( null === $smooth_enabled_filter ) { |
| 66 | + register_setting( 'simpletoc_settings', 'simpletoc_smooth_enabled' ); |
| 67 | + } |
| 68 | + |
| 69 | + if ( null === $absolute_urls_enabled_filter ) { |
| 70 | + register_setting( 'simpletoc_settings', 'simpletoc_absolute_urls_enabled' ); |
| 71 | + } |
| 72 | + |
| 73 | + if ( null === $autoupdate_enabled_filter ) { |
| 74 | + register_setting( 'simpletoc_settings', 'simpletoc_autoupdate_enabled', array( 'show_in_rest' => true ) ); |
| 75 | + } |
| 76 | + |
| 77 | + // Add settings sections and fields. |
| 78 | + add_settings_section( |
| 79 | + 'simpletoc_wrapper_section', |
| 80 | + esc_html__( 'Global settings', 'simpletoc' ), |
| 81 | + __NAMESPACE__ . '\simpletoc_wrapper_section_callback', |
| 82 | + 'simpletoc' |
| 83 | + ); |
| 84 | + |
| 85 | + add_settings_field( |
| 86 | + 'simpletoc_accordion_enabled', |
| 87 | + esc_html__( 'Force accordion menu', 'simpletoc' ), |
| 88 | + __NAMESPACE__ . '\simpletoc_accordion_enabled_callback', |
| 89 | + 'simpletoc', |
| 90 | + 'simpletoc_wrapper_section' |
| 91 | + ); |
| 92 | + |
| 93 | + add_settings_field( |
| 94 | + 'simpletoc_wrapper_enabled', |
| 95 | + esc_html__( 'Force wrapper div', 'simpletoc' ), |
| 96 | + __NAMESPACE__ . '\simpletoc_wrapper_enabled_callback', |
| 97 | + 'simpletoc', |
| 98 | + 'simpletoc_wrapper_section' |
| 99 | + ); |
| 100 | + |
| 101 | + add_settings_field( |
| 102 | + 'simpletoc_smooth_enabled', |
| 103 | + esc_html__( 'Force smooth scrolling', 'simpletoc' ), |
| 104 | + __NAMESPACE__ . '\simpletoc_smooth_enabled_callback', |
| 105 | + 'simpletoc', |
| 106 | + 'simpletoc_wrapper_section' |
| 107 | + ); |
| 108 | + |
| 109 | + add_settings_field( |
| 110 | + 'simpletoc_absolute_urls_enabled', |
| 111 | + esc_html__( 'Force absolute urls', 'simpletoc' ), |
| 112 | + __NAMESPACE__ . '\simpletoc_absolute_urls_enabled_callback', |
| 113 | + 'simpletoc', |
| 114 | + 'simpletoc_wrapper_section' |
| 115 | + ); |
| 116 | + |
| 117 | + // Add the autoupdate settings field. |
| 118 | + add_settings_field( |
| 119 | + 'simpletoc_autoupdate_enabled', |
| 120 | + esc_html__( 'Force no auto refresh', 'simpletoc' ), |
| 121 | + __NAMESPACE__ . '\simpletoc_autoupdate_enabled_callback', |
| 122 | + 'simpletoc', |
| 123 | + 'simpletoc_wrapper_section' |
| 124 | + ); |
112 | 125 | } |
113 | 126 |
|
114 | | -add_action('admin_init', 'simpletoc_register_settings'); |
| 127 | +add_action( 'admin_init', __NAMESPACE__ . '\simpletoc_register_settings' ); |
115 | 128 |
|
116 | | -function simpletoc_wrapper_section_callback() |
117 | | -{ |
118 | | - $donatelink = '<a href="https://marc.tv/out/donate">' . __('Donate here!', 'simpletoc') . '</a>'; |
| 129 | +/** |
| 130 | + * SimpleTOC wrapper section callback. |
| 131 | + */ |
| 132 | +function simpletoc_wrapper_section_callback() { |
| 133 | + $donatelink = '<a href="https://marc.tv/out/donate">' . esc_html__( 'Donate here!', 'simpletoc' ) . '</a>'; |
119 | 134 |
|
120 | | - echo '<p>' . |
121 | | - __('Enforce these settings globally, ignoring any block-level configurations.', 'simpletoc') . '</p><p>' . |
122 | | - __('Think about making a donation if you use any of these features.', 'simpletoc') . ' ' . |
123 | | - $donatelink . |
124 | | - '</p>'; |
| 135 | + echo '<p>' . |
| 136 | + esc_html__( 'Enforce these settings globally, ignoring any block-level configurations.', 'simpletoc' ) . '</p><p>' . |
| 137 | + esc_html__( 'Think about making a donation if you use any of these features.', 'simpletoc' ) . ' ' . |
| 138 | + $donatelink . |
| 139 | + '</p>'; |
125 | 140 | } |
126 | 141 |
|
127 | | -function simpletoc_wrapper_enabled_callback() |
128 | | -{ |
129 | | - $wrapper_enabled = get_option('simpletoc_wrapper_enabled', false); |
130 | | - |
131 | | - if (has_filter('simpletoc_wrapper_enabled')) { |
132 | | - echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" checked="checked" disabled="disabled" />'; |
133 | | - echo '<label for="simpletoc_wrapper_enabled" class="description">' . __('Setting controlled by "simpletoc_wrapper_enabled" filter. Remove filter to adjust setting.', 'simpletoc') . '</label>'; |
134 | | - } else { |
135 | | - echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" ' . checked(1, $wrapper_enabled, false) . ' />'; |
136 | | - echo '<label for="simpletoc_wrapper_enabled" class="description">' . __('Additionally adds the role "navigation" and ARIA attributes.', 'simpletoc') . '</label>'; |
137 | | - } |
| 142 | +/** |
| 143 | + * SimpleTOC wrapper enabled callback. |
| 144 | + */ |
| 145 | +function simpletoc_wrapper_enabled_callback() { |
| 146 | + $wrapper_enabled = get_option( 'simpletoc_wrapper_enabled', false ); |
| 147 | + |
| 148 | + if ( has_filter( 'simpletoc_wrapper_enabled' ) ) { |
| 149 | + echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" checked="checked" disabled="disabled" />'; |
| 150 | + echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Setting controlled by "simpletoc_wrapper_enabled" filter. Remove filter to adjust setting.', 'simpletoc' ) . '</label>'; |
| 151 | + } else { |
| 152 | + echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" ' . checked( 1, $wrapper_enabled, false ) . ' />'; |
| 153 | + echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Additionally adds the role "navigation" and ARIA attributes.', 'simpletoc' ) . '</label>'; |
| 154 | + } |
138 | 155 | } |
139 | 156 |
|
140 | | -function simpletoc_accordion_enabled_callback() |
141 | | -{ |
142 | | - $accordion_enabled = get_option('simpletoc_accordion_enabled', false); |
143 | | - if ($accordion_enabled) { |
144 | | - update_option('simpletoc_wrapper_enabled', true); |
145 | | - } |
146 | | - echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked(1, $accordion_enabled, false) . ' />'; |
147 | | - echo '<label for="simpletoc_accordion_enabled" class="description">' . __('Adds minimal JavaScript and css styles.', 'simpletoc') . ' <strong>' . __('Notice:', 'simpletoc') . '</strong> ' . __('This will automatically enable the wrapper div.', 'simpletoc') . '</label>'; |
| 157 | +/** |
| 158 | + * SimpleTOC accordion enabled callback. |
| 159 | + */ |
| 160 | +function simpletoc_accordion_enabled_callback() { |
| 161 | + $accordion_enabled = get_option( 'simpletoc_accordion_enabled', false ); |
| 162 | + if ( $accordion_enabled ) { |
| 163 | + update_option( 'simpletoc_wrapper_enabled', true ); |
| 164 | + } |
| 165 | + echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked( 1, $accordion_enabled, false ) . ' />'; |
| 166 | + 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>'; |
148 | 167 | } |
149 | 168 |
|
150 | | -function simpletoc_smooth_enabled_callback() |
151 | | -{ |
152 | | - $smooth_enabled = get_option('simpletoc_smooth_enabled', false); |
153 | | - echo '<input type="checkbox" name="simpletoc_smooth_enabled" id="simpletoc_smooth_enabled" value="1" ' . checked(1, $smooth_enabled, false) . ' />'; |
154 | | - echo '<label for="simpletoc_smooth_enabled" class="description">' . __('Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc') . '</label>'; |
| 169 | +/** |
| 170 | + * SimpleTOC smooth enabled callback. |
| 171 | + */ |
| 172 | +function simpletoc_smooth_enabled_callback() { |
| 173 | + $smooth_enabled = get_option( 'simpletoc_smooth_enabled', false ); |
| 174 | + echo '<input type="checkbox" name="simpletoc_smooth_enabled" id="simpletoc_smooth_enabled" value="1" ' . checked( 1, $smooth_enabled, false ) . ' />'; |
| 175 | + echo '<label for="simpletoc_smooth_enabled" class="description">' . esc_html__( 'Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc' ) . '</label>'; |
155 | 176 | } |
156 | 177 |
|
157 | | -function simpletoc_absolute_urls_enabled_callback() |
158 | | -{ |
159 | | - $absolute_urls_enabled = get_option('simpletoc_absolute_urls_enabled', false); |
160 | | - echo '<input type="checkbox" name="simpletoc_absolute_urls_enabled" id="simpletoc_absolute_urls_enabled" value="1" ' . checked(1, $absolute_urls_enabled, false) . ' />'; |
161 | | - echo '<label for="simpletoc_absolute_urls_enabled" class="description">' . __('Adds the permalink url to the fragment.', 'simpletoc') . '</label>'; |
| 178 | +/** |
| 179 | + * SimpleTOC absolute urls enabled callback. |
| 180 | + */ |
| 181 | +function simpletoc_absolute_urls_enabled_callback() { |
| 182 | + $absolute_urls_enabled = get_option( 'simpletoc_absolute_urls_enabled', false ); |
| 183 | + echo '<input type="checkbox" name="simpletoc_absolute_urls_enabled" id="simpletoc_absolute_urls_enabled" value="1" ' . checked( 1, $absolute_urls_enabled, false ) . ' />'; |
| 184 | + echo '<label for="simpletoc_absolute_urls_enabled" class="description">' . esc_html__( 'Adds the permalink url to the fragment.', 'simpletoc' ) . '</label>'; |
162 | 185 | } |
163 | 186 |
|
164 | | -function simpletoc_autoupdate_enabled_callback() |
165 | | -{ |
166 | | - $autoupdate_enabled = get_option('simpletoc_autoupdate_enabled', false); |
167 | | - echo '<input type="checkbox" name="simpletoc_autoupdate_enabled" id="simpletoc_autoupdate_enabled" value="1" ' . checked(1, $autoupdate_enabled, false) . ' />'; |
168 | | - echo '<label for="simpletoc_autoupdate_enabled" class="description">' . __('Deactivate the automatic table of contents refresh feature.', 'simpletoc') . '</label>'; |
| 187 | +/** |
| 188 | + * SimpleTOC autoupdate enabled callback. |
| 189 | + */ |
| 190 | +function simpletoc_autoupdate_enabled_callback() { |
| 191 | + $autoupdate_enabled = get_option( 'simpletoc_autoupdate_enabled', false ); |
| 192 | + echo '<input type="checkbox" name="simpletoc_autoupdate_enabled" id="simpletoc_autoupdate_enabled" value="1" ' . checked( 1, $autoupdate_enabled, false ) . ' />'; |
| 193 | + echo '<label for="simpletoc_autoupdate_enabled" class="description">' . esc_html__( 'Deactivate the automatic table of contents refresh feature.', 'simpletoc' ) . '</label>'; |
169 | 194 | } |
0 commit comments