*/ public static function to_array( \WP_Post $post, bool $include_content = true ): array { $data = array( 'id' => (int) $post->ID, 'type' => $post->post_type, 'status' => $post->post_status, 'title' => self::title( $post ), 'slug' => $post->post_name, 'excerpt' => has_excerpt( $post ) ? get_the_excerpt( $post ) : '', 'author_id' => (int) $post->post_author, 'date_gmt' => self::date_gmt( $post ), 'modified_gmt' => self::modified_gmt( $post ), // Additional CSS has no page of its own; its permalink is a 404. 'link' => Custom_Css::TYPE === $post->post_type ? null : get_permalink( $post ), 'edit_link' => get_edit_post_link( $post->ID, 'raw' ), ); // The "Protected: " prefix used to say this; the title no longer does. if ( '' !== (string) $post->post_password ) { $data['password_protected'] = true; } if ( $include_content ) { $data['content'] = $post->post_content; } return $data; } /** * A post's title as a person reads it: "Tom & Jerry" for a title stored * as "Tom & Jerry", and without the "Private: " or "Protected: " * that get_the_title() puts in front, which an assistant writing the * title back would save into it. * * Decoded only where writing the decoded title back shows the same * thing. A title stored as the literal text "<b>" would come back * as a real tag, and saved by an administrator it would turn the * title bold; such a title is returned as stored, the REST API's * title.raw. * * @param \WP_Post|int $post Post or post id. */ public static function title( $post ): string { $post = get_post( $post ); if ( ! $post instanceof \WP_Post ) { return ''; } return self::decoded( (string) $post->post_title ); } /** * Any short text WordPress stores with entities, as a person reads it, * under the same rule as title(): a user's display name ("O'Brien & * Co"), the site's tagline ("'"), a media title. * * @param string $stored Text as stored. */ public static function decoded( string $stored ): string { $decoded = html_entity_decode( $stored, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); if ( $decoded === $stored ) { return $stored; } // Markup the stored text only spelled out ("<b>"), or an // entity spelled out ("&lt;"): decoded, either reads // differently once saved again. if ( 1 === preg_match( '/<[a-zA-Z\/!?]/', $decoded ) || html_entity_decode( $decoded, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) !== $decoded ) { return $stored; } return $decoded; } /** * A note when WordPress did not keep the slug asked for: another post * had it (or it is a reserved word), so it saved "name-2" without a * word. A draft keeps the slug until it is published, so for one the * note says what will happen then. * * @param \WP_Post $post The post as saved. * @param string $asked Slug asked for, as sanitize_title() made it. */ public static function slug_note( \WP_Post $post, string $asked ): string { if ( '' === $asked ) { return ''; } $stored = (string) $post->post_name; if ( '' !== $stored && $stored !== $asked ) { return sprintf( /* translators: 1: slug asked for, 2: slug WordPress saved. */ __( 'The slug "%1$s" was already taken (or is reserved), so WordPress saved "%2$s".', 'gripmcp' ), urldecode( $asked ), urldecode( $stored ) ); } if ( in_array( (string) $post->post_status, array( 'draft', 'pending' ), true ) ) { $unique = wp_unique_post_slug( $asked, (int) $post->ID, 'publish', (string) $post->post_type, (int) $post->post_parent ); if ( $unique !== $asked ) { return sprintf( /* translators: 1: slug asked for, 2: slug WordPress will use. */ __( 'The slug "%1$s" is already taken (or is reserved), so WordPress will change it to "%2$s" when this is published.', 'gripmcp' ), urldecode( $asked ), urldecode( $unique ) ); } } return ''; } /** * When a post was last changed, in GMT. * * WordPress leaves the GMT column at zero on a draft until it is first * updated, and on revisions taken of one; the local column is always * set. Its REST API converts the local time in that case, and so does * this. * * @param \WP_Post $post Post. */ public static function modified_gmt( \WP_Post $post ): string { return self::gmt( (string) $post->post_modified_gmt, (string) $post->post_modified ); } /** * A post's date in GMT, with the same fallback for drafts. * * @param \WP_Post $post Post. */ public static function date_gmt( \WP_Post $post ): string { return self::gmt( (string) $post->post_date_gmt, (string) $post->post_date ); } /** * A GMT column's value, or the local one converted when it is zero. * * @param string $gmt GMT column. * @param string $local Local column. */ private static function gmt( string $gmt, string $local ): string { $zero = '0000-00-00 00:00:00'; if ( '' !== $gmt && $zero !== $gmt ) { return $gmt; } return ( '' === $local || $zero === $local ) ? $gmt : get_gmt_from_date( $local ); } }