PluginProbe ʕ •ᴥ•ʔ
Presto Player / 2.3.3
Presto Player v2.3.3
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 2 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 2 years ago Preset.php 2 years ago ReusableVideo.php 2 years ago Setting.php 5 years ago Video.php 2 years ago Webhook.php 3 years ago
ReusableVideo.php
209 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 WP_Query;
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 'post_status' => array( 'publish' )
49 ]);
50
51 return (new WP_Query($args))->posts;
52 }
53
54 public function all($args = [])
55 {
56 $args = wp_parse_args($args, [
57 'post_type' => $this->post_type,
58 'per_page' => -1
59 ]);
60
61 return get_posts($args);
62 }
63
64 public function first($args = [])
65 {
66 $fetched = $this->fetch(wp_parse_args($args, ['per_page' => 1]));
67 return !empty($fetched[0]) ? new static($fetched[0]) : false;
68 }
69
70 /**
71 * Get block from video post
72 *
73 * @return array
74 */
75 public function getBlock()
76 {
77 if (empty($this->post->post_content)) {
78 return [];
79 }
80 $blocks = \parse_blocks($this->post->post_content);
81
82 return !empty($blocks[0]['innerBlocks'][0]) ? $blocks[0]['innerBlocks'][0] : [];
83 }
84
85 public function getAttributes($overrides = [])
86 {
87 $block = $this->getBlock();
88 if (empty($block)) {
89 return '';
90 }
91
92 // allow overriding attributes
93 $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']);
94
95 // maybe switch provider depending on url
96 if (!empty($overrides)) {
97 $block = $this->maybeSwitchProvider($block);
98 }
99
100 switch ($block['blockName']) {
101 case 'presto-player/self-hosted':
102 return (new SelfHostedBlock())->getAttributes($block['attrs']);
103
104 case 'presto-player/youtube':
105 return (new YouTubeBlock())->getAttributes($block['attrs']);
106
107 case 'presto-player/vimeo':
108 return (new VimeoBlock())->getAttributes($block['attrs']);
109
110 case 'presto-player/bunny':
111 return (new BunnyCDNBlock())->getAttributes($block['attrs']);
112
113 case 'presto-player/audio':
114 return (new AudioBlock())->getAttributes($block['attrs']);
115 }
116 }
117
118 public function renderBlock($overrides = [])
119 {
120 $block = $this->getBlock();
121 if (empty($block)) {
122 return '';
123 }
124
125 // allow overriding attributes
126 $block['attrs'] = wp_parse_args($overrides, (array)$block['attrs']);
127
128 // maybe switch provider depending on url
129 if (!empty($overrides)) {
130 $block = $this->maybeSwitchProvider($block);
131 }
132
133 // remove attachment_id if the src changes.
134 if (!empty($overrides['src'])) {
135 $block['attrs']['attachment_id'] = null;
136 }
137
138 switch ($block['blockName']) {
139 case 'presto-player/self-hosted':
140 return (new SelfHostedBlock(true, '1'))->html($block['attrs'], '');
141
142 case 'presto-player/youtube':
143 return (new YouTubeBlock(true, '1'))->html($block['attrs'], '');
144
145 case 'presto-player/vimeo':
146 return (new VimeoBlock(true, '1'))->html($block['attrs'], '');
147
148 case 'presto-player/bunny':
149 return (new BunnyCDNBlock(true, '1'))->html($block['attrs'], '');
150
151 case 'presto-player/audio':
152 return (new AudioBlock(true, '1'))->html($block['attrs'], '');
153 }
154 }
155
156 /**
157 * Maybe switch provider if the url is overridden
158 */
159 protected function maybeSwitchProvider($block)
160 {
161 if (empty($block) || !is_array($block)) {
162 return $block;
163 }
164
165 if (!empty($block['attrs']['src'])) {
166 if ($block['attrs']['src']) {
167 $filetype = wp_check_filetype($block['attrs']['src']);
168 if (isset($filetype['type']) && false !== strpos($filetype['type'], 'audio')) {
169 $block['blockName'] = 'presto-player/audio';
170 return $block;
171 }
172 }
173
174 $yt_rx = '/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$/';
175 $has_match_youtube = preg_match($yt_rx, $block['attrs']['src'], $yt_matches);
176
177 if ($has_match_youtube) {
178 $block['blockName'] = 'presto-player/youtube';
179 return $block;
180 }
181
182 $vm_rx = '/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([‌​0-9]{6,11})[?]?.*/';
183 $has_match_vimeo = preg_match($vm_rx, $block['attrs']['src'], $vm_matches);
184
185 if ($has_match_vimeo) {
186 $block['blockName'] = 'presto-player/vimeo';
187 return $block;
188 }
189
190 // default to self-hsoted
191 $block['blockName'] = 'presto-player/self-hosted';
192 return $block;
193 }
194
195 return $block;
196 }
197
198 /**
199 * Get reusable video block function.
200 *
201 * @param mixed $id The ID of the reusable block.
202 * @return $content The content of the block.
203 */
204 public function content()
205 {
206 return !empty($this->post->post_content) ? $this->post->post_content : '';
207 }
208 }
209