Skip to content

Commit 3921f5b

Browse files
release: Allow Page Templates Support
- Allow Page Templates Support
2 parents 8934195 + 0d8ef52 commit 3921f5b

File tree

17 files changed

+389
-552
lines changed

17 files changed

+389
-552
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
**Contributors:** [themeisle](https://profiles.wordpress.org/themeisle)
33
**Tags:** neve, templates, patterns, blocks, starter, sites, demo, content, import
44
**Requires at least:** 4.7
5-
**Tested up to:** 5.7
5+
**Tested up to:** 5.8
66
**Requires PHP:** 5.6
77
**Stable tag:** trunk
88
**License:** GPLv3

editor/build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-keycodes', 'wp-plugins', 'wp-polyfill', 'wp-primitives'), 'version' => '6dee5b8d2c58119d8483ef50207dfa20');
1+
<?php return array('dependencies' => array('wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-keycodes', 'wp-plugins', 'wp-polyfill', 'wp-primitives'), 'version' => 'e0e46189f874af1ba2715737a636a00d');

editor/build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editor/src/data/templates-cloud/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-undef */
12
/* global localStorage, tiTpc */
23
import { stringifyUrl } from 'query-string';
34
import { v4 as uuidv4 } from 'uuid';
@@ -282,7 +283,6 @@ export const publishTemplate = async (
282283
premade: publishStatus ? 'yes' : 'no',
283284
link,
284285
...omit( tiTpc.params, 'meta' ),
285-
meta: JSON.stringify( tiTpc.params.meta )
286286
},
287287
} );
288288

editor/src/edit.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
/* eslint-disable no-undef */
12
/* eslint-disable camelcase */
2-
import { withDispatch, withSelect, useDispatch, useSelect } from '@wordpress/data';
3+
import {
4+
withDispatch,
5+
withSelect,
6+
useDispatch,
7+
useSelect,
8+
} from '@wordpress/data';
39
import { useState, useEffect } from '@wordpress/element';
410
import { Modal, Button } from '@wordpress/components';
511
import { compose } from '@wordpress/compose';
@@ -12,6 +18,8 @@ import Content from './components/content';
1218
import PreviewFrame from '../../assets/src/Components/CloudLibrary/PreviewFrame';
1319
import { importTemplate } from './data/templates-cloud';
1420

21+
const { omit } = lodash;
22+
1523
const Edit = ( {
1624
clientId,
1725
isPreview,
@@ -21,9 +29,7 @@ const Edit = ( {
2129
replaceBlocks,
2230
closePreview,
2331
} ) => {
24-
const {
25-
type,
26-
} = useSelect( ( select ) => ( {
32+
const { type } = useSelect( ( select ) => ( {
2733
type: select( 'core/editor' ).getEditedPostAttribute( 'type' ),
2834
} ) );
2935

@@ -100,14 +106,42 @@ const Edit = ( {
100106
return sortingOrder.library;
101107
};
102108

109+
const tryParseJSON = ( jsonString ) => {
110+
try {
111+
const o = JSON.parse( jsonString );
112+
113+
// Handle non-exception-throwing cases:
114+
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking,
115+
// but... JSON.parse(null) returns null, and typeof null === "object",
116+
// so we must check for that, too. Thankfully, null is falsey, so this suffices:
117+
// Source: https://stackoverflow.com/a/20392392
118+
if ( o && typeof o === 'object' ) {
119+
return o;
120+
}
121+
} catch ( e ) {}
122+
123+
return false;
124+
};
125+
103126
const importBlocks = ( content, metaFields = [] ) => {
104127
updateLibrary( [] );
105128
updateTemplates( [] );
106129

107-
if ( 0 < metaFields.length && [ 'post', 'page' ].includes( type ) ) {
108-
editPost( {
109-
meta: { ...JSON.parse( metaFields ) },
110-
} );
130+
if (
131+
0 < Object.keys( tryParseJSON( metaFields ) || {} ).length &&
132+
[ 'post', 'page' ].includes( type )
133+
) {
134+
const fields = JSON.parse( metaFields );
135+
const meta = {
136+
...omit( { ...fields }, '_wp_page_template' ),
137+
};
138+
editPost( { meta } );
139+
140+
if ( 'page' === type && fields._wp_page_template ) {
141+
editPost( {
142+
template: fields._wp_page_template,
143+
} );
144+
}
111145
}
112146

113147
replaceBlocks( clientId, parse( content ) );

editor/src/extension.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-undef */
12
/* eslint-disable camelcase */
23
import { __ } from '@wordpress/i18n';
34
import apiFetch from '@wordpress/api-fetch';
@@ -66,10 +67,28 @@ const Exporter = () => {
6667
return getEditedPostAttribute( 'meta' );
6768
}, [] );
6869

70+
const pageTemplate = useSelect( ( select ) => {
71+
const { getEditedPostAttribute } = select( 'core/editor' );
72+
return getEditedPostAttribute( 'template' );
73+
}, [] );
74+
6975
useEffect( () => {
7076
const metaKeys = window.tiTpc.metaKeys;
71-
window.tiTpc.params.meta = Object.fromEntries( Object.entries( getMetaFields || {} ).filter(( [key, value] ) => metaKeys.includes( key )) );
72-
}, [ getMetaFields ] );
77+
window.tiTpc.params.meta = Object.fromEntries(
78+
Object.entries( getMetaFields || {} ).filter( ( [ key ] ) =>
79+
metaKeys.includes( key )
80+
)
81+
);
82+
83+
if ( pageTemplate ) {
84+
window.tiTpc.params.meta._wp_page_template = pageTemplate;
85+
} else if (
86+
'' === pageTemplate &&
87+
window.tiTpc.params.meta._wp_page_template
88+
) {
89+
delete window.tiTpc.params.meta._wp_page_template;
90+
}
91+
}, [ getMetaFields, pageTemplate ] );
7392

7493
const {
7594
meta,

elementor/build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-keycodes', 'wp-polyfill', 'wp-primitives'), 'version' => '6eb35ac9420a3150e7242a1550f19a2b');
1+
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-keycodes', 'wp-polyfill', 'wp-primitives'), 'version' => 'd3c730dad6ad805a41cf255b7c7781b9');

elementor/build/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

elementor/src/components/export.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,23 @@ const Export = ( { updateCurrentTab } ) => {
5252
doesExist = await getTemplate( templateID );
5353
}
5454

55+
const meta = window.tiTpc.params.meta;
56+
57+
const currentTemplate = elementor.documents
58+
.getCurrent()
59+
.container.settings.get( 'template' );
60+
61+
if ( currentTemplate ) {
62+
meta._wp_page_template = currentTemplate;
63+
}
64+
5565
if ( doesExist ) {
5666
await updateTemplate( {
5767
template_id: templateID,
5868
template_name: title,
5969
content,
6070
link: elementor.config.initial_document.urls.permalink,
61-
meta: JSON.stringify( tiTpc.params.meta ),
71+
meta: JSON.stringify( meta ),
6272
} );
6373
} else {
6474
await exportTemplate( {

elementor/src/components/templates-content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ const TemplatesContent = ( {
136136
item={ item }
137137
id={ item.template_id }
138138
title={ item.template_name }
139-
meta={ item.meta || [] }
139+
meta={ item.meta || '' }
140140
onImport={ onImport }
141141
onUpdateTemplate={ onUpdateTemplate }
142142
onDelete={ onDelete }

0 commit comments

Comments
 (0)