PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.8
Jetpack – WP Security, Backup, Speed, & Growth v14.8
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
1,067 lines 31.2 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 post revision ids, 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 $args = array(
372 'posts_per_page' => -1,
373 'post_type' => 'revision',
374 'post_status' => 'any',
375 'fields' => 'ids', // Fetch only the IDs.
376 'post_parent' => $this->post->ID,
377 );
378
379 $revision_query = new WP_Query( $args );
380 return $revision_query->posts; // This returns an array of revision IDs.
381 }
382
383 /**
384 * Returns an object with extra post permalink suggestions.
385 *
386 * @return object
387 */
388 public function get_other_urls() {
389 $other_urls = array();
390
391 if ( 'publish' !== $this->post->post_status ) {
392 $other_urls = $this->get_permalink_suggestions( $this->post->post_title );
393 }
394
395 return (object) $other_urls;
396 }
397
398 /**
399 * Calls the WPCOM_JSON_API_Links get_site_link() function to generate a site link endpoint URL.
400 *
401 * @return string Endpoint URL including site information.
402 */
403 protected function get_site_link() {
404 return $this->links->get_site_link( $this->site->get_id() );
405 }
406
407 /**
408 * Calls the WPCOM_JSON_API_Links get_post_link() function to generate a posts endpoint URL.
409 *
410 * @param string $path Optional path to be appended to the URL.
411 * @return string Endpoint URL including post information.
412 */
413 protected function get_post_link( $path = null ) {
414 return $this->links->get_post_link( $this->site->get_id(), $this->post->ID, $path );
415 }
416
417 /**
418 * Returns an array of user and post specific social media post URLs.
419 *
420 * @return array
421 */
422 public function get_publicize_urls() {
423 $publicize_urls = array();
424 $publicize = get_post_meta( $this->post->ID, 'publicize_results', true );
425 if ( $publicize ) {
426 foreach ( $publicize as $service => $data ) {
427 switch ( $service ) {
428 // @todo explore removing once Twitter is removed from Publicize.
429 case 'twitter':
430 foreach ( $data as $datum ) {
431 $publicize_urls[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
432 }
433 break;
434 case 'fb':
435 foreach ( $data as $datum ) {
436 $publicize_urls[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
437 }
438 break;
439 }
440 }
441 }
442 return (array) $publicize_urls;
443 }
444
445 /**
446 * Returns a string with the page's custom template metadata.
447 *
448 * @return string
449 */
450 public function get_page_template() {
451 return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
452 }
453
454 /**
455 * Returns a string representing the source URL of a post's featured image (or an empty string otherwise).
456 *
457 * Note - this is overridden in jetpack-shadow
458 *
459 * @return string
460 */
461 public function get_featured_image() {
462 $image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
463 if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
464 return (string) $image_attributes[0];
465 } else {
466 return '';
467 }
468 }
469
470 /**
471 * Returns an object representing a post's featured image thumbnail image.
472 *
473 * @return object
474 */
475 public function get_post_thumbnail() {
476 $thumb = null;
477
478 $thumb_id = get_post_thumbnail_id( $this->post->ID );
479
480 if ( ! empty( $thumb_id ) ) {
481 $attachment = get_post( $thumb_id );
482 if ( ! empty( $attachment ) ) {
483 $featured_image_object = $this->get_attachment( $attachment );
484 }
485 if ( ! empty( $featured_image_object ) ) {
486 $thumb = (object) $featured_image_object;
487 }
488 }
489
490 return $thumb;
491 }
492
493 /**
494 * Returns the format slug for a post (for example 'link', 'image' - the default being 'standard').
495 *
496 * @return string
497 */
498 public function get_format() {
499 $format = (string) get_post_format( $this->post->ID );
500 if ( ! $format ) {
501 $format = 'standard';
502 }
503
504 return $format;
505 }
506
507 /**
508 * Returns an object with the post's attachment details.
509 *
510 * @param WP_POST $attachment The post's attachment details in the form of a WP_POST object.
511 *
512 * @return object
513 */
514 private function get_attachment( $attachment ) {
515 $metadata = wp_get_attachment_metadata( $attachment->ID );
516
517 $result = array(
518 'ID' => (int) $attachment->ID,
519 'URL' => (string) wp_get_attachment_url( $attachment->ID ),
520 'guid' => (string) $attachment->guid,
521 'mime_type' => (string) $attachment->post_mime_type,
522 'width' => (int) isset( $metadata['width'] ) ? $metadata['width'] : 0,
523 'height' => (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
524 );
525
526 if ( isset( $metadata['duration'] ) ) {
527 $result['duration'] = (int) $metadata['duration'];
528 }
529
530 /** This filter is documented in class.jetpack-sync.php */
531 return (object) apply_filters( 'get_attachment', $result );
532 }
533
534 /**
535 * Returns an ISO 8601 formatted datetime string representing the date of post creation.
536 *
537 * @return string
538 */
539 public function get_date() {
540 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_date_gmt, $this->post->post_date );
541 }
542
543 /**
544 * Returns an ISO 8601 formatted datetime string representing the date the post was last modified.
545 *
546 * @return string
547 */
548 public function get_modified_date() {
549 return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_modified_gmt, $this->post->post_modified );
550 }
551
552 /**
553 * Returns the post's title.
554 *
555 * @return string
556 */
557 public function get_title() {
558 if ( 'display' === $this->context ) {
559 return (string) get_the_title( $this->post->ID );
560 } else {
561 return (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
562 }
563 }
564
565 /**
566 * Returns the permalink for the post (or the post parent if the post type is a revision).
567 *
568 * @return string
569 */
570 public function get_url() {
571 if ( 'revision' === $this->post->post_type ) {
572 return (string) esc_url_raw( get_permalink( $this->post->post_parent ) );
573 } else {
574 return (string) esc_url_raw( get_permalink( $this->post->ID ) );
575 }
576 }
577
578 /**
579 * Returns the shortlink for the post.
580 *
581 * @return string
582 */
583 public function get_shortlink() {
584 return (string) esc_url_raw( wp_get_shortlink( $this->post->ID ) );
585 }
586
587 /**
588 * Returns the post content, or a string saying 'This post is password protected' if that is the case.
589 *
590 * @return string
591 */
592 public function get_content() {
593 if ( 'display' === $this->context ) {
594 // @todo: move this WPCOM-specific hack
595 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
596 $content = (string) $this->get_the_post_content_for_display();
597 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
598 return $content;
599 } else {
600 return (string) $this->post->post_content;
601 }
602 }
603
604 /**
605 * Returns the post excerpt, or a string saying 'This post is password protected' if that is the case.
606 *
607 * @return string
608 */
609 public function get_excerpt() {
610 if ( 'display' === $this->context ) {
611 add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
612 ob_start();
613 the_excerpt();
614 $response = (string) ob_get_clean();
615 remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
616 } else {
617 $response = htmlspecialchars_decode( (string) $this->post->post_excerpt, ENT_QUOTES );
618 }
619 return $response;
620 }
621
622 /**
623 * Returns the current post status (publish, future, draft, pending, private).
624 *
625 * @return string
626 */
627 public function get_status() {
628 return (string) get_post_status( $this->post->ID );
629 }
630
631 /**
632 * Returns true if the post is a sticky post, false otherwise.
633 *
634 * @return bool
635 */
636 public function is_sticky() {
637 return (bool) is_sticky( $this->post->ID );
638 }
639
640 /**
641 * Returns the post's slug.
642 *
643 * @return string
644 */
645 public function get_slug() {
646 return (string) $this->post->post_name;
647 }
648
649 /**
650 * Returns the post's password, if password protected.
651 *
652 * @return string
653 */
654 public function get_password() {
655 $password = (string) $this->post->post_password;
656 if ( 'edit' === $this->context ) {
657 $password = htmlspecialchars_decode( (string) $password, ENT_QUOTES );
658 }
659 return $password;
660 }
661
662 /**
663 * Returns an object representing a post's parent, and false if it doesn't have one.
664 *
665 * @return object|bool
666 */
667 public function get_parent() {
668 if ( $this->post->post_parent ) {
669 $parent = get_post( $this->post->post_parent );
670 if ( 'display' === $this->context ) {
671 $parent_title = (string) get_the_title( $parent->ID );
672 } else {
673 $parent_title = (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
674 }
675 return (object) array(
676 'ID' => (int) $parent->ID,
677 'type' => (string) $parent->post_type,
678 'link' => (string) $this->links->get_post_link( $this->site->get_id(), $parent->ID ),
679 'title' => $parent_title,
680 );
681 } else {
682 return false;
683 }
684 }
685
686 /**
687 * Returns a string saying 'This post is password protected' (to be later used within the_password_form filter).
688 *
689 * @return string
690 */
691 public function the_password_form() {
692 return __( 'This post is password protected.', 'jetpack' );
693 }
694
695 /**
696 * Returns an array with information related to the comment and ping status of a post.
697 *
698 * @return array
699 */
700 public function get_discussion() {
701 return array(
702 'comments_open' => (bool) comments_open( $this->post->ID ),
703 'comment_status' => (string) $this->post->comment_status,
704 'pings_open' => (bool) pings_open( $this->post->ID ),
705 'ping_status' => (string) $this->post->ping_status,
706 'comment_count' => (int) $this->post->comment_count,
707 );
708 }
709
710 /**
711 * Returns true if likes are enabled - either for the post, or site-wide.
712 *
713 * @return bool
714 */
715 public function is_likes_enabled() {
716 /** This filter is documented in modules/likes.php */
717 $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
718 $post_likes_switched = get_post_meta( $this->post->ID, 'switch_like_status', true );
719
720 return $post_likes_switched || ( $sitewide_likes_enabled && '0' !== $post_likes_switched );
721 }
722
723 /**
724 * Returns true if sharing is enabled, false otherwise.
725 *
726 * @return bool
727 */
728 public function is_sharing_enabled() {
729 $show = true;
730 /** This filter is documented in modules/sharedaddy/sharing-service.php */
731 $show = apply_filters( 'sharing_show', $show, $this->post );
732
733 $switched_status = get_post_meta( $this->post->ID, 'sharing_disabled', false );
734
735 if ( ! empty( $switched_status ) ) {
736 $show = false;
737 }
738
739 return (bool) $show;
740 }
741
742 /**
743 * Returns the post content in the form of a string, ready for displaying.
744 *
745 * Note: No Blog ID parameter. No Post ID parameter. Depends on globals.
746 * Expects setup_postdata() to already have been run
747 *
748 * @return string
749 */
750 public function get_the_post_content_for_display() {
751 global $pages, $page;
752
753 $old_pages = $pages;
754 $old_page = $page;
755
756 $content = implode( "\n\n", $pages );
757 $content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
758 // phpcs:disable WordPress.WP.GlobalVariablesOverride.Prohibited -- Assignment to globals is intentional
759 $pages = array( $content );
760 $page = 1;
761
762 ob_start();
763 the_content();
764 $return = ob_get_clean();
765
766 $pages = $old_pages;
767 $page = $old_page;
768 // phpcs:enable WordPress.WP.GlobalVariablesOverride.Prohibited
769 return $return;
770 }
771
772 /**
773 * Returns an object containing the post author's information (eg. ID, display name, email if the user has post editing capabilities).
774 *
775 * @return object
776 */
777 public function get_author() {
778 if ( 0 == $this->post->post_author ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- numbers could be numeric strings.
779 return null;
780 }
781
782 $show_email = 'edit' === $this->context && current_user_can( 'edit_post', $this->post->ID );
783
784 $user = get_user_by( 'id', $this->post->post_author );
785
786 if ( ! $user || is_wp_error( $user ) ) {
787 return null;
788 }
789
790 // @todo: factor this out
791 // phpcs:disable WordPress.NamingConventions.ValidVariableName
792 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
793 $active_blog = get_active_blog_for_user( $user->ID );
794 $site_id = $active_blog->blog_id ?? -1;
795 $profile_URL = "https://gravatar.com/{$user->user_login}";
796 } else {
797 $profile_URL = 'https://gravatar.com/' . md5( strtolower( trim( $user->user_email ) ) );
798 $site_id = -1;
799 }
800
801 $author = array(
802 'ID' => (int) $user->ID,
803 'login' => (string) $user->user_login,
804 'email' => $show_email ? (string) $user->user_email : false,
805 'name' => (string) $user->display_name,
806 'first_name' => (string) $user->first_name,
807 'last_name' => (string) $user->last_name,
808 'nice_name' => (string) $user->user_nicename,
809 'URL' => (string) esc_url_raw( $user->user_url ),
810 'avatar_URL' => (string) esc_url_raw( $this->get_avatar_url( $user->user_email ) ),
811 'profile_URL' => (string) esc_url_raw( $profile_URL ),
812 );
813 // phpcs:enable WordPress.NamingConventions.ValidVariableName
814
815 if ( $site_id > -1 ) {
816 $author['site_ID'] = (int) $site_id;
817 }
818
819 return (object) $author;
820 }
821
822 /**
823 * Returns the avatar URL for a user, or an empty string if there isn't a valid avatar.
824 *
825 * @param string $email The user's email.
826 * @param int $avatar_size The size of the avatar in pixels.
827 *
828 * @todo Provide a non-WP.com option.
829 *
830 * @return string
831 */
832 protected function get_avatar_url( $email, $avatar_size = 96 ) {
833 $avatar_url = function_exists( 'wpcom_get_avatar_url' ) ? wpcom_get_avatar_url( $email, $avatar_size ) : '';
834 if ( ! $avatar_url || is_wp_error( $avatar_url ) ) {
835 return '';
836 }
837
838 return esc_url_raw( htmlspecialchars_decode( $avatar_url[0], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ) );
839 }
840
841 /**
842 * Return extra post permalink suggestions in an array including the 'permalink_URL' and the 'suggested_slug'.
843 *
844 * @param string $title The current post title.
845 *
846 * @return array
847 */
848 public function get_permalink_suggestions( $title ) {
849 $suggestions = array();
850 list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $this->post->ID, $title );
851 return $suggestions;
852 }
853
854 /**
855 * Returns an object with formatted taxonomy information such as slug and meta information.
856 *
857 * Otherwise, returns an error if the edit or display permissions aren't correct.
858 *
859 * @param WP_Term $taxonomy The current taxonomy.
860 * @param string $taxonomy_type The current taxonomy type, for example 'category'.
861 * @param string $context The current context, for example 'edit' or 'display'.
862 *
863 * @return object
864 */
865 private function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
866 // Permissions.
867 switch ( $context ) {
868 case 'edit':
869 $tax = get_taxonomy( $taxonomy_type );
870 if ( ! current_user_can( $tax->cap->edit_terms ) ) {
871 return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
872 }
873 break;
874 case 'display':
875 if ( ( new Status() )->is_private_site() && ! current_user_can( 'read' ) ) {
876 return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
877 }
878 break;
879 default:
880 return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
881 }
882
883 $response = array();
884 $response['ID'] = (int) $taxonomy->term_id;
885 $response['name'] = (string) $taxonomy->name;
886 $response['slug'] = (string) $taxonomy->slug;
887 $response['description'] = (string) $taxonomy->description;
888 $response['post_count'] = (int) $taxonomy->count;
889
890 if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
891 $response['parent'] = (int) $taxonomy->parent;
892 }
893
894 $response['meta'] = (object) array(
895 'links' => (object) array(
896 'self' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type ),
897 'help' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type, 'help' ),
898 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
899 ),
900 );
901
902 return (object) $response;
903 }
904
905 /**
906 * Builds and returns the media item's details.
907 *
908 * @param int $media_id The media item ID.
909 * @todo: factor this out into site.
910 *
911 * @return object
912 */
913 private function get_media_item_v1_1( $media_id ) {
914 $media_item = get_post( $media_id );
915
916 if ( ! $media_item || is_wp_error( $media_item ) ) {
917 return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
918 }
919
920 $file = basename( wp_get_attachment_url( $media_item->ID ) );
921 $file_info = pathinfo( $file );
922 $ext = isset( $file_info['extension'] ) ? $file_info['extension'] : '';
923
924 $response = array(
925 'ID' => $media_item->ID,
926 'URL' => wp_get_attachment_url( $media_item->ID ),
927 'guid' => $media_item->guid,
928 'date' => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_date_gmt, $media_item->post_date ),
929 'post_ID' => $media_item->post_parent,
930 'author_ID' => (int) $media_item->post_author,
931 'file' => $file,
932 'mime_type' => $media_item->post_mime_type,
933 'extension' => $ext,
934 'title' => $media_item->post_title,
935 'caption' => $media_item->post_excerpt,
936 'description' => $media_item->post_content,
937 'alt' => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
938 'thumbnails' => array(),
939 );
940
941 if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif', 'webp' ), true ) ) {
942 $metadata = wp_get_attachment_metadata( $media_item->ID );
943 if ( isset( $metadata['height'] ) && isset( $metadata['width'] ) ) {
944 $response['height'] = $metadata['height'];
945 $response['width'] = $metadata['width'];
946 }
947
948 if ( isset( $metadata['sizes'] ) ) {
949 /**
950 * Filter the thumbnail sizes available for each attachment ID.
951 *
952 * @module json-api
953 *
954 * @since 3.9.0
955 *
956 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
957 * @param int $media_id The media item ID.
958 */
959 $sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_id );
960 if ( is_array( $sizes ) ) {
961 foreach ( $sizes as $size => $size_details ) {
962 $response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
963 }
964 }
965 }
966
967 if ( isset( $metadata['image_meta'] ) ) {
968 $response['exif'] = $metadata['image_meta'];
969 }
970 }
971
972 if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ), true ) ) {
973 $metadata = wp_get_attachment_metadata( $media_item->ID );
974 if ( isset( $metadata['length'] ) ) {
975 $response['length'] = $metadata['length'];
976 }
977
978 $response['exif'] = $metadata;
979 }
980
981 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ), true ) ) {
982 $metadata = wp_get_attachment_metadata( $media_item->ID );
983 if ( isset( $metadata['height'] ) && isset( $metadata['width'] ) ) {
984 $response['height'] = $metadata['height'];
985 $response['width'] = $metadata['width'];
986 }
987
988 if ( isset( $metadata['length'] ) ) {
989 $response['length'] = $metadata['length'];
990 }
991
992 if ( empty( $response['length'] ) && isset( $metadata['duration'] ) ) {
993 $response['length'] = (int) $metadata['duration'];
994 }
995
996 if ( empty( $response['length'] ) && isset( $metadata['videopress']['duration'] ) ) {
997 $response['length'] = ceil( $metadata['videopress']['duration'] / 1000 );
998 }
999
1000 // add VideoPress info.
1001 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
1002 $info = video_get_info_by_blogpostid( $this->site->get_id(), $media_id );
1003
1004 // Thumbnails.
1005 if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
1006 $response['thumbnails'] = array(
1007 'fmt_hd' => '',
1008 'fmt_dvd' => '',
1009 'fmt_std' => '',
1010 );
1011 foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
1012 if ( video_format_done( $info, $size ) ) {
1013 $response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
1014 } else {
1015 unset( $response['thumbnails'][ $size ] );
1016 }
1017 }
1018 }
1019
1020 $response['videopress_guid'] = $info->guid ?? null;
1021 $response['videopress_processing_done'] = true;
1022 $response['videopress_processing_done'] = isset( $info->finish_date_gmt ) && '0000-00-00 00:00:00' !== $info->finish_date_gmt ? $info->finish_date_gmt : false;
1023 }
1024 }
1025
1026 $response['thumbnails'] = (object) $response['thumbnails'];
1027
1028 $response['meta'] = (object) array(
1029 'links' => (object) array(
1030 'self' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id ),
1031 'help' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id, 'help' ),
1032 'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
1033 ),
1034 );
1035
1036 // add VideoPress link to the meta.
1037 if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ), true ) ) {
1038 if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
1039 $response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
1040 }
1041 }
1042
1043 if ( $media_item->post_parent > 0 ) {
1044 $response['meta']->links->parent = (string) $this->links->get_post_link( $this->site->get_id(), $media_item->post_parent );
1045 }
1046
1047 return (object) $response;
1048 }
1049
1050 /**
1051 * Temporary wrapper around maybe_unserialize() to catch exceptions thrown by unserialize().
1052 *
1053 * Can be removed after https://core.trac.wordpress.org/ticket/45895 lands in Core.
1054 *
1055 * @param string $original Serialized string.
1056 *
1057 * @return string Unserialized string or original string if an exception was raised.
1058 **/
1059 protected function safe_maybe_unserialize( $original ) {
1060 try {
1061 return maybe_unserialize( $original );
1062 } catch ( Exception $e ) {
1063 return $original;
1064 }
1065 }
1066 }
1067