| 1 |
<?php |
| 2 |
namespace TSB; |
| 3 |
|
| 4 |
class ShortCode { |
| 5 |
private $post_type = 'tsb'; |
| 6 |
|
| 7 |
function __construct() { |
| 8 |
add_shortcode( 'tsb', [$this, 'onAddShortcode'] ); |
| 9 |
add_action( 'use_block_editor_for_post', [$this, 'useBlockEditorForPost'], 999, 2 ); |
| 10 |
} |
| 11 |
|
| 12 |
function onAddShortcode( $atts ) { |
| 13 |
$post_id = $atts['id']; |
| 14 |
$post = get_post( $post_id ); |
| 15 |
|
| 16 |
if ( !$post ) { |
| 17 |
return ''; |
| 18 |
} |
| 19 |
|
| 20 |
if ( post_password_required( $post ) ) { |
| 21 |
return get_the_password_form( $post ); |
| 22 |
} |
| 23 |
|
| 24 |
switch ( $post->post_status ) { |
| 25 |
case 'publish': |
| 26 |
return $this->displayContent( $post ); |
| 27 |
|
| 28 |
case 'private': |
| 29 |
if ( current_user_can( 'read_private_posts' ) ) { |
| 30 |
return $this->displayContent( $post ); |
| 31 |
} |
| 32 |
return ''; |
| 33 |
|
| 34 |
case 'draft': |
| 35 |
case 'pending': |
| 36 |
case 'future': |
| 37 |
if ( current_user_can( 'edit_post', $post_id ) ) { |
| 38 |
return $this->displayContent( $post ); |
| 39 |
} |
| 40 |
return ''; |
| 41 |
|
| 42 |
default: |
| 43 |
return ''; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
function displayContent( $post ) { |
| 48 |
$content = apply_filters( 'the_content', $post->post_content ); |
| 49 |
return $content; |
| 50 |
} |
| 51 |
|
| 52 |
function useBlockEditorForPost( $use, $post ){ |
| 53 |
if ( is_object( $post ) && isset( $post->post_type ) && $this->post_type === $post->post_type ) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
return $use; |
| 57 |
} |
| 58 |
|
| 59 |
} |
| 60 |
|