PluginProbe ʕ •ᴥ•ʔ
Presto Player / 4.2.1
Presto Player v4.2.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 1 month ago Block.php 9 months ago CurrentUser.php 1 year ago EmailCollection.php 1 year ago Model.php 3 months ago ModelInterface.php 1 year ago Player.php 1 year ago Post.php 1 year ago Preset.php 1 month ago ReusableVideo.php 1 month ago Setting.php 1 year ago Video.php 1 month ago Webhook.php 1 year ago
Video.php
265 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 url.
156 $args['title'] = empty( $args['title'] ) ? $args['src'] : $args['title'];
157
158 // Return args.
159 return $args;
160 }
161
162 /**
163 * Create a new video.
164 *
165 * @param array $args Video attributes.
166 * @return int|\WP_Error
167 */
168 public function create( $args = array() ) {
169 // Required params.
170 if ( empty( $args['external_id'] ) && empty( $args['attachment_id'] ) && empty( $args['src'] ) ) {
171 return new \WP_Error( 'invalid_parameters', 'You must enter an attachment_id, external_id or src.' );
172 }
173
174 $args = $this->maybeAutoCreateTitle( $args );
175
176 // Create.
177 return parent::create( $args );
178 }
179
180 /**
181 * Update a video record.
182 *
183 * @param array $args Video attributes.
184 * @return int|false
185 */
186 public function update( $args = array() ) {
187 if ( ! empty( $args['attachment_id'] ) && ! empty( $args['title'] ) ) {
188 wp_update_post(
189 array(
190 'ID' => $args['attachment_id'],
191 'post_title' => $args['title'],
192 )
193 );
194 }
195 return parent::update( $args );
196 }
197
198 /**
199 * Get the video's created at date.
200 *
201 * @return string Created At date
202 */
203 public function getCreatedAt() {
204 return $this->created_at;
205 }
206
207 /**
208 * Get the video title.
209 *
210 * @return string Title
211 */
212 public function getTitle() {
213 return $this->title;
214 }
215
216 /**
217 * Get the attachment id.
218 *
219 * @return int Attachment ID
220 */
221 public function getAttachmentID() {
222 return $this->attachment_id;
223 }
224
225 /**
226 * Get the attachment post title.
227 *
228 * @param int $attachment_id Attachment ID.
229 * @return string|false Title or false if not found.
230 */
231 public function getAttachmentPostTitle( $attachment_id = null ) {
232 if ( empty( $attachment_id ) ) {
233 return false;
234 }
235 $attachment = get_post( $attachment_id );
236 $attachment_title = $attachment->post_title;
237 if ( ! empty( $attachment_title ) ) {
238 return $attachment_title;
239 }
240 return false;
241 }
242
243 /**
244 * Get external_id (GUID) from database by video ID or src.
245 *
246 * @param string $src The video source URL (optional).
247 * @return string The external_id (GUID) or empty string if not found.
248 */
249 public function getExternalId( $src = '' ) {
250 // If external_id is already set, return it.
251 if ( isset( $this->external_id ) ) {
252 return $this->external_id;
253 }
254
255 // If video_id is not set, return empty string.
256 if ( empty( $src ) ) {
257 return '';
258 }
259
260 // Find the video by id and return the external_id.
261 $video = $this->findWhere( array( 'src' => $src ) );
262 return $video->external_id ?? '';
263 }
264 }
265