@@ -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 : / ^ p o s t m e t a \[ ( .+ ?) ] \[ ( \d + ) ] \[ _ t h u m b n a i l _ i d ] $ / ,
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 ( / ^ p o s t m e t a \[ . + ? ] \[ ( \d + ) ] \[ _ t h u m b n a i l _ i d ] $ / ) ;
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