admin-notifications.js
1 month ago
sbr-blocks.js
1 month ago
sbr-feed.js
1 month ago
sbr-feed.min.js
1 month ago
sbr-review-alerts.js
1 month ago
sbr-blocks.js
240 lines
| 1 | "use strict"; |
| 2 | (function () { |
| 3 | const extractFeedId = (value) => { |
| 4 | if (value !== undefined && |
| 5 | value !== null && |
| 6 | typeof value === 'string' |
| 7 | ) { |
| 8 | let newValue = value.replaceAll('feed', '').replaceAll('=', '').replaceAll('"', ''); |
| 9 | value = parseInt(newValue); |
| 10 | } |
| 11 | return value; |
| 12 | } |
| 13 | |
| 14 | var _wp = wp, |
| 15 | _wp$serverSideRender = _wp.serverSideRender, |
| 16 | createElement = wp.element.createElement, |
| 17 | ServerSideRender = _wp$serverSideRender === void 0 ? wp.components.ServerSideRender : _wp$serverSideRender, |
| 18 | _ref = wp.blockEditor || wp.editor, |
| 19 | InspectorControls = _ref.InspectorControls, |
| 20 | useBlockProps = _ref.useBlockProps, |
| 21 | _wp$components = wp.components, |
| 22 | TextareaControl = _wp$components.TextareaControl, |
| 23 | SelectControl = _wp$components.SelectControl, |
| 24 | Button = _wp$components.Button, |
| 25 | PanelBody = _wp$components.PanelBody, |
| 26 | Placeholder = _wp$components.Placeholder, |
| 27 | registerBlockType = wp.blocks.registerBlockType; |
| 28 | |
| 29 | // Locate the WP 7.0 block editor canvas iframe. Returns null if the |
| 30 | // iframe doesn't exist yet or its contentDocument isn't reachable. |
| 31 | function getEditorIframe() { |
| 32 | var selectors = [ |
| 33 | 'iframe[name="editor-canvas"]', |
| 34 | 'iframe.edit-post-visual-editor__content-area', |
| 35 | 'iframe.editor-canvas' |
| 36 | ]; |
| 37 | for (var i = 0; i < selectors.length; i++) { |
| 38 | var el = document.querySelector(selectors[i]); |
| 39 | if (el && el.contentDocument && el.contentDocument.head) { |
| 40 | return el; |
| 41 | } |
| 42 | } |
| 43 | return null; |
| 44 | } |
| 45 | |
| 46 | // Inject sbr-feed.min.js (and its config + jQuery if needed) into the |
| 47 | // editor iframe's <head>, so window.sbr_init exists inside the iframe |
| 48 | // and can find the feed DOM that ServerSideRender mounts there. |
| 49 | var iframeAssetsPromise = null; |
| 50 | function ensureIframeFeedAssets() { |
| 51 | if (iframeAssetsPromise) { |
| 52 | return iframeAssetsPromise; |
| 53 | } |
| 54 | iframeAssetsPromise = new Promise(function (resolve, reject) { |
| 55 | var attempts = 0; |
| 56 | var tryInject = function () { |
| 57 | attempts++; |
| 58 | var iframe = getEditorIframe(); |
| 59 | if (!iframe) { |
| 60 | if (attempts > 40) { |
| 61 | reject(new Error('sbr: editor iframe not found')); |
| 62 | return; |
| 63 | } |
| 64 | setTimeout(tryInject, 250); |
| 65 | return; |
| 66 | } |
| 67 | var doc = iframe.contentDocument; |
| 68 | if (doc.documentElement.getAttribute('data-sbr-feed-assets-injected')) { |
| 69 | resolve(iframe); |
| 70 | return; |
| 71 | } |
| 72 | doc.documentElement.setAttribute('data-sbr-feed-assets-injected', '1'); |
| 73 | |
| 74 | var config = doc.createElement('script'); |
| 75 | config.textContent = |
| 76 | 'window.sbrOptions = ' + JSON.stringify(sbr_block_editor.sbrOptions || {}) + ';'; |
| 77 | doc.head.appendChild(config); |
| 78 | |
| 79 | var loadScript = function (src) { |
| 80 | return new Promise(function (res, rej) { |
| 81 | var s = doc.createElement('script'); |
| 82 | s.src = src; |
| 83 | s.onload = function () { res(); }; |
| 84 | s.onerror = function () { rej(new Error('sbr: failed to load ' + src)); }; |
| 85 | doc.head.appendChild(s); |
| 86 | }); |
| 87 | }; |
| 88 | |
| 89 | var chain = Promise.resolve(); |
| 90 | if (!iframe.contentWindow.jQuery && sbr_block_editor.jqueryUrl) { |
| 91 | chain = chain.then(function () { return loadScript(sbr_block_editor.jqueryUrl); }); |
| 92 | } |
| 93 | if (sbr_block_editor.iframeScriptUrl) { |
| 94 | chain = chain.then(function () { return loadScript(sbr_block_editor.iframeScriptUrl); }); |
| 95 | } |
| 96 | chain.then(function () { resolve(iframe); }, reject); |
| 97 | }; |
| 98 | tryInject(); |
| 99 | }); |
| 100 | return iframeAssetsPromise; |
| 101 | } |
| 102 | |
| 103 | // Call sbr_init() inside the iframe (WP 7.0+) or in the outer scope as a |
| 104 | // fallback for pre-iframe editors. |
| 105 | function triggerSbrInit() { |
| 106 | var iframe = getEditorIframe(); |
| 107 | if (iframe && iframe.contentWindow && typeof iframe.contentWindow.sbr_init === 'function') { |
| 108 | try { iframe.contentWindow.sbr_init(); } catch (e) {} |
| 109 | return; |
| 110 | } |
| 111 | if (!iframe && typeof sbr_init !== 'undefined') { |
| 112 | try { sbr_init(); } catch (e) {} |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | var sbrIcon = createElement('svg', { |
| 117 | width: 20, |
| 118 | height: 20, |
| 119 | viewBox: '0 0 16 17', |
| 120 | className: 'dashicon' |
| 121 | }, createElement('path', { |
| 122 | fill: 'currentColor', |
| 123 | d: 'M2.66683 1.83331H13.3335C14.0668 1.83331 14.6668 2.43331 14.6668 3.16665V11.1666C14.6668 11.9 14.0668 12.5 13.3335 12.5H10.2502L8.15756 15.0222C7.94521 15.2781 7.54681 15.2593 7.35955 14.9845L5.66683 12.5L2.58349 12.4212C1.88845 12.4049 1.3335 11.8368 1.3335 11.1416V3.16665C1.3335 2.43331 1.9335 1.83331 2.66683 1.83331ZM8.11539 3.77526C8.07255 3.67298 7.92763 3.67298 7.8848 3.77526L6.96671 5.96725C6.94868 6.01028 6.9082 6.03969 6.86171 6.04353L4.49329 6.23933C4.38278 6.24846 4.338 6.38628 4.42204 6.45863L6.22304 8.00915C6.2584 8.03959 6.27386 8.08718 6.26315 8.13258L5.71748 10.4456C5.69201 10.5535 5.80925 10.6387 5.90403 10.5811L7.9352 9.3474C7.97507 9.32318 8.02511 9.32318 8.06498 9.3474L10.0962 10.5811C10.1909 10.6387 10.3082 10.5535 10.2827 10.4456L9.73703 8.13258C9.72632 8.08718 9.74179 8.03959 9.77714 8.00915L11.5781 6.45863C11.6622 6.38628 11.6174 6.24846 11.5069 6.23933L9.13847 6.04353C9.09198 6.03969 9.0515 6.01028 9.03348 5.96725L8.11539 3.77526Z' |
| 124 | })); |
| 125 | |
| 126 | registerBlockType('sbr/sbr-feed-block', { |
| 127 | apiVersion: 3, |
| 128 | title: 'Reviews Feed', |
| 129 | icon: sbrIcon, |
| 130 | category: 'widgets', |
| 131 | attributes: { |
| 132 | noNewChanges: { |
| 133 | type: 'boolean', |
| 134 | }, |
| 135 | shortcodeSettings: { |
| 136 | type: 'string', |
| 137 | }, |
| 138 | executed: { |
| 139 | type: 'boolean' |
| 140 | } |
| 141 | }, |
| 142 | edit: function edit(props) { |
| 143 | var blockProps = typeof useBlockProps === 'function' ? useBlockProps() : {}; |
| 144 | var _props = props, |
| 145 | setAttributes = _props.setAttributes, |
| 146 | _props$attributes = _props.attributes, |
| 147 | _props$attributes$sho = _props$attributes.shortcodeSettings, |
| 148 | shortcodeSettings = _props$attributes$sho === void 0 ? sbr_block_editor.shortcodeSettings : _props$attributes$sho, |
| 149 | _props$attributes$cli = _props$attributes.noNewChanges, |
| 150 | noNewChanges = _props$attributes$cli === void 0 ? true : _props$attributes$cli, |
| 151 | _props$attributes$exe = _props$attributes.executed, |
| 152 | executed = _props$attributes$exe === void 0 ? false : _props$attributes$exe; |
| 153 | |
| 154 | props.attributes.shortcodeSettings = shortcodeSettings; |
| 155 | |
| 156 | function setState(shortcodeSettingsContent) { |
| 157 | setAttributes({ |
| 158 | noNewChanges: false, |
| 159 | shortcodeSettings: shortcodeSettingsContent |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | function previewClick(content) { |
| 164 | setAttributes({ |
| 165 | noNewChanges: true, |
| 166 | executed: false, |
| 167 | }); |
| 168 | } |
| 169 | function afterRender() { |
| 170 | // no way to run a script after AJAX call to get feed so we just try to execute it on a few intervals |
| 171 | if (! executed |
| 172 | || typeof window.sbrGutenberg === 'undefined') { |
| 173 | window.sbr = true; |
| 174 | window.sbrGutenberg = true; |
| 175 | // Inject sbr-feed into the WP 7.0 iframe (no-op once injected), |
| 176 | // then poll-trigger sbr_init in iframe scope. ServerSideRender |
| 177 | // doesn't expose an onload callback, so we retry on intervals. |
| 178 | ensureIframeFeedAssets().catch(function () {}); |
| 179 | setTimeout(triggerSbrInit, 1000); |
| 180 | setTimeout(triggerSbrInit, 2000); |
| 181 | setTimeout(triggerSbrInit, 3000); |
| 182 | setTimeout(triggerSbrInit, 5000); |
| 183 | setTimeout(triggerSbrInit, 10000); |
| 184 | } |
| 185 | setAttributes({ |
| 186 | executed: true, |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | var jsx = [React.createElement(InspectorControls, { |
| 191 | key: "sbr-gutenberg-setting-selector-inspector-controls" |
| 192 | }, React.createElement(PanelBody, { |
| 193 | title: sbr_block_editor.i18n.addSettings |
| 194 | }, |
| 195 | React.createElement( |
| 196 | SelectControl, |
| 197 | { |
| 198 | key: "sbr-gutenberg-settings", |
| 199 | className: "sbr-gutenberg-settings", |
| 200 | label: sbr_block_editor.i18n.selectFeedLabel, |
| 201 | options: sbr_block_editor.feedsListOption, |
| 202 | value : extractFeedId(shortcodeSettings), |
| 203 | onChange: (valueID) => { |
| 204 | setState(valueID) |
| 205 | previewClick() |
| 206 | } |
| 207 | } |
| 208 | ), |
| 209 | ) |
| 210 | ) |
| 211 | ]; |
| 212 | |
| 213 | if (noNewChanges) { |
| 214 | afterRender(); |
| 215 | jsx.push(React.createElement(ServerSideRender, { |
| 216 | key: "reviews-feed/reviews-feed", |
| 217 | block: "sbr/sbr-feed-block", |
| 218 | attributes: props.attributes, |
| 219 | })); |
| 220 | } else { |
| 221 | props.attributes.noNewChanges = false; |
| 222 | jsx.push(React.createElement(Placeholder, { |
| 223 | key: "sbr-gutenberg-setting-selector-select-wrap", |
| 224 | className: "sbr-gutenberg-setting-selector-select-wrap" |
| 225 | }, React.createElement(Button, { |
| 226 | key: "sbr-gutenberg-preview", |
| 227 | className: "sbr-gutenberg-preview", |
| 228 | onClick: previewClick, |
| 229 | isDefault: true |
| 230 | }, sbr_block_editor.i18n.preview))); |
| 231 | } |
| 232 | |
| 233 | return createElement('div', blockProps, jsx); |
| 234 | }, |
| 235 | save: function save() { |
| 236 | return null; |
| 237 | } |
| 238 | }); |
| 239 | })(); |
| 240 |