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

Commit a447949

Browse files
authored
Merge pull request #337 from xwp/develop
Release 0.8.5
2 parents 94b27cd + e33de6f commit a447949

20 files changed

+455
-105
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "xwp/wp-customize-posts",
33
"description": "Manage posts and postmeta via the Customizer.",
4-
"version": "0.8.4",
4+
"version": "0.8.5",
55
"type": "wordpress-plugin",
66
"keywords": [ "customizer", "customize", "posts", "postmeta", "preview", "featured-image", "page-template" ],
77
"homepage": "https://github.com/xwp/wp-customize-posts/",

customize-posts.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Customize Posts
44
* Description: Manage posts and postmeta via the Customizer. Works best in conjunction with the <a href="https://wordpress.org/plugins/customize-setting-validation/">Customize Setting Validation</a> plugin.
55
* Plugin URI: https://github.com/xwp/wp-customize-posts/
6-
* Version: 0.8.4
6+
* Version: 0.8.5
77
* Author: XWP
88
* Author URI: https://make.xwp.co/
99
* License: GPLv2+

js/customize-post-field-partial.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
api.selectiveRefresh.partialConstructor.deferred.prototype.initialize.call( partial, id, args );
3636

3737
partial.addInstantPreviews();
38-
39-
// @todo If singular_only, and this is not the post singular post for this partial, then no refresh!
4038
},
4139

4240
/**
@@ -123,6 +121,41 @@
123121
return refreshPromise;
124122
},
125123

124+
/**
125+
* Handle fail to render partial.
126+
*
127+
* {@inheritdoc}
128+
*
129+
* @this {wp.customize.selectiveRefresh.partialConstructor.deferred}
130+
* @returns {void}
131+
*/
132+
fallback: function postFieldPartialFallback() {
133+
var partial = this, dependentSelector;
134+
135+
/*
136+
* Skip invoking fallback behavior for partials on documents that lack matches for
137+
* the fallback dependent selector. The default fallback dependent selector is
138+
* essentially checking to see if a body_class or post_class exists in the document
139+
* which references the given post. If the dependent selector fails to match any
140+
* elements, then the selector dependency fails and the partial should not be added.
141+
* Note that the dependent selector could have been used as a determiner for whether
142+
* the partial was added in the first place. However, this would have meant that no
143+
* selective refresh requests would have been spawned by the change, and this would
144+
* have meant that any Backbone models for the WP-API would not have had the chance
145+
* to get the rendered updates from the server.
146+
*/
147+
dependentSelector = partial.params.fallbackDependentSelector;
148+
if ( ! dependentSelector ) {
149+
dependentSelector = '.hentry.post-%d, body.page-id-%d, body.postid-%d';
150+
}
151+
dependentSelector = dependentSelector.replace( /%d/g, String( partial.params.post_id ) );
152+
if ( 0 === $( dependentSelector ).length ) {
153+
return;
154+
}
155+
156+
api.selectiveRefresh.partialConstructor.deferred.prototype.fallback.call( partial );
157+
},
158+
126159
/**
127160
* @inheritdoc
128161
*/

js/customize-preview-featured-image.js

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
77

88
var component = {
99
data: {
10-
partialSelector: '',
11-
partialContainerInclusive: true
10+
partialArgs: {
11+
selector: '',
12+
containerInclusive: true,
13+
fallbackDependentSelector: ''
14+
}
1215
}
1316
};
1417

@@ -23,6 +26,27 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
2326
_.extend( component.data, configData );
2427
}
2528
component.registerPartials();
29+
30+
api.previewPosts.wpApiModelInstances.bind( 'add', component.handleWpApiBackboneModelAdd );
31+
};
32+
33+
/**
34+
* Sync changes to featured image into Backbone models
35+
*
36+
* @param {wp.api.WPApiBaseModel|wp.api.models.Post} postModel Post model.
37+
* @returns {void}
38+
*/
39+
component.handleWpApiBackboneModelAdd = function handleWpApiBackboneModelAdd( postModel ) {
40+
var settingId;
41+
if ( _.isUndefined( postModel.get( 'featured_media' ) ) ) {
42+
return;
43+
}
44+
settingId = 'postmeta[' + postModel.get( 'type' ) + '][' + String( postModel.get( 'id' ) ) + '][_thumbnail_id]';
45+
api( settingId, function( postmetaSetting ) {
46+
postmetaSetting.bind( function( featuredImageId ) {
47+
postModel.set( 'featured_media', featuredImageId );
48+
} );
49+
} );
2650
};
2751

2852
/**
@@ -35,6 +59,37 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
3559
*/
3660
component.FeaturedImagePartial = api.selectiveRefresh.partialConstructor.deferred.extend({
3761

62+
idPattern: /^postmeta\[(.+?)]\[(\d+)]\[_thumbnail_id]$/,
63+
64+
/**
65+
* Initialize.
66+
*
67+
* @param {string} id Partial ID.
68+
* @param {object} args Args.
69+
* @param {object} args.params Params.
70+
* @returns {void}
71+
*/
72+
initialize: function( id, args ) {
73+
var partial = this, matches, postId, postType, params;
74+
matches = id.match( partial.idPattern );
75+
postType = matches[1];
76+
postId = parseInt( matches[2], 10 );
77+
params = _.extend(
78+
{
79+
post_id: postId,
80+
post_type: postType,
81+
selector: component.data.partialArgs.selector.replace( /%d/g, String( postId ) ),
82+
settings: [ id ],
83+
primarySetting: id,
84+
containerInclusive: component.data.partialArgs.containerInclusive,
85+
fallbackDependentSelector: component.data.partialArgs.fallbackDependentSelector
86+
},
87+
args ? args.params || {} : {}
88+
);
89+
90+
api.selectiveRefresh.partialConstructor.deferred.prototype.initialize.call( partial, id, { params: params } );
91+
},
92+
3893
/**
3994
* Force fallback (full page refresh) behavior when the featured image is removed.
4095
*
@@ -72,6 +127,27 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
72127
} else {
73128
return api.selectiveRefresh.Partial.prototype.renderContent.call( partial, placement );
74129
}
130+
},
131+
132+
/**
133+
* Handle fail to render partial.
134+
*
135+
* Skip performing fallback behavior if post does not appear on the current template.
136+
*
137+
* {@inheritdoc}
138+
*
139+
* @this {wp.customize.selectiveRefresh.partialConstructor.deferred}
140+
* @returns {void}
141+
*/
142+
fallback: function postFieldPartialFallback() {
143+
var partial = this, dependentSelector;
144+
145+
dependentSelector = partial.params.fallbackDependentSelector.replace( /%d/g, String( partial.params.post_id ) );
146+
if ( 0 === $( dependentSelector ).length ) {
147+
return;
148+
}
149+
150+
api.selectiveRefresh.partialConstructor.deferred.prototype.fallback.call( partial );
75151
}
76152
});
77153

@@ -82,8 +158,8 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
82158
* @returns {component.FeaturedImagePartial|null} New or existing featured image partial, or null if not relevant setting.
83159
*/
84160
component.ensurePartialForSetting = function ensurePartialForSetting( setting ) {
85-
var ensuredPartial, partialId, matches = setting.id.match( /^postmeta\[.+?]\[(\d+)]\[_thumbnail_id]$/ );
86-
if ( ! matches ) {
161+
var ensuredPartial, partialId;
162+
if ( ! component.FeaturedImagePartial.prototype.idPattern.test( setting.id ) ) {
87163
return null;
88164
}
89165
partialId = setting.id;
@@ -93,10 +169,7 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
93169
}
94170
ensuredPartial = new component.FeaturedImagePartial( partialId, {
95171
params: {
96-
selector: component.data.partialSelector,
97-
settings: [ setting.id ],
98-
primarySetting: setting.id,
99-
containerInclusive: component.data.partialContainerInclusive
172+
settings: [ setting.id ]
100173
}
101174
} );
102175
api.selectiveRefresh.partial.add( partialId, ensuredPartial );

0 commit comments

Comments
 (0)