Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Add control to manipulate the post parent #233

Merged
merged 28 commits into from
Sep 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d7ff904
Add Page Parent label. #65
sunnyratilal Jun 26, 2016
c52644d
Add control for page parent if supported
sunnyratilal Jun 26, 2016
706814f
Add in page parent control. #65
sunnyratilal Jun 26, 2016
4e762ab
Add in code to display page parent. #65
sunnyratilal Jun 26, 2016
4fa7fb5
Remove unecessary line breaks
sunnyratilal Jun 26, 2016
4aea0c6
Remove unused vars
sunnyratilal Jun 26, 2016
a8d35ad
Fix translation error
sunnyratilal Jun 26, 2016
42a47d9
Ensure that the current page is not listed as in the dropdown for pag…
sunnyratilal Jun 26, 2016
2eceeed
Fix WordPress.Arrays.ArrayDeclaration.NoComma
sunnyratilal Jun 26, 2016
2aaefe7
Merge branch 'develop' of https://github.com/sunnyratilal/wp-customiz…
westonruter Aug 30, 2016
2337cdc
Add support for post parent control via object selector
westonruter Aug 30, 2016
d484097
Merge branch 'develop' of https://github.com/xwp/wp-customize-posts i…
westonruter Sep 1, 2016
9783f42
Add recognition of 'parent' post type support for adding post parent …
westonruter Sep 1, 2016
a59f620
Remove 'post' from post parent identifiers
westonruter Sep 1, 2016
62188fb
Remove incorrect esc_html() used in JSON context
westonruter Sep 1, 2016
312e81c
Remove escaping from outputing description in dynamic control template
westonruter Sep 2, 2016
a8e6b35
Make customize object selector plugin a dependency for post parent co…
westonruter Sep 2, 2016
e7227f4
Add tree_args post_query_var via https://github.com/xwp/wp-customize-…
westonruter Sep 3, 2016
58502b5
Use parent_item_colon for parent label; add placeholder and allowClear
westonruter Sep 3, 2016
e9c4c06
Merge branch 'develop' of https://github.com/xwp/wp-customize-posts i…
westonruter Sep 9, 2016
1e64bb4
Add rudimentary support for number arg for get_pages()
westonruter Sep 9, 2016
f7002c5
Disable add buttons for post parent control
westonruter Sep 9, 2016
0f332e9
Let changes to post_parent cause preview refresh
westonruter Sep 9, 2016
8bd0015
Update object selector args
westonruter Sep 10, 2016
b989014
Add show_initial_dropdown post query var
westonruter Sep 10, 2016
6c41c6c
Ensure featured image and post author are cast to integers (prevent i…
westonruter Sep 10, 2016
a41304f
Add syncing of parent between edit post screen and customizer post pr…
westonruter Sep 10, 2016
ffdbe67
Defer updating settings with previewed values until preview has fully…
westonruter Sep 10, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions js/customize-post-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@
section.addStatusControl();
section.addDateControl();
}
if ( postTypeObj.supports['page-attributes'] || postTypeObj.supports.parent ) {
section.addParentControl();
}
if ( postTypeObj.supports.editor ) {
section.addContentControl();
}
Expand Down Expand Up @@ -471,6 +474,75 @@
return control;
},

/**
* Add parent control.
*
* @returns {wp.customize.Control} Added control.
*/
addParentControl: function() {
var section = this, control, setting = api( section.id ), controlId, params, postTypeObj;
postTypeObj = api.Posts.data.postTypes[ section.params.post_type ];

controlId = section.id + '[post_parent]';
params = {
section: section.id,
priority: 20,
label: postTypeObj.labels.parent_item_colon ? postTypeObj.labels.parent_item_colon.replace( /:$/, '' ) : api.Posts.data.l10n.fieldParentLabel,
active: true,
settings: {
'default': setting.id
},
field_type: 'select',
setting_property: 'post_parent'
};

if ( api.controlConstructor.object_selector ) {
control = new api.controlConstructor.object_selector( controlId, {
params: _.extend( params, {
post_query_vars: {
post_type: section.params.post_type,
post_status: 'publish',
post__not_in: [ section.params.post_id ],
show_initial_dropdown: true,
dropdown_args: {
exclude_tree: section.params.post_id,
sort_column: 'menu_order, post_title'
},
apply_dropdown_args_filters_post_id: section.params.post_id // Applies page_attributes_dropdown_pages_args filters.
},
show_add_buttons: false,
select2_options: {
multiple: false,
allowClear: true,
placeholder: postTypeObj.labels.search_items
}
} )
} );
} else {
control = new api.controlConstructor.dynamic( controlId, {
params: _.extend( params, {
field_type: 'hidden',
description: api.Posts.data.l10n.installCustomizeObjectSelector
} )
} );
}

// Override preview trying to de-activate control not present in preview context.
control.active.validate = function() {
return true;
};

// Register.
section.postFieldControls.page_parent = control;
api.control.add( control.id, control );

if ( control.notifications ) {
control.notifications.add = section.addPostFieldControlNotification;
control.notifications.setting_property = control.params.setting_property;
}
return control;
},

/**
* Add post date control.
*
Expand Down
2 changes: 1 addition & 1 deletion js/edit-post-preview-admin-featured-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
featuredImageId = parseInt( $( inputSelector ).val(), 10 );

if ( featuredImageId <= 0 ) {
featuredImageId = '';
featuredImageId = 0;
}

settings[ settingId ] = featuredImageId;
Expand Down
16 changes: 14 additions & 2 deletions js/edit-post-preview-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var EditPostPreviewAdmin = (function( $ ) {
settings = {},
postSettingValue,
editor = tinymce.get( 'content' ),
wasMobile;
wasMobile,
parentId;

event.preventDefault();

Expand All @@ -44,15 +45,23 @@ var EditPostPreviewAdmin = (function( $ ) {
// Override default close behavior.
wp.customize.Loader.close = component.closeLoader;

parentId = $( '#parent_id' ).val();
if ( ! parentId ) {
parentId = 0;
} else {
parentId = parseInt( parentId, 10 );
}

// Send the current input fields from the edit post page to the Customizer via sessionStorage.
postSettingValue = {
post_title: $( '#title' ).val(),
post_name: $( '#post_name' ).val(),
post_parent: parentId,
post_content: editor && ! editor.isHidden() ? wp.editor.removep( editor.getContent() ) : $( '#content' ).val(),
post_excerpt: $( '#excerpt' ).val(),
comment_status: $( '#comment_status' ).prop( 'checked' ) ? 'open' : 'closed',
ping_status: $( '#ping_status' ).prop( 'checked' ) ? 'open' : 'closed',
post_author: $( '#post_author_override' ).val()
post_author: parseInt( $( '#post_author_override' ).val(), 10 )
};
postSettingId = 'post[' + postType + '][' + postId + ']';
settings[ postSettingId ] = postSettingValue;
Expand All @@ -66,6 +75,7 @@ var EditPostPreviewAdmin = (function( $ ) {

// Sync changes from the Customizer to the post input fields.
wp.customize.Loader.messenger.bind( 'customize-post-settings-data', function( data ) {
var settingParentId;
if ( data[ postSettingId ] ) {
$( '#title' ).val( data[ postSettingId ].post_title ).trigger( 'change' );
if ( editor ) {
Expand All @@ -79,6 +89,8 @@ var EditPostPreviewAdmin = (function( $ ) {
$( '#ping_status' ).prop( 'checked', 'open' === data[ postSettingId ].ping_status ).trigger( 'change' );
$( '#post_author_override' ).val( data[ postSettingId ].post_author ).trigger( 'change' );
$( '#post_name' ).val( data[ postSettingId ].post_name ).trigger( 'change' );
settingParentId = data[ postSettingId ].post_parent;
$( '#parent_id' ).val( settingParentId > 0 ? String( settingParentId ) : '' ).trigger( 'change' );
$( '#new-post-slug' ).val( data[ postSettingId ].post_name );
$( '#editable-post-name, #editable-post-name-full' ).text( data[ postSettingId ].post_name );
}
Expand Down
25 changes: 17 additions & 8 deletions js/edit-post-preview-customize.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ var EditPostPreviewCustomize = (function( $, api ) {
}

component.init = function() {
component.populateSettings();

wp.customize.bind( 'ready', function() {
api.bind( 'ready', function() {
component.ready();
} );
};
Expand All @@ -28,6 +26,14 @@ var EditPostPreviewCustomize = (function( $, api ) {
*/
component.populateSettings = function() {
var itemId, settings;

// Only run once.
if ( component.settingsPopulated ) {
return;
}
component.settingsPopulated = true;
api.previewer.unbind( 'customized-posts', component.populateSettings );

itemId = 'previewedCustomizePostSettings[' + String( component.data.previewed_post.ID ) + ']';
settings = sessionStorage.getItem( itemId );
if ( ! settings ) {
Expand Down Expand Up @@ -58,8 +64,11 @@ var EditPostPreviewCustomize = (function( $, api ) {
*/
component.ready = function() {

// Wait to populate the settings until the customized-posts message is received so we know the preview is ready to receive mesages.
api.previewer.bind( 'customized-posts', component.populateSettings );

// Prevent 'saved' state from becoming false, since we only want to save from the admin page.
wp.customize.state( 'saved' ).set( true ).validate = function() {
api.state( 'saved' ).set( true ).validate = function() {
return true;
};

Expand All @@ -74,15 +83,15 @@ var EditPostPreviewCustomize = (function( $, api ) {
*
* @see wp.customize.Loader
*/
component.parentFrame = new wp.customize.Messenger( {
url: wp.customize.settings.url.parent,
component.parentFrame = new api.Messenger( {
url: api.settings.url.parent,
channel: 'loader'
} );

// @todo Include nonce?
component.parentFrame.bind( 'populate-setting', function( setting ) {
if ( wp.customize.has( setting.id ) ) {
wp.customize( setting.id ).set( setting.value );
if ( api.has( setting.id ) ) {
api( setting.id ).set( setting.value );
}
} );

Expand Down
4 changes: 2 additions & 2 deletions php/class-wp-customize-dynamic-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ protected function content_template() {
/>
{{{ data.label }}}
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<span class="description customize-control-description">{{{ data.description }}}</span>
<# } #>
</label>
<# } else { #>
<span class="customize-control-title"><label for="{{ data.input_id }}">{{ data.label }}</label></span>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<span class="description customize-control-description">{{{ data.description }}}</span>
<# } #>
<# if ( 'textarea' === data.field_type ) { #>
<textarea
Expand Down
12 changes: 10 additions & 2 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,11 @@ public function filter_the_posts_to_preview_settings( array $posts, WP_Query $qu
* @type string $sort_column Supported.
* @type string $authors Supported.
* @type string $post_status Supported.
* @type int $number Supported, but there won't be 100% fidelity due to customized posts being amended to the subset results without being aware of underlying placement in full results.
* @type array $exclude No special support needed.
* @type array $include No special support needed.
* @type string $meta_key Not supported.
* @type string $meta_value Not supported.
* @type int $number Not supported.
* @type int $offset Not supported.
* @type bool $hierarchical Not needing to be examined since this is a property of the registered post type itself.
* @type string $post_type Not needing to be examined since post_type is immutable.
Expand All @@ -293,7 +293,7 @@ public function filter_get_pages_to_preview_settings( $initial_posts, $args ) {
return $initial_posts;
}

$unsupported_args = array( 'number', 'offset', 'meta_key', 'meta_value' );
$unsupported_args = array( 'offset', 'meta_key', 'meta_value' );
foreach ( $unsupported_args as $unsupported_arg ) {
if ( ! empty( $args[ $unsupported_arg ] ) ) {
_doing_it_wrong( 'get_pages', sprintf( esc_html__( 'The %s argument for get_pages() is not supported by Customize Posts.', 'customize-posts' ), esc_html( $unsupported_arg ) ), '0.8.0' );
Expand Down Expand Up @@ -330,6 +330,7 @@ public function filter_get_pages_to_preview_settings( $initial_posts, $args ) {
$args['include'] = array_filter( wp_parse_id_list( $args['include'] ) );
$args['parent'] = intval( $args['parent'] );
$args['child_of'] = intval( $args['child_of'] );
$args['number'] = intval( $args['number'] );

if ( ! empty( $args['include'] ) ) {
$args['child_of'] = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
Expand Down Expand Up @@ -493,6 +494,10 @@ public function filter_get_pages_to_preview_settings( $initial_posts, $args ) {
usort( $filtered_posts, array( $this, 'compare_posts_for_get_pages' ) );
$this->current_get_pages_args = array();

if ( ! empty( $args['number'] ) ) {
$filtered_posts = array_slice( $filtered_posts, 0, $args['number'] );
}

return array_values( $filtered_posts );
}

Expand Down Expand Up @@ -1063,6 +1068,9 @@ public function get_post_field_partial_schema( $field_id = '' ) {
'post_status' => array(
'fallback_refresh' => true,
),
'post_parent' => array(
'fallback_refresh' => true,
),
'post_date' => array(
'selector' => 'time.entry-date',
'fallback_refresh' => false,
Expand Down
16 changes: 14 additions & 2 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ public function get_author_choices() {
foreach ( (array) $users as $user ) {
$choices[] = array(
'value' => (int) $user->ID,
'text' => esc_html( sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'customize-posts' ), $user->display_name, $user->user_login ) ),
'text' => sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'customize-posts' ), $user->display_name, $user->user_login ),
);
}
}
Expand Down Expand Up @@ -599,7 +599,7 @@ public function enqueue_scripts() {
$exports = array(
'postTypes' => $post_types,
'postStatusChoices' => $this->get_post_status_choices(),
'authorChoices' => $this->get_author_choices(),
'authorChoices' => $this->get_author_choices(), // @todo Use Ajax to fetch this data or Customize Object Selector (once it supports users).
'dateMonthChoices' => $this->get_date_month_choices(),
'initialServerDate' => current_time( 'mysql', false ),
'initialServerTimestamp' => floor( microtime( true ) * 1000 ),
Expand All @@ -614,11 +614,23 @@ public function enqueue_scripts() {
'fieldExcerptLabel' => __( 'Excerpt', 'customize-posts' ),
'fieldDiscussionLabel' => __( 'Discussion', 'customize-posts' ),
'fieldAuthorLabel' => __( 'Author', 'customize-posts' ),
'fieldParentLabel' => __( 'Parent', 'customize-posts' ),
'noTitle' => __( '(no title)', 'customize-posts' ),
'theirChange' => __( 'Their change: %s', 'customize-posts' ),
'openEditor' => __( 'Open Editor', 'customize-posts' ), // @todo Move this into editor control?
'closeEditor' => __( 'Close Editor', 'customize-posts' ),
'invalidDateError' => __( 'Whoops, the provided date is invalid.', 'customize-posts' ),
'installCustomizeObjectSelector' => sprintf(
__( 'This control depends on having the %s plugin installed and activated.', 'customize-posts' ),
sprintf(
'<a href="%s" target="_blank">%s</a>',
'https://github.com/xwp/wp-customize-object-selector',
__( 'Customize Object Selector', 'customize-posts' )
)
),

/* translators: %s post type */
'jumpToPostPlaceholder' => __( 'Jump to %s', 'customize-posts' ),
),
);

Expand Down
34 changes: 34 additions & 0 deletions tests/php/test-class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,40 @@ public function test_filter_get_pages_to_preview_settings_parent_and_exclude_tre
$this->assertEquals( array( $page_c, $page_e ), wp_list_pluck( get_pages( array( 'parent' => $page_b ) ), 'ID' ) );
}

/**
* Test get_pages() with number args.
*
* @see get_pages()
* @covers WP_Customize_Posts_Preview::filter_get_pages_to_preview_settings()
*/
public function test_filter_get_pages_to_preview_settings_number() {
$page_a = $this->factory()->post->create( array( 'post_title' => 'Page A', 'post_type' => 'page' ) );
$page_a1 = $this->factory()->post->create( array( 'post_title' => 'Page A.1', 'post_type' => 'page', 'post_parent' => $page_a ) );
$page_a2 = $this->factory()->post->create( array( 'post_title' => 'Page A.2', 'post_type' => 'page', 'post_parent' => $page_a ) );

$pages = get_pages( array( 'parent' => $page_a, 'number' => 1, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
$this->assertEquals( array( $page_a1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => $page_a, 'number' => 1, 'sort_column' => 'post_title', 'sort_order' => 'DESC' ) );
$this->assertEquals( array( $page_a2 ), wp_list_pluck( $pages, 'ID' ) );

// Now try adding a new sibling.
$this->posts_component->preview->customize_preview_init();
$page_a3_post_obj = $this->posts_component->insert_auto_draft_post( 'page' );
$page_a3 = $page_a3_post_obj->ID;
$page_a3_setting_id = WP_Customize_Post_Setting::get_post_setting_id( get_post( $page_a3 ) );
$page_a3_setting = $this->posts_component->manager->add_setting( new WP_Customize_Post_Setting( $this->posts_component->manager, $page_a3_setting_id ) );
$this->posts_component->manager->set_post_value( $page_a3_setting_id, array_merge(
$page_a3_setting->value(),
array( 'post_parent' => $page_a, 'post_title' => 'Page A.3' )
) );
$page_a3_setting->preview();

$pages = get_pages( array( 'parent' => $page_a, 'number' => 1, 'sort_column' => 'post_title', 'sort_order' => 'ASC' ) );
$this->assertEquals( array( $page_a1 ), wp_list_pluck( $pages, 'ID' ) );
$pages = get_pages( array( 'parent' => $page_a, 'number' => 1, 'sort_column' => 'post_title', 'sort_order' => 'DESC' ) );
$this->assertEquals( array( $page_a3 ), wp_list_pluck( $pages, 'ID' ) );
}

/**
* Test filter_the_posts_to_tally_orderby_keys().
*
Expand Down
18 changes: 0 additions & 18 deletions tests/php/test-class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,23 +305,6 @@ public function test_get_post_status_choices() {
}
}

/**
* Tests get_author_choices().
*
* @covers WP_Customize_Posts::get_author_choices()
*/
public function test_get_author_choices() {
$posts = new WP_Customize_Posts( $this->wp_customize );
$choices = $posts->get_author_choices();
$this->assertTrue( count( $choices ) > 0 );
foreach ( $choices as $choice ) {
$this->assertInternalType( 'array', $choice );
$this->assertArrayHasKey( 'text', $choice );
$this->assertArrayHasKey( 'value', $choice );
$this->assertTrue( (bool) get_user_by( 'ID', $choice['value'] ) );
}
}

/**
* Get month choices.
*
Expand Down Expand Up @@ -385,7 +368,6 @@ public function test_enqueue_scripts() {
$this->assertArrayHasKey( 'post', $exports['postTypes'] );
$this->assertArrayHasKey( 'postStatusChoices', $exports );
$this->assertArrayHasKey( 'dateMonthChoices', $exports );
$this->assertArrayHasKey( 'authorChoices', $exports );
$this->assertArrayHasKey( 'initialServerDate', $exports );
$this->assertInternalType( 'int', strtotime( $exports['initialServerDate'] ) );
$this->assertArrayHasKey( 'initialServerTimestamp', $exports );
Expand Down