| 1 |
<?php |
| 2 |
|
| 3 |
namespace BPMP; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Shortcode { |
| 10 |
function __construct() { |
| 11 |
add_shortcode('audio_player', [$this, 'bpmp_audio_player_block_shortcode']); |
| 12 |
} |
| 13 |
|
| 14 |
function bpmp_audio_player_block_shortcode($atts){ |
| 15 |
|
| 16 |
if (!isset($atts['id'])) { |
| 17 |
return esc_html__( 'Error: Missing Audio Player ID.', 'audio-player-block' ); |
| 18 |
} |
| 19 |
|
| 20 |
$post_id = $atts['id']; |
| 21 |
$post = get_post( $post_id ); |
| 22 |
|
| 23 |
if ( !$post ) { |
| 24 |
return ''; |
| 25 |
} |
| 26 |
|
| 27 |
if ( post_password_required( $post ) ) { |
| 28 |
return get_the_password_form( $post ); |
| 29 |
} |
| 30 |
|
| 31 |
switch ( $post->post_status ) { |
| 32 |
case 'publish': |
| 33 |
return $this->displayContent( $post ); |
| 34 |
|
| 35 |
case 'private': |
| 36 |
if (current_user_can('read_private_posts')) { |
| 37 |
return $this->displayContent( $post ); |
| 38 |
} |
| 39 |
return ''; |
| 40 |
|
| 41 |
case 'draft': |
| 42 |
case 'pending': |
| 43 |
case 'future': |
| 44 |
if ( current_user_can( 'edit_post', $post_id ) ) { |
| 45 |
return $this->displayContent( $post ); |
| 46 |
} |
| 47 |
return ''; |
| 48 |
|
| 49 |
default: |
| 50 |
return ''; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
function displayContent( $post ){ |
| 55 |
$blocks = parse_blocks( $post->post_content ); |
| 56 |
if ( ! empty( $blocks ) && isset( $blocks[0] ) ) { |
| 57 |
return render_block( $blocks[0] ); |
| 58 |
} |
| 59 |
return ''; |
| 60 |
} |
| 61 |
} |
| 62 |
|