@@ -7,8 +7,11 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
7
7
8
8
var component = {
9
9
data : {
10
- partialSelector : '' ,
11
- partialContainerInclusive : true
10
+ partialArgs : {
11
+ selector : '' ,
12
+ containerInclusive : true ,
13
+ fallbackDependentSelector : ''
14
+ }
12
15
}
13
16
} ;
14
17
@@ -23,6 +26,27 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
23
26
_ . extend ( component . data , configData ) ;
24
27
}
25
28
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
+ } ) ;
26
50
} ;
27
51
28
52
/**
@@ -35,6 +59,37 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
35
59
*/
36
60
component . FeaturedImagePartial = api . selectiveRefresh . partialConstructor . deferred . extend ( {
37
61
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
+
38
93
/**
39
94
* Force fallback (full page refresh) behavior when the featured image is removed.
40
95
*
@@ -72,6 +127,27 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
72
127
} else {
73
128
return api . selectiveRefresh . Partial . prototype . renderContent . call ( partial , placement ) ;
74
129
}
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 ) ;
75
151
}
76
152
} ) ;
77
153
@@ -82,8 +158,8 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
82
158
* @returns {component.FeaturedImagePartial|null } New or existing featured image partial, or null if not relevant setting.
83
159
*/
84
160
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 ) ) {
87
163
return null ;
88
164
}
89
165
partialId = setting . id ;
@@ -93,10 +169,7 @@ var CustomizePreviewFeaturedImage = (function( api, $ ) {
93
169
}
94
170
ensuredPartial = new component . FeaturedImagePartial ( partialId , {
95
171
params : {
96
- selector : component . data . partialSelector ,
97
- settings : [ setting . id ] ,
98
- primarySetting : setting . id ,
99
- containerInclusive : component . data . partialContainerInclusive
172
+ settings : [ setting . id ]
100
173
}
101
174
} ) ;
102
175
api . selectiveRefresh . partial . add ( partialId , ensuredPartial ) ;
0 commit comments