index.js
423 lines
| 1 | /* global localStorage, tiTpc, elementor, lodash */ |
| 2 | import { stringifyUrl } from 'query-string'; |
| 3 | import { v4 as uuidv4 } from 'uuid'; |
| 4 | |
| 5 | import apiFetch from '@wordpress/api-fetch'; |
| 6 | import { dispatch, select } from '@wordpress/data'; |
| 7 | import { cleanTemplateContent } from '../../../../shared/utils'; |
| 8 | |
| 9 | const dispatchNotification = ( message ) => |
| 10 | elementor.notifications.showToast( { message } ); |
| 11 | |
| 12 | export const fetchTemplates = async ( additionalParams = {} ) => { |
| 13 | const { meta, ...filteredParams } = tiTpc.params; |
| 14 | const { isScroll, ...filteredAdditionalParams } = additionalParams; |
| 15 | const params = { |
| 16 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 17 | ...filteredParams, |
| 18 | per_page: 20, |
| 19 | page: 0, |
| 20 | premade: true, |
| 21 | template_site_slug: 'general', |
| 22 | ...filteredAdditionalParams, |
| 23 | }; |
| 24 | |
| 25 | const url = stringifyUrl( { |
| 26 | url: window.tiTpc.endpoint + 'page-templates', |
| 27 | query: params, |
| 28 | } ); |
| 29 | |
| 30 | try { |
| 31 | const response = await apiFetch( { |
| 32 | url, |
| 33 | method: 'GET', |
| 34 | parse: false, |
| 35 | } ); |
| 36 | |
| 37 | if ( response.ok ) { |
| 38 | const templates = await response.json(); |
| 39 | |
| 40 | if ( templates.message ) { |
| 41 | return dispatchNotification( templates.message ); |
| 42 | } |
| 43 | |
| 44 | let items = templates; |
| 45 | |
| 46 | if ( additionalParams.isScroll ) { |
| 47 | const library = select( 'tpc/elementor' ).getTemplates(); |
| 48 | items = [ ...library.items, ...templates ]; |
| 49 | } |
| 50 | |
| 51 | const totalPages = response.headers.get( 'x-wp-totalpages' ); |
| 52 | const currentPage = params.page; |
| 53 | |
| 54 | dispatch( 'tpc/elementor' ).updateTemplates( |
| 55 | items, |
| 56 | currentPage, |
| 57 | totalPages |
| 58 | ); |
| 59 | } |
| 60 | } catch ( error ) { |
| 61 | if ( error.message ) { |
| 62 | dispatchNotification( error.message ); |
| 63 | } |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | export const fetchLibrary = async ( additionalParams = {} ) => { |
| 68 | const { meta, ...filteredParams } = tiTpc.params; |
| 69 | const { isScroll, ...filteredAdditionalParams } = additionalParams; |
| 70 | const params = { |
| 71 | per_page: 20, |
| 72 | page: 0, |
| 73 | ...filteredAdditionalParams, |
| 74 | }; |
| 75 | |
| 76 | const url = stringifyUrl( { |
| 77 | url: window.tiTpc.endpoint + 'templates', |
| 78 | query: { |
| 79 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 80 | ...filteredParams, |
| 81 | ...params, |
| 82 | }, |
| 83 | } ); |
| 84 | |
| 85 | try { |
| 86 | const response = await apiFetch( { |
| 87 | url, |
| 88 | method: 'GET', |
| 89 | parse: false, |
| 90 | } ); |
| 91 | |
| 92 | if ( response.ok ) { |
| 93 | const templates = await response.json(); |
| 94 | |
| 95 | if ( templates.message ) { |
| 96 | return dispatchNotification( templates.message ); |
| 97 | } |
| 98 | |
| 99 | let items = templates; |
| 100 | |
| 101 | if ( additionalParams.isScroll ) { |
| 102 | const library = select( 'tpc/elementor' ).getLibrary(); |
| 103 | items = [ ...library.items, ...templates ]; |
| 104 | } |
| 105 | |
| 106 | const totalPages = response.headers.get( 'x-wp-totalpages' ); |
| 107 | const currentPage = params.page; |
| 108 | |
| 109 | dispatch( 'tpc/elementor' ).updateLibrary( |
| 110 | items, |
| 111 | currentPage, |
| 112 | totalPages |
| 113 | ); |
| 114 | } |
| 115 | } catch ( error ) { |
| 116 | if ( error.message ) { |
| 117 | dispatchNotification( error.message ); |
| 118 | } |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | export const getTemplate = async ( template ) => { |
| 123 | const { meta, ...filteredParams } = tiTpc.params; |
| 124 | |
| 125 | const url = stringifyUrl( { |
| 126 | url: `${ window.tiTpc.endpoint }templates/${ template }`, |
| 127 | query: { |
| 128 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 129 | ...filteredParams, |
| 130 | }, |
| 131 | } ); |
| 132 | |
| 133 | try { |
| 134 | const response = await apiFetch( { |
| 135 | url, |
| 136 | method: 'GET', |
| 137 | parse: false, |
| 138 | } ); |
| 139 | |
| 140 | if ( response.ok ) { |
| 141 | const content = await response.json(); |
| 142 | |
| 143 | if ( content.message ) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return content; |
| 148 | } |
| 149 | } catch ( error ) { |
| 150 | return false; |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | /** |
| 155 | * Import a template from TPC server for Elementor. |
| 156 | * |
| 157 | * @param {string} template The template slug. |
| 158 | * @return {any} The template content. |
| 159 | */ |
| 160 | export const importTemplate = async ( template ) => { |
| 161 | const { meta, ...filteredParams } = tiTpc.params; |
| 162 | |
| 163 | const url = stringifyUrl( { |
| 164 | url: `${ window.tiTpc.endpoint }templates/${ template }/import`, |
| 165 | query: { |
| 166 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 167 | ...filteredParams, |
| 168 | }, |
| 169 | } ); |
| 170 | |
| 171 | let content = {}; |
| 172 | |
| 173 | try { |
| 174 | const response = await apiFetch( { |
| 175 | url, |
| 176 | method: 'GET', |
| 177 | parse: false, |
| 178 | } ); |
| 179 | |
| 180 | if ( response.ok ) { |
| 181 | content = await response.json(); |
| 182 | |
| 183 | if ( content.message ) { |
| 184 | return dispatchNotification( content.message ); |
| 185 | } |
| 186 | } |
| 187 | } catch ( error ) { |
| 188 | if ( error.message ) { |
| 189 | dispatchNotification( error.message ); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | cleanTemplateContent( content, ( element ) => { |
| 194 | // Remove imported images ID since they are not available on the current site via Media Library. |
| 195 | delete element?.settings?.image?.id; |
| 196 | delete element?.settings?.background_image?.id; |
| 197 | delete element?.settings?.background_overlay_image?.id; |
| 198 | } ); |
| 199 | |
| 200 | return content; |
| 201 | }; |
| 202 | |
| 203 | export const duplicateTemplate = async ( id ) => { |
| 204 | const { meta, ...filteredParams } = tiTpc.params; |
| 205 | const url = stringifyUrl( { |
| 206 | url: `${ window.tiTpc.endpoint }templates/${ id }/clone`, |
| 207 | query: { |
| 208 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 209 | ...filteredParams, |
| 210 | }, |
| 211 | } ); |
| 212 | |
| 213 | try { |
| 214 | const response = await apiFetch( { url, method: 'POST' } ); |
| 215 | |
| 216 | if ( response.ok ) { |
| 217 | const content = await response.json(); |
| 218 | |
| 219 | if ( content.message ) { |
| 220 | return dispatchNotification( content.message ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 225 | |
| 226 | await fetchLibrary(); |
| 227 | } catch ( error ) { |
| 228 | if ( error.message ) { |
| 229 | dispatchNotification( error.message ); |
| 230 | } |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | export const updateTemplate = async ( params ) => { |
| 235 | const { meta, ...filteredParams } = tiTpc.params; |
| 236 | const { content, ...filteredAdditionalParams } = params; |
| 237 | const url = stringifyUrl( { |
| 238 | url: `${ window.tiTpc.endpoint }templates/${ params.template_id }`, |
| 239 | query: { |
| 240 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 241 | ...filteredParams, |
| 242 | meta: JSON.stringify( tiTpc.params.meta ), |
| 243 | ...filteredAdditionalParams, |
| 244 | }, |
| 245 | } ); |
| 246 | |
| 247 | try { |
| 248 | const obj = { |
| 249 | url, |
| 250 | method: 'POST', |
| 251 | parse: false, |
| 252 | }; |
| 253 | |
| 254 | if ( params.content ) { |
| 255 | const data = { |
| 256 | title: |
| 257 | elementor.config.initial_document.settings.settings |
| 258 | .post_title || '', |
| 259 | version: '0.4', |
| 260 | type: 'page', |
| 261 | content: params.content, |
| 262 | }; |
| 263 | |
| 264 | obj.data = data; |
| 265 | } |
| 266 | |
| 267 | const response = await apiFetch( { ...obj } ); |
| 268 | |
| 269 | if ( response.ok ) { |
| 270 | const content = await response.json(); |
| 271 | |
| 272 | if ( content.message ) { |
| 273 | return dispatchNotification( content.message ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 278 | |
| 279 | await fetchLibrary(); |
| 280 | } catch ( error ) { |
| 281 | if ( error.message ) { |
| 282 | dispatchNotification( error.message ); |
| 283 | } |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | export const deleteTemplate = async ( template ) => { |
| 288 | const { meta, ...filteredParams } = tiTpc.params; |
| 289 | const url = stringifyUrl( { |
| 290 | url: `${ window.tiTpc.endpoint }templates/${ template }`, |
| 291 | query: { |
| 292 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 293 | _method: 'DELETE', |
| 294 | ...filteredParams, |
| 295 | }, |
| 296 | } ); |
| 297 | |
| 298 | try { |
| 299 | const response = await apiFetch( { url, method: 'POST' } ); |
| 300 | |
| 301 | if ( response.ok ) { |
| 302 | const content = await response.json(); |
| 303 | |
| 304 | if ( content.message ) { |
| 305 | return dispatchNotification( content.message ); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 310 | |
| 311 | await fetchLibrary(); |
| 312 | } catch ( error ) { |
| 313 | if ( error.message ) { |
| 314 | dispatchNotification( error.message ); |
| 315 | } |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | export const exportTemplate = async ( { |
| 320 | title, |
| 321 | type, |
| 322 | content, |
| 323 | link = '', |
| 324 | callback = () => {}, |
| 325 | } ) => { |
| 326 | const data = { |
| 327 | version: '0.4', |
| 328 | title, |
| 329 | type, |
| 330 | content, |
| 331 | }; |
| 332 | |
| 333 | const { meta, ...filteredParams } = window.tiTpc.params; |
| 334 | |
| 335 | const currentTemplate = elementor.documents |
| 336 | .getCurrent() |
| 337 | .container.settings.get( 'template' ); |
| 338 | |
| 339 | if ( currentTemplate ) { |
| 340 | meta._wp_page_template = currentTemplate; |
| 341 | } |
| 342 | |
| 343 | const url = stringifyUrl( { |
| 344 | url: window.tiTpc.endpoint + 'templates', |
| 345 | query: { |
| 346 | ...filteredParams, |
| 347 | meta: 'page' === type ? JSON.stringify( meta ) : '', |
| 348 | template_name: title || window.tiTpc.exporter.textPlaceholder, |
| 349 | template_type: 'elementor', |
| 350 | link, |
| 351 | }, |
| 352 | } ); |
| 353 | |
| 354 | try { |
| 355 | const response = await apiFetch( { |
| 356 | url, |
| 357 | method: 'POST', |
| 358 | data, |
| 359 | parse: false, |
| 360 | } ); |
| 361 | |
| 362 | if ( response.ok ) { |
| 363 | const res = await response.json(); |
| 364 | |
| 365 | if ( res.message ) { |
| 366 | dispatchNotification( res.message ); |
| 367 | } else { |
| 368 | callback( res ); |
| 369 | window.localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 370 | |
| 371 | dispatchNotification( window.tiTpc.exporter.templateSaved ); |
| 372 | } |
| 373 | } |
| 374 | } catch ( error ) { |
| 375 | if ( error.message ) { |
| 376 | dispatchNotification( error.message ); |
| 377 | } |
| 378 | } |
| 379 | }; |
| 380 | |
| 381 | export const publishTemplate = async ( params ) => { |
| 382 | const { meta, ...filteredParams } = tiTpc.params; |
| 383 | const { template_id, ...filteredAdditionalParams } = params; |
| 384 | const url = stringifyUrl( { |
| 385 | url: `${ window.tiTpc.endpoint }templates/${ params.template_id }/publish`, |
| 386 | query: { |
| 387 | cache: localStorage.getItem( 'tpcCacheBuster' ), |
| 388 | method: 'POST', |
| 389 | ...filteredParams, |
| 390 | ...filteredAdditionalParams, |
| 391 | }, |
| 392 | } ); |
| 393 | |
| 394 | try { |
| 395 | const response = await apiFetch( { |
| 396 | url, |
| 397 | method: 'POST', |
| 398 | headers: { |
| 399 | Authorization: `Bearer ${ window.tiTpc.bearer || '' } `, |
| 400 | }, |
| 401 | } ); |
| 402 | if ( response.ok ) { |
| 403 | const content = await response.json(); |
| 404 | if ( content.message ) { |
| 405 | dispatchNotification( content.message ); |
| 406 | return { success: false }; |
| 407 | } |
| 408 | } else if ( response.message ) { |
| 409 | dispatchNotification( response.message ); |
| 410 | return { success: false }; |
| 411 | } |
| 412 | |
| 413 | localStorage.setItem( 'tpcCacheBuster', uuidv4() ); |
| 414 | |
| 415 | return { success: true }; |
| 416 | } catch ( error ) { |
| 417 | if ( error.message ) { |
| 418 | dispatchNotification( error.message ); |
| 419 | return { success: false }; |
| 420 | } |
| 421 | } |
| 422 | }; |
| 423 |