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

Make use of section notifications API in WP 4.9 when available; otherwise polyfill #366

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
56 changes: 39 additions & 17 deletions js/customize-post-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,37 +826,27 @@
},

/**
* Set up section notifications.
* Polyfill notifications API onto sections if not yet on WordPress 4.9 where it is defined in core.
*
* @link <https://core.trac.wordpress.org/ticket/38794>
* @returns {void}
*/
setupSectionNotifications: function() {
var section = this, setting = api( section.id ), debouncedRenderNotifications, setPageForPostsNotice;
if ( ! setting.notifications ) {
polyfillNotifications: function() {
var section = this, debouncedRenderNotifications;
if ( section.notifications ) {
return;
}

// Add the notifications API.
section.notifications = new api.Values({ defaultConstructor: api.Notification });
section.notificationsContainer = $( '<div class="customize-control-notifications-container"></div>' );
section.notifications.container = section.notificationsContainer;
section.notificationsTemplate = wp.template( 'customize-post-section-notifications' );
section.container.find( '.customize-section-title' ).after( section.notificationsContainer );
section.getNotificationsContainerElement = function() {
return section.notificationsContainer;
};
section.renderNotifications = api.Control.prototype.renderNotifications;

// Sync setting notifications into the section notifications
setting.notifications.bind( 'add', function( settingNotification ) {
var notification = new api.Notification( setting.id + ':' + settingNotification.code, settingNotification );
if ( ! settingNotification.data || ! settingNotification.data.setting_property || ! api.control.has( section.id + '[' + settingNotification.data.setting_property + ']' ) ) {
section.notifications.add( notification.code, notification );
}
} );
setting.notifications.bind( 'remove', function( settingNotification ) {
section.notifications.remove( setting.id + ':' + settingNotification.code );
} );

/*
* Render notifications when the collection is updated.
* Note that this debounced/deferred rendering is needed for two reasons:
Expand All @@ -872,9 +862,40 @@
} );
section.notifications.bind( 'remove', debouncedRenderNotifications );
section.renderNotifications();
},

/**
* Set up section notifications.
*
* @returns {void}
*/
setupSectionNotifications: function() {
var section = this, setting = api( section.id ), setPageForPostsNotice, notificationTemplate;
if ( ! setting.notifications ) {
return;
}

// Add the notifications API if not present.
section.polyfillNotifications();

// Sync setting notifications into the section notifications
setting.notifications.bind( 'add', function( settingNotification ) {
var notification = new api.Notification( setting.id + ':' + settingNotification.code, _.extend( {},
settingNotification,
{
template: notificationTemplate
}
) );
if ( ! settingNotification.data || ! settingNotification.data.setting_property || ! api.control.has( section.id + '[' + settingNotification.data.setting_property + ']' ) ) {
section.notifications.add( notification.code, notification );
}
} );
setting.notifications.bind( 'remove', function( settingNotification ) {
section.notifications.remove( setting.id + ':' + settingNotification.code );
} );

// Dismiss conflict block when clicking on button.
section.notificationsContainer.on( 'click', '.override-post-conflict', function( e ) {
section.notifications.container.on( 'click', '.override-post-conflict', function( e ) {
var ourValue;
e.preventDefault();
ourValue = _.clone( setting.get() );
Expand All @@ -890,6 +911,7 @@
} );

// Detect conflict errors.
notificationTemplate = wp.template( 'customize-post-field-notification' );
api.bind( 'error', function( response ) {
var theirValue, ourValue,
conflictedControls = [];
Expand Down
12 changes: 12 additions & 0 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,18 @@ public function render_templates() {
</button>
</script>

<script type="text/html" id="tmpl-customize-post-field-notification">
<li class="notice notice-{{ data.type || 'info' }} {{ data.altNotice ? 'notice-alt' : '' }}" data-code="{{ data.code }}" data-type="{{ data.type }}">
<# if ( /post_update_conflict/.test( data.code ) ) { #>
<button class="button override-post-conflict" type="button"><?php esc_html_e( 'Override', 'customize-posts' ); ?></button>
<# } #>
{{ data.message || data.code }}
</li>
</script>

<?php
// The following template is obsolete as of WordPress 4.9.
?>
<script type="text/html" id="tmpl-customize-post-section-notifications">
<ul>
<# _.each( data.notifications, function( notification ) { #>
Expand Down