PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.7
Jetpack – WP Security, Backup, Speed, & Growth v9.7
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sal / class.json-api-post-base.php

class.json-api-post-base.php in Jetpack – WP Security, Backup, Speed, & Growth 9.7, at sal/class.json-api-post-base.php

698 lines 21.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This class wraps a WP_Post and proxies any undefined attributes
4 * and methods to the wrapped class. We need to do this because at present
5 * the WP_Post class is marked as final (in 4.5 this will change, though it's
6 * not clear if there will be a mechanism to retrieve from the DB into the over-
7 * ridden class dynamically).
8 **/
9
10 require_once dirname( __FILE__ ) . '/class.json-api-metadata.php';
11 require_once dirname( __FILE__ ) . '/class.json-api-date.php';
12 require_once ABSPATH . 'wp-admin/includes/post.php';
13 require_once ABSPATH . 'wp-includes/post.php';
14
15 abstract class SAL_Post {
16 public $post;
17 public $context;
18 public $site;
19
20 function __construct( $site, $post, $context ) {
21 $this->post = $post;
22 $this->context = $context;
23 $this->site = $site;
24 }
25
26 public function __set( $key, $value ) {
27 $this->post->{ $key } = $value;
28 }
29
30 public function __get( $key ) {
31 if ( $key === 'links' ) {
32 require_once dirname( __FILE__ ) . '/class.json-api-links.php';
33 return WPCOM_JSON_API_Links::getInstance();
34 }
35 return $this->post->{ $key };
36 }
37
38 public function __call( $name, $arguments ) {
39 if ( is_callable( array( $this->post, $name ) ) ) {
40 return call_user_func_array( array( $this->post, $name ), $arguments );
41 } else {
42 trigger_error("Call to undefined method '{$name}'");
43 }
44 }
45
46 public function __isset ( $name ) {
47 return isset( $this->post->{ $name } );
48 }
49
50 abstract public function get_like_count();
51 abstract public function is_liked();
52 abstract public function is_reblogged();
53 abstract public function is_following();
54 abstract public function get_global_id();
55 abstract public function get_geo();
56
57 public function get_menu_order() {
58 return (int) $this->post->menu_order;
59 }
60
61 public function get_guid() {
62 return (string) $this->post->guid;
63 }
64
65 public function get_type() {
66 return (string) $this->post->post_type;
67 }
68
69 public function get_terms() {
70 $taxonomies = get_object_taxonomies( $this->post, 'objects' );
71 $terms = array();
72 foreach ( $taxonomies as $taxonomy ) {
73 if ( ! $taxonomy->public && ! current_user_can( $taxonomy->cap->assign_terms ) ) {
74 continue;
75 }
76
77 $terms[ $taxonomy->name ] = array();
78
79 $taxonomy_terms = wp_get_object_terms( $this->post->ID, $taxonomy->name, array( 'fields' => 'all' ) );
80 foreach ( $taxonomy_terms as $term ) {
81 $formatted_term = $this->format_taxonomy( $term, $taxonomy->name, 'display' );
82 $terms[ $taxonomy->name ][ $term->name ] = $formatted_term;
83 }
84
85 $terms[ $taxonomy->name ] = (object) $terms[ $taxonomy->name ];
86 }
87
88 return (object) $terms;
89 }
90
91 public function get_tags() {
92 $tags = array();
93 $terms = wp_get_post_tags( $this->post->ID );
94 foreach ( $terms as $term ) {
95 if ( !empty( $term->name ) ) {
96 $tags[$term->name] = $this->format_taxonomy( $term, 'post_tag', 'display' );
97 }
98 }
99 return (object) $tags;
100 }
101
102 public function get_categories() {
103 $categories = array();
104 $terms = wp_get_object_terms( $this->post->ID, 'category', array( 'fields' => 'all' ) );
105 foreach ( $terms as $term ) {
106 if ( !empty( $term->name ) ) {
107 $categories[$term->name] = $this->format_taxonomy( $term, 'category', 'display' );
108 }
109 }
110 return (object) $categories;
111 }
112
113 public function get_attachments_and_count() {
114 $attachments = array();
115 $_attachments = new WP_Query( array( 'post_parent' => $this->post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => '20' ) );
116 foreach ( $_attachments->posts as $attachment ) {
117 $attachments[$attachment->ID] = $this->get_media_item_v1_1( $attachment->ID );
118 }
119 return array( (object) $attachments, (int) $_attachments->found_posts );
120 }
121
122 public function get_metadata() {
123 $metadata = array();
124 foreach ( (array) has_meta( $this->post->ID ) as $meta ) {
125 // Don't expose protected fields.
126 $meta_key = $meta['meta_key'];
127
128 $show = !( WPCOM_JSON_API_Metadata::is_internal_only( $meta_key ) )
129 &&
130 (
131 WPCOM_JSON_API_Metadata::is_public( $meta_key )
132 ||
133 current_user_can( 'edit_post_meta', $this->post->ID , $meta_key )
134 );
135
136 if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY == $meta_key && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
137 $show = false;
138 }
139
140 if ( $show ) {
141 $metadata[] = array(
142 'id' => $meta['meta_id'],
143 'key' => $meta['meta_key'],
144 'value' => $this->safe_maybe_unserialize( $meta['meta_value'] ),
145 );
146 }
147 }
148
149 return $metadata;
150 }
151
152 public function get_meta() {
153 $meta = (object) array(
154 'links' => (object) array(
155 'self' => (string) $this->get_post_link(),
156 'help' => (string) $this->get_post_link( 'help' ),
157 'site' => (string) $this->get_site_link(),
158 'replies' => (string) $this->get_post_link( 'replies/' ),
159 'likes' => (string) $this->get_post_link( 'likes/' ),
160 ),
161 );
162
163 $amp_permalink = get_post_meta( $this->post->ID, '_jetpack_amp_permalink', true );
164
165 if ( ! empty( $amp_permalink ) ) {
166 $meta->links->amp = (string) $amp_permalink;
167 }
168
169 // add autosave link if a more recent autosave exists
170 if ( 'edit' === $this->context ) {
171 $autosave = wp_get_post_autosave( $this->post->ID );
172 if ( $autosave && $autosave->post_modified > $this->post->post_modified )
173 $meta->links->autosave = (string) $this->get_post_link() . '/autosave';
174 }
175
176 return $meta;
177 }
178
179 public function get_current_user_capabilities() {
180 return array(
181 'publish_post' => current_user_can( 'publish_post', $this->post->ID ),
182 'delete_post' => current_user_can( 'delete_post', $this->post->ID ),
183 'edit_post' => current_user_can( 'edit_post', $this->post->ID )
184 );
185 }
186
187 public function get_revisions() {
188 if ( 'edit' !== $this->context ) {
189 return false;
190 }
191
192 $revisions = array();
193 $post_revisions = wp_get_post_revisions( $this->post->ID );
194
195 foreach ( $post_revisions as $_post ) {
196 $revisions[] = $_post->ID;
197 }
198
199 return $revisions;
200 }
201
202 public function get_other_urls() {
203 $other_urls = array();
204
205 if ( 'publish' !== $this->post->post_status ) {
206 $other_urls = $this->get_permalink_suggestions( $this->post->post_title );
207 }
208
209 return (object) $other_urls;
210 }
211
212 protected function get_site_link() {
213 return $this->links->get_site_link( $this->site->get_id() );
214 }
215
216 protected function get_post_link( $path = null ) {
217 return $this->links->get_post_link( $this->site->get_id(), $this->post->ID, $path );
218 }
219
220 public function get_publicize_urls() {
221 $publicize_URLs = array();
222 $publicize = get_post_meta( $this->post->ID, 'publicize_results', true );
223 if ( $publicize ) {
224 foreach ( $publicize as $service => $data ) {
225 switch ( $service ) {
226 case 'twitter' :
227 foreach ( $data as $datum ) {
228 $publicize_URLs[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
229 }
230 break;
231 case 'fb' :
232 foreach ( $data as $datum ) {
233 $publicize_URLs[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
234 }
235 break;
236 }
237 }
238 }
239 return (array) $publicize_URLs;
240 }
241
242 public function get_page_template() {
243 return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
244 }
245
246 // note this is overridden in jetpack-shadow
247 public function get_featured_image() {
248 $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
249 if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
250 return (string) $image_attributes[0];
251 } else {
252 return '';
253 }
254 }
255
256 public function get_post_thumbnail() {
257 $thumb = null;
258
259 $thumb_id = get_post_thumbnail_id( $this->post->ID );
260
261 if ( ! empty( $thumb_id ) ) {
262 $attachment = get_post( $thumb_id );
263 if ( ! empty( $attachment ) )
264 $featured_image_object = $this->get_attachment( $attachment );
265
266 if ( ! empty( $featured_image_object ) ) {
267 $thumb = (object) $featured_image_object;
268 }
269 }
270
271 return $thumb;
272 }
273
274 public function get_format() {
275 $format = (string) get_post_format( $this->post->ID );
276 if ( !$format ) {
277 $format = 'standard';
278 }
279
280 return $format;
281 }
282
283 private function get_attachment( $attachment ) {
284 $metadata = wp_get_attachment_metadata( $attachment->ID );
285
286 $result = array(
287 'ID' => (int) $attachment->ID,
288 'URL' => (string) wp_get_attachment_url( $attachment->ID ),
289 'guid' => (string) $attachment->guid,
290 'mime_type' => (string) $attachment->post_mime_type,
291 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
292 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
293 );
294
295 if ( isset( $metadata['duration'] ) ) {
296 $result['duration'] = (int) $metadata['duration'];
297 }
298
299 /** This filter is documented in class.jetpack-sync.php */
300 return (object) apply_filters( 'get_attachment', $result );
301 }
302
303 public function get_date() {
304 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_date_gmt, $this->post->post_date );
305 }
306
307 public function get_modified_date() {
308 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_modified_gmt, $this->post->post_modified );
309 }
310
311 public function get_title() {
312 if ( 'display' === $this->context ) {
313 return (string) get_the_title( $this->post->ID );
314 } else {
315 return (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
316 }
317 }
318
319 public function get_url() {
320 if ( 'revision' === $this->post->post_type ) {
321 return (string) esc_url_raw( get_permalink( $this->post->post_parent ) );
322 } else {
323 return (string) esc_url_raw( get_permalink( $this->post->ID ) );
324 }
325 }
326
327 public function get_shortlink() {
328 return (string) esc_url_raw( wp_get_shortlink( $this->post->ID ) );
329 }
330
331 public function get_content() {
332 if ( 'display' === $this->context ) {
333 // TODO: move this WPCOM-specific hack
334 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
335 $content = (string) $this->get_the_post_content_for_display();
336 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
337 return $content;
338 } else {
339 return (string) $this->post->post_content;
340 }
341 }
342
343 public function get_excerpt() {
344 if ( 'display' === $this->context ) {
345 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
346 ob_start();
347 the_excerpt();
348 $response = (string) ob_get_clean();
349 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
350 } else {
351 $response = htmlspecialchars_decode( (string) $this->post->post_excerpt, ENT_QUOTES );
352 }
353 return $response;
354 }
355
356 public function get_status() {
357 return (string) get_post_status( $this->post->ID );
358 }
359
360 public function is_sticky() {
361 return (bool) is_sticky( $this->post->ID );
362 }
363
364 public function get_slug() {
365 return (string) $this->post->post_name;
366 }
367
368 public function get_password() {
369 $password = (string) $this->post->post_password;
370 if ( 'edit' === $this->context ) {
371 $password = htmlspecialchars_decode( (string) $password, ENT_QUOTES );
372 }
373 return $password;
374 }
375
376 public function get_parent() {
377 if ( $this->post->post_parent ) {
378 $parent = get_post( $this->post->post_parent );
379 if ( 'display' === $this->context ) {
380 $parent_title = (string) get_the_title( $parent->ID );
381 } else {
382 $parent_title = (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
383 }
384 return (object) array(
385 'ID' => (int) $parent->ID,
386 'type' => (string) $parent->post_type,
387 'link' => (string) $this->links->get_post_link( $this->site->get_id(), $parent->ID ),
388 'title' => $parent_title,
389 );
390 } else {
391 return false;
392 }
393 }
394
395 function the_password_form() {
396 return __( 'This post is password protected.', 'jetpack' );
397 }
398
399 public function get_discussion() {
400 return array(
401 'comments_open' => (bool) comments_open( $this->post->ID ),
402 'comment_status' => (string) $this->post->comment_status,
403 'pings_open' => (bool) pings_open( $this->post->ID ),
404 'ping_status' => (string) $this->post->ping_status,
405 'comment_count' => (int) $this->post->comment_count,
406 );
407 }
408
409 public function is_likes_enabled() {
410 /** This filter is documented in modules/likes.php */
411 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
412 $post_likes_switched = get_post_meta( $this->post->ID, 'switch_like_status', true );
413
414 return $post_likes_switched || ( $sitewide_likes_enabled && $post_likes_switched !== '0' );
415 }
416
417 public function is_sharing_enabled() {
418 $show = true;
419 /** This filter is documented in modules/sharedaddy/sharing-service.php */
420 $show = apply_filters( 'sharing_show', $show, $this->post );
421
422 $switched_status = get_post_meta( $this->post->ID, 'sharing_disabled', false );
423
424 if ( !empty( $switched_status ) )
425 $show = false;
426
427 return (bool) $show;
428 }
429
430 // No Blog ID parameter. No Post ID parameter. Depends on globals.
431 // Expects setup_postdata() to already have been run
432 function get_the_post_content_for_display() {
433 global $pages, $page;
434
435 $old_pages = $pages;
436 $old_page = $page;
437
438 $content = join( "\n\n", $pages );
439 $content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
440 $pages = array( $content );
441 $page = 1;
442
443 ob_start();
444 the_content();
445 $return = ob_get_clean();
446
447 $pages = $old_pages;
448 $page = $old_page;
449
450 return $return;
451 }
452
453 public function get_author() {
454 if ( 0 == $this->post->post_author )
455 return null;
456
457 $show_email = $this->context === 'edit' && current_user_can( 'edit_post', $this->post->ID );
458
459 $user = get_user_by( 'id', $this->post->post_author );
460
461 if ( ! $user || is_wp_error( $user ) ) {
462 trigger_error( 'Unknown user', E_USER_WARNING );
463
464 return null;
465 }
466
467 // TODO factor this out
468 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
469 $active_blog = get_active_blog_for_user( $user->ID );
470 $site_id = $active_blog->blog_id;
471 $profile_URL = "https://en.gravatar.com/{$user->user_login}";
472 } else {
473 $profile_URL = 'https://en.gravatar.com/' . md5( strtolower( trim( $user->user_email ) ) );
474 $site_id = -1;
475 }
476
477 $author = array(
478 'ID' => (int) $user->ID,
479 'login' => (string) $user->user_login,
480 'email' => $show_email ? (string) $user->user_email : false, // (string|bool)
481 'name' => (string) $user->display_name,
482 'first_name' => (string) $user->first_name,
483 'last_name' => (string) $user->last_name,
484 'nice_name' => (string) $user->user_nicename,
485 'URL' => (string) esc_url_raw( $user->user_url ),
486 'avatar_URL' => (string) esc_url_raw( $this->get_avatar_url( $user->user_email ) ),
487 'profile_URL' => (string) esc_url_raw( $profile_URL )
488 );
489
490 if ($site_id > -1) {
491 $author['site_ID'] = (int) $site_id;
492 }
493
494 return (object) $author;
495 }
496
497 protected function get_avatar_url( $email, $avatar_size = 96 ) {
498 $avatar_url = wpcom_get_avatar_url( $email, $avatar_size, '', true );
499 if ( ! $avatar_url || is_wp_error( $avatar_url ) ) {
500 return '';
501 }
502
503 return esc_url_raw( htmlspecialchars_decode( $avatar_url[0] ) );
504 }
505
506 /**
507 * Get extra post permalink suggestions
508 * @return array array of permalink suggestions: 'permalink_URL', 'suggested_slug'
509 */
510 public function get_permalink_suggestions( $title ) {
511 $suggestions = array();
512 list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $this->post->ID, $title );
513 return $suggestions;
514 }
515
516 private function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
517 // Permissions
518 switch ( $context ) {
519 case 'edit' :
520 $tax = get_taxonomy( $taxonomy_type );
521 if ( !current_user_can( $tax->cap->edit_terms ) )
522 return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
523 break;
524 case 'display' :
525 if ( -1 == get_option( 'blog_public' ) && ! current_user_can( 'read' ) ) {
526 return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
527 }
528 break;
529 default :
530 return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
531 }
532
533 $response = array();
534 $response['ID'] = (int) $taxonomy->term_id;
535 $response['name'] = (string) $taxonomy->name;
536 $response['slug'] = (string) $taxonomy->slug;
537 $response['description'] = (string) $taxonomy->description;
538 $response['post_count'] = (int) $taxonomy->count;
539
540 if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
541 $response['parent'] = (int) $taxonomy->parent;
542 }
543
544 $response['meta'] = (object) array(
545 'links' => (object) array(
546 'self' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type ),
547 'help' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type, 'help' ),
548 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
549 ),
550 );
551
552 return (object) $response;
553 }
554
555 // TODO: factor this out into site
556 private function get_media_item_v1_1( $media_id ) {
557 $media_item = get_post( $media_id );
558
559 if ( ! $media_item || is_wp_error( $media_item ) )
560 return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
561
562 $file = basename( wp_get_attachment_url( $media_item->ID ) );
563 $file_info = pathinfo( $file );
564 $ext = $file_info['extension'];
565
566 $response = array(
567 'ID' => $media_item->ID,
568 'URL' => wp_get_attachment_url( $media_item->ID ),
569 'guid' => $media_item->guid,
570 'date' => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_date_gmt, $media_item->post_date ),
571 'post_ID' => $media_item->post_parent,
572 'author_ID' => (int) $media_item->post_author,
573 'file' => $file,
574 'mime_type' => $media_item->post_mime_type,
575 'extension' => $ext,
576 'title' => $media_item->post_title,
577 'caption' => $media_item->post_excerpt,
578 'description' => $media_item->post_content,
579 'alt' => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
580 'thumbnails' => array()
581 );
582
583 if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) {
584 $metadata = wp_get_attachment_metadata( $media_item->ID );
585 if ( isset( $metadata['height'], $metadata['width'] ) ) {
586 $response['height'] = $metadata['height'];
587 $response['width'] = $metadata['width'];
588 }
589
590 if ( isset( $metadata['sizes'] ) ) {
591 /**
592 * Filter the thumbnail sizes available for each attachment ID.
593 *
594 * @module json-api
595 *
596 * @since 3.9.0
597 *
598 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
599 * @param string $media_id Attachment ID.
600 */
601 $sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_id );
602 if ( is_array( $sizes ) ) {
603 foreach ( $sizes as $size => $size_details ) {
604 $response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
605 }
606 }
607 }
608
609 if ( isset( $metadata['image_meta'] ) ) {
610 $response['exif'] = $metadata['image_meta'];
611 }
612 }
613
614 if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ) ) ) {
615 $metadata = wp_get_attachment_metadata( $media_item->ID );
616 if ( isset( $metadata['length'] ) ) {
617 $response['length'] = $metadata['length'];
618 }
619
620 $response['exif'] = $metadata;
621 }
622
623 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
624 $metadata = wp_get_attachment_metadata( $media_item->ID );
625 if ( isset( $metadata['height'], $metadata['width'] ) ) {
626 $response['height'] = $metadata['height'];
627 $response['width'] = $metadata['width'];
628 }
629
630 if ( isset( $metadata['length'] ) ) {
631 $response['length'] = $metadata['length'];
632 }
633
634 // add VideoPress info
635 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
636 $info = video_get_info_by_blogpostid( $this->site->get_id(), $media_id );
637
638 // Thumbnails
639 if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
640 $response['thumbnails'] = array( 'fmt_hd' => '', 'fmt_dvd' => '', 'fmt_std' => '' );
641 foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
642 if ( video_format_done( $info, $size ) ) {
643 $response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
644 } else {
645 unset( $response['thumbnails'][ $size ] );
646 }
647 }
648 }
649
650 $response['videopress_guid'] = $info->guid;
651 $response['videopress_processing_done'] = true;
652 if ( '0000-00-00 00:00:00' == $info->finish_date_gmt ) {
653 $response['videopress_processing_done'] = false;
654 }
655 }
656 }
657
658 $response['thumbnails'] = (object) $response['thumbnails'];
659
660 $response['meta'] = (object) array(
661 'links' => (object) array(
662 'self' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id ),
663 'help' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id, 'help' ),
664 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
665 ),
666 );
667
668 // add VideoPress link to the meta
669 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
670 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
671 $response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
672 }
673 }
674
675 if ( $media_item->post_parent > 0 ) {
676 $response['meta']->links->parent = (string) $this->links->get_post_link( $this->site->get_id(), $media_item->post_parent );
677 }
678
679 return (object) $response;
680 }
681
682 /**
683 * Temporary wrapper around maybe_unserialize() to catch exceptions thrown by unserialize().
684 *
685 * Can be removed after https://core.trac.wordpress.org/ticket/45895 lands in Core.
686 *
687 * @param string $original Serialized string.
688 * @return string Unserialized string or original string if an exception was raised.
689 **/
690 protected function safe_maybe_unserialize( $original ) {
691 try {
692 return maybe_unserialize( $original );
693 } catch ( Exception $e ) {
694 return $original;
695 }
696 }
697 }
698