diff --git a/assets/js/blocks.js b/assets/js/blocks.js index d11966e..4f3f107 100644 --- a/assets/js/blocks.js +++ b/assets/js/blocks.js @@ -1 +1 @@ -(()=>{"use strict";var e="jfb_update_fields_options_enabled",t="jfb_update_fields_value_enabled",n="jfb_update_fields_update_on_button",l="jfb_update_fields_button_name",r="jfb_update_fields_field_to_listen",o="jfb_update_fields_callback",a="jfb_update_fields_listen_all",i="jfb_update_fields_cache_enabled",u="jfb_update_fields_cache_timeout",c={"jet-forms/select-field":"options","jet-forms/radio-field":"options","jet-forms/checkbox-field":"options","jet-forms/text-field":"value","jet-forms/number-field":"value","jet-forms/textarea-field":"value","jet-forms/hidden-field":"value","jet-forms/submit-field":"update_other"};function p(e){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p(e)}function f(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);t&&(l=l.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,l)}return n}function m(e){for(var t=1;t{"use strict";var e="jfb_update_fields_options_enabled",t="jfb_update_fields_value_enabled",n="jfb_update_fields_update_on_button",l="jfb_update_fields_button_name",r="jfb_update_fields_field_to_listen",o="jfb_update_fields_callback",a="jfb_update_fields_listen_all",i="jfb_update_fields_cache_enabled",u="jfb_update_fields_cache_timeout",c={"jet-forms/select-field":"options","jet-forms/radio-field":"options","jet-forms/checkbox-field":"options","jet-forms/text-field":"value","jet-forms/number-field":"value","jet-forms/textarea-field":"value","jet-forms/hidden-field":"value","jet-forms/submit-field":"update_other"};function p(e){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p(e)}function f(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);t&&(l=l.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,l)}return n}function d(e){for(var t=1;t1&&void 0!==arguments[1]?arguments[1]:"",r=n.name;if(!r)return!1;var o=c[r]||!1;if(!o)return!1;if(l&&o!==l)return!1;var a=null==n?void 0:n.attributes;return!!a&&(a[e]&&"options"===o||a[t]&&"value"===o)}function b(e){return b="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},b(e)}function y(e,t,n){return(t=function(e){var t=function(e){if("object"!=b(e)||!e)return e;var t=e[Symbol.toPrimitive];if(void 0!==t){var n=t.call(e,"string");if("object"!=b(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==b(t)?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var h=wp.hooks.addFilter,v=wp.compose.createHigherOrderComponent,w=wp.blockEditor.InspectorControls,_=wp.components,g=_.TextControl,j=_.ToggleControl,E=_.Panel,k=_.PanelRow,C=_.PanelBody,O=v((function(p){return function(f){var d=f.name,m=c[d]||!1;if(!m)return wp.element.createElement(p,f);var b=f.attributes,h=f.setAttributes,v=f.isSelected;return wp.element.createElement(React.Fragment,null,wp.element.createElement(p,f),v&&wp.element.createElement(w,null,wp.element.createElement(E,null,("update_other"!==m||"update"===(null==b?void 0:b.action_type))&&wp.element.createElement(C,{title:"Field updater",initialOpen:!1},"update_other"===m&&"update"===(null==b?void 0:b.action_type)&&wp.element.createElement(k,null,wp.element.createElement(g,{label:"Button name",help:"Latin lowercase letters, underscore, dash, numbers.",value:b[l],onChange:function(e){h(y({},l,e))}})),"options"===m&&wp.element.createElement(k,null,wp.element.createElement(j,{label:"Enable options updater",help:b[e]?"Enabled.":"Disabled.",checked:b[e],onChange:function(){h(y({},e,!b[e]))}})),"value"===m&&wp.element.createElement(k,null,wp.element.createElement(j,{label:"Enable value updater",help:b[t]?"Enabled.":"Disabled.",checked:b[t],onChange:function(){h(y({},t,!b[t]))}})),s(f)&&wp.element.createElement(k,null,wp.element.createElement(j,{label:"Update on action button",help:b[n]?"Yes.":"No.",checked:b[n],onChange:function(){h(y({},n,!b[n]))}})),s(f)&&b[n]&&wp.element.createElement(k,null,wp.element.createElement(g,{label:"Button name",help:"",value:b[l],onChange:function(e){h(y({},l,e))}})),s(f)&&!b[n]&&wp.element.createElement(k,null,wp.element.createElement(g,{label:"Fields to listen",help:"comma-separated",value:b[r],onChange:function(e){h(y({},r,e))}})),s(f)&&!b[n]&&wp.element.createElement(k,null,wp.element.createElement(j,{label:"Listen all",help:b[a]?"All listened fields have to have value for this field to be updated":"At least one field has to have value for this field to be updated",checked:b[a],onChange:function(){h(y({},a,!b[a]))}})),s(f,"value")&&b[t]&&wp.element.createElement(k,null,wp.element.createElement(g,{label:"Callback or query parameters",value:b[o],help:"Callback which parameters are $field_name (this field name), $form_id (this form ID), $form_fields (array of all form fields). Alternatively JetEngine query_id|property to get a specified propery from the first object from query.",onChange:function(e){h(y({},o,e))}})),s(f)&&wp.element.createElement(k,null,wp.element.createElement(j,{label:"Enable cache",help:b[i]?"Yes.":"No.",checked:b[i],onChange:function(){h(y({},i,!b[i]))}})),s(f)&&b[i]&&wp.element.createElement(k,null,wp.element.createElement(g,{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:b[u],onChange:function(e){h(y({},u,e))}}))))))}}),"addControls");h("editor.BlockEdit","jet-form-builder/update-fields",O),wp.blocks.registerBlockType,wp.components.TextControl,wp.element.createElement;var P=wp.blocks.registerBlockVariation,S={name:"update",title:"Update Field",isActive:["action_type"],description:"Update fields that are dependent on this button",icon:"update-alt",scope:["block","inserter","transform"],attributes:{action_type:"update"}};const B=function(){P("jet-forms/submit-field",S)};var F=wp.hooks.addFilter;F("blocks.registerBlockType","jfb-update-field/block-attributes",(function(p,f){return c[f]?(p.attributes=d(d({},p.attributes),{},m(m(m(m(m(m(m(m(m({},e,{type:"boolean",default:!1}),t,{type:"boolean",default:!1}),r,{type:"string",default:""}),a,{type:"boolean",default:!1}),o,{type:"string",default:""}),n,{type:"boolean",default:!1}),l,{type:"string",default:""}),i,{type:"boolean",default:!0}),u,{type:"string",default:"60"})),p):p})),window.isJFBUF_Button_Registered=!1,F("jet.fb.register.fields.handler","jfb-update-field/block-attributes",(function(e){return window.isJFBUF_Button_Registered||(window.isJFBUF_Button_Registered,B()),e})),document.addEventListener("jet-form-builder-initialized",(function(){B()}))})(); \ No newline at end of file diff --git a/assets/js/src/constants.js b/assets/js/src/constants.js index d18d743..8668df0 100644 --- a/assets/js/src/constants.js +++ b/assets/js/src/constants.js @@ -29,4 +29,4 @@ export { CALLBACK, CACHE_ENABLED, CACHE_TIMEOUT, -}; \ No newline at end of file +}; diff --git a/assets/js/src/controls.js b/assets/js/src/controls.js index fdeb4db..a723c66 100644 --- a/assets/js/src/controls.js +++ b/assets/js/src/controls.js @@ -11,6 +11,8 @@ import { CACHE_TIMEOUT, } from './constants'; +import { isUpdaterEnabled } from './functions'; + const { addFilter } = wp.hooks; const { createHigherOrderComponent } = wp.compose; @@ -89,7 +91,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && + { ( isUpdaterEnabled( props ) ) && { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && attributes[ UPDATE_ON_BUTTON ] && + { ( isUpdaterEnabled( props ) ) && attributes[ UPDATE_ON_BUTTON ] && { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && ! attributes[ UPDATE_ON_BUTTON ] && + { ( isUpdaterEnabled( props ) ) && ! attributes[ UPDATE_ON_BUTTON ] && { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && ! attributes[ UPDATE_ON_BUTTON ] && + { ( isUpdaterEnabled( props ) ) && ! attributes[ UPDATE_ON_BUTTON ] && { @@ -145,7 +147,7 @@ const addControls = createHigherOrderComponent( ( BlockEdit ) => { /> } - { supportType === 'value' && attributes[ VALUE_LISTENER_ENABLED ] && + { isUpdaterEnabled( props, 'value' ) && attributes[ VALUE_LISTENER_ENABLED ] && { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && + { ( isUpdaterEnabled( props ) ) && { /> } - { ( attributes[ OPTIONS_LISTENER_ENABLED ] || attributes[ VALUE_LISTENER_ENABLED ] ) && attributes[ CACHE_ENABLED ] && + { ( isUpdaterEnabled( props ) ) && attributes[ CACHE_ENABLED ] && 'options', + 'jet-forms/radio-field' => 'options', + 'jet-forms/checkbox-field' => 'options', + 'jet-forms/text-field' => 'value', + 'jet-forms/number-field' => 'value', + 'jet-forms/textarea-field' => 'value', + 'jet-forms/hidden-field' => 'value', + ); + + if ( empty( $name ) ) { + return $supported_blocks; + } + + return $supported_blocks[ $name ] ?? false; + } + + public function get_support_type( $attrs ) { + $block_name = $attrs['blockName']; + return $this->get_supported_blocks( $block_name ); + } + public function callback( $request ) { $params = json_decode( $request->get_body() ); @@ -50,7 +73,7 @@ public function callback( $request ) { $block = \Jet_Form_Builder\Blocks\Block_Helper::find_block_by_name( $field_name, $blocks ); - if ( ! empty( $block['attrs']['jfb_update_fields_value_enabled'] ) ) { + if ( ! empty( $block['attrs']['jfb_update_fields_value_enabled'] ) && $this->get_support_type( $block ) === 'value' ) { $value = $this->get_value( $block['attrs'], $field_name, $form_id, $form_fields ); @@ -65,10 +88,10 @@ public function callback( $request ) { } - if ( empty( $block['attrs']['jfb_update_fields_options_enabled'] ) ) { + if ( empty( $block['attrs']['jfb_update_fields_options_enabled'] ) || $this->get_support_type( $block ) !== 'options' ) { return array( 'type' => 'error', - 'value' => 'This field does not have Field Updater enabled', + 'value' => 'This field does not have Field Updater enabled or its settings are invalid.', ); }