Skip to content

Commit

Permalink
Added automatic refresh option
Browse files Browse the repository at this point in the history
  • Loading branch information
mtoensing committed Jan 31, 2024
1 parent 62674c7 commit 2b10baa
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
Binary file modified .wordpress-org/screenshot-9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-server-side-render'), 'version' => 'de37826b0b827cb86a2d');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-primitives', 'wp-server-side-render'), 'version' => '83a72e27f0da141aa26b');
2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SimpleTOC is open-source and developed on [GitHub Pages](https://github.com/mtoe
* Choose between an ordered, bullet HTML list. Or indent the list.
* Select a heading level or turn it into a paragraph.
* Disable the h2 heading of the TOC block and add your own.
* Configure many options globally in WordPress SimpleTOC settings.

= Compatibility =

Expand All @@ -52,7 +53,8 @@ SimpleTOC is open-source and developed on [GitHub Pages](https://github.com/mtoe
== Changelog ==

= 6.3.0 =
* Added: Option for autoupdates. This can be disabled in the blocks advanced settings.
* Added: Option for automatic refresh. This can be disabled in the blocks advanced settings.
* Added: Option to globally disable automatic refresh in the WordPress SimpleTOC settings.

= 6.2.0 =
* Added: Implemented smooth animation for improved user interaction in the accordion menu.
Expand Down
54 changes: 39 additions & 15 deletions simpletoc-admin-settings.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<?php

// Add SimpleTOC global settings page
function simpletoc_add_settings_page()
{
function simpletoc_add_settings_page() {
add_options_page(
__('SimpleTOC Settings', 'simpletoc'),
__('SimpleTOC', 'simpletoc'),
Expand All @@ -14,8 +13,7 @@ function simpletoc_add_settings_page()
add_action('admin_menu', 'simpletoc_add_settings_page');

// SimpleTOC settings page content
function simpletoc_settings_page()
{
function simpletoc_settings_page() {
if (!current_user_can('manage_options')) {
return;
}
Expand All @@ -26,26 +24,22 @@ function simpletoc_settings_page()
<form method="post" action="options.php">
<?php
settings_fields('simpletoc_settings');
do_settings_sections('simpletoc');
submit_button();
?>
do_settings_sections('simpletoc');
submit_button();
?>
</form>
</div>
<?php
<?php
}

// Register SimpleTOC settings
function simpletoc_register_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);

if ($absolute_urls_enabled_filter === null) {
register_setting('simpletoc_settings', 'simpletoc_absolute_urls_enabled');
}

if ($wrapper_enabled_filter === null) {
register_setting('simpletoc_settings', 'simpletoc_wrapper_enabled');
}
Expand All @@ -58,6 +52,21 @@ function simpletoc_register_settings()
register_setting('simpletoc_settings', 'simpletoc_smooth_enabled');
}

if ($absolute_urls_enabled_filter === null) {
register_setting('simpletoc_settings', 'simpletoc_absolute_urls_enabled');
}

// Register setting for autoupdate feature
$autoupdate_enabled_filter = apply_filters('simpletoc_autoupdate_enabled', null);
if ($autoupdate_enabled_filter === null) {
$defaults = array(
'show_in_rest' => true,
'default' => true
);
register_setting('simpletoc_settings', 'simpletoc_autoupdate_enabled', $defaults );
}

// Add settings sections and fields
add_settings_section(
'simpletoc_wrapper_section',
__('Global settings', 'simpletoc'),
Expand All @@ -81,7 +90,6 @@ function simpletoc_register_settings()
'simpletoc_wrapper_section'
);


add_settings_field(
'simpletoc_smooth_enabled',
__('Force smooth scrolling', 'simpletoc'),
Expand All @@ -97,6 +105,15 @@ function simpletoc_register_settings()
'simpletoc',
'simpletoc_wrapper_section'
);

// Add the autoupdate settings field
add_settings_field(
'simpletoc_autoupdate_enabled',
__('Enable TOC Autoupdate', 'simpletoc'),
'simpletoc_autoupdate_enabled_callback',
'simpletoc',
'simpletoc_wrapper_section'
);
}

add_action('admin_init', 'simpletoc_register_settings');
Expand Down Expand Up @@ -148,3 +165,10 @@ function simpletoc_absolute_urls_enabled_callback()
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">' . __('Adds the permalink url to the fragment.', 'simpletoc') . '</label>';
}

function simpletoc_autoupdate_enabled_callback()
{
$autoupdate_enabled = get_option('simpletoc_autoupdate_enabled', true);
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">' . __('Automatic updating of the table of contents.', 'simpletoc') . '</label>';
}
31 changes: 21 additions & 10 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ import './accordion.css';
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();

// Get the current option value.
const autoupdateOption = useSelect( ( select ) => {
const optionValue =
select( 'core' ).getSite()?.simpletoc_autoupdate_enabled;
return Boolean( optionValue ); // or use !!optionValue
}, [] );

const { autoupdate } = attributes;

const { returnisSaving, returnisSavingNonPostEntityChanges } = useSelect(
Expand Down Expand Up @@ -96,13 +103,16 @@ export default function Edit( { attributes, setAttributes } ) {
setAttributes( { remove_indent: false } );
} }
/>
{ ! attributes.autoupdate && (
<ToolbarButton
icon={ update }
label={ __( 'Update table of contents', 'simpletoc' ) }
onClick={ () => setAttributes( { updated: Date.now() } ) }
/>
) }
{ ! attributes.autoupdate ||
( ! autoupdateOption && (
<ToolbarButton
icon={ update }
label={ __( 'Update table of contents', 'simpletoc' ) }
onClick={ () =>
setAttributes( { updated: Date.now() } )
}
/>
) ) }
</BlockControls>
);

Expand Down Expand Up @@ -348,9 +358,9 @@ export default function Edit( { attributes, setAttributes } ) {
</PanelRow>
<PanelRow>
<ToggleControl
label={ __( 'Autoupdate', 'simpletoc' ) }
label={ __( 'Automatic refresh', 'simpletoc' ) }
help={ __(
'Enables the auto refresh of the toc.',
'Automatic updating of the table of contents.',
'simpletoc'
) }
checked={ attributes.autoupdate }
Expand All @@ -371,7 +381,8 @@ export default function Edit( { attributes, setAttributes } ) {
{ controls }
{ controlssidebar }
{ /* Conditional rendering based on autoupdate attribute */ }
{ autoupdate &&
{ autoupdateOption &&
autoupdate &&
( returnisSaving || returnisSavingNonPostEntityChanges ) ? (
<Spinner />
) : (
Expand Down

0 comments on commit 2b10baa

Please sign in to comment.