Skip to content

Commit

Permalink
ADD: cache controls
Browse files Browse the repository at this point in the history
  • Loading branch information
ihslimn committed Sep 6, 2024
1 parent 55f67bf commit 0522a44
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
12 changes: 12 additions & 0 deletions additional-block-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ public function add_attributes( $block ) {
$block->add_attribute( 'data-update-field-is-empty', 'true' );
}

if ( ! empty( $attrs['jfb_update_fields_cache_enabled'] ) && ! empty( $attrs['jfb_update_fields_cache_timeout'] ) ) {
$block->add_attribute( 'data-update-cache-timeout', $attrs['jfb_update_fields_cache_timeout'] );
}

$cache_timeout = $attrs['jfb_update_fields_cache_timeout'] ?? 60;

if ( isset( $attrs['jfb_update_fields_cache_enabled'] ) && $attrs['jfb_update_fields_cache_enabled'] === false ) {
$cache_timeout = 0;
}

$block->add_attribute( 'data-update-cache-timeout', $cache_timeout );

if ( ! $this->script_enqueued ) {
$this->enqueue_script();
$this->script_enqueued = true;
Expand Down
2 changes: 1 addition & 1 deletion assets/js/blocks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions assets/js/frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
const formId = observable.form ? observable.form.getFormId() : observable.parent.root.form.getFormId(),
formFields = getFormValues( observable ),
updatedCalculated = observable.rootNode.querySelectorAll( `[data-formula*=${updated}]` ),
cacheTime = updatedNode.dataset.cacheTime || 60;
cacheTime = updatedNode.dataset.updateCacheTimeout || 60;

if ( aborters[ updated + formId ] ) {
aborters[ updated + formId ].abort();
Expand All @@ -310,8 +310,9 @@
if ( cache.has( hash ) ) {
const cached = cache.get( hash );

if ( + Date.now() - + cached.time < cacheTime * 1000 ) {
if ( + Date.now() - + cached.time < cacheTime * 1000 || cacheTime < 0 ) {
const response = cached.response;

updateFieldFromResponse(
{
response,
Expand Down
21 changes: 20 additions & 1 deletion assets/js/src/attributes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { FIELD_TO_LISTEN, LISTEN_ALL, OPTIONS_LISTENER_ENABLED, VALUE_LISTENER_ENABLED, SUPPORTED_BLOCKS, CALLBACK, UPDATE_ON_BUTTON, BUTTON_NAME } from './constants';
import {
FIELD_TO_LISTEN,
LISTEN_ALL,
OPTIONS_LISTENER_ENABLED,
VALUE_LISTENER_ENABLED,
SUPPORTED_BLOCKS,
CALLBACK,
UPDATE_ON_BUTTON,
BUTTON_NAME,
CACHE_ENABLED,
CACHE_TIMEOUT,
} from './constants';

function registerAttributes( settings, name ) {

Expand Down Expand Up @@ -36,6 +47,14 @@ function registerAttributes( settings, name ) {
type: 'string',
default: '',
},
[ CACHE_ENABLED ]: {
type: 'boolean',
default: true,
},
[ CACHE_TIMEOUT ]: {
type: 'string',
default: '60',
},
};

return settings;
Expand Down
4 changes: 4 additions & 0 deletions assets/js/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const BUTTON_NAME = 'jfb_update_fields_button_name';
const FIELD_TO_LISTEN = 'jfb_update_fields_field_to_listen';
const CALLBACK = 'jfb_update_fields_callback';
const LISTEN_ALL = 'jfb_update_fields_listen_all';
const CACHE_ENABLED = 'jfb_update_fields_cache_enabled';
const CACHE_TIMEOUT = 'jfb_update_fields_cache_timeout';
const SUPPORTED_BLOCKS = {
'jet-forms/select-field' : 'options',
'jet-forms/radio-field' : 'options',
Expand All @@ -25,4 +27,6 @@ export {
LISTEN_ALL,
SUPPORTED_BLOCKS,
CALLBACK,
CACHE_ENABLED,
CACHE_TIMEOUT,
};
45 changes: 43 additions & 2 deletions assets/js/src/controls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { FIELD_TO_LISTEN, UPDATE_ON_BUTTON, LISTEN_ALL, OPTIONS_LISTENER_ENABLED, VALUE_LISTENER_ENABLED, CALLBACK, BUTTON_NAME } from './constants';
import { SUPPORTED_BLOCKS } from './constants';
import {
SUPPORTED_BLOCKS,
FIELD_TO_LISTEN,
UPDATE_ON_BUTTON,
LISTEN_ALL,
OPTIONS_LISTENER_ENABLED,
VALUE_LISTENER_ENABLED,
CALLBACK,
BUTTON_NAME,
CACHE_ENABLED,
CACHE_TIMEOUT,
} from './constants';

const { addFilter } = wp.hooks;
const { createHigherOrderComponent } = wp.compose;
Expand Down Expand Up @@ -147,6 +157,37 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => {
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) &&
<PanelRow>
<ToggleControl
label="Enable cache"
help={
attributes[ CACHE_ENABLED ]
? 'Yes.'
: 'No.'
}
checked={ attributes[ CACHE_ENABLED ] }
onChange={ () => {
setAttributes( { [ CACHE_ENABLED ] : ! attributes[ CACHE_ENABLED ] } );
} }
/>
</PanelRow>
}
{ ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && attributes[ CACHE_ENABLED ] &&
<PanelRow>
<TextControl
type="number"
label="Cache timeout"
help="Cache timeout in seconds; -1 for unlimited cache time. Cache is cleared on page reload."
min="-1"
max="86400"
value={ attributes[ CACHE_TIMEOUT ] }
onChange={ newValue => {
setAttributes( { [ CACHE_TIMEOUT ] : newValue } );
} }
/>
</PanelRow>
}
</PanelBody>
}
</Panel>
Expand Down
4 changes: 2 additions & 2 deletions jet-form-builder-update-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: JetFormBuilder - Update Fields
* Plugin URI:
* Description:
* Version: 1.0.1
* Version: 1.1.1
* Author:
* Author URI:
* Text Domain:
Expand Down Expand Up @@ -33,7 +33,7 @@ class Plugin {

public $storage = null;

private $version = '1.1.0';
private $version = '1.1.1';

public function __construct() {
add_action( 'plugins_loaded', array( $this, 'jec_init' ) );
Expand Down

0 comments on commit 0522a44

Please sign in to comment.