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

Commit

Permalink
Merge 9742159 into c6605fe
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Sep 17, 2016
2 parents c6605fe + 9742159 commit 03623d4
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 5 deletions.
88 changes: 88 additions & 0 deletions js/customize-post-section.js
Expand Up @@ -65,6 +65,13 @@
args.params.title = api.Posts.data.l10n.noTitle;
}

// Sync the page data to any dropdown-pages controls.
if ( 'page' === args.params.post_type ) {
setting.bind( function( newData, oldData ) {
section.syncPageData( newData, oldData );
} );
}

section.postFieldControls = {};

section.contentsEmbedded = $.Deferred();
Expand Down Expand Up @@ -903,6 +910,87 @@
isContextuallyActive: function() {
var section = this;
return section.active();
},

/**
* Sync page changes to all dropdown-pages controls and to the page_on_front/page_for_posts settings.
*
* @see wp.customize.Posts.preventStaticFrontPageCollision()
* @see wp.customize.Posts.PostSection.removeFromDropdownPagesControls()
* @this {wp.customize.Section}
* @param {Object} newPostData Updated page data.
* @returns {void}
*/
syncPageData: function syncPageData( newPostData ) {
var section = this;

// Make sure the page_for_posts and page_on_front settings get unset if the selected page is trashed.
if ( 'trash' === newPostData.post_status ) {
_.each( [ api( 'page_for_posts' ), api( 'page_on_front' ) ], function( setting ) {
if ( setting && parseInt( setting.get(), 10 ) === section.params.post_id ) {
setting.set( 0 );
}
} );
}

// Update the dropdown-pages controls.
api.control.each( function( control ) {
var pageOption, select, isTrashed, optionText;

// Skip anything but the dropdown-pages control, including the Customize Object Selector control..
if ( 'dropdown-pages' !== control.params.type ) {
return;
}

// Remove a trashed post from being selected.
isTrashed = 'trash' === newPostData.post_status;
if ( isTrashed && parseInt( control.setting.get(), 10 ) === section.params.post_id ) {
control.setting.set( 0 );
}

// Sync the page option into the select options.
optionText = newPostData.post_title || api.Posts.data.l10n.noTitle;
if ( isTrashed ) {
optionText = api.Posts.data.l10n.dropdownPagesOptionTrashed.replace( '%s', optionText );
}
select = control.container.find( 'select' );
pageOption = select.find( 'option[value="' + String( section.params.post_id ) + '"]' );
if ( 0 === pageOption.length ) {
pageOption = $( new Option(
optionText,
section.params.post_id
) );
select.append( pageOption );
} else {
pageOption.text( optionText );
}

// Note that the option may or may not also be hidden. The visibility is tied a collision-prevention state.
pageOption.prop( 'disabled', isTrashed );
});
},

/**
* Remove pages from dropdown-pages controls.
*
* @returns {void}
*/
removeFromDropdownPagesControls: function() {
var section = this;

api.control.each( function( control ) {

// Skip anything but the dropdown-pages control, including the Customize Object Selector control..
if ( 'dropdown-pages' !== control.params.type ) {
return;
}

// Remove the option for the page.
control.container
.find( 'select' )
.find( 'option[value="' + String( section.params.post_id ) + '"]' )
.remove();
});
}
});

Expand Down
89 changes: 85 additions & 4 deletions js/customize-posts.js
Expand Up @@ -421,6 +421,10 @@
// @todo Also remove all postmeta settings for this post?
api.remove( section.id );
delete component.fetchedPosts[ section.params.post_id ];

if ( 'page' === section.params.post_type ) {
section.removeFromDropdownPagesControls();
}
}
} );
};
Expand Down Expand Up @@ -534,6 +538,83 @@
} );
};

/**
* Prevent the page on front and the page for posts from being set to be the same.
*
* Note that when the static front page is set to a given page, this same page will
* be hidden from the page on front dropdown, and vice versa. In contrast, when
* a page is trashed it will be *disabled* in the dropdowns. So there are two states
* that effect whether or not an option should be selected. So it is taking advantage
* of the `disabled` and `hidden` to correspond to these two separate states so that
* they don't overwrite each other and accidentally allow an option to be selected.
*
* @see wp.customize.Posts.PostSection.syncPageData()
* @see wp.customize.Posts.PostSection.removeFromDropdownPagesControls()
*
* See also https://github.com/xwp/wp-customize-object-selector/blob/develop/js/customize-object-selector-static-front-page.js
*
* @returns {void}
*/
component.preventStaticFrontPageCollision = function preventStaticFrontPageCollision() {

api( 'page_for_posts', 'page_on_front', function( pageForPostsSetting, pageOnFrontSetting ) {

// Prevent the settings from being set to be the same.
pageForPostsSetting.bind( function onChangePageForPosts( pageId ) {
if ( parseInt( pageId, 10 ) === parseInt( pageOnFrontSetting.get(), 10 ) ) {
pageOnFrontSetting.set( 0 );
}
} );
pageOnFrontSetting.bind( function onChangePageOnFront( pageId ) {
if ( parseInt( pageId, 10 ) === parseInt( pageForPostsSetting.get(), 10 ) ) {
pageForPostsSetting.set( 0 );
}
} );

// Hide the page options to prevent selecting the same. Note that trashed posts get disabled
api.control( 'page_for_posts', 'page_on_front', function( pageForPostsControl, pageOnFrontControl ) {
var onChangePageForPostsControl, onChangePageOnFrontControl;

if ( 'dropdown-pages' !== pageForPostsControl.params.type || 'dropdown-pages' !== pageOnFrontControl.params.type ) {
return;
}

// Note that the options below may or may not also be disabled. The disabled is tied a the trashed state of the pages.
onChangePageForPostsControl = function( newPageForPosts, oldPageForPosts ) {
var oldPageForPostsId, newPageForPostsId;
oldPageForPostsId = parseInt( oldPageForPosts, 10 );
newPageForPostsId = parseInt( newPageForPosts, 10 );
if ( 0 !== oldPageForPostsId ) {
pageOnFrontControl.container.find( 'option[value="' + String( oldPageForPostsId ) + '"]' ).show();
}
if ( 0 !== newPageForPostsId ) {
pageOnFrontControl.container.find( 'option[value="' + String( newPageForPostsId ) + '"]' ).hide();
}
};

onChangePageOnFrontControl = function( newPageOnFront, oldPageOnFront ) {
var oldPageOnFrontId, newPageOnFrontId;
oldPageOnFrontId = parseInt( oldPageOnFront, 10 );
newPageOnFrontId = parseInt( newPageOnFront, 10 );
if ( 0 !== oldPageOnFrontId ) {
pageForPostsControl.container.find( 'option[value="' + String( oldPageOnFrontId ) + '"]' ).show();
}
if ( 0 !== newPageOnFrontId ) {
pageForPostsControl.container.find( 'option[value="' + String( newPageOnFrontId ) + '"]' ).hide();
}
};

$.when( pageForPostsControl.deferred.embedded, pageOnFrontControl.deferred.embedded ).done( function() {
pageForPostsSetting.bind( onChangePageForPostsControl );
onChangePageForPostsControl( pageForPostsSetting.get() );
pageOnFrontSetting.bind( onChangePageOnFrontControl );
onChangePageOnFrontControl( pageOnFrontSetting.get() );
} );
} );
} );

};

/**
* Ensure that the post associated with an autofocused section or control is loaded.
*
Expand Down Expand Up @@ -583,13 +664,10 @@
if ( data.saved_post_setting_values ) {
component.updateSettingsQuietly( data.saved_post_setting_values );
}

component.purgeTrash();
} );

/**
* Ensure a post is added to the Customizer and focus on its section when an edit post link is clicked in preview.
*/
// Ensure a post is added to the Customizer and focus on its section when an edit post link is clicked in preview.
api.previewer.bind( 'edit-post', function( postId ) {
var ensuredPromise = api.Posts.ensurePosts( [ postId ] );
ensuredPromise.done( function( postsData ) {
Expand All @@ -600,6 +678,9 @@
} );
} );

// Prevent page_on_front and page_for_posts from being set to be the same.
component.preventStaticFrontPageCollision();

api.previewer.bind( 'focus-control', component.focusControl );

component.ensureAutofocusConstructPosts();
Expand Down
4 changes: 3 additions & 1 deletion php/class-wp-customize-posts.php
Expand Up @@ -377,10 +377,10 @@ public function ensure_static_front_page_constructs_registered( WP_Customize_Man
public function has_published_pages() {

// @todo Also look to see if there are any pages among in $this->get_setting( 'nav_menus_created_posts' )->value().
// Note we cannot use number=>1 since the first-returned page may be previewed to not be published.
return 0 !== count( get_pages( array(
'post_type' => 'page',
'post_status' => 'publish',
'number' => 1,
) ) );
}

Expand Down Expand Up @@ -723,6 +723,8 @@ public function enqueue_scripts() {
'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' ),
/* translators: %s is the trashed page name */
'dropdownPagesOptionTrashed' => __( '%s (Trashed)', 'customize-posts' ),
'installCustomizeObjectSelector' => sprintf(
__( 'This control depends on having the %s plugin installed and activated.', 'customize-posts' ),
sprintf(
Expand Down

0 comments on commit 03623d4

Please sign in to comment.