-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Add ability to set page/post template #4662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
21a60c0
Added ability to set page/post template.
jorgefilipecosta 31baa6b
Used correct attribute label, instead of "Page Attributes" for all po…
jorgefilipecosta 8a01337
Updated PageAttributesCheck to show if post supports page attributes …
jorgefilipecosta 9c1604f
Only show order input field if post type supports page attributes, sa…
jorgefilipecosta 9af433d
Added pageparentdiv to the core_side_meta_boxes array.
jorgefilipecosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .editor-page-attributes__template { | ||
| label, select { | ||
| width: 100%; | ||
| } | ||
| margin-bottom: 10px; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { connect } from 'react-redux'; | ||
| import { isEmpty, map } from 'lodash'; | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { withContext, withInstanceId } from '@wordpress/components'; | ||
| import { compose } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import './style.scss'; | ||
| import { getEditedPostAttribute } from '../../store/selectors'; | ||
| import { editPost } from '../../store/actions'; | ||
|
|
||
| export function PageTemplate( { availableTemplates, selectedTemplate, instanceId, onUpdate } ) { | ||
| if ( isEmpty( availableTemplates ) ) { | ||
| return null; | ||
| } | ||
| const selectId = `template-selector-${ instanceId }`; | ||
| const onEventUpdate = ( event ) => onUpdate( event.target.value ); | ||
| return ( | ||
| <div className="editor-page-attributes__template"> | ||
| <label htmlFor={ selectId }>{ __( 'Template:' ) }</label> | ||
| <select | ||
| id={ selectId } | ||
| value={ selectedTemplate } | ||
| onBlur={ onEventUpdate } | ||
| onChange={ onEventUpdate } | ||
| > | ||
| { map( { '': __( 'Default template' ), ...availableTemplates }, ( templateName, templateSlug ) => ( | ||
| <option key={ templateSlug } value={ templateSlug }>{ templateName }</option> | ||
| ) ) } | ||
| </select> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const applyConnect = connect( | ||
| ( state ) => { | ||
| return { | ||
| selectedTemplate: getEditedPostAttribute( state, 'template' ), | ||
| }; | ||
| }, | ||
| { | ||
| onUpdate( templateSlug ) { | ||
| return editPost( { template: templateSlug || '' } ); | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| const applyWithContext = withContext( 'editor' )( | ||
| ( settings ) => ( { | ||
| availableTemplates: settings.availableTemplates, | ||
| } ) | ||
| ); | ||
|
|
||
| export default compose( | ||
| applyConnect, | ||
| applyWithContext, | ||
| withInstanceId, | ||
| )( PageTemplate ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,15 +17,15 @@ describe( 'PageAttributesCheck', () => { | |
| }, | ||
| }; | ||
|
|
||
| it( 'should not render anything if unknown page attributes support', () => { | ||
| it( 'should not render anything if unknown page attributes and available templates support', () => { | ||
| const wrapper = shallow( <PageAttributesCheck postType={ {} }>content</PageAttributesCheck> ); | ||
|
|
||
| expect( wrapper.type() ).toBe( null ); | ||
| } ); | ||
|
|
||
| it( 'should not render anything if no page attributes support', () => { | ||
| it( 'should not render anything if no page attributes support and no available templates exist', () => { | ||
| const wrapper = shallow( | ||
| <PageAttributesCheck postType={ { | ||
| <PageAttributesCheck availableTemplates={ {} } postType={ { | ||
|
||
| data: { | ||
| supports: { | ||
| 'page-attributes': false, | ||
|
|
@@ -39,9 +39,21 @@ describe( 'PageAttributesCheck', () => { | |
| expect( wrapper.type() ).toBe( null ); | ||
| } ); | ||
|
|
||
| it( 'should render if page attributes support', () => { | ||
| it( 'should render if page attributes support is true and no available templates exist', () => { | ||
| const wrapper = shallow( <PageAttributesCheck postType={ postType }>content</PageAttributesCheck> ); | ||
|
|
||
| expect( wrapper.type() ).not.toBe( null ); | ||
| expect( wrapper ).toHaveText( 'content' ); | ||
| } ); | ||
|
|
||
| it( 'should render if page attributes support is false/unknown and available templates exist', () => { | ||
| const wrapper = shallow( <PageAttributesCheck availableTemplates={ { 'example.php': 'Example template' } } >content</PageAttributesCheck> ); | ||
|
|
||
| expect( wrapper ).toHaveText( 'content' ); | ||
| } ); | ||
|
|
||
| it( 'should render if page attributes support is true and available templates exist', () => { | ||
| const wrapper = shallow( <PageAttributesCheck availableTemplates={ { 'example.php': 'Example template' } } postType={ postType }>content</PageAttributesCheck> ); | ||
|
|
||
| expect( wrapper ).toHaveText( 'content' ); | ||
| } ); | ||
| } ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space missing after
'', before:.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we add the space e.g make things like ''' : __( 'Default template' )' we get a linting error because of the extra space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skip that, I thought it is
ifstatement :)