@@ -176,35 +176,63 @@ public function register()
176
176
{
177
177
global $ post , $ comment ;
178
178
179
- $ postType = null ;
179
+ $ postType = $ pageTemplate = null ;
180
180
181
181
if ($ capability = $ this ->getCapability ()) {
182
182
if (!current_user_can ($ capability )) {
183
183
return $ this ;
184
184
}
185
185
}
186
186
187
- if (!empty ($ post )) {
188
- $ postType = get_post_type ( $ post ->ID );
187
+ if (!empty ($ post ) && is_object ($ post )) {
188
+ $ postType = $ post ->post_type ?? false ;
189
+ $ postParents = get_post_ancestors ( $ post );
189
190
$ pageTemplate = get_post_meta ( $ post ->ID , '_wp_page_template ' , true );
190
- }
191
191
192
- if (!empty ($ post ) && post_type_supports ( $ postType , $ this ->id )) {
193
- $ this ->addPostType ( $ postType );
192
+ if (post_type_supports ( $ postType , $ this ->id )) {
193
+ $ this ->addPostType ( $ postType );
194
+ }
194
195
}
195
196
196
197
foreach ($ this ->screens as $ screen ) {
197
- $ isPageTemplate = $ isFrontPage = $ isPostsPage = false ;
198
+ $ isPageTemplate = $ isFrontPage = $ isPostsPage = $ isPostParentMatch = $ isPostId = false ;
199
+
198
200
if (isset ($ post )) {
199
- $ isPageTemplate = $ pageTemplate == $ screen
200
- && $ post ->ID != get_option ( 'page_on_front ' )
201
- && $ post ->ID != get_option ( 'page_for_posts ' );
202
- $ isFrontPage = $ post ->ID == get_option ( 'page_on_front ' ) && $ screen == 'front_page ' ;
203
- $ isPostsPage = $ post ->ID == get_option ( 'page_for_posts ' ) && $ screen == 'posts_page ' ;
201
+ $ isPageTemplate = $ pageTemplate == $ screen
202
+ && $ post ->ID != get_option ( 'page_on_front ' )
203
+ && $ post ->ID != get_option ( 'page_for_posts ' );
204
+
205
+ $ isFrontPage = $ post ->ID == get_option ( 'page_on_front ' ) && $ screen == 'front_page ' ;
206
+ $ isPostsPage = $ post ->ID == get_option ( 'page_for_posts ' ) && $ screen == 'posts_page ' ;
207
+ $ isPostId = "post_id: {$ post ->ID }" == $ screen ;
208
+
209
+ if (!empty ($ postParents )) {
210
+ $ isChildOfParentPostType = $ isChildOfParentPostId = false ;
211
+ foreach ($ postParents as $ postParent ) {
212
+ $ postParent = get_post ($ postParent );
213
+ $ isChildOfParentPostType = "parent_post_type: {$ postParent ->post_type }" == $ screen ;
214
+ $ isChildOfParentPostId = "parent_post_id: {$ postParent ->ID }" == $ screen ;
215
+
216
+ if ($ isChildOfParentPostType || $ isChildOfParentPostId ) {
217
+ break ;
218
+ }
219
+ }
220
+
221
+ $ firstPostParent = get_post ($ postParents [0 ]);
222
+ $ isFirstChildOfParentPostType = "first_parent_post_type: {$ firstPostParent ->post_type }" == $ screen ;
223
+ $ isFirstChildOfParentPostId = "first_parent_post_id: {$ firstPostParent ->ID }" == $ screen ;
224
+
225
+ if ($ isChildOfParentPostType || $ isChildOfParentPostId || $ isFirstChildOfParentPostType || $ isFirstChildOfParentPostId ) {
226
+ $ isPostParentMatch = true ;
227
+ }
228
+ }
229
+
204
230
}
205
231
206
232
if ( $ postType == $ screen ||
207
233
$ isPageTemplate ||
234
+ $ isPostParentMatch ||
235
+ $ isPostId ||
208
236
$ isFrontPage ||
209
237
$ isPostsPage ||
210
238
( $ screen == 'comment ' && isset ( $ comment ) ) ||
@@ -228,7 +256,7 @@ public function register()
228
256
echo '</div> ' ;
229
257
};
230
258
231
- $ used_screen = $ isPageTemplate || $ isFrontPage || $ isPostsPage ? $ postType : $ screen ;
259
+ $ used_screen = $ isPageTemplate || $ isFrontPage || $ isPostsPage || $ isPostId || $ isPostParentMatch ? $ postType : $ screen ;
232
260
$ used_context = $ used_screen == 'dashboard ' || $ used_screen == 'comment ' ? 'normal ' : $ this ->context ;
233
261
234
262
add_meta_box (
0 commit comments