index.js
125 lines
| 1 | import "./attributes/sidebar"; |
| 2 | import "./attributes/post-sidebar"; |
| 3 | import "./attributes/customize-sidebar"; |
| 4 | |
| 5 | function addWidgetOptionAttributes(settings, name) { |
| 6 | if (settings.attributes) { |
| 7 | let isWidgetBlockEditor = document.body?.classList.contains("widgets-php"); |
| 8 | let isWpCustomizer = typeof wp.customize !== "undefined"; |
| 9 | |
| 10 | //add here the blocks that are serversiderender |
| 11 | const originalRenderCallback = settings.edit; |
| 12 | settings.edit = function (props) { |
| 13 | // Modify or extend the props.attributes here |
| 14 | if (name == "luckywp/tableofcontents") { |
| 15 | //if block type is luckywp/tableofcontents use type string |
| 16 | if (isWidgetBlockEditor || isWpCustomizer) { |
| 17 | if (props.attributes.extended_widget_opts_block == undefined) { |
| 18 | props.attributes.extended_widget_opts_block = JSON.stringify({}); |
| 19 | } |
| 20 | |
| 21 | if (isWpCustomizer) { |
| 22 | if (props.attributes.extended_widget_opts == undefined) { |
| 23 | props.attributes.extended_widget_opts = JSON.stringify({}); |
| 24 | } |
| 25 | } |
| 26 | } else { |
| 27 | if (props.attributes.extended_widget_opts == undefined) { |
| 28 | props.attributes.extended_widget_opts = JSON.stringify({}); |
| 29 | } |
| 30 | } |
| 31 | } else { |
| 32 | //if block type is not luckywp/tableofcontents use type object |
| 33 | if (isWidgetBlockEditor || isWpCustomizer) { |
| 34 | if (props.attributes.extended_widget_opts_block == undefined) { |
| 35 | props.attributes.extended_widget_opts_block = {}; |
| 36 | } |
| 37 | |
| 38 | if (isWpCustomizer) { |
| 39 | if (props.attributes.extended_widget_opts == undefined) { |
| 40 | props.attributes.extended_widget_opts = {}; |
| 41 | } |
| 42 | } |
| 43 | } else { |
| 44 | if (props.attributes.extended_widget_opts == undefined) { |
| 45 | props.attributes.extended_widget_opts = {}; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if (props.attributes.extended_widget_opts_state == undefined) { |
| 51 | props.attributes.extended_widget_opts_state = 0; |
| 52 | } |
| 53 | |
| 54 | if (props.attributes.extended_widget_opts_clientid == undefined) { |
| 55 | props.attributes.extended_widget_opts_clientid = ""; |
| 56 | } |
| 57 | |
| 58 | if (props.attributes.dateUpdated == undefined) { |
| 59 | props.attributes.dateUpdated = ""; |
| 60 | } |
| 61 | |
| 62 | try { |
| 63 | // Now, call the original render callback with the modified props |
| 64 | return originalRenderCallback(props); |
| 65 | } catch (error) { |
| 66 | return new originalRenderCallback(props); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | if (name == "luckywp/tableofcontents") { |
| 71 | //if block type is luckywp/tableofcontents use type string |
| 72 | settings.attributes.extended_widget_opts_block = { |
| 73 | type: "object", |
| 74 | default: JSON.stringify({}), |
| 75 | }; |
| 76 | settings.attributes.extended_widget_opts = isWidgetBlockEditor |
| 77 | ? { |
| 78 | type: "object", |
| 79 | } |
| 80 | : { |
| 81 | type: "object", |
| 82 | default: JSON.stringify({}), |
| 83 | }; |
| 84 | } else { |
| 85 | //if block type is not luckywp/tableofcontents use type object |
| 86 | settings.attributes.extended_widget_opts_block = { |
| 87 | type: "object", |
| 88 | default: {}, |
| 89 | }; |
| 90 | settings.attributes.extended_widget_opts = isWidgetBlockEditor |
| 91 | ? { |
| 92 | type: "object", |
| 93 | } |
| 94 | : { |
| 95 | type: "object", |
| 96 | default: {}, |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | settings.attributes.extended_widget_opts_state = { |
| 101 | type: "string", |
| 102 | default: "", |
| 103 | }; |
| 104 | |
| 105 | settings.attributes.extended_widget_opts_clientid = { |
| 106 | type: "string", |
| 107 | default: "", |
| 108 | }; |
| 109 | |
| 110 | settings.attributes.dateUpdated = { |
| 111 | type: "string", |
| 112 | default: "", |
| 113 | }; |
| 114 | } |
| 115 | return settings; |
| 116 | } |
| 117 | |
| 118 | wp.domReady(function () { |
| 119 | wp.hooks.addFilter( |
| 120 | "blocks.registerBlockType", |
| 121 | "extended-widget-options/sidebar-component", |
| 122 | addWidgetOptionAttributes |
| 123 | ); |
| 124 | }); |
| 125 |