$post->ID, 'type' => $post->post_type, 'status' => $post->post_status, 'title' => $post->post_title, 'slug' => $post->post_name, 'link' => (string) get_permalink( $post ), 'edit_link' => (string) get_edit_post_link( $post->ID, 'raw' ), 'date' => self::date( $post->post_date ), 'modified' => self::date( $post->post_modified ), 'author' => (int) $post->post_author, 'parent' => $post->post_parent, 'menu_order' => $post->menu_order, 'excerpt' => $readable ? $post->post_excerpt : '', 'featured_image' => (int) get_post_thumbnail_id( $post ), 'protected' => '' !== $post->post_password, ]; if ( $full ) { $data['content'] = $readable ? $post->post_content : ''; $data['comment_status'] = $post->comment_status; $data['sticky'] = 'post' === $post->post_type && is_sticky( $post->ID ); $data['template'] = (string) get_page_template_slug( $post ); $data['featured_image_url'] = $data['featured_image'] ? (string) wp_get_attachment_image_url( $data['featured_image'], 'full' ) : ''; $data['terms'] = self::post_terms( $post ); } return $data; } /** * Formats the terms of a post, keyed by taxonomy. * * @param WP_Post $post Post. * * @return array */ public static function post_terms( WP_Post $post ): array { $terms = []; foreach ( get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy ) { if ( empty( $taxonomy->show_in_rest ) ) { continue; } $post_terms = get_the_terms( $post, $taxonomy->name ); $terms[ $taxonomy->name ] = is_array( $post_terms ) ? array_map( function ( $term ) { return [ 'id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, ]; }, $post_terms ) : []; } return $terms; } /** * Formats a term. * * @param WP_Term $term Term. * * @return array */ public static function term( WP_Term $term ): array { $link = get_term_link( $term ); return [ 'id' => $term->term_id, 'taxonomy' => $term->taxonomy, 'name' => $term->name, 'slug' => $term->slug, 'description' => $term->description, 'parent' => $term->parent, 'count' => $term->count, 'link' => is_wp_error( $link ) ? '' : $link, ]; } /** * Formats a media library item. * * @param WP_Post $attachment Attachment. * * @return array */ public static function attachment( WP_Post $attachment ): array { $meta = wp_get_attachment_metadata( $attachment->ID ); return [ 'id' => $attachment->ID, 'title' => $attachment->post_title, 'url' => (string) wp_get_attachment_url( $attachment->ID ), 'mime_type' => $attachment->post_mime_type, 'alt' => (string) get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), 'caption' => $attachment->post_excerpt, 'description' => $attachment->post_content, 'width' => isset( $meta['width'] ) ? (int) $meta['width'] : 0, 'height' => isset( $meta['height'] ) ? (int) $meta['height'] : 0, 'date' => self::date( $attachment->post_date ), 'parent' => $attachment->post_parent, 'edit_link' => (string) get_edit_post_link( $attachment->ID, 'raw' ), ]; } /** * Formats a comment. * * @param WP_Comment $comment Comment. * * @return array */ public static function comment( WP_Comment $comment ): array { return [ 'id' => (int) $comment->comment_ID, 'post_id' => (int) $comment->comment_post_ID, 'post_title' => get_the_title( (int) $comment->comment_post_ID ), 'parent' => (int) $comment->comment_parent, 'user_id' => (int) $comment->user_id, 'author_name' => $comment->comment_author, 'author_email' => $comment->comment_author_email, 'author_url' => $comment->comment_author_url, 'date' => self::date( $comment->comment_date ), 'content' => $comment->comment_content, 'status' => (string) wp_get_comment_status( $comment ), 'type' => $comment->comment_type, 'link' => (string) get_comment_link( $comment ), ]; } /** * Formats a user. Never includes the password. * * @param WP_User $user User. * * @return array */ public static function user( WP_User $user ): array { return [ 'id' => $user->ID, 'username' => $user->user_login, 'display_name' => $user->display_name, 'email' => $user->user_email, 'url' => $user->user_url, 'roles' => array_values( $user->roles ), 'registered' => self::date( $user->user_registered ), ]; } /** * Formats a user-created pattern (wp_block post). * * @param WP_Post $pattern Pattern post. * @param bool $with_content Whether to include the block markup. * * @return array */ public static function pattern( WP_Post $pattern, bool $with_content = false ): array { $categories = get_the_terms( $pattern, 'wp_pattern_category' ); $data = [ 'source' => 'user', 'id' => $pattern->ID, 'name' => 'core/block/' . $pattern->ID, 'title' => $pattern->post_title, 'synced' => 'unsynced' !== get_post_meta( $pattern->ID, 'wp_pattern_sync_status', true ), 'categories' => is_array( $categories ) ? wp_list_pluck( $categories, 'name' ) : [], 'status' => $pattern->post_status, ]; if ( $with_content ) { $data['content'] = $pattern->post_content; } return $data; } /** * Formats a classic menu item. * * @param object $item Menu item, set up with wp_setup_nav_menu_item(). * * @return array */ public static function menu_item( $item ): array { return [ 'id' => (int) $item->ID, 'title' => (string) $item->title, 'url' => (string) $item->url, 'type' => (string) $item->type, 'object' => (string) $item->object, 'object_id' => (int) $item->object_id, 'parent' => (int) $item->menu_item_parent, 'order' => (int) $item->menu_order, 'target' => (string) $item->target, 'classes' => array_values( array_filter( (array) $item->classes ) ), ]; } /** * Collapses HTML into plain text. * * @param string $html HTML. * * @return string */ public static function text( string $html ): string { return trim( (string) preg_replace( '/\s+/', ' ', wp_strip_all_tags( $html ) ) ); } }