PluginProbe ʕ •ᴥ•ʔ
Presto Player / 1.10.2
Presto Player v1.10.2
4.3.0 4.2.4 4.2.3 4.2.2 4.2.0 4.2.1 trunk 1.10.0 1.10.1 1.10.2 1.11.0 1.12.0 1.13.0 1.14.0 1.14.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.13 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.9.0 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.14 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3-beta1 2.3.0 2.3.1 2.3.2 2.3.3 3.0.0 3.0.0-beta1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1.0 3.1.1 3.1.2 3.1.3 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4
presto-player / inc / Models / ReusableVideo.php
presto-player / inc / Models Last commit date
AudioPreset.php 4 years ago Block.php 4 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 4 years ago ReusableVideo.php 4 years ago Setting.php 5 years ago Video.php 4 years ago Webhook.php 3 years ago
ReusableVideo.php
208 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
12 class ReusableVideo
13 {
14 public $post;
15 private $post_type = 'pp_video_block';
16
17 public function __construct($id = 0)
18 {
19 if (!empty($id)) {
20 $this->post = \get_post($id);
21 return $this;
22 }
23 return $this;
24 }
25
26 /**
27 * Get attributes properties
28 *
29 * @param string $property
30 * @return mixed
31 */
32 public function __get($property)
33 {
34 return isset($this->post->$property) ? $this->post->$property : null;
35 }
36
37 public function create($args = [])
38 {
39 return wp_insert_post(wp_parse_args($args, [
40 'post_type' => $this->post_type
41 ]));
42 }
43
44 public function fetch($args = [])
45 {
46 $args = wp_parse_args($args, [
47 'post_type' => $this->post_type
48 ]);
49
50 return get_posts($args);
51 }
52
53 public function all($args = [])
54 {
55 $args = wp_parse_args($args, [
56 'post_type' => $this->post_type,
57 'per_page' => -1
58 ]);
59
60 return get_posts($args);
61 }
62
63 public function first($args = [])
64 {
65 $fetched = $this->fetch(wp_parse_args($args, ['per_page' => 1]));
66 return !empty($fetched[0]) ? new static($fetched[0]) : false;
67 }
68
69 /**
70 * Get block from video post
71 *
72 * @return array
73 */
74 public function getBlock()
75 {
76 if (empty($this->post->post_content)) {
77 return [];
78 }
79 $blocks = \parse_blocks($this->post->post_content);
80
81 return !empty($blocks[0]['innerBlocks'][0]) ? $blocks[0]['innerBlocks'][0] : [];
82 }
83
84 public function getAttributes($overrides = [])
85 {
86 $block = $this->getBlock();
87 if (empty($block)) {
88 return '';
89 }
90
91 // allow overriding attributes
92 $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']);
93
94 // maybe switch provider depending on url
95 if (!empty($overrides)) {
96 $block = $this->maybeSwitchProvider($block);
97 }
98
99 switch ($block['blockName']) {
100 case 'presto-player/self-hosted':
101 return (new SelfHostedBlock())->getAttributes($block['attrs']);
102
103 case 'presto-player/youtube':
104 return (new YouTubeBlock())->getAttributes($block['attrs']);
105
106 case 'presto-player/vimeo':
107 return (new VimeoBlock())->getAttributes($block['attrs']);
108
109 case 'presto-player/bunny':
110 return (new BunnyCDNBlock())->getAttributes($block['attrs']);
111
112 case 'presto-player/audio':
113 return (new AudioBlock())->getAttributes($block['attrs']);
114 }
115 }
116
117 public function renderBlock($overrides = [])
118 {
119 $block = $this->getBlock();
120 if (empty($block)) {
121 return '';
122 }
123
124 // allow overriding attributes
125 $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']);
126
127 // maybe switch provider depending on url
128 if (!empty($overrides)) {
129 $block = $this->maybeSwitchProvider($block);
130 }
131
132 // remove attachment_id if the src changes.
133 if (!empty($overrides['src'])) {
134 $block['attrs']['attachment_id'] = null;
135 }
136
137 switch ($block['blockName']) {
138 case 'presto-player/self-hosted':
139 return (new SelfHostedBlock(true, '1'))->html($block['attrs'], '');
140
141 case 'presto-player/youtube':
142 return (new YouTubeBlock(true, '1'))->html($block['attrs'], '');
143
144 case 'presto-player/vimeo':
145 return (new VimeoBlock(true, '1'))->html($block['attrs'], '');
146
147 case 'presto-player/bunny':
148 return (new BunnyCDNBlock(true, '1'))->html($block['attrs'], '');
149
150 case 'presto-player/audio':
151 return (new AudioBlock(true, '1'))->html($block['attrs'], '');
152 }
153 }
154
155 /**
156 * Maybe switch provider if the url is overridden
157 */
158 protected function maybeSwitchProvider($block)
159 {
160 if (empty($block) || !is_array($block)) {
161 return $block;
162 }
163
164 if (!empty($block['attrs']['src'])) {
165 if ($block['attrs']['src']) {
166 $filetype = wp_check_filetype($block['attrs']['src']);
167 if (isset($filetype['type']) && false !== strpos($filetype['type'], 'audio')) {
168 $block['blockName'] = 'presto-player/audio';
169 return $block;
170 }
171 }
172
173 $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/';
174 $has_match_youtube = preg_match($yt_rx, $block['attrs']['src'], $yt_matches);
175
176 if ($has_match_youtube) {
177 $block['blockName'] = 'presto-player/youtube';
178 return $block;
179 }
180
181 $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([‌​0-9]{6,11})[?]?.*/';
182 $has_match_vimeo = preg_match($vm_rx, $block['attrs']['src'], $vm_matches);
183
184 if ($has_match_vimeo) {
185 $block['blockName'] = 'presto-player/vimeo';
186 return $block;
187 }
188
189 // default to self-hsoted
190 $block['blockName'] = 'presto-player/self-hosted';
191 return $block;
192 }
193
194 return $block;
195 }
196
197 /**
198 * Get reusable video block function.
199 *
200 * @param mixed $id The ID of the reusable block.
201 * @return $content The content of the block.
202 */
203 public function content()
204 {
205 return !empty($this->post->post_content) ? $this->post->post_content : '';
206 }
207 }
208