AudioBlock.php
9 months ago
MediaHubBlock.php
9 months ago
PopupBlock.php
10 months ago
PopupMediaBlock.php
7 months ago
PopupTriggerBlock.php
10 months ago
ReusableEditBlock.php
9 months ago
ReusableVideoBlock.php
2 weeks ago
SelfHostedBlock.php
9 months ago
VimeoBlock.php
9 months ago
YouTubeBlock.php
9 months ago
SelfHostedBlock.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Self Hosted Block. |
| 4 | * |
| 5 | * @package PrestoPlayer\Blocks |
| 6 | */ |
| 7 | |
| 8 | namespace PrestoPlayer\Blocks; |
| 9 | |
| 10 | use PrestoPlayer\Attachment; |
| 11 | use PrestoPlayer\Support\Block; |
| 12 | use PrestoPlayer\Models\CurrentUser; |
| 13 | |
| 14 | /** |
| 15 | * Self Hosted Block. |
| 16 | */ |
| 17 | class SelfHostedBlock extends Block { |
| 18 | |
| 19 | /** |
| 20 | * Block name |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | protected $name = 'self-hosted'; |
| 25 | |
| 26 | /** |
| 27 | * Register the block type. |
| 28 | * |
| 29 | * @return void |
| 30 | */ |
| 31 | public function registerBlockType() { |
| 32 | |
| 33 | register_block_type( |
| 34 | PRESTO_PLAYER_PLUGIN_DIR . 'src/admin/blocks/blocks/hosted', |
| 35 | array( |
| 36 | 'render_callback' => array( $this, 'html' ), |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Bail if user cannot access video. |
| 43 | * |
| 44 | * @param array $attributes Block attributes. |
| 45 | * @param string $content Block content. |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function middleware( $attributes, $content ) { |
| 49 | // If private and user cannot access video, don't load. |
| 50 | if ( ! empty( $attributes['visibility'] ) && 'private' === $attributes['visibility'] ) { |
| 51 | if ( ! CurrentUser::canAccessVideo( $attributes['id'] ) ) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return parent::middleware( $attributes, $content ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add src to video. |
| 61 | * |
| 62 | * @param array $attributes Block attributes. |
| 63 | * @param array $default_config Default config. |
| 64 | * @return array |
| 65 | */ |
| 66 | public function sanitizeAttributes( $attributes, $default_config ) { |
| 67 | $src = ! empty( $attributes['src'] ) ? $attributes['src'] : ''; |
| 68 | |
| 69 | if ( ! empty( $this->isHls( $src ) ) ) { |
| 70 | wp_enqueue_script( 'hls.js' ); |
| 71 | } |
| 72 | |
| 73 | return array( |
| 74 | 'src' => ! empty( $attributes['attachment_id'] ) ? Attachment::getSrc( $attributes['attachment_id'] ) : $src, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Override attributes. |
| 80 | * |
| 81 | * @param array $attributes Block attributes. |
| 82 | * @return array |
| 83 | */ |
| 84 | public function overrideAttributes( $attributes ) { |
| 85 | $load = $this->middleware( $attributes, '' ); |
| 86 | |
| 87 | if ( ! $load ) { |
| 88 | return array(); |
| 89 | } |
| 90 | |
| 91 | return $attributes; |
| 92 | } |
| 93 | } |
| 94 |