| 1 |
<?php |
| 2 |
/** |
| 3 |
* SimpleTOC admin settings page. |
| 4 |
* |
| 5 |
* @package simpletoc |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MToensing\SimpleTOC; |
| 9 |
|
| 10 |
/** |
| 11 |
* Add SimpleTOC global settings page. |
| 12 |
*/ |
| 13 |
function simpletoc_add_settings_page() { |
| 14 |
add_options_page( |
| 15 |
__( 'SimpleTOC Settings', 'simpletoc' ), |
| 16 |
__( 'SimpleTOC', 'simpletoc' ), |
| 17 |
'manage_options', |
| 18 |
'simpletoc', |
| 19 |
__NAMESPACE__ . '\simpletoc_settings_page' |
| 20 |
); |
| 21 |
} |
| 22 |
add_action( 'admin_menu', __NAMESPACE__ . '\simpletoc_add_settings_page' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* SimpleTOC settings page content. |
| 26 |
*/ |
| 27 |
function simpletoc_settings_page() { |
| 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 |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register SimpleTOC settings. |
| 48 |
*/ |
| 49 |
function simpletoc_register_settings() { |
| 50 |
// Register settings and filters for existing features. |
| 51 |
$accordion_enabled_filter = apply_filters( 'simpletoc_accordion_enabled', null ); |
| 52 |
$smooth_enabled_filter = apply_filters( 'simpletoc_smooth_enabled', null ); |
| 53 |
$absolute_urls_enabled_filter = apply_filters( 'simpletoc_absolute_urls_enabled', null ); |
| 54 |
$autoupdate_enabled_filter = apply_filters( 'simpletoc_autoupdate_enabled', null ); |
| 55 |
$box_style_enabled_filter = apply_filters( 'simpletoc_box_style_enabled', null ); |
| 56 |
|
| 57 |
if ( null === $accordion_enabled_filter ) { |
| 58 |
register_setting( 'simpletoc_settings', 'simpletoc_accordion_enabled' ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( null === $smooth_enabled_filter ) { |
| 62 |
register_setting( 'simpletoc_settings', 'simpletoc_smooth_enabled' ); |
| 63 |
} |
| 64 |
|
| 65 |
if ( null === $absolute_urls_enabled_filter ) { |
| 66 |
register_setting( 'simpletoc_settings', 'simpletoc_absolute_urls_enabled' ); |
| 67 |
} |
| 68 |
|
| 69 |
if ( null === $autoupdate_enabled_filter ) { |
| 70 |
register_setting( 'simpletoc_settings', 'simpletoc_autoupdate_enabled', array( 'show_in_rest' => true ) ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( null === $box_style_enabled_filter ) { |
| 74 |
register_setting( 'simpletoc_settings', 'simpletoc_box_style_enabled' ); |
| 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_smooth_enabled', |
| 95 |
esc_html__( 'Force smooth scrolling', 'simpletoc' ), |
| 96 |
__NAMESPACE__ . '\simpletoc_smooth_enabled_callback', |
| 97 |
'simpletoc', |
| 98 |
'simpletoc_wrapper_section' |
| 99 |
); |
| 100 |
|
| 101 |
add_settings_field( |
| 102 |
'simpletoc_absolute_urls_enabled', |
| 103 |
esc_html__( 'Force absolute urls', 'simpletoc' ), |
| 104 |
__NAMESPACE__ . '\simpletoc_absolute_urls_enabled_callback', |
| 105 |
'simpletoc', |
| 106 |
'simpletoc_wrapper_section' |
| 107 |
); |
| 108 |
|
| 109 |
// Add the autoupdate settings field. |
| 110 |
add_settings_field( |
| 111 |
'simpletoc_autoupdate_enabled', |
| 112 |
esc_html__( 'Force no auto refresh', 'simpletoc' ), |
| 113 |
__NAMESPACE__ . '\simpletoc_autoupdate_enabled_callback', |
| 114 |
'simpletoc', |
| 115 |
'simpletoc_wrapper_section' |
| 116 |
); |
| 117 |
|
| 118 |
add_settings_field( |
| 119 |
'simpletoc_box_style_enabled', |
| 120 |
esc_html__( 'Force box style', 'simpletoc' ), |
| 121 |
__NAMESPACE__ . '\simpletoc_box_style_enabled_callback', |
| 122 |
'simpletoc', |
| 123 |
'simpletoc_wrapper_section' |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
add_action( 'admin_init', __NAMESPACE__ . '\simpletoc_register_settings' ); |
| 128 |
|
| 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>'; |
| 134 |
|
| 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>'; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* SimpleTOC accordion enabled callback. |
| 144 |
*/ |
| 145 |
function simpletoc_accordion_enabled_callback() { |
| 146 |
$accordion_enabled = get_option( 'simpletoc_accordion_enabled', false ); |
| 147 |
echo '<input type="checkbox" name="simpletoc_accordion_enabled" id="simpletoc_accordion_enabled" value="1" ' . checked( 1, $accordion_enabled, false ) . ' />'; |
| 148 |
echo '<label for="simpletoc_accordion_enabled" class="description">' . esc_html__( 'Adds minimal JavaScript and css styles.', 'simpletoc' ) . '</label>'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* SimpleTOC smooth enabled callback. |
| 153 |
*/ |
| 154 |
function simpletoc_smooth_enabled_callback() { |
| 155 |
$smooth_enabled = get_option( 'simpletoc_smooth_enabled', false ); |
| 156 |
echo '<input type="checkbox" name="simpletoc_smooth_enabled" id="simpletoc_smooth_enabled" value="1" ' . checked( 1, $smooth_enabled, false ) . ' />'; |
| 157 |
echo '<label for="simpletoc_smooth_enabled" class="description">' . esc_html__( 'Adds the following CSS to the HTML element: "scroll-behavior: smooth;"', 'simpletoc' ) . '</label>'; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* SimpleTOC absolute urls enabled callback. |
| 162 |
*/ |
| 163 |
function simpletoc_absolute_urls_enabled_callback() { |
| 164 |
$absolute_urls_enabled = get_option( 'simpletoc_absolute_urls_enabled', false ); |
| 165 |
echo '<input type="checkbox" name="simpletoc_absolute_urls_enabled" id="simpletoc_absolute_urls_enabled" value="1" ' . checked( 1, $absolute_urls_enabled, false ) . ' />'; |
| 166 |
echo '<label for="simpletoc_absolute_urls_enabled" class="description">' . esc_html__( 'Adds the permalink url to the fragment.', 'simpletoc' ) . '</label>'; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* SimpleTOC autoupdate enabled callback. |
| 171 |
*/ |
| 172 |
function simpletoc_autoupdate_enabled_callback() { |
| 173 |
$autoupdate_enabled = get_option( 'simpletoc_autoupdate_enabled', false ); |
| 174 |
echo '<input type="checkbox" name="simpletoc_autoupdate_enabled" id="simpletoc_autoupdate_enabled" value="1" ' . checked( 1, $autoupdate_enabled, false ) . ' />'; |
| 175 |
echo '<label for="simpletoc_autoupdate_enabled" class="description">' . esc_html__( 'Deactivate the automatic table of contents refresh feature.', 'simpletoc' ) . '</label>'; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* SimpleTOC box style enabled callback. |
| 180 |
*/ |
| 181 |
function simpletoc_box_style_enabled_callback() { |
| 182 |
$box_style_enabled = get_option( 'simpletoc_box_style_enabled', false ); |
| 183 |
|
| 184 |
if ( has_filter( 'simpletoc_box_style_enabled' ) ) { |
| 185 |
echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" checked="checked" disabled="disabled" />'; |
| 186 |
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>'; |
| 187 |
} else { |
| 188 |
echo '<input type="checkbox" name="simpletoc_box_style_enabled" id="simpletoc_box_style_enabled" value="1" ' . checked( 1, $box_style_enabled, false ) . ' />'; |
| 189 |
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>'; |
| 190 |
} |
| 191 |
} |
| 192 |
|