index2.js
565 lines
| 1 | (function($, blocks, editor, element, components, api) { |
| 2 | |
| 3 | var el = element.createElement, |
| 4 | InspectorControls = editor.InspectorControls, |
| 5 | PanelBody = components.PanelBody, |
| 6 | SelectControl = components.SelectControl, |
| 7 | TextControl = components.TextControl, |
| 8 | CheckboxControl = components.CheckboxControl, |
| 9 | __ = wp.i18n.__; |
| 10 | |
| 11 | function LangControl(def, lang) { |
| 12 | let opts = []; |
| 13 | opts.push(el('option', {value: ''}, def)); |
| 14 | |
| 15 | for (let i = 0; i < GRW_LANGS.length; i++) { |
| 16 | let param = {value: GRW_LANGS[i][0]}; |
| 17 | if (GRW_LANGS[i][0] == lang) { |
| 18 | param.selected = 'selected'; |
| 19 | } |
| 20 | opts.push(el('option', param, GRW_LANGS[i][1])); |
| 21 | } |
| 22 | return el |
| 23 | ( |
| 24 | 'select', |
| 25 | { |
| 26 | name : 'lang', |
| 27 | type : 'select', |
| 28 | className : 'grw-connect-lang' |
| 29 | }, |
| 30 | opts |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | function CollsControl(def, lang) { |
| 35 | /*const data = {action: 'grw_feed_list_ajax', grw_nonce: grwBlockData.nonce, v: new Date().getTime()}; |
| 36 | wp.apiFetch({ |
| 37 | method: 'GET', |
| 38 | url: wp.url.addQueryArgs(ajaxurl, data) |
| 39 | }) |
| 40 | .then(colls => { |
| 41 | if (colls && colls.length) { |
| 42 | let select = document.querySelector('.grw-connect-coll'); |
| 43 | for (let i = 0; i < colls.length; i++) { |
| 44 | let param = {value: colls[i].id}; |
| 45 | select.appendChild(el('option', param, colls[i].name)); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | }) |
| 50 | .catch(error => { |
| 51 | console.error('Error during AJAX request:', error); |
| 52 | });*/ |
| 53 | |
| 54 | let feeds = grwBlockData.feeds, |
| 55 | opts = [el('option', {value: 0}, 'Use existing reviews widget')]; |
| 56 | |
| 57 | for (let i = 0; i < feeds.length; i++) { |
| 58 | let param = {value: feeds[i].id}; |
| 59 | opts.push(el('option', param, feeds[i].name)); |
| 60 | } |
| 61 | return el |
| 62 | ( |
| 63 | 'select', |
| 64 | { |
| 65 | name : 'coll', |
| 66 | type : 'select', |
| 67 | className : 'grw-connect-coll' |
| 68 | }, |
| 69 | opts |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | const OPTIONS = { |
| 74 | |
| 75 | 'Common Options' : { |
| 76 | pagination: { |
| 77 | type: 'integer', |
| 78 | label: 'Pagination'/*, |
| 79 | default: 10*/ |
| 80 | }, |
| 81 | text_size: { |
| 82 | type: 'string', |
| 83 | label: 'Maximum characters before \'read more\' link' |
| 84 | }, |
| 85 | header_center: { |
| 86 | type: 'boolean', |
| 87 | label: 'Show rating by center' |
| 88 | }, |
| 89 | header_hide_photo: { |
| 90 | type: 'boolean', |
| 91 | label: 'Hide business photo' |
| 92 | }, |
| 93 | header_hide_name: { |
| 94 | type: 'boolean', |
| 95 | label: 'Hide business name' |
| 96 | }, |
| 97 | hide_based_on: { |
| 98 | type: 'boolean', |
| 99 | label: 'Hide \'Based on ... reviews\'' |
| 100 | }, |
| 101 | hide_writereview: { |
| 102 | type: 'boolean', |
| 103 | label: 'Hide \'review us on G\' button' |
| 104 | }, |
| 105 | header_hide_social: { |
| 106 | type: 'boolean', |
| 107 | label: 'Hide rating header, leave only reviews' |
| 108 | }, |
| 109 | hide_reviews: { |
| 110 | type: 'boolean', |
| 111 | label: 'Hide reviews, leave only rating header' |
| 112 | } |
| 113 | }, |
| 114 | |
| 115 | 'Slider Options' : { |
| 116 | slider_speed: { |
| 117 | type: 'integer', |
| 118 | label: 'Speed in second', |
| 119 | default: 5 |
| 120 | }, |
| 121 | slider_text_height: { |
| 122 | type: 'string', |
| 123 | label: 'Text height' |
| 124 | }, |
| 125 | slider_autoplay: { |
| 126 | type: 'boolean', |
| 127 | label: 'Auto-play', |
| 128 | default: true |
| 129 | }, |
| 130 | slider_hide_border: { |
| 131 | type: 'boolean', |
| 132 | label: 'Hide background' |
| 133 | }, |
| 134 | slider_hide_prevnext: { |
| 135 | type: 'boolean', |
| 136 | label: 'Hide prev & next buttons' |
| 137 | }, |
| 138 | slider_hide_dots: { |
| 139 | type: 'boolean', |
| 140 | label: 'Hide dots' |
| 141 | } |
| 142 | }, |
| 143 | |
| 144 | 'Style Options' : { |
| 145 | max_width: { |
| 146 | type: 'string', |
| 147 | label: 'Container max-width' |
| 148 | }, |
| 149 | max_height: { |
| 150 | type: 'string', |
| 151 | label: 'Container max-height' |
| 152 | }, |
| 153 | centered: { |
| 154 | type: 'boolean', |
| 155 | label: 'Place by center (only if max-width is set)' |
| 156 | }, |
| 157 | dark_theme: { |
| 158 | type: 'boolean', |
| 159 | label: 'Dark background' |
| 160 | } |
| 161 | }, |
| 162 | |
| 163 | 'Advance Options' : { |
| 164 | lazy_load_img: { |
| 165 | type: 'boolean', |
| 166 | label: 'Lazy load images', |
| 167 | default: true |
| 168 | }, |
| 169 | google_def_rev_link: { |
| 170 | type: 'boolean', |
| 171 | label: 'Use default Google reviews link', |
| 172 | default: false |
| 173 | }, |
| 174 | open_link: { |
| 175 | type: 'boolean', |
| 176 | label: 'Open links in new Window', |
| 177 | default: true |
| 178 | }, |
| 179 | nofollow_link: { |
| 180 | type: 'boolean', |
| 181 | label: 'Use no follow links', |
| 182 | default: true |
| 183 | }, |
| 184 | reviewer_avatar_size: { |
| 185 | type: 'integer', |
| 186 | label: 'Reviewer avatar size', |
| 187 | default: 56 |
| 188 | }, |
| 189 | cache: { |
| 190 | type: 'integer', |
| 191 | label: 'Cache data', |
| 192 | default: 12 |
| 193 | }, |
| 194 | reviews_limit: { |
| 195 | type: 'string', |
| 196 | label: 'Reviews limit' |
| 197 | } |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | blocks.registerBlockType('widget-google-reviews/reviews', { |
| 202 | title: __('Google Reviews Block', 'widget-google-reviews'), |
| 203 | icon: 'star-filled', |
| 204 | category: 'widgets', |
| 205 | keywords: ['google', 'reviews', 'google reviews', 'rating'], |
| 206 | |
| 207 | attributes: (function() { |
| 208 | var atts = { |
| 209 | id: { |
| 210 | type: 'integer' |
| 211 | }, |
| 212 | connections: { |
| 213 | type: 'array', |
| 214 | default: [], |
| 215 | query: { |
| 216 | id: { type: 'string', }, |
| 217 | name: { type: 'string', }, |
| 218 | photo: { type: 'string', }, |
| 219 | lang: { type: 'string', }, |
| 220 | refresh: { type: 'boolean', }, |
| 221 | local_img: { type: 'boolean', }, |
| 222 | platform: { type: 'string', } |
| 223 | }, |
| 224 | group: 'Connections', |
| 225 | }, |
| 226 | view_mode: { |
| 227 | type: 'string', |
| 228 | default: 'list' |
| 229 | } |
| 230 | }; |
| 231 | for (let o in OPTIONS) { |
| 232 | for (let op in OPTIONS[o]) { |
| 233 | atts[op] = {type: OPTIONS[o][op].type}; |
| 234 | if (OPTIONS[o][op].default) { |
| 235 | atts[op].default = OPTIONS[o][op].default; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | return atts; |
| 240 | })(), |
| 241 | |
| 242 | edit: function(props) { |
| 243 | var attributes = props.attributes; |
| 244 | var blockProps = wp.blockEditor.useBlockProps(); |
| 245 | |
| 246 | function updateArray(newValue) { |
| 247 | props.setAttributes({ connections: newValue }); |
| 248 | }; |
| 249 | |
| 250 | function addToArray(connection) { |
| 251 | const newArray = [...props.attributes.connections, connection]; |
| 252 | updateArray(newArray); |
| 253 | }; |
| 254 | |
| 255 | function removeFromArray(index) { |
| 256 | const newArray = props.attributes.connections.filter((_, i) => i !== index); |
| 257 | updateArray(newArray); |
| 258 | }; |
| 259 | |
| 260 | function addConnection(i, place) { |
| 261 | let title = place.name; |
| 262 | if (place.lang) title += ' (' + place.lang + ')'; |
| 263 | |
| 264 | return el( |
| 265 | 'div', |
| 266 | { |
| 267 | title: title, |
| 268 | initialOpen: false |
| 269 | }, |
| 270 | el('div', {className: 'grw-builder-option'}, |
| 271 | el( |
| 272 | 'img', |
| 273 | { |
| 274 | src: place.photo, |
| 275 | alt: place.name, |
| 276 | className: 'grw-connect-photo' |
| 277 | } |
| 278 | ), |
| 279 | el( |
| 280 | 'a', |
| 281 | { |
| 282 | className: 'grw-connect-photo-change', |
| 283 | href: '#', |
| 284 | }, |
| 285 | 'Change' |
| 286 | ), |
| 287 | el( |
| 288 | 'a', |
| 289 | { |
| 290 | className: 'grw-connect-photo-default', |
| 291 | href: '#', |
| 292 | }, |
| 293 | 'Default' |
| 294 | ), |
| 295 | el( |
| 296 | TextControl, |
| 297 | { |
| 298 | type: 'hidden', |
| 299 | name: 'photo', |
| 300 | className: 'grw-connect-photo-hidden', |
| 301 | value: place.id, |
| 302 | tabindex: 2 |
| 303 | } |
| 304 | ) |
| 305 | ), |
| 306 | el('div', {className: 'grw-builder-option'}, |
| 307 | el( |
| 308 | 'input', |
| 309 | { |
| 310 | name: 'name', |
| 311 | value: place.name, |
| 312 | type: 'text' |
| 313 | } |
| 314 | ), |
| 315 | ), |
| 316 | el('div', {className: 'grw-builder-option'}, |
| 317 | LangControl('Show all connected languages', place.lang) |
| 318 | ), |
| 319 | el('div', {className: 'grw-builder-option'}, |
| 320 | el( |
| 321 | 'button', |
| 322 | { |
| 323 | className: 'grw-connect-reconnect', |
| 324 | onClick: function() { |
| 325 | |
| 326 | } |
| 327 | }, |
| 328 | 'Reconnect' |
| 329 | ) |
| 330 | ), |
| 331 | el('div', {className: 'grw-builder-option'}, |
| 332 | el( |
| 333 | 'button', |
| 334 | { |
| 335 | className: 'grw-connect-delete', |
| 336 | onClick: function() { |
| 337 | removeFromArray(i); |
| 338 | } |
| 339 | }, |
| 340 | 'Delete connection' |
| 341 | ) |
| 342 | ), |
| 343 | ) |
| 344 | }; |
| 345 | |
| 346 | var connectGoogle = function(e) { |
| 347 | let btn = e.target, |
| 348 | input = btn.parentNode.querySelector('.grw-connect-id'), |
| 349 | select = btn.parentNode.querySelector('.grw-connect-lang'); |
| 350 | |
| 351 | if (!input.value) { |
| 352 | input.focus(); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | const data = new URLSearchParams(); |
| 357 | data.append('id', decodeURIComponent(input.value)); |
| 358 | data.append('lang', select.value); |
| 359 | data.append('grw_wpnonce', grwBlockData.nonce); |
| 360 | data.append('action', 'grw_connect_google'); |
| 361 | data.append('v', new Date().getTime()); |
| 362 | |
| 363 | wp.apiFetch({ |
| 364 | method: 'POST', |
| 365 | url: ajaxurl, |
| 366 | headers: { |
| 367 | 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', |
| 368 | }, |
| 369 | body: data.toString() |
| 370 | }) |
| 371 | .then(response => { |
| 372 | console.log('Response from server:', response); |
| 373 | let result = response.result; |
| 374 | if (result && result.id) { |
| 375 | addToArray({ |
| 376 | id : result.id, |
| 377 | name : result.name, |
| 378 | photo : result.photo, |
| 379 | lang : select.value, |
| 380 | refresh : true, |
| 381 | local_img : false, |
| 382 | platform : 'google' |
| 383 | }); |
| 384 | } |
| 385 | }) |
| 386 | .catch(error => { |
| 387 | console.error('Error during AJAX request:', error); |
| 388 | }); |
| 389 | }; |
| 390 | |
| 391 | var connEls = []; |
| 392 | for (let i = 0; i < attributes.connections.length; i++) { |
| 393 | (function(i, connection) { |
| 394 | connEls.push(addConnection(i, connection)); |
| 395 | })(i, attributes.connections[i]); |
| 396 | } |
| 397 | |
| 398 | function BuildOptions() { |
| 399 | let result = []; |
| 400 | for (let o in OPTIONS) { |
| 401 | let opts = []; |
| 402 | for (let op in OPTIONS[o]) { |
| 403 | (function(name, opt) { |
| 404 | let params = { |
| 405 | name : name, |
| 406 | label : opt.label, |
| 407 | onChange : function(val) { |
| 408 | //let name = event.target.name; |
| 409 | let att = {}; |
| 410 | att[name] = val |
| 411 | props.setAttributes(att); |
| 412 | } |
| 413 | }; |
| 414 | if (opt.type == 'boolean') { |
| 415 | params.checked = /*attributes[name] != undefined ?*/ attributes[name] /*: (opt.default || false)*/; |
| 416 | } else { |
| 417 | params.value = /*attributes[name] != undefined ?*/ attributes[name] /*: (opt.default || '')*/; |
| 418 | } |
| 419 | opts.push( |
| 420 | el( |
| 421 | opt.type == 'boolean' ? CheckboxControl : TextControl, |
| 422 | params |
| 423 | ) |
| 424 | ); |
| 425 | })(op, OPTIONS[o][op]); |
| 426 | |
| 427 | } |
| 428 | result.push( |
| 429 | el( |
| 430 | PanelBody, |
| 431 | { |
| 432 | title: __(o), |
| 433 | initialOpen: false |
| 434 | }, |
| 435 | opts |
| 436 | ) |
| 437 | ); |
| 438 | } |
| 439 | return result; |
| 440 | } |
| 441 | |
| 442 | return el( |
| 443 | 'div', |
| 444 | blockProps, |
| 445 | el( |
| 446 | InspectorControls, |
| 447 | { |
| 448 | key: 'inspector' |
| 449 | }, |
| 450 | el( |
| 451 | 'div', |
| 452 | { |
| 453 | id: 'grw-builder-option', |
| 454 | className: 'grw-builder-options grw-block-options' |
| 455 | }, |
| 456 | el( |
| 457 | PanelBody, |
| 458 | { |
| 459 | title: __('Layout'), |
| 460 | initialOpen: true |
| 461 | }, |
| 462 | el( |
| 463 | SelectControl, |
| 464 | { |
| 465 | id: 'view_mode', |
| 466 | name: 'view_mode', |
| 467 | value: props.attributes.view_mode, |
| 468 | options: [ |
| 469 | {label: 'Slider', value: 'slider'}, |
| 470 | {label: 'List', value: 'list'} |
| 471 | ], |
| 472 | onChange: function(newValue) { |
| 473 | props.setAttributes({ view_mode: newValue }); |
| 474 | } |
| 475 | } |
| 476 | ) |
| 477 | ), |
| 478 | BuildOptions() |
| 479 | ) |
| 480 | ), |
| 481 | |
| 482 | el( |
| 483 | 'div', |
| 484 | { |
| 485 | id: 'grw-connect-wizard', |
| 486 | title: 'Easy steps to connect Google Reviews', |
| 487 | style: { |
| 488 | 'display': 'block', |
| 489 | 'padding': '10px 20px', |
| 490 | 'border-radius': '5px', |
| 491 | 'background': '#fff' |
| 492 | } |
| 493 | }, |
| 494 | el( |
| 495 | 'p', |
| 496 | null, |
| 497 | el('span', null, '1'), |
| 498 | ' Find your Google place on the map below (', |
| 499 | el('u', { className: 'grw-wiz-arr' }, 'Enter a location'), |
| 500 | ') and copy found ', |
| 501 | el('u', null, 'Place ID') |
| 502 | ), |
| 503 | el('iframe', { |
| 504 | src: 'https://geo-devrel-javascript-samples.web.app/samples/places-placeid-finder/app/dist', |
| 505 | loading: 'lazy', |
| 506 | style: {width: '98%', height: '250px'} |
| 507 | }), |
| 508 | el( |
| 509 | 'small', |
| 510 | {style: { fontSize: '13px', color: '#555'}}, |
| 511 | 'If you can\'t find your place on this map, please read ', |
| 512 | el('a', { href: GRW_VARS.supportUrl + '&grw_tab=fig#place_id', target: '_blank'}, 'this manual how to find any Google Place ID'), |
| 513 | '.' |
| 514 | ), |
| 515 | el( |
| 516 | 'p', |
| 517 | null, |
| 518 | el( 'span', null, '2' ), |
| 519 | ' Paste copied ', |
| 520 | el('u', null, 'Place ID'), |
| 521 | 'in this field and select language if needed ', |
| 522 | ), |
| 523 | el( |
| 524 | 'p', |
| 525 | null, |
| 526 | el('input', { |
| 527 | type: 'text', |
| 528 | className: 'grw-connect-id', |
| 529 | placeholder: 'Place ID' |
| 530 | }), |
| 531 | LangControl('Choose language if needed') |
| 532 | ), |
| 533 | el( |
| 534 | 'p', |
| 535 | null, |
| 536 | el('span', null, '3'), |
| 537 | ' Click CONNECT GOOGLE button' |
| 538 | ), |
| 539 | el('button', {className: 'grw-connect-btn', onClick: connectGoogle}, 'Connect Google'), |
| 540 | el('small', {className: 'grw-connect-error'}) |
| 541 | ), |
| 542 | |
| 543 | el( |
| 544 | 'div', |
| 545 | { |
| 546 | title: __('Connections'), |
| 547 | initialOpen: true, |
| 548 | }, |
| 549 | connEls |
| 550 | ), |
| 551 | ); |
| 552 | }, |
| 553 | |
| 554 | save: function(props) { |
| 555 | return null; |
| 556 | } |
| 557 | }); |
| 558 | }( |
| 559 | jQuery, |
| 560 | window.wp.blocks, |
| 561 | window.wp.editor, |
| 562 | window.wp.element, |
| 563 | window.wp.components, |
| 564 | window.wp.api |
| 565 | )); |