| 1 |
<?php |
| 2 |
namespace PFB; |
| 3 |
|
| 4 |
class Shortcode |
| 5 |
{ |
| 6 |
function __construct() |
| 7 |
{ |
| 8 |
add_shortcode('pfb', [$this, 'onAddShortcode']); |
| 9 |
} |
| 10 |
|
| 11 |
function onAddShortcode($atts) |
| 12 |
{ |
| 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 |
{ |
| 49 |
$blocks = parse_blocks($post->post_content); |
| 50 |
return render_block($blocks[0]); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
} |