components
2 years ago
customize-sidebar.js
2 years ago
post-sidebar.js
2 years ago
sidebar.js
2 years ago
toolbar.js
2 years ago
sidebar.js
1041 lines
| 1 | /* |
| 2 | * Block Editor Sidebar |
| 3 | * |
| 4 | * Copyright (c) 2023 Boholweb WP |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | /* Add widget options attribute to all existing blocks, in Sidebar */ |
| 9 | |
| 10 | const { __ } = wp.i18n; |
| 11 | |
| 12 | const { createHigherOrderComponent } = wp.compose; |
| 13 | const { Fragment, Component, useState } = wp.element; |
| 14 | const { InspectorControls } = wp.blockEditor; |
| 15 | const { PanelBody, SelectControl } = wp.components; |
| 16 | import { parse } from "@wordpress/block-serialization-default-parser"; |
| 17 | |
| 18 | import WidgetOptionsTab from "./components/widgetopts-tab"; |
| 19 | import { useDispatch, useSelect, subscribe } from "@wordpress/data"; |
| 20 | |
| 21 | let initialize_click_event = false; |
| 22 | let totalFetchData = 8; |
| 23 | let saveCachedCounter = 0; |
| 24 | let waitingCachedCounter = 0; |
| 25 | let intervalExecutionTime = 0; |
| 26 | let setCacheTime = 0; |
| 27 | let done_Saved = false; |
| 28 | var validLicense = false; |
| 29 | var isFetchDataDone = 0; |
| 30 | var nowFetchingData = false; |
| 31 | let widgetopts_types = {}, |
| 32 | widgetopts_taxonomies = {}, |
| 33 | widgetopts_acf_get_field_groups = {}, |
| 34 | widgetopts_get_settings = {}, |
| 35 | widgetopts_pages = [], |
| 36 | widgetopts_terms = [], |
| 37 | widgetopts_users = [], |
| 38 | widgetopts_ajax_roles_search = {}; |
| 39 | let events = []; |
| 40 | let widgetopts_ids = []; |
| 41 | |
| 42 | if (window.jQuery != undefined) { |
| 43 | jQuery(document).ready(function ($) { |
| 44 | if ( |
| 45 | jQuery("body").hasClass("wp-admin") && |
| 46 | jQuery("body").hasClass("widgets-php") |
| 47 | ) { |
| 48 | let selected_tab = "Alignment"; |
| 49 | $(document).on( |
| 50 | "click", |
| 51 | ".edit-widgets-sidebar .widget.is-opened form.form.widgetopts-form .ui-tabs button.components-panel__body-toggle", |
| 52 | function () { |
| 53 | let _cselected_tab = $(this).text(); |
| 54 | // if (selected_tab.toLowerCase() == _cselected_tab.toLowerCase()) { |
| 55 | // return; |
| 56 | // } |
| 57 | |
| 58 | selected_tab = _cselected_tab; |
| 59 | |
| 60 | (function () { |
| 61 | let intervalId = null; |
| 62 | intervalId = setInterval(() => { |
| 63 | if ($(".widget.is-opened form.form.widgetopts-form").length > 0) { |
| 64 | clearInterval(intervalId); |
| 65 | window.wpWidgetOpts.loaded( |
| 66 | $(".widget.is-opened form.form.widgetopts-form"), |
| 67 | "updated" |
| 68 | ); |
| 69 | |
| 70 | if ( |
| 71 | $( |
| 72 | ".extended-widget-opts-tabs .extendedwopts-roles-author .select2-search__field" |
| 73 | ).length > 0 |
| 74 | ) { |
| 75 | $( |
| 76 | ".extended-widget-opts-tabs .extendedwopts-roles-author .select2-search__field" |
| 77 | ).attr("placeholder", "Search Authors"); |
| 78 | } |
| 79 | } |
| 80 | }, 500); |
| 81 | })(); |
| 82 | |
| 83 | /* select2 onchange event */ |
| 84 | $(document).on( |
| 85 | "change", |
| 86 | "select.extended-widget-opts-select2-dropdown:not(.currently-executed)", |
| 87 | function (e) { |
| 88 | $(this).addClass("currently-executed"); |
| 89 | |
| 90 | e.currentTarget.dispatchEvent( |
| 91 | new Event("change", { |
| 92 | view: window, |
| 93 | bubbles: true, |
| 94 | cancelable: false, |
| 95 | }) |
| 96 | ); |
| 97 | |
| 98 | $(this).removeClass("currently-executed"); |
| 99 | } |
| 100 | ); |
| 101 | } |
| 102 | ); |
| 103 | |
| 104 | $(document).on( |
| 105 | "focus", |
| 106 | ".extended-widget-opts-tabs .extendedwopts-roles-author .select2-search__field", |
| 107 | function () { |
| 108 | $(this).attr("placeholder", "Search Authors"); |
| 109 | } |
| 110 | ); |
| 111 | |
| 112 | $(document).on( |
| 113 | "blur", |
| 114 | ".extended-widget-opts-tabs .extendedwopts-roles-author .select2-search__field", |
| 115 | function () { |
| 116 | //$(this).attr("placeholder", ""); |
| 117 | } |
| 118 | ); |
| 119 | |
| 120 | $(document).on("blur", "button.media-button-select", function (e) { |
| 121 | let selector = document.querySelector( |
| 122 | ".block-editor-widgetopts-container.is-opened .extended-widget-opts-tabcontent-styling input.extended_widget_opts-bg-image" |
| 123 | ); |
| 124 | $(selector).addClass("extended-widgetopts-uploading-image"); |
| 125 | }); |
| 126 | |
| 127 | $(document).on( |
| 128 | "change", |
| 129 | ".block-editor-widgetopts-container.is-opened .extended-widget-opts-tabcontent-styling input.extended_widget_opts-bg-image.extended-widgetopts-uploading-image", |
| 130 | function (e) { |
| 131 | $(this).removeClass("extended-widgetopts-uploading-image"); |
| 132 | |
| 133 | let _reservedVal = $(this).val(); |
| 134 | $(this).val(""); |
| 135 | |
| 136 | let triggerReact = new Event("change", { bubbles: true }); |
| 137 | let woptsInputValueSetter = Object.getOwnPropertyDescriptor( |
| 138 | window.HTMLInputElement.prototype, |
| 139 | "value" |
| 140 | ).set; |
| 141 | |
| 142 | woptsInputValueSetter.call(e.currentTarget, _reservedVal); |
| 143 | e.currentTarget.dispatchEvent(triggerReact); |
| 144 | } |
| 145 | ); |
| 146 | |
| 147 | $(document).on("focusin", ".wp-picker-clear", function () { |
| 148 | $(this) |
| 149 | .parent() |
| 150 | .find("input.widget-opts-event-trigger") |
| 151 | .each(function () { |
| 152 | $(this).val("").change(); |
| 153 | }); |
| 154 | }); |
| 155 | |
| 156 | $(document).on( |
| 157 | "change", |
| 158 | ".wp-picker-container .widget-opts-event-trigger", |
| 159 | function (e) { |
| 160 | e.preventDefault(); |
| 161 | |
| 162 | if ($(e.target).hasClass("manual-trigger-event")) { |
| 163 | $(e.target).removeClass("manual-trigger-event"); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | let _reservedVal = $(e.target).val(); |
| 168 | $(e.target).val(0); |
| 169 | |
| 170 | let triggerReact = new Event("change", { bubbles: true }); |
| 171 | let woptsInputValueSetter = Object.getOwnPropertyDescriptor( |
| 172 | window.HTMLInputElement.prototype, |
| 173 | "value" |
| 174 | ).set; |
| 175 | |
| 176 | $(e.target).addClass("manual-trigger-event"); |
| 177 | |
| 178 | woptsInputValueSetter.call(e.target, _reservedVal); |
| 179 | e.target.dispatchEvent(triggerReact); |
| 180 | } |
| 181 | ); |
| 182 | |
| 183 | //toggle accordions |
| 184 | $(document).on( |
| 185 | "click", |
| 186 | ".extended-widget-opts-inner-lists .h4-taxo", |
| 187 | function (e) { |
| 188 | var getid = $(this).attr("id"); |
| 189 | $(this) |
| 190 | .parent() |
| 191 | .find("." + getid + ", .h4-taxo>small:nth-child(3)") |
| 192 | .slideToggle(150); |
| 193 | } |
| 194 | ); |
| 195 | |
| 196 | $(document).on( |
| 197 | "click", |
| 198 | ".extended-widget-opts-inner-lists h4", |
| 199 | function (e) { |
| 200 | var getid = $(this).attr("id"); |
| 201 | $(this).find("small:nth-child(2)").slideToggle(260); |
| 202 | } |
| 203 | ); |
| 204 | } |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Add Custom Select to Image Sidebar |
| 210 | */ |
| 211 | const withSidebarTab = (BlockEdit) => { |
| 212 | //this part will be executed only once for all blocks |
| 213 | const fetchData = (prop) => { |
| 214 | if (nowFetchingData) { |
| 215 | return; |
| 216 | } else { |
| 217 | nowFetchingData = true; |
| 218 | } |
| 219 | |
| 220 | const d = new Date(); |
| 221 | |
| 222 | wp.ajax.post("widgetopts_get_settings_ajax", {}).then(function (response) { |
| 223 | isFetchDataDone++; |
| 224 | widgetopts_get_settings = response; |
| 225 | |
| 226 | if (isFetchDataDone == totalFetchData) { |
| 227 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 228 | } |
| 229 | }); |
| 230 | |
| 231 | wp.ajax.post("widgetopts_get_types", {}).then(function (response) { |
| 232 | isFetchDataDone++; |
| 233 | widgetopts_types = response; |
| 234 | |
| 235 | if (isFetchDataDone == totalFetchData) { |
| 236 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 237 | } |
| 238 | }); |
| 239 | |
| 240 | wp.ajax.post("widgetopts_get_taxonomies", {}).then(function (response) { |
| 241 | isFetchDataDone++; |
| 242 | widgetopts_taxonomies = response; |
| 243 | |
| 244 | if (isFetchDataDone == totalFetchData) { |
| 245 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 246 | } |
| 247 | }); |
| 248 | |
| 249 | wp.ajax |
| 250 | .post("widgetopts_acf_get_field_groups", {}) |
| 251 | .then(function (response) { |
| 252 | isFetchDataDone++; |
| 253 | widgetopts_acf_get_field_groups = response; |
| 254 | |
| 255 | if (isFetchDataDone == totalFetchData) { |
| 256 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | wp.ajax |
| 261 | .post("widgetopts_ajax_roles_search_block", {}) |
| 262 | .then(function (response) { |
| 263 | isFetchDataDone++; |
| 264 | widgetopts_ajax_roles_search = response; |
| 265 | |
| 266 | if (isFetchDataDone == totalFetchData) { |
| 267 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 268 | } |
| 269 | }); |
| 270 | |
| 271 | wp.ajax.post("widgetopts_get_pages", {}).then(function (response) { |
| 272 | isFetchDataDone++; |
| 273 | widgetopts_pages = response; |
| 274 | |
| 275 | if (isFetchDataDone == totalFetchData) { |
| 276 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 277 | } |
| 278 | }); |
| 279 | |
| 280 | wp.ajax.post("widgetopts_get_terms", {}).then(function (response) { |
| 281 | isFetchDataDone++; |
| 282 | widgetopts_terms = response; |
| 283 | |
| 284 | if (isFetchDataDone == totalFetchData) { |
| 285 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 286 | } |
| 287 | }); |
| 288 | |
| 289 | wp.ajax.post("widgetopts_get_users", {}).then(function (response) { |
| 290 | isFetchDataDone++; |
| 291 | widgetopts_users = response; |
| 292 | |
| 293 | if (isFetchDataDone == totalFetchData) { |
| 294 | prop.setAttributes({ ...prop.attributes, dateUpdated: d.getTime() }); |
| 295 | } |
| 296 | }); |
| 297 | }; |
| 298 | |
| 299 | const clone_object = (_obj) => { |
| 300 | if (_obj == undefined || _obj == null) { |
| 301 | return {}; |
| 302 | } |
| 303 | |
| 304 | let new_obj = Object.create(_obj); |
| 305 | let keys = Object.keys(_obj); |
| 306 | for (let i = 0; i < keys.length; i++) { |
| 307 | if (Object(_obj[keys[i]]) === _obj[keys[i]]) { |
| 308 | new_obj[keys[i]] = { ..._obj[keys[i]] }; |
| 309 | } else { |
| 310 | new_obj[keys[i]] = _obj[keys[i]]; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return { ...new_obj }; |
| 315 | }; |
| 316 | |
| 317 | var selector = document.querySelector( |
| 318 | ".edit-widgets-header__actions button.components-button.is-primary" |
| 319 | ); |
| 320 | |
| 321 | const _return = (props) => { |
| 322 | let isWidgetBlockEditor = document.body.classList.contains("widgets-php"); |
| 323 | //If it is not widget block editor |
| 324 | if (!isWidgetBlockEditor) { |
| 325 | return <BlockEdit {...props} />; |
| 326 | } |
| 327 | |
| 328 | if ( |
| 329 | (props.attributes.__internalWidgetId == undefined && |
| 330 | props.name === "core/widget-area") || |
| 331 | (props.attributes.__internalWidgetId == undefined && |
| 332 | props.__unstableParentLayout != undefined) |
| 333 | ) { |
| 334 | return <BlockEdit {...props} />; |
| 335 | } |
| 336 | |
| 337 | const { editEntityRecord, saveEditedEntityRecord } = useDispatch("core"); |
| 338 | |
| 339 | useSelect((select) => { |
| 340 | if ( |
| 341 | props.attributes.__internalWidgetId == undefined || |
| 342 | props.name != "core/legacy-widget" || |
| 343 | events.includes(props.attributes.__internalWidgetId) |
| 344 | ) { |
| 345 | return; |
| 346 | } |
| 347 | events.push(props.attributes.__internalWidgetId); |
| 348 | selector.addEventListener("click", async function (e) { |
| 349 | try { |
| 350 | if ( |
| 351 | wp.data |
| 352 | .select("core") |
| 353 | .hasEditsForEntityRecord( |
| 354 | "root", |
| 355 | "widget", |
| 356 | props.attributes.__internalWidgetId |
| 357 | ) |
| 358 | ) { |
| 359 | await saveEditedEntityRecord( |
| 360 | "root", |
| 361 | "widget", |
| 362 | props.attributes.__internalWidgetId |
| 363 | ); |
| 364 | |
| 365 | widgetopts_ids = widgetopts_ids.filter( |
| 366 | (value) => props.attributes.__internalWidgetId !== value |
| 367 | ); |
| 368 | |
| 369 | //set custom saving |
| 370 | if (widgetopts_ids.length == 0) { |
| 371 | wp.data.dispatch("core/edit-widgets").saveEditedWidgetAreas(); |
| 372 | } |
| 373 | } |
| 374 | } catch (e) {} |
| 375 | }); |
| 376 | return; |
| 377 | }, []); |
| 378 | |
| 379 | fetchData(props); |
| 380 | |
| 381 | let _myprops = {}; |
| 382 | let id_base = -1; |
| 383 | if (props.attributes.__internalWidgetId != undefined) |
| 384 | id_base = props.attributes.__internalWidgetId.split("-")[0]; |
| 385 | |
| 386 | let widget_opts = { |
| 387 | id_base: id_base, |
| 388 | column: { |
| 389 | desktop: "12", |
| 390 | tablet: "12", |
| 391 | mobile: "12", |
| 392 | }, |
| 393 | alignment: { |
| 394 | desktop: "default", |
| 395 | tablet: "default", |
| 396 | mobile: "default", |
| 397 | }, |
| 398 | roles: { |
| 399 | state: "", |
| 400 | options: "hide", |
| 401 | }, |
| 402 | visibility: { |
| 403 | selected: "0", |
| 404 | options: "hide", |
| 405 | acf: { |
| 406 | visibility: "hide", |
| 407 | field: "", |
| 408 | condition: "", |
| 409 | value: "", |
| 410 | }, |
| 411 | }, |
| 412 | author_page: { |
| 413 | author_pages: { |
| 414 | selections: "1", |
| 415 | }, |
| 416 | }, |
| 417 | devices: { |
| 418 | options: "hide", |
| 419 | }, |
| 420 | days: { |
| 421 | options: "hide", |
| 422 | }, |
| 423 | dates: { |
| 424 | options: "hide", |
| 425 | from: "", |
| 426 | to: "", |
| 427 | }, |
| 428 | styling: { |
| 429 | selected: "0", |
| 430 | bg_image: "", |
| 431 | background: "", |
| 432 | background_hover: "", |
| 433 | heading: "", |
| 434 | text: "", |
| 435 | links: "", |
| 436 | links_hover: "", |
| 437 | border_color: "", |
| 438 | border_type: "", |
| 439 | border_width: "", |
| 440 | background_input: "", |
| 441 | text_input: "", |
| 442 | border_color_input: "", |
| 443 | border_type_input: "", |
| 444 | border_width_input: "", |
| 445 | background_submit: "", |
| 446 | background_submit_hover: "", |
| 447 | text_submit: "", |
| 448 | border_color_submit: "", |
| 449 | border_type_submit: "", |
| 450 | border_width_submit: "", |
| 451 | list_border_color: "", |
| 452 | table_border_color: "", |
| 453 | }, |
| 454 | class: { |
| 455 | selected: "0", |
| 456 | link: "", |
| 457 | id: "", |
| 458 | classes: "", |
| 459 | animation: "", |
| 460 | event: "enters", |
| 461 | speed: "", |
| 462 | offset: "", |
| 463 | delay: "", |
| 464 | logic: "", |
| 465 | }, |
| 466 | tabselect: "0", |
| 467 | }; |
| 468 | |
| 469 | let fetch_myprops = null; |
| 470 | try { |
| 471 | if ( |
| 472 | wp.data |
| 473 | .select("core") |
| 474 | .hasEditsForEntityRecord( |
| 475 | "root", |
| 476 | "widget", |
| 477 | props.attributes.__internalWidgetId |
| 478 | ) |
| 479 | ) { |
| 480 | fetch_myprops = useSelect( |
| 481 | (select) => |
| 482 | select("core").getEntityRecord( |
| 483 | "root", |
| 484 | "widget", |
| 485 | props.attributes.__internalWidgetId |
| 486 | ), |
| 487 | [props.attributes.__internalWidgetId] |
| 488 | ); |
| 489 | } else { |
| 490 | fetch_myprops = useSelect( |
| 491 | (select) => |
| 492 | select("core").getEditedEntityRecord( |
| 493 | "root", |
| 494 | "widget", |
| 495 | props.attributes.__internalWidgetId |
| 496 | ), |
| 497 | [props.attributes.__internalWidgetId] |
| 498 | ); |
| 499 | } |
| 500 | } catch (e) {} |
| 501 | |
| 502 | /*Note: props.attributes.extended_widget_opts == undefined it means it is currently in post block editor |
| 503 | * id_base != -1 it means newly added block |
| 504 | */ |
| 505 | if ( |
| 506 | props.attributes.extended_widget_opts == undefined && |
| 507 | id_base != -1 && |
| 508 | (props.attributes.instance == undefined || |
| 509 | props.attributes.instance.raw == undefined || |
| 510 | props.attributes.instance.raw[ |
| 511 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 512 | ] == undefined) |
| 513 | ) { |
| 514 | if ( |
| 515 | props.attributes.extended_widget_opts_state == 0 || |
| 516 | (done_Saved && props.attributes.extended_widget_opts_state != 0) |
| 517 | ) { |
| 518 | done_Saved = false; |
| 519 | |
| 520 | _myprops = fetch_myprops; |
| 521 | |
| 522 | if ( |
| 523 | _myprops != undefined && |
| 524 | _myprops.instance != undefined && |
| 525 | _myprops.instance.raw != undefined && |
| 526 | _myprops.instance.raw.content != undefined |
| 527 | ) { |
| 528 | const blocks = parse(_myprops.instance.raw.content); |
| 529 | _myprops.instance.raw[ |
| 530 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 531 | ] = |
| 532 | blocks[0].attrs.extended_widget_opts ?? |
| 533 | _myprops.instance.raw[ |
| 534 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 535 | ]; |
| 536 | } |
| 537 | } else { |
| 538 | _myprops = fetch_myprops; |
| 539 | } |
| 540 | |
| 541 | if ( |
| 542 | window.widgetopts_cached != undefined && |
| 543 | window.widgetopts_cached[ |
| 544 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 545 | ] != undefined |
| 546 | ) { |
| 547 | if (_myprops.instance == undefined) { |
| 548 | _myprops.instance = |
| 549 | props.attributes.instance == undefined |
| 550 | ? { raw: {} } |
| 551 | : props.attributes.instance; |
| 552 | } |
| 553 | |
| 554 | if (_myprops.instance.raw == undefined) { |
| 555 | _myprops.instance.raw = |
| 556 | props.attributes.instance.raw == undefined |
| 557 | ? {} |
| 558 | : props.attributes.instance.raw; |
| 559 | } |
| 560 | |
| 561 | _myprops.instance.raw[ |
| 562 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 563 | ] = { |
| 564 | ...window.widgetopts_cached[ |
| 565 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 566 | ], |
| 567 | }; |
| 568 | } |
| 569 | } else { |
| 570 | if (props.attributes.extended_widget_opts != undefined) { |
| 571 | _myprops = props.attributes; |
| 572 | |
| 573 | if ( |
| 574 | window.widgetopts_cached != undefined && |
| 575 | window.widgetopts_cached["extended_widget_opts-" + props.clientId] != |
| 576 | undefined |
| 577 | ) { |
| 578 | _myprops["extended_widget_opts"] = { |
| 579 | ...window.widgetopts_cached[ |
| 580 | "extended_widget_opts-" + props.clientId |
| 581 | ], |
| 582 | }; |
| 583 | } |
| 584 | } else { |
| 585 | if ( |
| 586 | window.widgetopts_cached != undefined && |
| 587 | window.widgetopts_cached[ |
| 588 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 589 | ] != undefined |
| 590 | ) { |
| 591 | if (_myprops.instance == undefined) { |
| 592 | _myprops.instance = |
| 593 | props.attributes.instance == undefined |
| 594 | ? { raw: {} } |
| 595 | : props.attributes.instance; |
| 596 | } |
| 597 | |
| 598 | if (_myprops.instance.raw == undefined) { |
| 599 | _myprops.instance.raw = |
| 600 | props.attributes.instance.raw == undefined |
| 601 | ? {} |
| 602 | : props.attributes.instance.raw; |
| 603 | } |
| 604 | |
| 605 | _myprops.instance.raw[ |
| 606 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 607 | ] = { |
| 608 | ...window.widgetopts_cached[ |
| 609 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 610 | ], |
| 611 | }; |
| 612 | } else { |
| 613 | _myprops.instance = props.attributes.instance; |
| 614 | _myprops.id = props.attributes.__internalWidgetId; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | if (props.attributes.extended_widget_opts != undefined) { |
| 620 | if ( |
| 621 | _myprops["extended_widget_opts"] == undefined || |
| 622 | (_myprops["extended_widget_opts"] != undefined && |
| 623 | Object.keys(_myprops["extended_widget_opts"]).length == 0) |
| 624 | ) |
| 625 | _myprops["extended_widget_opts"] = { |
| 626 | ...widget_opts, |
| 627 | }; |
| 628 | } else { |
| 629 | if ( |
| 630 | _myprops == undefined || |
| 631 | _myprops.instance == undefined || |
| 632 | _myprops.instance.raw == undefined || |
| 633 | _myprops.instance.raw["extended_widget_opts-" + _myprops.id] == |
| 634 | undefined |
| 635 | ) { |
| 636 | if (_myprops == undefined) { |
| 637 | _myprops = props.attributes; |
| 638 | } |
| 639 | |
| 640 | if (_myprops.instance == undefined) { |
| 641 | _myprops.instance = {}; |
| 642 | } |
| 643 | |
| 644 | if (_myprops.instance.raw == undefined) { |
| 645 | _myprops.instance.raw = {}; |
| 646 | } |
| 647 | |
| 648 | _myprops.instance.raw["extended_widget_opts-" + _myprops.id] = { |
| 649 | ...widget_opts, |
| 650 | }; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | let inner_block = false; |
| 655 | //for inner blocks without widget id |
| 656 | if (props.attributes.hasOwnProperty("__internalWidgetId")) { |
| 657 | // console.log("yes"); |
| 658 | } else { |
| 659 | if ( |
| 660 | Object.keys(props.attributes?.extended_widget_opts_block ?? []).length > |
| 661 | 0 |
| 662 | ) { |
| 663 | inner_block = true; |
| 664 | _myprops["extended_widget_opts"] = { |
| 665 | ...props.attributes.extended_widget_opts_block, |
| 666 | }; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | const updateDynamicAttribute = (newValue, widget_id) => { |
| 671 | setCacheTime = new Date().getTime(); |
| 672 | |
| 673 | if (window.autosave == undefined) { |
| 674 | window.autosave = []; |
| 675 | } |
| 676 | window.autosave[widget_id] = true; |
| 677 | let _instance = { |
| 678 | raw: {}, |
| 679 | }; |
| 680 | |
| 681 | //++saveCachedCounter; |
| 682 | |
| 683 | document.querySelector( |
| 684 | ".edit-widgets-header__actions button.components-button.is-primary" |
| 685 | ).disabled = true; |
| 686 | |
| 687 | let _attribute = { |
| 688 | ...props.attributes, |
| 689 | }; |
| 690 | //This is for legacy |
| 691 | if (props.attributes.instance) { |
| 692 | if ( |
| 693 | _attribute.instance != undefined && |
| 694 | _attribute.instance.raw != undefined |
| 695 | ) { |
| 696 | _attribute.instance.raw["extended_widget_opts-" + widget_id] = { |
| 697 | ...newValue, |
| 698 | }; |
| 699 | |
| 700 | _instance.raw = { ..._attribute.instance.raw }; |
| 701 | } |
| 702 | } else { |
| 703 | //This is fo block |
| 704 | _attribute["extended_widget_opts-" + widget_id] = { |
| 705 | ...newValue, |
| 706 | }; |
| 707 | _instance.raw = { ..._attribute }; |
| 708 | } |
| 709 | |
| 710 | if (window.widgetopts_cached == undefined) { |
| 711 | window.widgetopts_cached = {}; |
| 712 | } |
| 713 | window.widgetopts_cached["extended_widget_opts-" + widget_id] = { |
| 714 | ..._instance.raw["extended_widget_opts-" + widget_id], |
| 715 | }; |
| 716 | |
| 717 | if ( |
| 718 | props.attributes.__internalWidgetId != undefined && |
| 719 | props.name == "core/legacy-widget" |
| 720 | ) { |
| 721 | if (!widgetopts_ids.includes(props.attributes.__internalWidgetId)) { |
| 722 | widgetopts_ids.push(props.attributes.__internalWidgetId); |
| 723 | } |
| 724 | |
| 725 | editEntityRecord( |
| 726 | "root", |
| 727 | "widget", |
| 728 | props.attributes.__internalWidgetId, |
| 729 | { |
| 730 | instance: _instance, |
| 731 | id: props.attributes.__internalWidgetId, |
| 732 | } |
| 733 | ); |
| 734 | } |
| 735 | |
| 736 | if (props.name == "core/legacy-widget") { |
| 737 | props.setAttributes({ |
| 738 | extended_widget_opts_clientid: props.clientId, |
| 739 | extended_widget_opts_state: Math.random().toString(), |
| 740 | }); |
| 741 | } else { |
| 742 | props.setAttributes({ |
| 743 | extended_widget_opts_block: |
| 744 | _instance.raw["extended_widget_opts-" + widget_id], |
| 745 | extended_widget_opts_clientid: props.clientId, |
| 746 | extended_widget_opts_state: Math.random().toString(), |
| 747 | // instance: { |
| 748 | // raw: { ..._instance.raw }, |
| 749 | // }, |
| 750 | }); |
| 751 | } |
| 752 | |
| 753 | document.querySelector( |
| 754 | ".edit-widgets-header__actions button.components-button.is-primary" |
| 755 | ).disabled = false; |
| 756 | }; |
| 757 | |
| 758 | const updatePostAttribute = (newValue) => { |
| 759 | let _attribute = { |
| 760 | ...props.attributes, |
| 761 | }; |
| 762 | |
| 763 | if (_attribute != undefined) { |
| 764 | _attribute["extended_widget_opts"] = clone_object({ ...newValue }); |
| 765 | } |
| 766 | |
| 767 | props.setAttributes({ |
| 768 | extended_widget_opts: { ...newValue }, |
| 769 | extended_widget_opts_clientid: props.clientId, |
| 770 | extended_widget_opts_state: Math.random().toString(), |
| 771 | }); |
| 772 | |
| 773 | if (window.widgetopts_cached == undefined) { |
| 774 | window.widgetopts_cached = {}; |
| 775 | } |
| 776 | window.widgetopts_cached["extended_widget_opts-" + props.clientId] = { |
| 777 | ..._attribute["extended_widget_opts"], |
| 778 | }; |
| 779 | }; |
| 780 | |
| 781 | // Example: Update dynamicAttribute on input change |
| 782 | const handleInputChange = (_attribute, widget_id) => { |
| 783 | if (props.attributes.extended_widget_opts != undefined) { |
| 784 | updatePostAttribute(_attribute); |
| 785 | } else { |
| 786 | updateDynamicAttribute(_attribute, widget_id); |
| 787 | } |
| 788 | }; |
| 789 | |
| 790 | return ( |
| 791 | <> |
| 792 | <BlockEdit {...props} /> |
| 793 | <InspectorControls> |
| 794 | <PanelBody |
| 795 | title={__("Widget Options")} |
| 796 | className="block-editor-widgetopts-container widget" |
| 797 | icon="admin-generic" |
| 798 | > |
| 799 | <form |
| 800 | className="form widgetopts-form" |
| 801 | method="post" |
| 802 | id={"widgetopts-form-" + props.attributes.__internalWidgetId} |
| 803 | > |
| 804 | <input |
| 805 | type="hidden" |
| 806 | name="extended_widget_opts_name" |
| 807 | value={ |
| 808 | "extended_widget_opts-" + props.attributes.__internalWidgetId |
| 809 | } |
| 810 | /> |
| 811 | <input |
| 812 | type="hidden" |
| 813 | name={ |
| 814 | "extended_widget_opts-" + |
| 815 | props.attributes.__internalWidgetId + |
| 816 | "[extended_widget_opts][id_base]" |
| 817 | } |
| 818 | value={props.attributes.__internalWidgetId} |
| 819 | /> |
| 820 | |
| 821 | {isFetchDataDone >= totalFetchData ? ( |
| 822 | <WidgetOptionsTab |
| 823 | widgetId={props.attributes.__internalWidgetId} |
| 824 | extended_widget_opts={ |
| 825 | props.attributes.extended_widget_opts != undefined || |
| 826 | inner_block === true |
| 827 | ? _myprops["extended_widget_opts"] |
| 828 | : _myprops.instance.raw[ |
| 829 | "extended_widget_opts-" + |
| 830 | props.attributes.__internalWidgetId |
| 831 | ] |
| 832 | } |
| 833 | onUpdateDynamicAttribute={handleInputChange.bind(this)} |
| 834 | widgetopts_types={widgetopts_types} |
| 835 | widgetopts_taxonomies={widgetopts_taxonomies} |
| 836 | widgetopts_acf_get_field_groups={ |
| 837 | widgetopts_acf_get_field_groups |
| 838 | } |
| 839 | validLicense={validLicense} |
| 840 | widgetopts_get_settings={widgetopts_get_settings} |
| 841 | widgetopts_pages={widgetopts_pages} |
| 842 | widgetopts_terms={widgetopts_terms} |
| 843 | widgetopts_users={widgetopts_users} |
| 844 | widgetopts_ajax_roles_search={widgetopts_ajax_roles_search} |
| 845 | editor={isWidgetBlockEditor ? "widget" : "post"} |
| 846 | /> |
| 847 | ) : ( |
| 848 | <p>Loading...</p> |
| 849 | )} |
| 850 | </form> |
| 851 | </PanelBody> |
| 852 | <style> |
| 853 | {` |
| 854 | button.components-button.has-icon { |
| 855 | justify-content: center; |
| 856 | min-width: 30px; |
| 857 | padding: 6px; |
| 858 | } |
| 859 | button.components-button.active-tab { |
| 860 | color: var(--wp-components-color-accent,var(--wp-admin-theme-color,#007cba)); |
| 861 | } |
| 862 | .extended-widget-opts-tabcontent { |
| 863 | padding: 0px 0px 15px 0px; |
| 864 | } |
| 865 | .extended-widget-opts-inside-tabs .ui-tabs-nav li a { |
| 866 | padding: 5px !important; |
| 867 | } |
| 868 | .block-editor-widgetopts-container>h2.components-panel__body-title>button.components-panel__body-toggle { |
| 869 | padding-left: 40px !important; |
| 870 | } |
| 871 | .extended-widget-opts-tabs button.components-panel__body-toggle { |
| 872 | padding-left: 28px !important; |
| 873 | } |
| 874 | .extended-widget-opts-tabs .components-panel__body .components-panel__body button.components-panel__body-toggle { |
| 875 | padding-left: 16px !important; |
| 876 | } |
| 877 | .extended-widget-opts-tabs button.components-panel__body-toggle > span.components-panel__icon, .block-editor-widgetopts-container>h2.components-panel__body-title>button.components-panel__body-toggle > span.components-panel__icon { |
| 878 | position: absolute !important; |
| 879 | text-align: left !important; |
| 880 | margin: -25px !important; |
| 881 | } |
| 882 | .extended-widget-opts-tabs .border-0 { |
| 883 | border: 0px !important; |
| 884 | } |
| 885 | .extended-widget-opts-tabs .padding-0 { |
| 886 | padding: 0px !important; |
| 887 | } |
| 888 | .extended-widget-opts-tabs .height-auto { |
| 889 | height: auto !important; |
| 890 | } |
| 891 | .extended-widget-opts-tabs .padding-x-0 { |
| 892 | padding: 16px 0 !important; |
| 893 | } |
| 894 | .extended-widget-opts-tabs .padding-y-0 { |
| 895 | padding: 0 16px !important; |
| 896 | } |
| 897 | .select2-search .select2-search__field { |
| 898 | width: 100% !important; |
| 899 | } |
| 900 | .extended-widget-opts-tabs .widgetopts-subtitle { |
| 901 | border-top: 0px solid #ddd; |
| 902 | border-bottom: 0px solid #ddd; |
| 903 | text-align: left !important; |
| 904 | background: transparent; |
| 905 | } |
| 906 | .extended-widget-opts-tabs .margin-x-minus-16 { |
| 907 | margin-left: 0px; |
| 908 | margin-right: -16px; |
| 909 | } |
| 910 | .extended-widget-opts-tabs .extended-widget-opts-inner-lists { |
| 911 | padding: 5px 0px !important; |
| 912 | } |
| 913 | |
| 914 | .extended-widget-opts-tabs .extended-widget-opts-inner-roles { |
| 915 | padding: 5px 0px !important; |
| 916 | } |
| 917 | |
| 918 | .extended-widget-opts-tabs .form-table td { |
| 919 | padding: 7px 0px !important; |
| 920 | } |
| 921 | |
| 922 | .extended-widget-opts-tabs .extended-widget-opts-tabcontent-columns .form-table td, |
| 923 | .extended-widget-opts-tabs .extended-widget-opts-tabcontent-alignment .form-table td { |
| 924 | padding: 7px 22px 7px 0px !important; |
| 925 | } |
| 926 | |
| 927 | .extended-widget-opts-tabs .extended-widget-opts-tabcontent-columns .form-table td.all-devices { |
| 928 | padding: 7px 14px 7px 0px !important; |
| 929 | } |
| 930 | |
| 931 | .extended-widget-opts-tabs .extended-widget-opts-styling-tabcontent .form-table td { |
| 932 | padding: 0px 0px 0 !important; |
| 933 | margin-bottom: 1em; |
| 934 | } |
| 935 | |
| 936 | .extended-widget-opts-tabs .extended-widget-opts-styling-tabcontent .form-table tr>td:nth-child(1) { |
| 937 | margin-bottom: 0px; |
| 938 | } |
| 939 | |
| 940 | .extended-widget-opts-tabs .widgetopts-links-widget-opts .form-table td { |
| 941 | padding: 5px 0px !important; |
| 942 | } |
| 943 | |
| 944 | .extended-widget-opts-tabs .widgetopts_id_fld .form-table td { |
| 945 | padding: 8px 0px !important; |
| 946 | } |
| 947 | |
| 948 | .extended-widget-opts-tabs .form-table { |
| 949 | margin-top: 0px; |
| 950 | } |
| 951 | |
| 952 | .extended-widget-opts-tabs .widgetopts-subtitle { |
| 953 | padding-bottom: 0; |
| 954 | } |
| 955 | |
| 956 | .extended-widget-opts-tabs .margin-bottom-0 { |
| 957 | margin-bottom: 0 !important; |
| 958 | } |
| 959 | |
| 960 | .extended-widget-opts-tabcontent.extended-widget-opts-inside-tabcontent { |
| 961 | padding: 0px 0px !important; |
| 962 | } |
| 963 | |
| 964 | .extended-widget-opts-tabcontent .h4-taxo { |
| 965 | font-size: 1em; |
| 966 | cursor: pointer; |
| 967 | } |
| 968 | |
| 969 | .extended-widget-opts-tabs .select2-container .select2-search--inline .select2-search__field { |
| 970 | margin-top: 0 !important; |
| 971 | } |
| 972 | |
| 973 | .extended-widget-opts-tabs .select2-search.select2-search--inline, |
| 974 | .extended-widget-opts-tabs ul.select2-selection__rendered, |
| 975 | .extended-widget-opts-tabs .select2-container--default .select2-selection--multiple { |
| 976 | padding-top: 0 !important; |
| 977 | padding-bottom: 0 !important; |
| 978 | } |
| 979 | |
| 980 | .extended-widget-opts-tabs .select2-search.select2-search--inline { |
| 981 | margin-bottom: 0 !important; |
| 982 | } |
| 983 | |
| 984 | .extended-widget-opts-tabs .select2.select2-container ul.select2-selection__rendered { |
| 985 | margin-bottom: 0 !important; |
| 986 | margin-top: 0px !important; |
| 987 | } |
| 988 | |
| 989 | .extended-widget-opts-tabs .select2 ul.select2-selection__rendered:not( :has(li.select2-search.select2-search--inline) ) { |
| 990 | display: flex !important; |
| 991 | } |
| 992 | |
| 993 | .extended-widget-opts-tabs .select2-container.select2-container--default .select2-selection--multiple .select2-selection__choice { |
| 994 | display: inline-block !important; |
| 995 | margin-bottom: 0 !important; |
| 996 | } |
| 997 | |
| 998 | .extended-widget-opts-tabs .select2-container .select2-selection_rendered .select2-selection__choice { |
| 999 | margin-bottom: 0 !important; |
| 1000 | } |
| 1001 | |
| 1002 | .extended-widget-opts-tabs .extended-widget-opts-inner-lists { |
| 1003 | overflow: hidden; |
| 1004 | } |
| 1005 | |
| 1006 | .extended-widget-opts-tabs .multiselect-container{ |
| 1007 | max-height: 150px; |
| 1008 | overflow: auto; |
| 1009 | position: relative; |
| 1010 | } |
| 1011 | |
| 1012 | .extended-widget-opts-tabs .multiselect-native-select .btn-group { |
| 1013 | justify-content: start; |
| 1014 | flex-direction: column; |
| 1015 | } |
| 1016 | |
| 1017 | .extended-widget-opts-tabs .extended-widget-opts-parent-option { |
| 1018 | max-width: 200px; |
| 1019 | } |
| 1020 | |
| 1021 | .extended-widget-opts-tabs .extended-widget-opts-inner-roles { |
| 1022 | max-height: 280px !important; |
| 1023 | } |
| 1024 | `} |
| 1025 | </style> |
| 1026 | </InspectorControls> |
| 1027 | </> |
| 1028 | ); |
| 1029 | }; |
| 1030 | |
| 1031 | return _return; |
| 1032 | }; |
| 1033 | |
| 1034 | wp.domReady(function () { |
| 1035 | wp.hooks.addFilter( |
| 1036 | "editor.BlockEdit", |
| 1037 | "extended-widget-options/sidebar-component", |
| 1038 | withSidebarTab |
| 1039 | ); |
| 1040 | }); |
| 1041 |