AudioPreset.php
4 years ago
Block.php
2 years ago
CurrentUser.php
5 years ago
EmailCollection.php
5 years ago
LicensedProduct.php
5 years ago
Model.php
4 years ago
ModelInterface.php
5 years ago
Player.php
5 years ago
Post.php
5 years ago
Preset.php
3 years ago
ReusableVideo.php
3 years ago
Setting.php
5 years ago
Video.php
4 years ago
Webhook.php
3 years ago
ReusableVideo.php
210 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PrestoPlayer\Models; |
| 4 | |
| 5 | use PrestoPlayer\Blocks\AudioBlock; |
| 6 | use PrestoPlayer\Blocks\VimeoBlock; |
| 7 | use PrestoPlayer\Blocks\YouTubeBlock; |
| 8 | use PrestoPlayer\Blocks\SelfHostedBlock; |
| 9 | use PrestoPlayer\Pro\Blocks\BunnyCDNBlock; |
| 10 | use PrestoPlayer\Pro\Blocks\PrivateSelfHostedBlock; |
| 11 | use WP_Query; |
| 12 | |
| 13 | class ReusableVideo |
| 14 | { |
| 15 | public $post; |
| 16 | private $post_type = 'pp_video_block'; |
| 17 | |
| 18 | public function __construct($id = 0) |
| 19 | { |
| 20 | if (!empty($id)) { |
| 21 | $this->post = \get_post($id); |
| 22 | return $this; |
| 23 | } |
| 24 | return $this; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Get attributes properties |
| 29 | * |
| 30 | * @param string $property |
| 31 | * @return mixed |
| 32 | */ |
| 33 | public function __get($property) |
| 34 | { |
| 35 | return isset($this->post->$property) ? $this->post->$property : null; |
| 36 | } |
| 37 | |
| 38 | public function create($args = []) |
| 39 | { |
| 40 | return wp_insert_post(wp_parse_args($args, [ |
| 41 | 'post_type' => $this->post_type |
| 42 | ])); |
| 43 | } |
| 44 | |
| 45 | public function fetch($args = []) |
| 46 | { |
| 47 | $args = wp_parse_args($args, [ |
| 48 | 'post_type' => $this->post_type, |
| 49 | 'post_status' => array( 'publish' ) |
| 50 | ]); |
| 51 | |
| 52 | return (new WP_Query($args))->posts; |
| 53 | } |
| 54 | |
| 55 | public function all($args = []) |
| 56 | { |
| 57 | $args = wp_parse_args($args, [ |
| 58 | 'post_type' => $this->post_type, |
| 59 | 'per_page' => -1 |
| 60 | ]); |
| 61 | |
| 62 | return get_posts($args); |
| 63 | } |
| 64 | |
| 65 | public function first($args = []) |
| 66 | { |
| 67 | $fetched = $this->fetch(wp_parse_args($args, ['per_page' => 1])); |
| 68 | return !empty($fetched[0]) ? new static($fetched[0]) : false; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get block from video post |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | public function getBlock() |
| 77 | { |
| 78 | if (empty($this->post->post_content)) { |
| 79 | return []; |
| 80 | } |
| 81 | $blocks = \parse_blocks($this->post->post_content); |
| 82 | |
| 83 | return !empty($blocks[0]['innerBlocks'][0]) ? $blocks[0]['innerBlocks'][0] : []; |
| 84 | } |
| 85 | |
| 86 | public function getAttributes($overrides = []) |
| 87 | { |
| 88 | $block = $this->getBlock(); |
| 89 | if (empty($block)) { |
| 90 | return ''; |
| 91 | } |
| 92 | |
| 93 | // allow overriding attributes |
| 94 | $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']); |
| 95 | |
| 96 | // maybe switch provider depending on url |
| 97 | if (!empty($overrides)) { |
| 98 | $block = $this->maybeSwitchProvider($block); |
| 99 | } |
| 100 | |
| 101 | switch ($block['blockName']) { |
| 102 | case 'presto-player/self-hosted': |
| 103 | return (new SelfHostedBlock())->getAttributes($block['attrs']); |
| 104 | |
| 105 | case 'presto-player/youtube': |
| 106 | return (new YouTubeBlock())->getAttributes($block['attrs']); |
| 107 | |
| 108 | case 'presto-player/vimeo': |
| 109 | return (new VimeoBlock())->getAttributes($block['attrs']); |
| 110 | |
| 111 | case 'presto-player/bunny': |
| 112 | return (new BunnyCDNBlock())->getAttributes($block['attrs']); |
| 113 | |
| 114 | case 'presto-player/audio': |
| 115 | return (new AudioBlock())->getAttributes($block['attrs']); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function renderBlock($overrides = []) |
| 120 | { |
| 121 | $block = $this->getBlock(); |
| 122 | if (empty($block)) { |
| 123 | return ''; |
| 124 | } |
| 125 | |
| 126 | // allow overriding attributes |
| 127 | $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']); |
| 128 | |
| 129 | // maybe switch provider depending on url |
| 130 | if (!empty($overrides)) { |
| 131 | $block = $this->maybeSwitchProvider($block); |
| 132 | } |
| 133 | |
| 134 | // remove attachment_id if the src changes. |
| 135 | if (!empty($overrides['src'])) { |
| 136 | $block['attrs']['attachment_id'] = null; |
| 137 | } |
| 138 | |
| 139 | switch ($block['blockName']) { |
| 140 | case 'presto-player/self-hosted': |
| 141 | return (new SelfHostedBlock(true, '1'))->html($block['attrs'], ''); |
| 142 | |
| 143 | case 'presto-player/youtube': |
| 144 | return (new YouTubeBlock(true, '1'))->html($block['attrs'], ''); |
| 145 | |
| 146 | case 'presto-player/vimeo': |
| 147 | return (new VimeoBlock(true, '1'))->html($block['attrs'], ''); |
| 148 | |
| 149 | case 'presto-player/bunny': |
| 150 | return (new BunnyCDNBlock(true, '1'))->html($block['attrs'], ''); |
| 151 | |
| 152 | case 'presto-player/audio': |
| 153 | return (new AudioBlock(true, '1'))->html($block['attrs'], ''); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Maybe switch provider if the url is overridden |
| 159 | */ |
| 160 | protected function maybeSwitchProvider($block) |
| 161 | { |
| 162 | if (empty($block) || !is_array($block)) { |
| 163 | return $block; |
| 164 | } |
| 165 | |
| 166 | if (!empty($block['attrs']['src'])) { |
| 167 | if ($block['attrs']['src']) { |
| 168 | $filetype = wp_check_filetype($block['attrs']['src']); |
| 169 | if (isset($filetype['type']) && false !== strpos($filetype['type'], 'audio')) { |
| 170 | $block['blockName'] = 'presto-player/audio'; |
| 171 | return $block; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/'; |
| 176 | $has_match_youtube = preg_match($yt_rx, $block['attrs']['src'], $yt_matches); |
| 177 | |
| 178 | if ($has_match_youtube) { |
| 179 | $block['blockName'] = 'presto-player/youtube'; |
| 180 | return $block; |
| 181 | } |
| 182 | |
| 183 | $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/'; |
| 184 | $has_match_vimeo = preg_match($vm_rx, $block['attrs']['src'], $vm_matches); |
| 185 | |
| 186 | if ($has_match_vimeo) { |
| 187 | $block['blockName'] = 'presto-player/vimeo'; |
| 188 | return $block; |
| 189 | } |
| 190 | |
| 191 | // default to self-hsoted |
| 192 | $block['blockName'] = 'presto-player/self-hosted'; |
| 193 | return $block; |
| 194 | } |
| 195 | |
| 196 | return $block; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get reusable video block function. |
| 201 | * |
| 202 | * @param mixed $id The ID of the reusable block. |
| 203 | * @return $content The content of the block. |
| 204 | */ |
| 205 | public function content() |
| 206 | { |
| 207 | return !empty($this->post->post_content) ? $this->post->post_content : ''; |
| 208 | } |
| 209 | } |
| 210 |