| @@ -7,8 +7,17 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace MToensing\SimpleTOC; |
| 9 | 9 | |
| 10 | 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 | +/** | |
| 11 | 20 | * Add SimpleTOC global settings page. |
| 12 | 21 | */ |
| 13 | 22 | function simpletoc_add_settings_page() { |
| 14 | 23 | add_options_page( |
| @@ -47,9 +56,8 @@ | ||
| 47 | 56 | * Register SimpleTOC settings. |
| 48 | 57 | */ |
| 49 | 58 | function simpletoc_register_settings() { |
| 50 | 59 | // Register settings and filters for existing features. |
| 51 | - $wrapper_enabled_filter = apply_filters( 'simpletoc_wrapper_enabled', null ); | |
| 52 | 60 | $accordion_enabled_filter = apply_filters( 'simpletoc_accordion_enabled', null ); |
| 53 | 61 | $smooth_enabled_filter = apply_filters( 'simpletoc_smooth_enabled', null ); |
| 54 | 62 | $absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null ); |
| 55 | 63 | $autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null ); |
| @@ -54,12 +62,8 @@ | ||
| 54 | 62 | $absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null ); |
| 55 | 63 | $autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null ); |
| 56 | 64 | $box_style_enabled_filter = apply_filters( 'simpletoc_box_style_enabled', null ); |
| 57 | 65 | |
| 58 | - if ( null === $wrapper_enabled_filter ) { | |
| 59 | - register_setting( 'simpletoc_settings', 'simpletoc_wrapper_enabled' ); | |
| 60 | - } | |
| 61 | - | |
| 62 | 66 | if ( null === $accordion_enabled_filter ) { |
| 63 | 67 | register_setting( 'simpletoc_settings', 'simpletoc_accordion_enabled' ); |
| 64 | 68 | } |
| 65 | 69 | |
| @@ -78,8 +82,20 @@ | ||
| 78 | 82 | if ( null === $box_style_enabled_filter ) { |
| 79 | 83 | register_setting( 'simpletoc_settings', 'simpletoc_box_style_enabled' ); |
| 80 | 84 | } |
| 81 | 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 | + | |
| 82 | 98 | // Add settings sections and fields. |
| 83 | 99 | add_settings_section( |
| 84 | 100 | 'simpletoc_wrapper_section', |
| 85 | 101 | esc_html__( 'Global settings', 'simpletoc' ), |
| @@ -95,16 +111,8 @@ | ||
| 95 | 111 | 'simpletoc_wrapper_section' |
| 96 | 112 | ); |
| 97 | 113 | |
| 98 | 114 | add_settings_field( |
| 99 | - 'simpletoc_wrapper_enabled', | |
| 100 | - esc_html__( 'Force wrapper div', 'simpletoc' ), | |
| 101 | - __NAMESPACE__ . '\simpletoc_wrapper_enabled_callback', | |
| 102 | - 'simpletoc', | |
| 103 | - 'simpletoc_wrapper_section' | |
| 104 | - ); | |
| 105 | - | |
| 106 | - add_settings_field( | |
| 107 | 115 | 'simpletoc_smooth_enabled', |
| 108 | 116 | esc_html__( 'Force smooth scrolling', 'simpletoc' ), |
| 109 | 117 | __NAMESPACE__ . '\simpletoc_smooth_enabled_callback', |
| 110 | 118 | 'simpletoc', |
| @@ -128,8 +136,16 @@ | ||
| 128 | 136 | 'simpletoc_wrapper_section' |
| 129 | 137 | ); |
| 130 | 138 | |
| 131 | 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( | |
| 132 | 148 | 'simpletoc_box_style_enabled', |
| 133 | 149 | esc_html__( 'Force box style', 'simpletoc' ), |
| 134 | 150 | __NAMESPACE__ . '\simpletoc_box_style_enabled_callback', |
| 135 | 151 | 'simpletoc', |
| @@ -152,32 +168,14 @@ | ||
| 152 | 168 | '</p>'; |
| 153 | 169 | } |
| 154 | 170 | |
| 155 | 171 | /** |
| 156 | - * SimpleTOC wrapper enabled callback. | |
| 157 | - */ | |
| 158 | -function simpletoc_wrapper_enabled_callback() { | |
| 159 | - $wrapper_enabled = get_option( 'simpletoc_wrapper_enabled', false ); | |
| 160 | - | |
| 161 | - if ( has_filter( 'simpletoc_wrapper_enabled' ) ) { | |
| 162 | - echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" checked="checked" disabled="disabled" />'; | |
| 163 | - echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Setting controlled by "simpletoc_wrapper_enabled" filter. Remove filter to adjust setting.', 'simpletoc' ) . '</label>'; | |
| 164 | - } else { | |
| 165 | - echo '<input type="checkbox" name="simpletoc_wrapper_enabled" id="simpletoc_wrapper_enabled" value="1" ' . checked( 1, $wrapper_enabled, false ) . ' />'; | |
| 166 | - echo '<label for="simpletoc_wrapper_enabled" class="description">' . esc_html__( 'Additionally adds the role "navigation" and ARIA attributes.', 'simpletoc' ) . '</label>'; | |
| 167 | - } | |
| 168 | -} | |
| 169 | - | |
| 170 | -/** | |
| 171 | 172 | * SimpleTOC accordion enabled callback. |
| 172 | 173 | */ |
| 173 | 174 | function simpletoc_accordion_enabled_callback() { |
| 174 | 175 | $accordion_enabled = get_option( 'simpletoc_accordion_enabled', false ); |
| 175 | - if ( $accordion_enabled ) { | |
| 176 | - update_option( 'simpletoc_wrapper_enabled', true ); | |
| 177 | - } | |
| 178 | 176 | echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked( 1, $accordion_enabled, false ) . ' />'; |
| 179 | - 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>'; | |
| 177 | + echo '<label for="simpletoc_accordion_enabled" class="description">' . esc_html__( 'Adds minimal JavaScript and css styles.', 'simpletoc' ) . '</label>'; | |
| 180 | 178 | } |
| 181 | 179 | |
| 182 | 180 | /** |
| 183 | 181 | * SimpleTOC smooth enabled callback. |
| @@ -216,7 +214,19 @@ | ||
| 216 | 214 | echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" checked="checked" disabled="disabled" />'; |
| 217 | 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>'; |
| 218 | 216 | } else { |
| 219 | 217 | echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" ' . checked( 1, $box_style_enabled, false ) . ' />'; |
| 220 | - echo '<label for="simpletoc_box_style_enabled" class="description">' . esc_html__( 'Enables the box style for all SimpleTOC blocks with the default gray background. Wrapper markup is added automatically.', 'simpletoc' ) . '</label>'; | |
| 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>'; | |
| 221 | 231 | } |
| 222 | 232 | } |