Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Created May 12, 2023 16:41
Show Gist options
  • Save Shelob9/db3b373c4590aad86fc7b7719624adf3 to your computer and use it in GitHub Desktop.
Save Shelob9/db3b373c4590aad86fc7b7719624adf3 to your computer and use it in GitHub Desktop.
<?php
add_filter('get_default_comment_status', [
IM_M_Comments::class, 'get_default_comment_status'
], 100, 3 );
apply_filters( 'comments_open', [
IM_M_Comments::class, 'comments_open'
], 100, 2 );
class IM_M_Comments {
public static $allowed_post_type = 'tutorial';
public static $parents = [
777
];
public static function get_default_comment_status(
$status, $post_type, $comment_type
){
if( static::$allowed_post_type !== $post_type ){
return $status;
}
$post = get_post();
if( ! $post ){
return $status;
}
if( ! static::has_allowed_parent($post) ){
return $status;
}
return 'open';
}
public static function has_allowed_parent($post ){
$hasParent = ! empty( $post->post_parent ) && is_numeric($post->post_parent );
if( ! $hasParent ){
return false;
}
return in_array($post->post_parent, static::$parents);
}
public static function comments_open( $open, $post_id ){
$post = get_post( $post_id );
if( ! $post ){
return $open;
}
return static::has_allowed_parent($post);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment