PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.7
Jetpack – WP Security, Backup, Speed, & Growth v12.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 12.7, at sal/class.json-api-post-base.php

1,059 lines 30.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 * @package automattic/jetpack
10 */
11
12 use Automattic\Jetpack\Status;
13
14 require_once __DIR__ . '/class.json-api-metadata.php';
15 require_once __DIR__ . '/class.json-api-date.php';
16 require_once ABSPATH . 'wp-admin/includes/post.php';
17 require_once ABSPATH . 'wp-includes/post.php';
18
19 /**
20 * Base class for SAL_Post.
21 */
22 abstract class SAL_Post {
23
24 /**
25 * A WP_Post instance.
26 *
27 * @var WP_Post
28 */
29 public $post;
30
31 /**
32 * The post request context (for example 'edit' or 'display')
33 *
34 * @var string
35 */
36 public $context;
37
38 /**
39 * A Jetpack_Site instance.
40 *
41 * @var Jetpack_Site
42 */
43 public $site;
44
45 /**
46 * Constructor function
47 *
48 * @param Jetpack_Site $site A Jetpack_Site instance.
49 * @param WP_Post $post A WP_Post instance.
50 * @param string $context The post request context (for example 'edit' or 'display').
51 */
52 public function __construct( $site, $post, $context ) {
53 $this->post = $post;
54 $this->context = $context;
55 $this->site = $site;
56 }
57
58 /**
59 * Setting this WP_Post instance's key value
60 *
61 * @param string $key The post key to set.
62 * @param string $value The value to set the post key to (for example filter, ID, post_status).
63 */
64 public function __set( $key, $value ) {
65 $this->post->{ $key } = $value;
66 }
67
68 /**
69 * Returning a WPCOM_JSON_API_Links instance if the post key is set to 'links', or the post key value.
70 *
71 * @param string $key The post key value.
72 *
73 * @return WPCOM_JSON_API_Links|string
74 */
75 public function __get( $key ) {
76 if ( 'links' === $key ) {
77 require_once __DIR__ . '/class.json-api-links.php';
78 return WPCOM_JSON_API_Links::getInstance();
79 }
80 return $this->post->{ $key };
81 }
82
83 /**
84 * A function to either call a given function, or return an error if it doesn't exist.
85 *
86 * @param string $name A function name to be called.
87 * @param mixed $arguments Arguments to be passed into the given function.
88 *
89 * @return mixed|bool
90 */
91 public function __call( $name, $arguments ) {
92 if ( is_callable( array( $this->post, $name ) ) ) {
93 return call_user_func_array( array( $this->post, $name ), $arguments );
94 } else {
95 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
96 trigger_error(
97 esc_html(
98 sprintf(
99 /* translators: %s is the method name that has been called */
100 __( 'Call to undefined method %s', 'jetpack' ),
101 $name
102 )
103 )
104 );
105 }
106 }
107
108 /**
109 * Checking to see if a given property is set.
110 *
111 * @param string $name Property to check if set.
112 *
113 * @return bool
114 */
115 public function __isset( $name ) {
116 return isset( $this->post->{ $name } );
117 }
118
119 /**
120 * Defining a base get_like_count() function to be extended in the Jetpack_Post class.
121 *
122 * This will define a default value for the like counts on a post, if this hasn't been defined yet.
123 *
124 * @see class.json-api-post-jetpack.php
125 */
126 abstract public function get_like_count();
127
128 /**
129 * Defining a base is_liked() function to be extended in the Jetpack_Post class.
130 *
131 * This will define a default value for whether or not the current user likes this post, if this hasn't been defined yet.
132 *
133 * @see class.json-api-post-jetpack.php
134 */
135 abstract public function is_liked();
136
137 /**
138 * Defining a base is_reblogged() function to be extended in the Jetpack_Post class.
139 *
140 * This will define a default value for whether or not the current user reblogged this post, if this hasn't been defined yet.
141 *
142 * @see class.json-api-post-jetpack.php
143 */
144 abstract public function is_reblogged();
145
146 /**
147 * Defining a base is_following() function to be extended in the Jetpack_Post class.
148 *
149 * This will define a default value for whether or not the current user is following this blog, if this hasn't been defined yet.
150 *
151 * @see class.json-api-post-jetpack.php
152 */
153 abstract public function is_following();
154
155 /**
156 * Defining a base get_global_id() function to be extended in the Jetpack_Post class.
157 *
158 * This will define the unique WordPress.com-wide representation of a post, if this hasn't been defined yet.
159 *
160 * @see class.json-api-post-jetpack.php
161 */
162 abstract public function get_global_id();
163
164 /**
165 * Defining a base get_geo() function to be extended in the Jetpack_Post class.
166 *
167 * This will define a default value for whether or not there is gelocation data for this post, if this hasn't been defined yet.
168 *
169 * @see class.json-api-post-jetpack.php
170 */
171 abstract public function get_geo();
172
173 /**
174 * Returns an int which helps define the menu order for the post.
175 *
176 * @return int
177 */
178 public function get_menu_order() {
179 return (int) $this->post->menu_order;
180 }
181
182 /**
183 * Returns a string which represents the post's GUID.
184 *
185 * @return string
186 */
187 public function get_guid() {
188 return (string) $this->post->guid;
189 }
190
191 /**
192 * Returns a string which represents the post type.
193 *
194 * @return string
195 */
196 public function get_type() {
197 return (string) $this->post->post_type;
198 }
199
200 /**
201 * Returns an object which holds the terms associated with that post object.
202 *
203 * @return object
204 */
205 public function get_terms() {
206 $taxonomies = get_object_taxonomies( $this->post, 'objects' );
207 $terms = array();
208 foreach ( $taxonomies as $taxonomy ) {
209 if ( ! $taxonomy->public && ! current_user_can( $taxonomy->cap->assign_terms ) ) {
210 continue;
211 }
212
213 $terms[ $taxonomy->name ] = array();
214
215 $taxonomy_terms = wp_get_object_terms( $this->post->ID, $taxonomy->name, array( 'fields' => 'all' ) );
216 foreach ( $taxonomy_terms as $term ) {
217 $formatted_term = $this->format_taxonomy( $term, $taxonomy->name, 'display' );
218 $terms[ $taxonomy->name ][ $term->name ] = $formatted_term;
219 }
220
221 $terms[ $taxonomy->name ] = (object) $terms[ $taxonomy->name ];
222 }
223
224 return (object) $terms;
225 }
226
227 /**
228 * Returns an object which holds the posts tag details
229 *
230 * @return object
231 */
232 public function get_tags() {
233 $tags = array();
234 $terms = wp_get_post_tags( $this->post->ID );
235 foreach ( $terms as $term ) {
236 if ( ! empty( $term->name ) ) {
237 $tags[ $term->name ] = $this->format_taxonomy( $term, 'post_tag', 'display' );
238 }
239 }
240 return (object) $tags;
241 }
242
243 /**
244 * Returns an object which holds the posts category details
245 *
246 * @return object
247 */
248 public function get_categories() {
249 $categories = array();
250 $terms = wp_get_object_terms( $this->post->ID, 'category', array( 'fields' => 'all' ) );
251 foreach ( $terms as $term ) {
252 if ( ! empty( $term->name ) ) {
253 $categories[ $term->name ] = $this->format_taxonomy( $term, 'category', 'display' );
254 }
255 }
256 return (object) $categories;
257 }
258
259 /**
260 * Returns an array of objects which hold the posts attachment information and numbers representing how many associated posts are found.
261 *
262 * @return array
263 */
264 public function get_attachments_and_count() {
265 $attachments = array();
266 $_attachments = new WP_Query(
267 array(
268 'post_parent' => $this->post->ID,
269 'post_status' => 'inherit',
270 'post_type' => 'attachment',
271 'posts_per_page' => '20',
272 )
273 );
274 foreach ( $_attachments->posts as $attachment ) {
275 $attachments[ $attachment->ID ] = $this->get_media_item_v1_1( $attachment->ID );
276 }
277 return array( (object) $attachments, (int) $_attachments->found_posts );
278 }
279
280 /**
281 * Returns an array with a posts metadata information.
282 *
283 * @return array
284 */
285 public function get_metadata() {
286 $metadata = array();
287 foreach ( (array) has_meta( $this->post->ID ) as $meta ) {
288 // Don't expose protected fields.
289 $meta_key = $meta['meta_key'];
290
291 $show = ! ( WPCOM_JSON_API_Metadata::is_internal_only( $meta_key ) )
292 &&
293 (
294 WPCOM_JSON_API_Metadata::is_public( $meta_key )
295 ||
296 current_user_can( 'edit_post_meta', $this->post->ID, $meta_key )
297 );
298
299 if ( Jetpack_SEO_Posts::DESCRIPTION_META_KEY === $meta_key && ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
300 $show = false;
301 }
302
303 if ( $show ) {
304 $metadata[] = array(
305 'id' => $meta['meta_id'],
306 'key' => $meta['meta_key'],
307 'value' => $this->safe_maybe_unserialize( $meta['meta_value'] ),
308 );
309 }
310 }
311
312 return $metadata;
313 }
314
315 /**
316 * Returns an object with a posts link meta details.
317 *
318 * @return object
319 */
320 public function get_meta() {
321 $meta = (object) array(
322 'links' => (object) array(
323 'self' => (string) $this->get_post_link(),
324 'help' => (string) $this->get_post_link( 'help' ),
325 'site' => (string) $this->get_site_link(),
326 'replies' => (string) $this->get_post_link( 'replies/' ),
327 'likes' => (string) $this->get_post_link( 'likes/' ),
328 ),
329 );
330
331 $amp_permalink = get_post_meta( $this->post->ID, '_jetpack_amp_permalink', true );
332
333 if ( ! empty( $amp_permalink ) ) {
334 $meta->links->amp = (string) $amp_permalink;
335 }
336
337 // add autosave link if a more recent autosave exists.
338 if ( 'edit' === $this->context ) {
339 $autosave = wp_get_post_autosave( $this->post->ID );
340 if ( $autosave && $autosave->post_modified > $this->post->post_modified ) {
341 $meta->links->autosave = (string) $this->get_post_link() . '/autosave';
342 }
343 }
344
345 return $meta;
346 }
347
348 /**
349 * Returns an array with the current user's publish, deletion and edit capabilities.
350 *
351 * @return array
352 */
353 public function get_current_user_capabilities() {
354 return array(
355 'publish_post' => current_user_can( 'publish_post', $this->post->ID ),
356 'delete_post' => current_user_can( 'delete_post', $this->post->ID ),
357 'edit_post' => current_user_can( 'edit_post', $this->post->ID ),
358 );
359 }
360
361 /**
362 * Returns an array with details of the posts revisions, or false if 'edit' isn't the current post request context.
363 *
364 * @return bool|array
365 */
366 public function get_revisions() {
367 if ( 'edit' !== $this->context ) {
368 return false;
369 }
370
371 $revisions = array();
372 $post_revisions = wp_get_post_revisions( $this->post->ID );
373
374 foreach ( $post_revisions as $_post ) {
375 $revisions[] = $_post->ID;
376 }
377
378 return $revisions;
379 }
380
381 /**
382 * Returns an object with extra post permalink suggestions.
383 *
384 * @return object
385 */
386 public function get_other_urls() {
387 $other_urls = array();
388
389 if ( 'publish' !== $this->post->post_status ) {
390 $other_urls = $this->get_permalink_suggestions( $this->post->post_title );
391 }
392
393 return (object) $other_urls;
394 }
395
396 /**
397 * Calls the WPCOM_JSON_API_Links get_site_link() function to generate a site link endpoint URL.
398 *
399 * @return string Endpoint URL including site information.
400 */
401 protected function get_site_link() {
402 return $this->links->get_site_link( $this->site->get_id() );
403 }
404
405 /**
406 * Calls the WPCOM_JSON_API_Links get_post_link() function to generate a posts endpoint URL.
407 *
408 * @param string $path Optional path to be appended to the URL.
409 * @return string Endpoint URL including post information.
410 */
411 protected function get_post_link( $path = null ) {
412 return $this->links->get_post_link( $this->site->get_id(), $this->post->ID, $path );
413 }
414
415 /**
416 * Returns an array of user and post specific social media post URLs.
417 *
418 * @return array
419 */
420 public function get_publicize_urls() {
421 $publicize_urls = array();
422 $publicize = get_post_meta( $this->post->ID, 'publicize_results', true );
423 if ( $publicize ) {
424 foreach ( $publicize as $service => $data ) {
425 switch ( $service ) {
426 // @todo explore removing once Twitter is removed from Publicize.
427 case 'twitter':
428 foreach ( $data as $datum ) {
429 $publicize_urls[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
430 }
431 break;
432 case 'fb':
433 foreach ( $data as $datum ) {
434 $publicize_urls[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
435 }
436 break;
437 }
438 }
439 }
440 return (array) $publicize_urls;
441 }
442
443 /**
444 * Returns a string with the page's custom template metadata.
445 *
446 * @return string
447 */
448 public function get_page_template() {
449 return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
450 }
451
452 /**
453 * Returns a string representing the source URL of a post's featured image (or an empty string otherwise).
454 *
455 * Note - this is overridden in jetpack-shadow
456 *
457 * @return string
458 */
459 public function get_featured_image() {
460 $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
461 if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
462 return (string) $image_attributes[0];
463 } else {
464 return '';
465 }
466 }
467
468 /**
469 * Returns an object representing a post's featured image thumbnail image.
470 *
471 * @return object
472 */
473 public function get_post_thumbnail() {
474 $thumb = null;
475
476 $thumb_id = get_post_thumbnail_id( $this->post->ID );
477
478 if ( ! empty( $thumb_id ) ) {
479 $attachment = get_post( $thumb_id );
480 if ( ! empty( $attachment ) ) {
481 $featured_image_object = $this->get_attachment( $attachment );
482 }
483 if ( ! empty( $featured_image_object ) ) {
484 $thumb = (object) $featured_image_object;
485 }
486 }
487
488 return $thumb;
489 }
490
491 /**
492 * Returns the format slug for a post (for example 'link', 'image' - the default being 'standard').
493 *
494 * @return string
495 */
496 public function get_format() {
497 $format = (string) get_post_format( $this->post->ID );
498 if ( ! $format ) {
499 $format = 'standard';
500 }
501
502 return $format;
503 }
504
505 /**
506 * Returns an object with the post's attachment details.
507 *
508 * @param WP_POST $attachment The post's attachment details in the form of a WP_POST object.
509 *
510 * @return object
511 */
512 private function get_attachment( $attachment ) {
513 $metadata = wp_get_attachment_metadata( $attachment->ID );
514
515 $result = array(
516 'ID' => (int) $attachment->ID,
517 'URL' => (string) wp_get_attachment_url( $attachment->ID ),
518 'guid' => (string) $attachment->guid,
519 'mime_type' => (string) $attachment->post_mime_type,
520 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
521 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
522 );
523
524 if ( isset( $metadata['duration'] ) ) {
525 $result['duration'] = (int) $metadata['duration'];
526 }
527
528 /** This filter is documented in class.jetpack-sync.php */
529 return (object) apply_filters( 'get_attachment', $result );
530 }
531
532 /**
533 * Returns an ISO 8601 formatted datetime string representing the date of post creation.
534 *
535 * @return string
536 */
537 public function get_date() {
538 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_date_gmt, $this->post->post_date );
539 }
540
541 /**
542 * Returns an ISO 8601 formatted datetime string representing the date the post was last modified.
543 *
544 * @return string
545 */
546 public function get_modified_date() {
547 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_modified_gmt, $this->post->post_modified );
548 }
549
550 /**
551 * Returns the post's title.
552 *
553 * @return string
554 */
555 public function get_title() {
556 if ( 'display' === $this->context ) {
557 return (string) get_the_title( $this->post->ID );
558 } else {
559 return (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
560 }
561 }
562
563 /**
564 * Returns the permalink for the post (or the post parent if the post type is a revision).
565 *
566 * @return string
567 */
568 public function get_url() {
569 if ( 'revision' === $this->post->post_type ) {
570 return (string) esc_url_raw( get_permalink( $this->post->post_parent ) );
571 } else {
572 return (string) esc_url_raw( get_permalink( $this->post->ID ) );
573 }
574 }
575
576 /**
577 * Returns the shortlink for the post.
578 *
579 * @return string
580 */
581 public function get_shortlink() {
582 return (string) esc_url_raw( wp_get_shortlink( $this->post->ID ) );
583 }
584
585 /**
586 * Returns the post content, or a string saying 'This post is password protected' if that is the case.
587 *
588 * @return string
589 */
590 public function get_content() {
591 if ( 'display' === $this->context ) {
592 // @todo: move this WPCOM-specific hack
593 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
594 $content = (string) $this->get_the_post_content_for_display();
595 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
596 return $content;
597 } else {
598 return (string) $this->post->post_content;
599 }
600 }
601
602 /**
603 * Returns the post excerpt, or a string saying 'This post is password protected' if that is the case.
604 *
605 * @return string
606 */
607 public function get_excerpt() {
608 if ( 'display' === $this->context ) {
609 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
610 ob_start();
611 the_excerpt();
612 $response = (string) ob_get_clean();
613 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
614 } else {
615 $response = htmlspecialchars_decode( (string) $this->post->post_excerpt, ENT_QUOTES );
616 }
617 return $response;
618 }
619
620 /**
621 * Returns the current post status (publish, future, draft, pending, private).
622 *
623 * @return string
624 */
625 public function get_status() {
626 return (string) get_post_status( $this->post->ID );
627 }
628
629 /**
630 * Returns true if the post is a sticky post, false otherwise.
631 *
632 * @return bool
633 */
634 public function is_sticky() {
635 return (bool) is_sticky( $this->post->ID );
636 }
637
638 /**
639 * Returns the post's slug.
640 *
641 * @return string
642 */
643 public function get_slug() {
644 return (string) $this->post->post_name;
645 }
646
647 /**
648 * Returns the post's password, if password protected.
649 *
650 * @return string
651 */
652 public function get_password() {
653 $password = (string) $this->post->post_password;
654 if ( 'edit' === $this->context ) {
655 $password = htmlspecialchars_decode( (string) $password, ENT_QUOTES );
656 }
657 return $password;
658 }
659
660 /**
661 * Returns an object representing a post's parent, and false if it doesn't have one.
662 *
663 * @return object|bool
664 */
665 public function get_parent() {
666 if ( $this->post->post_parent ) {
667 $parent = get_post( $this->post->post_parent );
668 if ( 'display' === $this->context ) {
669 $parent_title = (string) get_the_title( $parent->ID );
670 } else {
671 $parent_title = (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
672 }
673 return (object) array(
674 'ID' => (int) $parent->ID,
675 'type' => (string) $parent->post_type,
676 'link' => (string) $this->links->get_post_link( $this->site->get_id(), $parent->ID ),
677 'title' => $parent_title,
678 );
679 } else {
680 return false;
681 }
682 }
683
684 /**
685 * Returns a string saying 'This post is password protected' (to be later used within the_password_form filter).
686 *
687 * @return string
688 */
689 public function the_password_form() {
690 return __( 'This post is password protected.', 'jetpack' );
691 }
692
693 /**
694 * Returns an array with information related to the comment and ping status of a post.
695 *
696 * @return array
697 */
698 public function get_discussion() {
699 return array(
700 'comments_open' => (bool) comments_open( $this->post->ID ),
701 'comment_status' => (string) $this->post->comment_status,
702 'pings_open' => (bool) pings_open( $this->post->ID ),
703 'ping_status' => (string) $this->post->ping_status,
704 'comment_count' => (int) $this->post->comment_count,
705 );
706 }
707
708 /**
709 * Returns true if likes are enabled - either for the post, or site-wide.
710 *
711 * @return bool
712 */
713 public function is_likes_enabled() {
714 /** This filter is documented in modules/likes.php */
715 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
716 $post_likes_switched = get_post_meta( $this->post->ID, 'switch_like_status', true );
717
718 return $post_likes_switched || ( $sitewide_likes_enabled && '0' !== $post_likes_switched );
719 }
720
721 /**
722 * Returns true if sharing is enabled, false otherwise.
723 *
724 * @return bool
725 */
726 public function is_sharing_enabled() {
727 $show = true;
728 /** This filter is documented in modules/sharedaddy/sharing-service.php */
729 $show = apply_filters( 'sharing_show', $show, $this->post );
730
731 $switched_status = get_post_meta( $this->post->ID, 'sharing_disabled', false );
732
733 if ( ! empty( $switched_status ) ) {
734 $show = false;
735 }
736
737 return (bool) $show;
738 }
739
740 /**
741 * Returns the post content in the form of a string, ready for displaying.
742 *
743 * Note: No Blog ID parameter. No Post ID parameter. Depends on globals.
744 * Expects setup_postdata() to already have been run
745 *
746 * @return string
747 */
748 public function get_the_post_content_for_display() {
749 global $pages, $page;
750
751 $old_pages = $pages;
752 $old_page = $page;
753
754 $content = implode( "\n\n", $pages );
755 $content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
756 // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- Assignment to globals is intentional
757 $pages = array( $content );
758 $page = 1;
759
760 ob_start();
761 the_content();
762 $return = ob_get_clean();
763
764 $pages = $old_pages;
765 $page = $old_page;
766 // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
767 return $return;
768 }
769
770 /**
771 * Returns an object containing the post author's information (eg. ID, display name, email if the user has post editing capabilities).
772 *
773 * @return object
774 */
775 public function get_author() {
776 if ( 0 == $this->post->post_author ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- numbers could be numeric strings.
777 return null;
778 }
779
780 $show_email = 'edit' === $this->context && current_user_can( 'edit_post', $this->post->ID );
781
782 $user = get_user_by( 'id', $this->post->post_author );
783
784 if ( ! $user || is_wp_error( $user ) ) {
785 return null;
786 }
787
788 // @todo: factor this out
789 // phpcs:disable WordPress.NamingConventions.ValidVariableName
790 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
791 $active_blog = get_active_blog_for_user( $user->ID );
792 $site_id = $active_blog->blog_id;
793 $profile_URL = "https://gravatar.com/{$user->user_login}";
794 } else {
795 $profile_URL = 'https://gravatar.com/' . md5( strtolower( trim( $user->user_email ) ) );
796 $site_id = -1;
797 }
798
799 $author = array(
800 'ID' => (int) $user->ID,
801 'login' => (string) $user->user_login,
802 'email' => $show_email ? (string) $user->user_email : false,
803 'name' => (string) $user->display_name,
804 'first_name' => (string) $user->first_name,
805 'last_name' => (string) $user->last_name,
806 'nice_name' => (string) $user->user_nicename,
807 'URL' => (string) esc_url_raw( $user->user_url ),
808 'avatar_URL' => (string) esc_url_raw( $this->get_avatar_url( $user->user_email ) ),
809 'profile_URL' => (string) esc_url_raw( $profile_URL ),
810 );
811 // phpcs:enable WordPress.NamingConventions.ValidVariableName
812
813 if ( $site_id > -1 ) {
814 $author['site_ID'] = (int) $site_id;
815 }
816
817 return (object) $author;
818 }
819
820 /**
821 * Returns the avatar URL for a user, or an empty string if there isn't a valid avatar.
822 *
823 * @param string $email The user's email.
824 * @param int $avatar_size The size of the avatar in pixels.
825 *
826 * @todo Provide a non-WP.com option.
827 *
828 * @return string
829 */
830 protected function get_avatar_url( $email, $avatar_size = 96 ) {
831 $avatar_url = function_exists( 'wpcom_get_avatar_url' ) ? wpcom_get_avatar_url( $email, $avatar_size ) : '';
832 if ( ! $avatar_url || is_wp_error( $avatar_url ) ) {
833 return '';
834 }
835
836 return esc_url_raw( htmlspecialchars_decode( $avatar_url[0], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) );
837 }
838
839 /**
840 * Return extra post permalink suggestions in an array including the 'permalink_URL' and the 'suggested_slug'.
841 *
842 * @param string $title The current post title.
843 *
844 * @return array
845 */
846 public function get_permalink_suggestions( $title ) {
847 $suggestions = array();
848 list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $this->post->ID, $title );
849 return $suggestions;
850 }
851
852 /**
853 * Returns an object with formatted taxonomy information such as slug and meta information.
854 *
855 * Otherwise, returns an error if the edit or display permissions aren't correct.
856 *
857 * @param WP_Term $taxonomy The current taxonomy.
858 * @param string $taxonomy_type The current taxonomy type, for example 'category'.
859 * @param string $context The current context, for example 'edit' or 'display'.
860 *
861 * @return object
862 */
863 private function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
864 // Permissions.
865 switch ( $context ) {
866 case 'edit':
867 $tax = get_taxonomy( $taxonomy_type );
868 if ( ! current_user_can( $tax->cap->edit_terms ) ) {
869 return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
870 }
871 break;
872 case 'display':
873 if ( ( new Status() )->is_private_site() && ! current_user_can( 'read' ) ) {
874 return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
875 }
876 break;
877 default:
878 return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
879 }
880
881 $response = array();
882 $response['ID'] = (int) $taxonomy->term_id;
883 $response['name'] = (string) $taxonomy->name;
884 $response['slug'] = (string) $taxonomy->slug;
885 $response['description'] = (string) $taxonomy->description;
886 $response['post_count'] = (int) $taxonomy->count;
887
888 if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
889 $response['parent'] = (int) $taxonomy->parent;
890 }
891
892 $response['meta'] = (object) array(
893 'links' => (object) array(
894 'self' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type ),
895 'help' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type, 'help' ),
896 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
897 ),
898 );
899
900 return (object) $response;
901 }
902
903 /**
904 * Builds and returns the media item's details.
905 *
906 * @param int $media_id The media item ID.
907 * @todo: factor this out into site.
908 *
909 * @return object
910 */
911 private function get_media_item_v1_1( $media_id ) {
912 $media_item = get_post( $media_id );
913
914 if ( ! $media_item || is_wp_error( $media_item ) ) {
915 return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
916 }
917
918 $file = basename( wp_get_attachment_url( $media_item->ID ) );
919 $file_info = pathinfo( $file );
920 $ext = isset( $file_info['extension'] ) ? $file_info['extension'] : '';
921
922 $response = array(
923 'ID' => $media_item->ID,
924 'URL' => wp_get_attachment_url( $media_item->ID ),
925 'guid' => $media_item->guid,
926 'date' => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_date_gmt, $media_item->post_date ),
927 'post_ID' => $media_item->post_parent,
928 'author_ID' => (int) $media_item->post_author,
929 'file' => $file,
930 'mime_type' => $media_item->post_mime_type,
931 'extension' => $ext,
932 'title' => $media_item->post_title,
933 'caption' => $media_item->post_excerpt,
934 'description' => $media_item->post_content,
935 'alt' => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
936 'thumbnails' => array(),
937 );
938
939 if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif', 'webp' ), true ) ) {
940 $metadata = wp_get_attachment_metadata( $media_item->ID );
941 if ( isset( $metadata['height'] ) && isset( $metadata['width'] ) ) {
942 $response['height'] = $metadata['height'];
943 $response['width'] = $metadata['width'];
944 }
945
946 if ( isset( $metadata['sizes'] ) ) {
947 /**
948 * Filter the thumbnail sizes available for each attachment ID.
949 *
950 * @module json-api
951 *
952 * @since 3.9.0
953 *
954 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
955 * @param int $media_id The media item ID.
956 */
957 $sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_id );
958 if ( is_array( $sizes ) ) {
959 foreach ( $sizes as $size => $size_details ) {
960 $response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
961 }
962 }
963 }
964
965 if ( isset( $metadata['image_meta'] ) ) {
966 $response['exif'] = $metadata['image_meta'];
967 }
968 }
969
970 if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ), true ) ) {
971 $metadata = wp_get_attachment_metadata( $media_item->ID );
972 if ( isset( $metadata['length'] ) ) {
973 $response['length'] = $metadata['length'];
974 }
975
976 $response['exif'] = $metadata;
977 }
978
979 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ), true ) ) {
980 $metadata = wp_get_attachment_metadata( $media_item->ID );
981 if ( isset( $metadata['height'] ) && isset( $metadata['width'] ) ) {
982 $response['height'] = $metadata['height'];
983 $response['width'] = $metadata['width'];
984 }
985
986 if ( isset( $metadata['length'] ) ) {
987 $response['length'] = $metadata['length'];
988 }
989
990 // add VideoPress info.
991 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
992 $info = video_get_info_by_blogpostid( $this->site->get_id(), $media_id );
993
994 // Thumbnails.
995 if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
996 $response['thumbnails'] = array(
997 'fmt_hd' => '',
998 'fmt_dvd' => '',
999 'fmt_std' => '',
1000 );
1001 foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
1002 if ( video_format_done( $info, $size ) ) {
1003 $response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
1004 } else {
1005 unset( $response['thumbnails'][ $size ] );
1006 }
1007 }
1008 }
1009
1010 $response['videopress_guid'] = $info->guid;
1011 $response['videopress_processing_done'] = true;
1012 if ( '0000-00-00 00:00:00' === $info->finish_date_gmt ) {
1013 $response['videopress_processing_done'] = false;
1014 }
1015 }
1016 }
1017
1018 $response['thumbnails'] = (object) $response['thumbnails'];
1019
1020 $response['meta'] = (object) array(
1021 'links' => (object) array(
1022 'self' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id ),
1023 'help' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id, 'help' ),
1024 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
1025 ),
1026 );
1027
1028 // add VideoPress link to the meta.
1029 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ), true ) ) {
1030 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
1031 $response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
1032 }
1033 }
1034
1035 if ( $media_item->post_parent > 0 ) {
1036 $response['meta']->links->parent = (string) $this->links->get_post_link( $this->site->get_id(), $media_item->post_parent );
1037 }
1038
1039 return (object) $response;
1040 }
1041
1042 /**
1043 * Temporary wrapper around maybe_unserialize() to catch exceptions thrown by unserialize().
1044 *
1045 * Can be removed after https://core.trac.wordpress.org/ticket/45895 lands in Core.
1046 *
1047 * @param string $original Serialized string.
1048 *
1049 * @return string Unserialized string or original string if an exception was raised.
1050 **/
1051 protected function safe_maybe_unserialize( $original ) {
1052 try {
1053 return maybe_unserialize( $original );
1054 } catch ( Exception $e ) {
1055 return $original;
1056 }
1057 }
1058 }
1059