content.js
1 year ago
export.js
1 year ago
header.js
1 year ago
template.js
5 years ago
templates-content.js
1 year ago
export.js
347 lines
| 1 | /* eslint-disable camelcase */ |
| 2 | /* global elementor */ |
| 3 | import classnames from 'classnames'; |
| 4 | import { Button, ToggleControl } from '@wordpress/components'; |
| 5 | import { withDispatch } from '@wordpress/data'; |
| 6 | import { useEffect, useState } from '@wordpress/element'; |
| 7 | import { __ } from '@wordpress/i18n'; |
| 8 | |
| 9 | import { |
| 10 | getTemplate, |
| 11 | exportTemplate, |
| 12 | publishTemplate, |
| 13 | updateTemplate, |
| 14 | } from './../data/templates-cloud/index.js'; |
| 15 | |
| 16 | const Export = ( { updateCurrentTab } ) => { |
| 17 | useEffect( () => { |
| 18 | const { |
| 19 | _ti_tpc_template_sync, |
| 20 | _ti_tpc_template_id, |
| 21 | _ti_tpc_screenshot_url, |
| 22 | _ti_tpc_site_slug, |
| 23 | _ti_tpc_published, |
| 24 | } = window.tiTpc.postModel.getMetas(); |
| 25 | |
| 26 | setTemplateSync( Boolean( _ti_tpc_template_sync ) ); |
| 27 | setTemplateID( _ti_tpc_template_id ); |
| 28 | setScreenshotURL( _ti_tpc_screenshot_url ); |
| 29 | setSiteSlug( _ti_tpc_site_slug ); |
| 30 | setPublished( Boolean( _ti_tpc_published ) ); |
| 31 | }, [] ); |
| 32 | |
| 33 | const title = |
| 34 | elementor.config.initial_document.settings.settings.post_title || ''; |
| 35 | |
| 36 | const [ isLoading, setLoading ] = useState( false ); |
| 37 | const [ templateSync, setTemplateSync ] = useState( false ); |
| 38 | const [ templateID, setTemplateID ] = useState( '' ); |
| 39 | const [ screenshotURL, setScreenshotURL ] = useState( '' ); |
| 40 | const [ siteSlug, setSiteSlug ] = useState( '' ); |
| 41 | const [ isPublished, setPublished ] = useState( '' ); |
| 42 | |
| 43 | const exportPage = async () => { |
| 44 | setLoading( true ); |
| 45 | |
| 46 | const content = elementor.elements.toJSON( { |
| 47 | remove: [ 'default', 'editSettings', 'defaultEditSettings' ], |
| 48 | } ); |
| 49 | |
| 50 | let doesExist = false; |
| 51 | |
| 52 | if ( templateID ) { |
| 53 | doesExist = await getTemplate( templateID ); |
| 54 | } |
| 55 | |
| 56 | const meta = window.tiTpc.params.meta; |
| 57 | |
| 58 | const currentTemplate = elementor.documents |
| 59 | .getCurrent() |
| 60 | .container.settings.get( 'template' ); |
| 61 | |
| 62 | if ( currentTemplate ) { |
| 63 | meta._wp_page_template = currentTemplate; |
| 64 | } |
| 65 | |
| 66 | if ( doesExist ) { |
| 67 | await updateTemplate( { |
| 68 | template_id: templateID, |
| 69 | template_name: title, |
| 70 | content, |
| 71 | link: elementor.config.initial_document.urls.permalink, |
| 72 | meta: JSON.stringify( meta ), |
| 73 | } ); |
| 74 | } else { |
| 75 | await exportTemplate( { |
| 76 | title, |
| 77 | type: 'page', |
| 78 | content, |
| 79 | link: elementor.config.initial_document.urls.permalink, |
| 80 | callback: ( res ) => { |
| 81 | setTemplateID( res.template_id ); |
| 82 | window.tiTpc.postModel.set( 'meta', { |
| 83 | _ti_tpc_template_id: res.template_id, |
| 84 | _ti_tpc_template_sync: templateSync, |
| 85 | } ); |
| 86 | |
| 87 | window.tiTpc.postModel.save(); |
| 88 | }, |
| 89 | } ); |
| 90 | } |
| 91 | |
| 92 | setLoading( false ); |
| 93 | updateCurrentTab( 'library' ); |
| 94 | }; |
| 95 | |
| 96 | const refreshData = async () => { |
| 97 | setLoading( true ); |
| 98 | try { |
| 99 | await getTemplate( templateID ).then( ( results ) => { |
| 100 | if ( templateID === results.template_id ) { |
| 101 | setScreenshotURL( results.template_thumbnail ); |
| 102 | elementor.notifications.showToast( { |
| 103 | message: __( |
| 104 | 'Template Data Refreshed.', |
| 105 | 'templates-patterns-collection' |
| 106 | ), |
| 107 | } ); |
| 108 | window.tiTpc.postModel.set( 'meta', { |
| 109 | _ti_tpc_template_id: templateID, |
| 110 | _ti_tpc_template_sync: templateSync, |
| 111 | _ti_tpc_screenshot_url: screenshotURL, |
| 112 | _ti_tpc_site_slug: siteSlug, |
| 113 | _ti_tpc_published: isPublished, |
| 114 | } ); |
| 115 | |
| 116 | window.tiTpc.postModel.save(); |
| 117 | } |
| 118 | } ); |
| 119 | } catch ( error ) { |
| 120 | elementor.notifications.showToast( { |
| 121 | message: |
| 122 | 'Something happened when refreshing the template data.', |
| 123 | } ); |
| 124 | } |
| 125 | setLoading( false ); |
| 126 | }; |
| 127 | |
| 128 | const publishPage = async () => { |
| 129 | setLoading( true ); |
| 130 | try { |
| 131 | await publishTemplate( { |
| 132 | template_id: templateID, |
| 133 | template_site_slug: siteSlug, |
| 134 | template_thumbnail: screenshotURL, |
| 135 | premade: ! isPublished ? 'yes' : 'no', |
| 136 | link: elementor.config.initial_document.urls.permalink, |
| 137 | } ).then( async ( r ) => { |
| 138 | if ( r.success ) { |
| 139 | await getTemplate( templateID ).then( ( results ) => { |
| 140 | if ( templateID === results.template_id ) { |
| 141 | setScreenshotURL( results.template_thumbnail ); |
| 142 | elementor.notifications.showToast( { |
| 143 | message: ! isPublished |
| 144 | ? window.tiTpc.exporter.templatePublished |
| 145 | : window.tiTpc.exporter.templateUnpublished, |
| 146 | } ); |
| 147 | |
| 148 | setPublished( ! isPublished ); |
| 149 | |
| 150 | window.tiTpc.postModel.set( 'meta', { |
| 151 | _ti_tpc_template_id: templateID, |
| 152 | _ti_tpc_template_sync: templateSync, |
| 153 | _ti_tpc_screenshot_url: screenshotURL, |
| 154 | _ti_tpc_site_slug: siteSlug, |
| 155 | _ti_tpc_published: ! isPublished, |
| 156 | } ); |
| 157 | |
| 158 | window.tiTpc.postModel.save(); |
| 159 | } |
| 160 | } ); |
| 161 | } |
| 162 | } ); |
| 163 | } catch ( error ) { |
| 164 | elementor.notifications.showToast( { |
| 165 | message: 'Something happened when publishing the template.', |
| 166 | } ); |
| 167 | } |
| 168 | |
| 169 | setLoading( false ); |
| 170 | }; |
| 171 | |
| 172 | const descriptionStyles = { |
| 173 | width: '650px', |
| 174 | margin: 'auto', |
| 175 | marginTop: '8px', |
| 176 | padding: '0 12px', |
| 177 | }; |
| 178 | |
| 179 | return ( |
| 180 | <div className="dialog-message dialog-lightbox-message"> |
| 181 | <div className="dialog-content dialog-lightbox-content"> |
| 182 | <div className="ti-tpc-template-library-export"> |
| 183 | <div className="ti-tpc-template-library-blank-icon"> |
| 184 | <i |
| 185 | className="eicon-library-save" |
| 186 | aria-hidden="true" |
| 187 | ></i> |
| 188 | <span className="elementor-screen-only"> |
| 189 | { window.tiTpc.library.export.save } |
| 190 | </span> |
| 191 | </div> |
| 192 | |
| 193 | <div className="ti-tpc-template-library-blank-title"> |
| 194 | { window.tiTpc.library.export.title } |
| 195 | </div> |
| 196 | |
| 197 | <div className="ti-tpc-template-library-blank-field"> |
| 198 | <input |
| 199 | className="ti-tpc-template-library-blank-field-input" |
| 200 | value={ |
| 201 | elementor.config.initial_document.settings |
| 202 | .settings.post_title |
| 203 | } |
| 204 | disabled |
| 205 | /> |
| 206 | |
| 207 | <Button |
| 208 | className={ classnames( |
| 209 | 'elementor-button elementor-button-success', |
| 210 | { 'elementor-button-state': isLoading } |
| 211 | ) } |
| 212 | onClick={ exportPage } |
| 213 | > |
| 214 | <span className="elementor-state-icon"> |
| 215 | <i |
| 216 | className="eicon-loading eicon-animation-spin" |
| 217 | aria-hidden="true" |
| 218 | ></i> |
| 219 | </span> |
| 220 | { window.tiTpc.library.export.save } |
| 221 | </Button> |
| 222 | </div> |
| 223 | |
| 224 | <div className="ti-tpc-template-library-blank-field"> |
| 225 | <ToggleControl |
| 226 | label={ window.tiTpc.exporter.toggleLabel } |
| 227 | checked={ templateSync } |
| 228 | onChange={ () => setTemplateSync( ! templateSync ) } |
| 229 | /> |
| 230 | </div> |
| 231 | |
| 232 | { window.tiTpc.canPredefine && ( |
| 233 | <> |
| 234 | <div className="ti-tpc-template-library-blank-field"> |
| 235 | <label |
| 236 | htmlFor="ti-tpc-template-screenshot" |
| 237 | className="ti-tpc-template-library-blank-field-input-label" |
| 238 | > |
| 239 | { |
| 240 | window.tiTpc.library.export |
| 241 | .labelScreenshot |
| 242 | } |
| 243 | </label> |
| 244 | |
| 245 | <input |
| 246 | className="ti-tpc-template-library-blank-field-input" |
| 247 | id="ti-tpc-template-screenshot" |
| 248 | value={ screenshotURL } |
| 249 | onChange={ ( e ) => |
| 250 | setScreenshotURL( e.target.value ) |
| 251 | } |
| 252 | /> |
| 253 | </div> |
| 254 | <p style={ descriptionStyles }> |
| 255 | { __( |
| 256 | 'Use `{generate_ss}` to publish this and have a screenshot automatically generated. Otherwise use the url to point to an image location for the template preview.', |
| 257 | 'templates-patterns-collection' |
| 258 | ) } |
| 259 | </p> |
| 260 | |
| 261 | <div className="ti-tpc-template-library-blank-field"> |
| 262 | <label |
| 263 | htmlFor="ti-tpc-template-slug" |
| 264 | className="ti-tpc-template-library-blank-field-input-label" |
| 265 | > |
| 266 | { window.tiTpc.library.export.labelSlug } |
| 267 | </label> |
| 268 | |
| 269 | <input |
| 270 | className="ti-tpc-template-library-blank-field-input" |
| 271 | id="ti-tpc-template-slug" |
| 272 | value={ siteSlug } |
| 273 | onChange={ ( e ) => |
| 274 | setSiteSlug( e.target.value ) |
| 275 | } |
| 276 | /> |
| 277 | </div> |
| 278 | <p style={ descriptionStyles }> |
| 279 | { __( |
| 280 | 'Use `general` to publish this as a global template. Otherwise use the starter site slug to make it available as a single page for the starter site.', |
| 281 | 'templates-patterns-collection' |
| 282 | ) } |
| 283 | </p> |
| 284 | |
| 285 | <div className="ti-tpc-template-library-blank-field"> |
| 286 | <Button |
| 287 | className={ classnames( |
| 288 | 'elementor-button elementor-button-success', |
| 289 | { 'elementor-button-state': isLoading } |
| 290 | ) } |
| 291 | onClick={ publishPage } |
| 292 | > |
| 293 | <span className="elementor-state-icon"> |
| 294 | <i |
| 295 | className="eicon-loading eicon-animation-spin" |
| 296 | aria-hidden="true" |
| 297 | ></i> |
| 298 | </span> |
| 299 | { isPublished |
| 300 | ? window.tiTpc.library.export.unpublish |
| 301 | : window.tiTpc.library.export.publish } |
| 302 | </Button> |
| 303 | |
| 304 | { isPublished && ( |
| 305 | <Button |
| 306 | className={ classnames( |
| 307 | 'elementor-button elementor-button-success', |
| 308 | { |
| 309 | 'elementor-button-state': |
| 310 | isLoading, |
| 311 | } |
| 312 | ) } |
| 313 | onClick={ refreshData } |
| 314 | style={ { |
| 315 | backgroundColor: 'dimgray', |
| 316 | marginLeft: '12px', |
| 317 | } } |
| 318 | > |
| 319 | <span className="elementor-state-icon"> |
| 320 | <i |
| 321 | className="eicon-loading eicon-animation-spin" |
| 322 | aria-hidden="true" |
| 323 | ></i> |
| 324 | </span> |
| 325 | { __( |
| 326 | 'Refresh', |
| 327 | 'templates-patterns-collection' |
| 328 | ) } |
| 329 | </Button> |
| 330 | ) } |
| 331 | </div> |
| 332 | </> |
| 333 | ) } |
| 334 | </div> |
| 335 | </div> |
| 336 | </div> |
| 337 | ); |
| 338 | }; |
| 339 | |
| 340 | export default withDispatch( ( dispatch ) => { |
| 341 | const { updateCurrentTab } = dispatch( 'tpc/elementor' ); |
| 342 | |
| 343 | return { |
| 344 | updateCurrentTab, |
| 345 | }; |
| 346 | } )( Export ); |
| 347 |