Changeset 2680515
- Timestamp:
- 02/17/2022 07:48:51 AM (4 years ago)
- Location:
- mywp-custom-patterns
- Files:
-
- 4 added
- 4 edited
-
tags/1.1.1/core/admin/Enqueue.php (modified) (1 diff)
-
tags/1.1.1/css/template.css (added)
-
tags/1.1.1/js/template.js (modified) (1 diff)
-
tags/1.1.1/js/template.min.js (added)
-
trunk/core/admin/Enqueue.php (modified) (1 diff)
-
trunk/css/template.css (added)
-
trunk/js/template.js (modified) (1 diff)
-
trunk/js/template.min.js (added)
Legend:
- Unmodified
- Added
- Removed
-
mywp-custom-patterns/tags/1.1.1/core/admin/Enqueue.php
r2668230 r2680515 55 55 wp_enqueue_script( 56 56 'mywp-custom-patterns-script', 57 $this->core->plugin_url . 'js/template. js',57 $this->core->plugin_url . 'js/template.min.js', 58 58 array( 'wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'wp-plugins', 'wp-edit-post' ) 59 59 ); -
mywp-custom-patterns/tags/1.1.1/js/template.js
r2668230 r2680515 1 (()=>{"use strict";function e(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var a,r,o=[],c=!0,s=!1;try{for(n=n.call(e);!(c=(a=n.next()).done)&&(o.push(a.value),!t||o.length!==t);c=!0);}catch(e){s=!0,r=e}finally{try{c||null==n.return||n.return()}finally{if(s)throw r}}return o}}(e,n)||function(e,n){if(e){if("string"==typeof e)return t(e,n);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?t(e,n):void 0}}(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,a=new Array(t);n<t;n++)a[n]=e[n];return a}var n=React.createElement("svg",{fillRule:"nonzero",clipRule:"evenodd",strokeLinecap:"round",strokeLinejoin:"round",xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"18",height:"18"},React.createElement("g",null,React.createElement("path",{d:"M13.7104+8.56898L23.9931+8.56898L23.9931+15.4242L13.7104+15.4242L13.7104+8.56898ZM23.5647+0L13.7104+0L13.7104+6.85518L23.9931+6.85518L23.9931+0.428449C23.9931+0.191823+23.8013-1.03432e-08+23.5647+0ZM13.7104+23.9931L23.5647+23.9931C23.8013+23.9931+23.9931+23.8013+23.9931+23.5647L23.9931+17.138L13.7104+17.138L13.7104+23.9931ZM0.428449+23.9931L10.2828+23.9931L10.2828+13.7104L0+13.7104L0+23.5647C0+23.7995+0.193659+23.9931+0.428449+23.9931ZM10.2828+0L0.428449+0C0.191823+0-2.06865e-08+0.191823+0+0.428449L0+10.2828L10.2828+10.2828L10.2828+0Z"})));!function(t){var a,r,o=t.plugins.registerPlugin,c=t.i18n.__,s=t.element,l=s.Fragment,m=s.useState,i=(t.hooks.addFilter,t.components),u=i.Modal,p=(i.Button,t.editPost),d=p.PluginSidebarMoreMenuItem,y=p.PluginSidebar,w=p.PluginBlockSettingsMenuItem;function f(e,n){var a=new FormData;a.append("action","save_template_who"),a.append("title",e),a.append("post_id",t.data.select("core/editor").getCurrentPostId()),a.append("template_content",n),a.append("nonce",MYWP_DATA.nonce),fetch(MYWP_DATA.ajaxurl,{method:"POST",credentials:"same-origin",body:a}).then((function(e){return e.json()})).then((function(e){if(1===e.response){t.data.dispatch("core/notices").createNotice("success",c("Pattern saved","mywp-custom-patterns"),{isDismissible:!0,actions:[{url:window.location.href,label:c("Reload page","mywp-custom-patterns")}]});var n=document.getElementsByName("list_patterns")[0],a=document.createElement("option");a.value=e.id,a.text=e.title,a.dataset.content=e.content,n.add(a,null)}else t.data.dispatch("core/notices").createNotice("warning",c("Pattern not saved","mywp-custom-patterns"));document.getElementById("saveMyWPCustomPattern").disabled=!1})).catch((function(e){}))}function g(){document.getElementById("saveMyWPCustomPattern").disabled=!0;var e=document.getElementsByName("mywp_name_pattern")[0].value;return""===e?(document.querySelector(".mywp-custom-patterns__wrapper .mywp-error-field").style.display="block",void(document.getElementById("saveMyWPCustomPattern").disabled=!1)):(document.querySelector(".mywp-custom-patterns__wrapper .mywp-error-field").style.display="none",void 0!==document.getElementsByName("mywp_template_cat")[0]&&document.getElementsByName("mywp_template_cat")[0].value,a=t.data.select("core/editor").getEditedPostContent(),f(e,JSON.stringify(a)),document.getElementById("saveMyWPCustomPattern").disabled=!1,!1)}function _(){if(confirm(c("Replace all you content by your custom pattern?","mywp-custom-patterns"))){var e=document.getElementsByName("list_patterns")[0],n=(e.value,e.selectedIndex),a=e.options[n].getAttribute("data-content");""!=a&&t.data.dispatch("core/editor").resetBlocks(t.blocks.parse(a))}return!1}o("mywp-custom-patterns-settings",{render:function(){var t=[];return t.push(React.createElement("option",{key:"0",disabled:!0,"data-content":"",value:""},c("Pattern name","mywp-custom-patterns"))),Object.entries(MYWP_DATA.template_list).forEach((function(n){var a=e(n,2),r=a[0],o=a[1];t.push(React.createElement("option",{key:r+1,"data-content":o.content,value:o.id},o.title))})),React.createElement(l,null,React.createElement(d,{target:"mywp-custom-patterns"},"MyWP Custom Patterns"),React.createElement(y,{name:"mywp-custom-patterns",title:c("MyWP Custom Patterns","mywp-custom-patterns")},React.createElement("div",{id:"mywp-custom-patterns__wrapper",className:"mywp-custom-patterns__wrapper"},React.createElement("div",{className:"mywp-custom-patterns__settings"},React.createElement("p",null,c("Save your current blocks patterns","mywp-custom-patterns")),React.createElement("div",{style:{display:"none"},className:"mywp-error-field"},c("Required field","mywp-custom-patterns")),React.createElement("input",{name:"mywp_name_pattern",type:"text",placeholder:c("Pattern name","mywp-custom-patterns")}),React.createElement("button",{onClick:g,className:"mywp-custom-patterns__button",id:"saveMyWPCustomPattern"},c("Save your pattern","mywp-custom-patterns"))),React.createElement("div",{className:"mywp-custom-patterns__settings"},React.createElement("p",null,React.createElement("strong",null,c("Load and replace all you content by your custom patterns","mywp-custom-patterns"))),React.createElement("select",{name:"list_patterns",id:"list_patterns",defaultValue:""},t),React.createElement("br",null),React.createElement("button",{onClick:_,className:"mywp-custom-patterns__button",id:"loadMyWPCustomPattern"},c("Load your pattern","mywp-custom-patterns"))))))},icon:n}),o("mywp-custom-patterns-settings-all-blocks",{render:function(){var a=e(m(!1),2),o=a[0],s=a[1];return r=function(){return s(!1)},React.createElement(l,null,React.createElement(w,{icon:n,label:c("Add to Block Patterns","mywp-custom-patterns"),onClick:function(){return s(!0)}}),o&&React.createElement(u,{title:c("Add to Block Patterns","mywp-custom-patterns"),onRequestClose:r,className:"mywp-modal-patterns",style:{width:"460px"}},React.createElement("input",{style:{width:"100%"},name:"mywp_name_pattern_single_block",type:"text",placeholder:c("Pattern name","mywp-custom-patterns")}),React.createElement("div",{style:{display:"none",color:"#c03",fontSize:"14px",marginBottom:"4px"},className:"mywp-error-field"},c("Required field","mywp-custom-patterns")),React.createElement("button",{className:"mywp-custom-patterns__button",onClick:function(e){!function(e){var n,a=document.getElementsByName("mywp_name_pattern_single_block")[0].value;if(document.querySelector(".mywp-modal-patterns .mywp-custom-patterns__button").disabled=!0,""===a)return document.querySelector(".mywp-modal-patterns .mywp-error-field").style.display="block",void(document.querySelector(".mywp-modal-patterns .mywp-custom-patterns__button").disabled=!1);document.querySelector(".mywp-modal-patterns .mywp-error-field").style.display="none",n=t.data.select("core/editor").getSelectedBlockCount()>1?Object(t.blocks.serialize)(t.data.select("core/editor").getMultiSelectedBlocks()):Object(t.blocks.serialize)(t.data.select("core/editor").getSelectedBlock()),f(a,JSON.stringify(n)),r()}()}},c("Save your pattern","mywp-custom-patterns")),React.createElement("p",{style:{marginTop:"3em",background:"#ff9b0014",padding:"0.5em",fontWeight:"700"}},c("Note: after the pattern has been created, you'll need to reload the page to make the pattern visible in the inserter.","mywp-custom-patterns"))))},icon:n})}(window.wp)})(); 1 /******/ (() => { // webpackBootstrap 2 /******/ "use strict"; 3 /******/ var __webpack_modules__ = ([ 4 /* 0 */, 5 /* 1 */ 6 /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { 7 8 __webpack_require__.r(__webpack_exports__); 9 // extracted by mini-css-extract-plugin 10 11 12 /***/ }) 13 /******/ ]); 14 /************************************************************************/ 15 /******/ // The module cache 16 /******/ var __webpack_module_cache__ = {}; 17 /******/ 18 /******/ // The require function 19 /******/ function __webpack_require__(moduleId) { 20 /******/ // Check if module is in cache 21 /******/ var cachedModule = __webpack_module_cache__[moduleId]; 22 /******/ if (cachedModule !== undefined) { 23 /******/ return cachedModule.exports; 24 /******/ } 25 /******/ // Create a new module (and put it into the cache) 26 /******/ var module = __webpack_module_cache__[moduleId] = { 27 /******/ // no module.id needed 28 /******/ // no module.loaded needed 29 /******/ exports: {} 30 /******/ }; 31 /******/ 32 /******/ // Execute the module function 33 /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); 34 /******/ 35 /******/ // Return the exports of the module 36 /******/ return module.exports; 37 /******/ } 38 /******/ 39 /************************************************************************/ 40 /******/ /* webpack/runtime/make namespace object */ 41 /******/ (() => { 42 /******/ // define __esModule on exports 43 /******/ __webpack_require__.r = (exports) => { 44 /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { 45 /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); 46 /******/ } 47 /******/ Object.defineProperty(exports, '__esModule', { value: true }); 48 /******/ }; 49 /******/ })(); 50 /******/ 51 /************************************************************************/ 52 var __webpack_exports__ = {}; 53 // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. 54 (() => { 55 __webpack_require__.r(__webpack_exports__); 56 /* harmony import */ var _scss_style_scss__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); 57 function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 58 59 function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 60 61 function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } 62 63 function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 64 65 function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } 66 67 function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 68 69 70 var CustomIcon = /*#__PURE__*/React.createElement("svg", { 71 fillRule: "nonzero", 72 clipRule: "evenodd", 73 strokeLinecap: "round", 74 strokeLinejoin: "round", 75 xmlns: "http://www.w3.org/2000/svg", 76 viewBox: "0 0 24 24", 77 width: "18", 78 height: "18" 79 }, /*#__PURE__*/React.createElement("g", null, /*#__PURE__*/React.createElement("path", { 80 d: "M13.7104+8.56898L23.9931+8.56898L23.9931+15.4242L13.7104+15.4242L13.7104+8.56898ZM23.5647+0L13.7104+0L13.7104+6.85518L23.9931+6.85518L23.9931+0.428449C23.9931+0.191823+23.8013-1.03432e-08+23.5647+0ZM13.7104+23.9931L23.5647+23.9931C23.8013+23.9931+23.9931+23.8013+23.9931+23.5647L23.9931+17.138L13.7104+17.138L13.7104+23.9931ZM0.428449+23.9931L10.2828+23.9931L10.2828+13.7104L0+13.7104L0+23.5647C0+23.7995+0.193659+23.9931+0.428449+23.9931ZM10.2828+0L0.428449+0C0.191823+0-2.06865e-08+0.191823+0+0.428449L0+10.2828L10.2828+10.2828L10.2828+0Z" 81 }))); 82 83 (function (wp) { 84 var registerPlugin = wp.plugins.registerPlugin; 85 var __ = wp.i18n.__; 86 var _wp$element = wp.element, 87 Fragment = _wp$element.Fragment, 88 useState = _wp$element.useState; 89 var addFilter = wp.hooks.addFilter; 90 var _wp$components = wp.components, 91 Modal = _wp$components.Modal, 92 Button = _wp$components.Button; 93 var _wp$editPost = wp.editPost, 94 PluginSidebarMoreMenuItem = _wp$editPost.PluginSidebarMoreMenuItem, 95 PluginSidebar = _wp$editPost.PluginSidebar, 96 PluginBlockSettingsMenuItem = _wp$editPost.PluginBlockSettingsMenuItem; 97 /* const {select, dispatch} = wp.data; 98 const {count} = wp.wordcount; 99 const {serialize} = wp.blocks; 100 const {Panel, PanelBody, PanelRow} = wp.components; */ 101 102 var list_blocks; 103 104 var mywpCustomPatternsPanel = function mywpCustomPatternsPanel() { 105 var options_template = []; // let options_cat = []; 106 107 options_template.push( /*#__PURE__*/React.createElement("option", { 108 key: "0", 109 disabled: true, 110 "data-content": "", 111 value: "" 112 }, __('Pattern name', 'mywp-custom-patterns'))); 113 Object.entries(MYWP_DATA.template_list).forEach(function (_ref) { 114 var _ref2 = _slicedToArray(_ref, 2), 115 index = _ref2[0], 116 item = _ref2[1]; 117 118 options_template.push( /*#__PURE__*/React.createElement("option", { 119 key: index + 1, 120 "data-content": item.content, 121 value: item.id 122 }, item.title)); 123 }); 124 /* 125 Object.entries(MYWP_DATA.categories_list).forEach(([index, item]) => { 126 options_cat.push( 127 <option data-content={item.content} value={item.id}>{item.title}</option> 128 ); 129 }); */ 130 131 var styleCustom = { 132 display: 'none' 133 }; 134 return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PluginSidebarMoreMenuItem, { 135 target: "mywp-custom-patterns" 136 }, "MyWP Custom Patterns"), /*#__PURE__*/React.createElement(PluginSidebar, { 137 name: "mywp-custom-patterns", 138 title: __('MyWP Custom Patterns', 'mywp-custom-patterns') 139 }, /*#__PURE__*/React.createElement("div", { 140 id: "mywp-custom-patterns__wrapper", 141 className: "mywp-custom-patterns__wrapper" 142 }, /*#__PURE__*/React.createElement("div", { 143 className: "mywp-custom-patterns__settings" 144 }, /*#__PURE__*/React.createElement("p", null, __('Save your current blocks patterns', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("div", { 145 style: styleCustom, 146 className: "mywp-error-field" 147 }, __('Required field', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("input", { 148 name: "mywp_name_pattern", 149 type: "text", 150 placeholder: __('Pattern name', 'mywp-custom-patterns') 151 }), /*#__PURE__*/React.createElement("button", { 152 onClick: saveMyWPCustomPattern, 153 className: "mywp-custom-patterns__button", 154 id: "saveMyWPCustomPattern" 155 }, __('Save your pattern', 'mywp-custom-patterns'))), /*#__PURE__*/React.createElement("div", { 156 className: "mywp-custom-patterns__settings" 157 }, /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement("strong", null, __('Load and replace all you content by your custom patterns', 'mywp-custom-patterns'))), /*#__PURE__*/React.createElement("select", { 158 name: "list_patterns", 159 id: "list_patterns", 160 defaultValue: "" 161 }, options_template), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement("button", { 162 onClick: loadMyWPCustomPattern, 163 className: "mywp-custom-patterns__button", 164 id: "loadMyWPCustomPattern" 165 }, __('Load your pattern', 'mywp-custom-patterns')))))); 166 }; 167 168 registerPlugin('mywp-custom-patterns-settings', { 169 render: mywpCustomPatternsPanel, 170 icon: CustomIcon 171 }); 172 var closeModal; 173 174 var mywpCustomPatternsAddSettings = function mywpCustomPatternsAddSettings() { 175 var _useState = useState(false), 176 _useState2 = _slicedToArray(_useState, 2), 177 isOpen = _useState2[0], 178 setOpen = _useState2[1]; 179 180 var openModal = function openModal() { 181 return setOpen(true); 182 }; 183 184 closeModal = function closeModal() { 185 return setOpen(false); 186 }; 187 188 return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PluginBlockSettingsMenuItem, { 189 icon: CustomIcon, 190 label: __('Add to Block Patterns', 'mywp-custom-patterns'), 191 onClick: openModal 192 }), isOpen && /*#__PURE__*/React.createElement(Modal, { 193 title: __('Add to Block Patterns', 'mywp-custom-patterns'), 194 onRequestClose: closeModal, 195 className: "mywp-modal-patterns", 196 style: { 197 width: "460px" 198 } 199 }, /*#__PURE__*/React.createElement("input", { 200 style: { 201 width: "100%" 202 }, 203 name: "mywp_name_pattern_single_block", 204 type: "text", 205 placeholder: __('Pattern name', 'mywp-custom-patterns') 206 }), /*#__PURE__*/React.createElement("div", { 207 style: { 208 display: "none", 209 color: "#c03", 210 fontSize: "14px", 211 marginBottom: "4px" 212 }, 213 className: "mywp-error-field" 214 }, __('Required field', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("button", { 215 className: "mywp-custom-patterns__button", 216 onClick: function onClick(e) { 217 saveMyWPCustomPatternSingleBlock(e); 218 } 219 }, __('Save your pattern', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("p", { 220 style: { 221 marginTop: "3em", 222 background: "#ff9b0014", 223 padding: "0.5em", 224 fontWeight: '700' 225 } 226 }, __("Note: after the pattern has been created, you'll need to reload the page to make the pattern visible in the inserter.", 'mywp-custom-patterns')))); 227 }; 228 229 registerPlugin('mywp-custom-patterns-settings-all-blocks', { 230 render: mywpCustomPatternsAddSettings, 231 icon: CustomIcon 232 }); 233 234 function saveMyWPCustomPatternAjax(name_pattern, content) { 235 var data = new FormData(); 236 data.append('action', 'save_template_who'); 237 data.append('title', name_pattern); 238 /* 239 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 240 data.append('mywp_template_cat', mywp_template_cat); 241 } */ 242 243 data.append('post_id', wp.data.select('core/editor').getCurrentPostId()); 244 data.append('template_content', content); 245 data.append('nonce', MYWP_DATA.nonce); 246 fetch(MYWP_DATA.ajaxurl, { 247 method: "POST", 248 credentials: 'same-origin', 249 body: data 250 }).then(function (res) { 251 return res.json(); 252 }).then(function (data) { 253 if (data.response === 1) { 254 wp.data.dispatch('core/notices').createNotice('success', __('Pattern saved', 'mywp-custom-patterns'), { 255 isDismissible: true, 256 actions: [{ 257 url: window.location.href, 258 label: __('Reload page', 'mywp-custom-patterns') 259 }] 260 }); 261 var item = document.getElementsByName('list_patterns')[0]; 262 var opt = document.createElement("option"); 263 opt.value = data.id; 264 opt.text = data.title; 265 opt.dataset.content = data.content; 266 item.add(opt, null); 267 } else { 268 wp.data.dispatch('core/notices').createNotice('warning', __('Pattern not saved', 'mywp-custom-patterns')); 269 } 270 271 document.getElementById('saveMyWPCustomPattern').disabled = false; 272 })["catch"](function (error) {// console.log(error); 273 }); 274 } 275 276 function saveMyWPCustomPatternSingleBlock(e) { 277 // console.log(e); 278 var name_pattern = document.getElementsByName('mywp_name_pattern_single_block')[0].value; 279 document.querySelector('.mywp-modal-patterns .mywp-custom-patterns__button').disabled = true; 280 281 if (name_pattern === '') { 282 document.querySelector('.mywp-modal-patterns .mywp-error-field').style.display = 'block'; 283 document.querySelector('.mywp-modal-patterns .mywp-custom-patterns__button').disabled = false; 284 return; 285 } 286 287 document.querySelector('.mywp-modal-patterns .mywp-error-field').style.display = 'none'; 288 var content; 289 290 if (wp.data.select('core/editor').getSelectedBlockCount() > 1) { 291 content = Object(wp.blocks.serialize)(wp.data.select('core/editor').getMultiSelectedBlocks()); 292 } else { 293 content = Object(wp.blocks.serialize)(wp.data.select('core/editor').getSelectedBlock()); 294 } 295 296 saveMyWPCustomPatternAjax(name_pattern, JSON.stringify(content)); // console.log(Object(wp.data.select('core/editor').getSelectedBlock().serialize)); // getSelectedBlockClientId 297 // console.log(name_pattern); 298 299 closeModal(); 300 } 301 /* 302 function who_add_cat() { 303 console.log('aa'); 304 document.getElementsByClassName('mywp_template_cat_add_input')[0].style.display = 'block'; 305 return false; 306 } 307 */ 308 309 310 function saveMyWPCustomPattern() { 311 document.getElementById('saveMyWPCustomPattern').disabled = true; 312 var name_pattern = document.getElementsByName('mywp_name_pattern')[0].value; 313 314 if (name_pattern === '') { 315 document.querySelector('.mywp-custom-patterns__wrapper .mywp-error-field').style.display = 'block'; // document.getElementById('wrapper-custom-patterns'); 316 317 document.getElementById('saveMyWPCustomPattern').disabled = false; 318 return; 319 } 320 321 document.querySelector('.mywp-custom-patterns__wrapper .mywp-error-field').style.display = 'none'; 322 323 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 324 var mywp_template_cat = document.getElementsByName('mywp_template_cat')[0].value; 325 } // list_blocks = wp.data.select('core/block-editor').getBlocks(); 326 327 328 list_blocks = wp.data.select('core/editor').getEditedPostContent(); 329 saveMyWPCustomPatternAjax(name_pattern, JSON.stringify(list_blocks)); 330 /* 331 const data = new FormData(); 332 data.append('action', 'save_template_who'); 333 data.append('title', name_pattern); 334 /* 335 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 336 data.append('mywp_template_cat', mywp_template_cat); 337 } */ 338 339 /* 340 data.append('post_id', wp.data.select('core/editor').getCurrentPostId()); 341 data.append('template_content', JSON.stringify(list_blocks)); 342 data.append('nonce', MYWP_DATA.nonce) 343 data.append('nonce', MYWP_DATA.nonce) 344 */ 345 346 /* 347 fetch(MYWP_DATA.ajaxurl, { 348 method: "POST", 349 credentials: 'same-origin', 350 body: data 351 }) 352 .then((res) => res.json()) 353 .then((data) => { 354 if (data.response === 1) { 355 let item = document.getElementsByName('list_patterns')[0]; 356 let opt = document.createElement("option"); 357 opt.value = data.id; 358 opt.text = data.title; 359 opt.dataset.content = data.content; 360 item.add(opt, null); 361 wp.data.dispatch('core/notices').createNotice('success', __('Pattern save', 'mywp-custom-patterns')); 362 } else { 363 wp.data.dispatch('core/notices').createNotice('warning', __('Pattern not saved', 'mywp-custom-patterns')); 364 } 365 document.getElementById('saveMyWPCustomPattern').disabled = false; 366 }) 367 .catch((error) => { 368 // console.log(error); 369 }); */ 370 371 document.getElementById('saveMyWPCustomPattern').disabled = false; 372 return false; 373 } 374 375 function loadMyWPCustomPattern() { 376 if (confirm(__('Replace all you content by your custom pattern?', 'mywp-custom-patterns'))) { 377 var item = document.getElementsByName('list_patterns')[0]; 378 var id_template = item.value; 379 var selectedIndex = item.selectedIndex; 380 var load_template = item.options[selectedIndex].getAttribute('data-content'); 381 382 if (load_template != '') { 383 // wp.data.dispatch('core/block-editor').insertBlocks(list_blocks); 384 wp.data.dispatch('core/editor').resetBlocks(wp.blocks.parse(load_template)); 385 } 386 } 387 388 return false; 389 } 390 })(window.wp); 391 })(); 392 393 /******/ })() 394 ; -
mywp-custom-patterns/trunk/core/admin/Enqueue.php
r2657061 r2680515 55 55 wp_enqueue_script( 56 56 'mywp-custom-patterns-script', 57 $this->core->plugin_url . 'js/template. js',57 $this->core->plugin_url . 'js/template.min.js', 58 58 array( 'wp-api-fetch', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor', 'wp-plugins', 'wp-edit-post' ) 59 59 ); -
mywp-custom-patterns/trunk/js/template.js
r2668230 r2680515 1 (()=>{"use strict";function e(e,n){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var a,r,o=[],c=!0,s=!1;try{for(n=n.call(e);!(c=(a=n.next()).done)&&(o.push(a.value),!t||o.length!==t);c=!0);}catch(e){s=!0,r=e}finally{try{c||null==n.return||n.return()}finally{if(s)throw r}}return o}}(e,n)||function(e,n){if(e){if("string"==typeof e)return t(e,n);var a=Object.prototype.toString.call(e).slice(8,-1);return"Object"===a&&e.constructor&&(a=e.constructor.name),"Map"===a||"Set"===a?Array.from(e):"Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a)?t(e,n):void 0}}(e,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,a=new Array(t);n<t;n++)a[n]=e[n];return a}var n=React.createElement("svg",{fillRule:"nonzero",clipRule:"evenodd",strokeLinecap:"round",strokeLinejoin:"round",xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",width:"18",height:"18"},React.createElement("g",null,React.createElement("path",{d:"M13.7104+8.56898L23.9931+8.56898L23.9931+15.4242L13.7104+15.4242L13.7104+8.56898ZM23.5647+0L13.7104+0L13.7104+6.85518L23.9931+6.85518L23.9931+0.428449C23.9931+0.191823+23.8013-1.03432e-08+23.5647+0ZM13.7104+23.9931L23.5647+23.9931C23.8013+23.9931+23.9931+23.8013+23.9931+23.5647L23.9931+17.138L13.7104+17.138L13.7104+23.9931ZM0.428449+23.9931L10.2828+23.9931L10.2828+13.7104L0+13.7104L0+23.5647C0+23.7995+0.193659+23.9931+0.428449+23.9931ZM10.2828+0L0.428449+0C0.191823+0-2.06865e-08+0.191823+0+0.428449L0+10.2828L10.2828+10.2828L10.2828+0Z"})));!function(t){var a,r,o=t.plugins.registerPlugin,c=t.i18n.__,s=t.element,l=s.Fragment,m=s.useState,i=(t.hooks.addFilter,t.components),u=i.Modal,p=(i.Button,t.editPost),d=p.PluginSidebarMoreMenuItem,y=p.PluginSidebar,w=p.PluginBlockSettingsMenuItem;function f(e,n){var a=new FormData;a.append("action","save_template_who"),a.append("title",e),a.append("post_id",t.data.select("core/editor").getCurrentPostId()),a.append("template_content",n),a.append("nonce",MYWP_DATA.nonce),fetch(MYWP_DATA.ajaxurl,{method:"POST",credentials:"same-origin",body:a}).then((function(e){return e.json()})).then((function(e){if(1===e.response){t.data.dispatch("core/notices").createNotice("success",c("Pattern saved","mywp-custom-patterns"),{isDismissible:!0,actions:[{url:window.location.href,label:c("Reload page","mywp-custom-patterns")}]});var n=document.getElementsByName("list_patterns")[0],a=document.createElement("option");a.value=e.id,a.text=e.title,a.dataset.content=e.content,n.add(a,null)}else t.data.dispatch("core/notices").createNotice("warning",c("Pattern not saved","mywp-custom-patterns"));document.getElementById("saveMyWPCustomPattern").disabled=!1})).catch((function(e){}))}function g(){document.getElementById("saveMyWPCustomPattern").disabled=!0;var e=document.getElementsByName("mywp_name_pattern")[0].value;return""===e?(document.querySelector(".mywp-custom-patterns__wrapper .mywp-error-field").style.display="block",void(document.getElementById("saveMyWPCustomPattern").disabled=!1)):(document.querySelector(".mywp-custom-patterns__wrapper .mywp-error-field").style.display="none",void 0!==document.getElementsByName("mywp_template_cat")[0]&&document.getElementsByName("mywp_template_cat")[0].value,a=t.data.select("core/editor").getEditedPostContent(),f(e,JSON.stringify(a)),document.getElementById("saveMyWPCustomPattern").disabled=!1,!1)}function _(){if(confirm(c("Replace all you content by your custom pattern?","mywp-custom-patterns"))){var e=document.getElementsByName("list_patterns")[0],n=(e.value,e.selectedIndex),a=e.options[n].getAttribute("data-content");""!=a&&t.data.dispatch("core/editor").resetBlocks(t.blocks.parse(a))}return!1}o("mywp-custom-patterns-settings",{render:function(){var t=[];return t.push(React.createElement("option",{key:"0",disabled:!0,"data-content":"",value:""},c("Pattern name","mywp-custom-patterns"))),Object.entries(MYWP_DATA.template_list).forEach((function(n){var a=e(n,2),r=a[0],o=a[1];t.push(React.createElement("option",{key:r+1,"data-content":o.content,value:o.id},o.title))})),React.createElement(l,null,React.createElement(d,{target:"mywp-custom-patterns"},"MyWP Custom Patterns"),React.createElement(y,{name:"mywp-custom-patterns",title:c("MyWP Custom Patterns","mywp-custom-patterns")},React.createElement("div",{id:"mywp-custom-patterns__wrapper",className:"mywp-custom-patterns__wrapper"},React.createElement("div",{className:"mywp-custom-patterns__settings"},React.createElement("p",null,c("Save your current blocks patterns","mywp-custom-patterns")),React.createElement("div",{style:{display:"none"},className:"mywp-error-field"},c("Required field","mywp-custom-patterns")),React.createElement("input",{name:"mywp_name_pattern",type:"text",placeholder:c("Pattern name","mywp-custom-patterns")}),React.createElement("button",{onClick:g,className:"mywp-custom-patterns__button",id:"saveMyWPCustomPattern"},c("Save your pattern","mywp-custom-patterns"))),React.createElement("div",{className:"mywp-custom-patterns__settings"},React.createElement("p",null,React.createElement("strong",null,c("Load and replace all you content by your custom patterns","mywp-custom-patterns"))),React.createElement("select",{name:"list_patterns",id:"list_patterns",defaultValue:""},t),React.createElement("br",null),React.createElement("button",{onClick:_,className:"mywp-custom-patterns__button",id:"loadMyWPCustomPattern"},c("Load your pattern","mywp-custom-patterns"))))))},icon:n}),o("mywp-custom-patterns-settings-all-blocks",{render:function(){var a=e(m(!1),2),o=a[0],s=a[1];return r=function(){return s(!1)},React.createElement(l,null,React.createElement(w,{icon:n,label:c("Add to Block Patterns","mywp-custom-patterns"),onClick:function(){return s(!0)}}),o&&React.createElement(u,{title:c("Add to Block Patterns","mywp-custom-patterns"),onRequestClose:r,className:"mywp-modal-patterns",style:{width:"460px"}},React.createElement("input",{style:{width:"100%"},name:"mywp_name_pattern_single_block",type:"text",placeholder:c("Pattern name","mywp-custom-patterns")}),React.createElement("div",{style:{display:"none",color:"#c03",fontSize:"14px",marginBottom:"4px"},className:"mywp-error-field"},c("Required field","mywp-custom-patterns")),React.createElement("button",{className:"mywp-custom-patterns__button",onClick:function(e){!function(e){var n,a=document.getElementsByName("mywp_name_pattern_single_block")[0].value;if(document.querySelector(".mywp-modal-patterns .mywp-custom-patterns__button").disabled=!0,""===a)return document.querySelector(".mywp-modal-patterns .mywp-error-field").style.display="block",void(document.querySelector(".mywp-modal-patterns .mywp-custom-patterns__button").disabled=!1);document.querySelector(".mywp-modal-patterns .mywp-error-field").style.display="none",n=t.data.select("core/editor").getSelectedBlockCount()>1?Object(t.blocks.serialize)(t.data.select("core/editor").getMultiSelectedBlocks()):Object(t.blocks.serialize)(t.data.select("core/editor").getSelectedBlock()),f(a,JSON.stringify(n)),r()}()}},c("Save your pattern","mywp-custom-patterns")),React.createElement("p",{style:{marginTop:"3em",background:"#ff9b0014",padding:"0.5em",fontWeight:"700"}},c("Note: after the pattern has been created, you'll need to reload the page to make the pattern visible in the inserter.","mywp-custom-patterns"))))},icon:n})}(window.wp)})(); 1 /******/ (() => { // webpackBootstrap 2 /******/ "use strict"; 3 /******/ var __webpack_modules__ = ([ 4 /* 0 */, 5 /* 1 */ 6 /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { 7 8 __webpack_require__.r(__webpack_exports__); 9 // extracted by mini-css-extract-plugin 10 11 12 /***/ }) 13 /******/ ]); 14 /************************************************************************/ 15 /******/ // The module cache 16 /******/ var __webpack_module_cache__ = {}; 17 /******/ 18 /******/ // The require function 19 /******/ function __webpack_require__(moduleId) { 20 /******/ // Check if module is in cache 21 /******/ var cachedModule = __webpack_module_cache__[moduleId]; 22 /******/ if (cachedModule !== undefined) { 23 /******/ return cachedModule.exports; 24 /******/ } 25 /******/ // Create a new module (and put it into the cache) 26 /******/ var module = __webpack_module_cache__[moduleId] = { 27 /******/ // no module.id needed 28 /******/ // no module.loaded needed 29 /******/ exports: {} 30 /******/ }; 31 /******/ 32 /******/ // Execute the module function 33 /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); 34 /******/ 35 /******/ // Return the exports of the module 36 /******/ return module.exports; 37 /******/ } 38 /******/ 39 /************************************************************************/ 40 /******/ /* webpack/runtime/make namespace object */ 41 /******/ (() => { 42 /******/ // define __esModule on exports 43 /******/ __webpack_require__.r = (exports) => { 44 /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { 45 /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); 46 /******/ } 47 /******/ Object.defineProperty(exports, '__esModule', { value: true }); 48 /******/ }; 49 /******/ })(); 50 /******/ 51 /************************************************************************/ 52 var __webpack_exports__ = {}; 53 // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. 54 (() => { 55 __webpack_require__.r(__webpack_exports__); 56 /* harmony import */ var _scss_style_scss__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); 57 function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 58 59 function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } 60 61 function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } 62 63 function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } 64 65 function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } 66 67 function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } 68 69 70 var CustomIcon = /*#__PURE__*/React.createElement("svg", { 71 fillRule: "nonzero", 72 clipRule: "evenodd", 73 strokeLinecap: "round", 74 strokeLinejoin: "round", 75 xmlns: "http://www.w3.org/2000/svg", 76 viewBox: "0 0 24 24", 77 width: "18", 78 height: "18" 79 }, /*#__PURE__*/React.createElement("g", null, /*#__PURE__*/React.createElement("path", { 80 d: "M13.7104+8.56898L23.9931+8.56898L23.9931+15.4242L13.7104+15.4242L13.7104+8.56898ZM23.5647+0L13.7104+0L13.7104+6.85518L23.9931+6.85518L23.9931+0.428449C23.9931+0.191823+23.8013-1.03432e-08+23.5647+0ZM13.7104+23.9931L23.5647+23.9931C23.8013+23.9931+23.9931+23.8013+23.9931+23.5647L23.9931+17.138L13.7104+17.138L13.7104+23.9931ZM0.428449+23.9931L10.2828+23.9931L10.2828+13.7104L0+13.7104L0+23.5647C0+23.7995+0.193659+23.9931+0.428449+23.9931ZM10.2828+0L0.428449+0C0.191823+0-2.06865e-08+0.191823+0+0.428449L0+10.2828L10.2828+10.2828L10.2828+0Z" 81 }))); 82 83 (function (wp) { 84 var registerPlugin = wp.plugins.registerPlugin; 85 var __ = wp.i18n.__; 86 var _wp$element = wp.element, 87 Fragment = _wp$element.Fragment, 88 useState = _wp$element.useState; 89 var addFilter = wp.hooks.addFilter; 90 var _wp$components = wp.components, 91 Modal = _wp$components.Modal, 92 Button = _wp$components.Button; 93 var _wp$editPost = wp.editPost, 94 PluginSidebarMoreMenuItem = _wp$editPost.PluginSidebarMoreMenuItem, 95 PluginSidebar = _wp$editPost.PluginSidebar, 96 PluginBlockSettingsMenuItem = _wp$editPost.PluginBlockSettingsMenuItem; 97 /* const {select, dispatch} = wp.data; 98 const {count} = wp.wordcount; 99 const {serialize} = wp.blocks; 100 const {Panel, PanelBody, PanelRow} = wp.components; */ 101 102 var list_blocks; 103 104 var mywpCustomPatternsPanel = function mywpCustomPatternsPanel() { 105 var options_template = []; // let options_cat = []; 106 107 options_template.push( /*#__PURE__*/React.createElement("option", { 108 key: "0", 109 disabled: true, 110 "data-content": "", 111 value: "" 112 }, __('Pattern name', 'mywp-custom-patterns'))); 113 Object.entries(MYWP_DATA.template_list).forEach(function (_ref) { 114 var _ref2 = _slicedToArray(_ref, 2), 115 index = _ref2[0], 116 item = _ref2[1]; 117 118 options_template.push( /*#__PURE__*/React.createElement("option", { 119 key: index + 1, 120 "data-content": item.content, 121 value: item.id 122 }, item.title)); 123 }); 124 /* 125 Object.entries(MYWP_DATA.categories_list).forEach(([index, item]) => { 126 options_cat.push( 127 <option data-content={item.content} value={item.id}>{item.title}</option> 128 ); 129 }); */ 130 131 var styleCustom = { 132 display: 'none' 133 }; 134 return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PluginSidebarMoreMenuItem, { 135 target: "mywp-custom-patterns" 136 }, "MyWP Custom Patterns"), /*#__PURE__*/React.createElement(PluginSidebar, { 137 name: "mywp-custom-patterns", 138 title: __('MyWP Custom Patterns', 'mywp-custom-patterns') 139 }, /*#__PURE__*/React.createElement("div", { 140 id: "mywp-custom-patterns__wrapper", 141 className: "mywp-custom-patterns__wrapper" 142 }, /*#__PURE__*/React.createElement("div", { 143 className: "mywp-custom-patterns__settings" 144 }, /*#__PURE__*/React.createElement("p", null, __('Save your current blocks patterns', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("div", { 145 style: styleCustom, 146 className: "mywp-error-field" 147 }, __('Required field', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("input", { 148 name: "mywp_name_pattern", 149 type: "text", 150 placeholder: __('Pattern name', 'mywp-custom-patterns') 151 }), /*#__PURE__*/React.createElement("button", { 152 onClick: saveMyWPCustomPattern, 153 className: "mywp-custom-patterns__button", 154 id: "saveMyWPCustomPattern" 155 }, __('Save your pattern', 'mywp-custom-patterns'))), /*#__PURE__*/React.createElement("div", { 156 className: "mywp-custom-patterns__settings" 157 }, /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement("strong", null, __('Load and replace all you content by your custom patterns', 'mywp-custom-patterns'))), /*#__PURE__*/React.createElement("select", { 158 name: "list_patterns", 159 id: "list_patterns", 160 defaultValue: "" 161 }, options_template), /*#__PURE__*/React.createElement("br", null), /*#__PURE__*/React.createElement("button", { 162 onClick: loadMyWPCustomPattern, 163 className: "mywp-custom-patterns__button", 164 id: "loadMyWPCustomPattern" 165 }, __('Load your pattern', 'mywp-custom-patterns')))))); 166 }; 167 168 registerPlugin('mywp-custom-patterns-settings', { 169 render: mywpCustomPatternsPanel, 170 icon: CustomIcon 171 }); 172 var closeModal; 173 174 var mywpCustomPatternsAddSettings = function mywpCustomPatternsAddSettings() { 175 var _useState = useState(false), 176 _useState2 = _slicedToArray(_useState, 2), 177 isOpen = _useState2[0], 178 setOpen = _useState2[1]; 179 180 var openModal = function openModal() { 181 return setOpen(true); 182 }; 183 184 closeModal = function closeModal() { 185 return setOpen(false); 186 }; 187 188 return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(PluginBlockSettingsMenuItem, { 189 icon: CustomIcon, 190 label: __('Add to Block Patterns', 'mywp-custom-patterns'), 191 onClick: openModal 192 }), isOpen && /*#__PURE__*/React.createElement(Modal, { 193 title: __('Add to Block Patterns', 'mywp-custom-patterns'), 194 onRequestClose: closeModal, 195 className: "mywp-modal-patterns", 196 style: { 197 width: "460px" 198 } 199 }, /*#__PURE__*/React.createElement("input", { 200 style: { 201 width: "100%" 202 }, 203 name: "mywp_name_pattern_single_block", 204 type: "text", 205 placeholder: __('Pattern name', 'mywp-custom-patterns') 206 }), /*#__PURE__*/React.createElement("div", { 207 style: { 208 display: "none", 209 color: "#c03", 210 fontSize: "14px", 211 marginBottom: "4px" 212 }, 213 className: "mywp-error-field" 214 }, __('Required field', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("button", { 215 className: "mywp-custom-patterns__button", 216 onClick: function onClick(e) { 217 saveMyWPCustomPatternSingleBlock(e); 218 } 219 }, __('Save your pattern', 'mywp-custom-patterns')), /*#__PURE__*/React.createElement("p", { 220 style: { 221 marginTop: "3em", 222 background: "#ff9b0014", 223 padding: "0.5em", 224 fontWeight: '700' 225 } 226 }, __("Note: after the pattern has been created, you'll need to reload the page to make the pattern visible in the inserter.", 'mywp-custom-patterns')))); 227 }; 228 229 registerPlugin('mywp-custom-patterns-settings-all-blocks', { 230 render: mywpCustomPatternsAddSettings, 231 icon: CustomIcon 232 }); 233 234 function saveMyWPCustomPatternAjax(name_pattern, content) { 235 var data = new FormData(); 236 data.append('action', 'save_template_who'); 237 data.append('title', name_pattern); 238 /* 239 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 240 data.append('mywp_template_cat', mywp_template_cat); 241 } */ 242 243 data.append('post_id', wp.data.select('core/editor').getCurrentPostId()); 244 data.append('template_content', content); 245 data.append('nonce', MYWP_DATA.nonce); 246 fetch(MYWP_DATA.ajaxurl, { 247 method: "POST", 248 credentials: 'same-origin', 249 body: data 250 }).then(function (res) { 251 return res.json(); 252 }).then(function (data) { 253 if (data.response === 1) { 254 wp.data.dispatch('core/notices').createNotice('success', __('Pattern saved', 'mywp-custom-patterns'), { 255 isDismissible: true, 256 actions: [{ 257 url: window.location.href, 258 label: __('Reload page', 'mywp-custom-patterns') 259 }] 260 }); 261 var item = document.getElementsByName('list_patterns')[0]; 262 var opt = document.createElement("option"); 263 opt.value = data.id; 264 opt.text = data.title; 265 opt.dataset.content = data.content; 266 item.add(opt, null); 267 } else { 268 wp.data.dispatch('core/notices').createNotice('warning', __('Pattern not saved', 'mywp-custom-patterns')); 269 } 270 271 document.getElementById('saveMyWPCustomPattern').disabled = false; 272 })["catch"](function (error) {// console.log(error); 273 }); 274 } 275 276 function saveMyWPCustomPatternSingleBlock(e) { 277 // console.log(e); 278 var name_pattern = document.getElementsByName('mywp_name_pattern_single_block')[0].value; 279 document.querySelector('.mywp-modal-patterns .mywp-custom-patterns__button').disabled = true; 280 281 if (name_pattern === '') { 282 document.querySelector('.mywp-modal-patterns .mywp-error-field').style.display = 'block'; 283 document.querySelector('.mywp-modal-patterns .mywp-custom-patterns__button').disabled = false; 284 return; 285 } 286 287 document.querySelector('.mywp-modal-patterns .mywp-error-field').style.display = 'none'; 288 var content; 289 290 if (wp.data.select('core/editor').getSelectedBlockCount() > 1) { 291 content = Object(wp.blocks.serialize)(wp.data.select('core/editor').getMultiSelectedBlocks()); 292 } else { 293 content = Object(wp.blocks.serialize)(wp.data.select('core/editor').getSelectedBlock()); 294 } 295 296 saveMyWPCustomPatternAjax(name_pattern, JSON.stringify(content)); // console.log(Object(wp.data.select('core/editor').getSelectedBlock().serialize)); // getSelectedBlockClientId 297 // console.log(name_pattern); 298 299 closeModal(); 300 } 301 /* 302 function who_add_cat() { 303 console.log('aa'); 304 document.getElementsByClassName('mywp_template_cat_add_input')[0].style.display = 'block'; 305 return false; 306 } 307 */ 308 309 310 function saveMyWPCustomPattern() { 311 document.getElementById('saveMyWPCustomPattern').disabled = true; 312 var name_pattern = document.getElementsByName('mywp_name_pattern')[0].value; 313 314 if (name_pattern === '') { 315 document.querySelector('.mywp-custom-patterns__wrapper .mywp-error-field').style.display = 'block'; // document.getElementById('wrapper-custom-patterns'); 316 317 document.getElementById('saveMyWPCustomPattern').disabled = false; 318 return; 319 } 320 321 document.querySelector('.mywp-custom-patterns__wrapper .mywp-error-field').style.display = 'none'; 322 323 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 324 var mywp_template_cat = document.getElementsByName('mywp_template_cat')[0].value; 325 } // list_blocks = wp.data.select('core/block-editor').getBlocks(); 326 327 328 list_blocks = wp.data.select('core/editor').getEditedPostContent(); 329 saveMyWPCustomPatternAjax(name_pattern, JSON.stringify(list_blocks)); 330 /* 331 const data = new FormData(); 332 data.append('action', 'save_template_who'); 333 data.append('title', name_pattern); 334 /* 335 if (document.getElementsByName('mywp_template_cat')[0] !== undefined) { 336 data.append('mywp_template_cat', mywp_template_cat); 337 } */ 338 339 /* 340 data.append('post_id', wp.data.select('core/editor').getCurrentPostId()); 341 data.append('template_content', JSON.stringify(list_blocks)); 342 data.append('nonce', MYWP_DATA.nonce) 343 data.append('nonce', MYWP_DATA.nonce) 344 */ 345 346 /* 347 fetch(MYWP_DATA.ajaxurl, { 348 method: "POST", 349 credentials: 'same-origin', 350 body: data 351 }) 352 .then((res) => res.json()) 353 .then((data) => { 354 if (data.response === 1) { 355 let item = document.getElementsByName('list_patterns')[0]; 356 let opt = document.createElement("option"); 357 opt.value = data.id; 358 opt.text = data.title; 359 opt.dataset.content = data.content; 360 item.add(opt, null); 361 wp.data.dispatch('core/notices').createNotice('success', __('Pattern save', 'mywp-custom-patterns')); 362 } else { 363 wp.data.dispatch('core/notices').createNotice('warning', __('Pattern not saved', 'mywp-custom-patterns')); 364 } 365 document.getElementById('saveMyWPCustomPattern').disabled = false; 366 }) 367 .catch((error) => { 368 // console.log(error); 369 }); */ 370 371 document.getElementById('saveMyWPCustomPattern').disabled = false; 372 return false; 373 } 374 375 function loadMyWPCustomPattern() { 376 if (confirm(__('Replace all you content by your custom pattern?', 'mywp-custom-patterns'))) { 377 var item = document.getElementsByName('list_patterns')[0]; 378 var id_template = item.value; 379 var selectedIndex = item.selectedIndex; 380 var load_template = item.options[selectedIndex].getAttribute('data-content'); 381 382 if (load_template != '') { 383 // wp.data.dispatch('core/block-editor').insertBlocks(list_blocks); 384 wp.data.dispatch('core/editor').resetBlocks(wp.blocks.parse(load_template)); 385 } 386 } 387 388 return false; 389 } 390 })(window.wp); 391 })(); 392 393 /******/ })() 394 ;
Note: See TracChangeset
for help on using the changeset viewer.