Skip to content

Commit 30d5088

Browse files
committed
add parent_post_type, first_parent_post_type, first_parent_post_id, post_id, and parent_post_id option to metabox registration
1 parent 35d8d5b commit 30d5088

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

src/Register/MetaBox.php

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,63 @@ public function register()
176176
{
177177
global $post, $comment;
178178

179-
$postType = null;
179+
$postType = $pageTemplate = null;
180180

181181
if($capability = $this->getCapability()) {
182182
if(!current_user_can($capability)) {
183183
return $this;
184184
}
185185
}
186186

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 );
189190
$pageTemplate = get_post_meta( $post->ID, '_wp_page_template', true );
190-
}
191191

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+
}
194195
}
195196

196197
foreach ($this->screens as $screen) {
197-
$isPageTemplate = $isFrontPage = $isPostsPage = false;
198+
$isPageTemplate = $isFrontPage = $isPostsPage = $isPostParentMatch = $isPostId = false;
199+
198200
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+
204230
}
205231

206232
if ( $postType == $screen ||
207233
$isPageTemplate ||
234+
$isPostParentMatch ||
235+
$isPostId ||
208236
$isFrontPage ||
209237
$isPostsPage ||
210238
( $screen == 'comment' && isset( $comment ) ) ||
@@ -228,7 +256,7 @@ public function register()
228256
echo '</div>';
229257
};
230258

231-
$used_screen = $isPageTemplate || $isFrontPage || $isPostsPage ? $postType : $screen;
259+
$used_screen = $isPageTemplate || $isFrontPage || $isPostsPage || $isPostId || $isPostParentMatch ? $postType : $screen;
232260
$used_context = $used_screen == 'dashboard' || $used_screen == 'comment' ? 'normal' : $this->context;
233261

234262
add_meta_box(

0 commit comments

Comments
 (0)