PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.3.2
Presto Player v4.3.2
4.3.2 4.3.1 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 / Video.php
presto-player / inc / Models Last commit date
AudioPreset.php 2 months ago Block.php 10 months ago CurrentUser.php 2 years ago EmailCollection.php 2 years ago Model.php 1 month ago ModelInterface.php 2 years ago Player.php 1 month ago Post.php 2 years ago Preset.php 2 months ago ReusableVideo.php 1 month ago Setting.php 2 years ago Video.php 1 day ago Webhook.php 2 years ago
Video.php
270 lines
1 <?php
2 /**
3 * Video model.
4 *
5 * @package PrestoPlayer
6 * @subpackage Models
7 */
8
9 namespace PrestoPlayer\Models;
10
11 use PrestoPlayer\Services\Blocks\VimeoBlockService;
12 use PrestoPlayer\Services\Blocks\YoutubeBlockService;
13
14 /**
15 * Represents a row in the presto_player_videos table.
16 *
17 * @property int $id
18 * @property string $title
19 * @property string $type
20 * @property string $src
21 * @property string $external_id
22 * @property int $attachment_id
23 * @property int $post_id
24 * @property int $created_by
25 * @property string $created_at
26 * @property string $updated_at
27 * @property string $deleted_at
28 */
29 class Video extends Model {
30
31 /**
32 * Table used to access db
33 *
34 * @var string
35 */
36 protected $table = 'presto_player_videos';
37
38 /**
39 * Model Schema
40 *
41 * @var array
42 */
43 public function schema() {
44 return array(
45 'id' => array(
46 'type' => 'integer',
47 ),
48 'title' => array(
49 'type' => 'string',
50 'sanitize_callback' => 'wp_kses_post',
51 ),
52 'type' => array(
53 'type' => 'string',
54 'sanitize_callback' => 'sanitize_text_field',
55 ),
56 'src' => array(
57 'type' => 'string',
58 'sanitize_callback' => 'esc_url_raw',
59 ),
60 'external_id' => array(
61 'type' => 'string',
62 'sanitize_callback' => 'sanitize_text_field',
63 ),
64 'attachment_id' => array(
65 'type' => 'integer',
66 ),
67 'post_id' => array(
68 'type' => 'integer',
69 ),
70 'created_by' => array(
71 'type' => 'integer',
72 'default' => get_current_user_id(),
73 ),
74 'created_at' => array(
75 'type' => 'string',
76 ),
77 'updated_at' => array(
78 'type' => 'string',
79 ),
80 'deleted_at' => array(
81 'type' => 'string',
82 ),
83 );
84 }
85
86 /**
87 * These attributes are queryable
88 *
89 * @var array
90 */
91 protected $queryable = array(
92 'src',
93 'video_id',
94 'title',
95 'type',
96 'attachment_id',
97 'external_id',
98 );
99
100 /**
101 * Hydrate the model from the given attributes.
102 *
103 * Auto-populates title and src from the attachment when an attachment_id is set.
104 *
105 * @param array $args Attribute values.
106 * @return self
107 */
108 public function set( $args ) {
109 parent::set( $args );
110
111 if ( ! empty( $this->attributes->attachment_id ) ) {
112 $title = get_the_title( $this->attributes->attachment_id );
113 $src = wp_get_attachment_url( $this->attributes->attachment_id );
114 $this->attributes->title = $title ? $title : $this->attributes->title;
115 $this->attributes->src = $src ? $src : $this->attributes->src;
116 }
117
118 return $this;
119 }
120
121 /**
122 * Get the video's embedded title from noembed.com.
123 *
124 * @param string $src Video source URL.
125 * @return string|\WP_Error Embedded title, or WP_Error on HTTP failure.
126 */
127 public function getEmbeddedTitle( $src = '' ) {
128 if ( empty( $src ) ) {
129 return '';
130 }
131 $response = wp_remote_get( 'https://noembed.com/embed?dataType=json&url=' . urlencode( $src ) );
132 if ( is_wp_error( $response ) ) {
133 return $response;
134 }
135 $body = wp_remote_retrieve_body( $response );
136 $api_response = json_decode( $body, true );
137 return $api_response['title'] ?? '';
138 }
139
140 /**
141 * Maybe auto-create title if not set.
142 *
143 * @param array $args Video attributes.
144 * @return array
145 */
146 public function maybeAutoCreateTitle( $args ) {
147 // Remotely get the title if not provided.
148 if ( empty( $args['title'] ) && in_array( $args['type'], array( 'youtube', 'vimeo' ), true ) ) {
149 $title = $this->getEmbeddedTitle( $args['src'] );
150 if ( ! is_wp_error( $title ) && ! empty( $title ) ) {
151 $args['title'] = $title;
152 }
153 }
154
155 // Fallback to a filename extracted from the url, or the raw url. Embed types keep the raw url
156 // so a failed noembed lookup doesn't turn e.g. /watch?v=x into the title "Watch".
157 if ( empty( $args['title'] ) && ! empty( $args['src'] ) ) {
158 $path = in_array( $args['type'], array( 'youtube', 'vimeo' ), true ) ? '' : wp_parse_url( $args['src'], PHP_URL_PATH );
159 $name = $path ? pathinfo( rawurldecode( $path ), PATHINFO_FILENAME ) : '';
160 $args['title'] = $name ? ucwords( str_replace( array( '-', '_' ), ' ', $name ) ) : $args['src'];
161 }
162
163 // Return args.
164 return $args;
165 }
166
167 /**
168 * Create a new video.
169 *
170 * @param array $args Video attributes.
171 * @return int|\WP_Error
172 */
173 public function create( $args = array() ) {
174 // Required params.
175 if ( empty( $args['external_id'] ) && empty( $args['attachment_id'] ) && empty( $args['src'] ) ) {
176 return new \WP_Error( 'invalid_parameters', 'You must enter an attachment_id, external_id or src.' );
177 }
178
179 $args = $this->maybeAutoCreateTitle( $args );
180
181 // Create.
182 return parent::create( $args );
183 }
184
185 /**
186 * Update a video record.
187 *
188 * @param array $args Video attributes.
189 * @return int|false
190 */
191 public function update( $args = array() ) {
192 if ( ! empty( $args['attachment_id'] ) && ! empty( $args['title'] ) ) {
193 wp_update_post(
194 array(
195 'ID' => $args['attachment_id'],
196 'post_title' => $args['title'],
197 )
198 );
199 }
200 return parent::update( $args );
201 }
202
203 /**
204 * Get the video's created at date.
205 *
206 * @return string Created At date
207 */
208 public function getCreatedAt() {
209 return $this->created_at;
210 }
211
212 /**
213 * Get the video title.
214 *
215 * @return string Title
216 */
217 public function getTitle() {
218 return $this->title;
219 }
220
221 /**
222 * Get the attachment id.
223 *
224 * @return int Attachment ID
225 */
226 public function getAttachmentID() {
227 return $this->attachment_id;
228 }
229
230 /**
231 * Get the attachment post title.
232 *
233 * @param int $attachment_id Attachment ID.
234 * @return string|false Title or false if not found.
235 */
236 public function getAttachmentPostTitle( $attachment_id = null ) {
237 if ( empty( $attachment_id ) ) {
238 return false;
239 }
240 $attachment = get_post( $attachment_id );
241 $attachment_title = $attachment->post_title;
242 if ( ! empty( $attachment_title ) ) {
243 return $attachment_title;
244 }
245 return false;
246 }
247
248 /**
249 * Get external_id (GUID) from database by video ID or src.
250 *
251 * @param string $src The video source URL (optional).
252 * @return string The external_id (GUID) or empty string if not found.
253 */
254 public function getExternalId( $src = '' ) {
255 // If external_id is already set, return it.
256 if ( isset( $this->external_id ) ) {
257 return $this->external_id;
258 }
259
260 // If video_id is not set, return empty string.
261 if ( empty( $src ) ) {
262 return '';
263 }
264
265 // Find the video by id and return the external_id.
266 $video = $this->findWhere( array( 'src' => $src ) );
267 return $video->external_id ?? '';
268 }
269 }
270