post_content !== $before->post_content || $after->post_title !== $before->post_title; if ( Post_Type::NAME === $after->post_type && $is_edit ) { $this->save( $post_id ); } } /** * Records the current user when a page's icon changes. * * @param int $meta_id Meta ID. * @param int $post_id Post ID. * @param string $meta_key Meta key. */ public function record_icon( int $meta_id, int $post_id, string $meta_key ): void { if ( Post_Type::ICON_META === $meta_key ) { $this->save( $post_id ); } } /** * Saves the current user as the last editor, when there is one. * * @param int $post_id Page ID. */ private function save( int $post_id ): void { if ( get_current_user_id() ) { update_post_meta( $post_id, '_edit_last', get_current_user_id() ); } } /** * Registers `last_editor` on pages. */ public function register_field(): void { register_rest_field( Post_Type::NAME, self::FIELD, array( 'get_callback' => array( $this, 'get_field' ), 'schema' => array( 'description' => __( 'Who last edited the page: the author until someone edits it.', 'tinta' ), 'type' => array( 'object', 'null' ), 'context' => array( 'view', 'edit' ), 'readonly' => true, 'properties' => array( 'id' => array( 'type' => 'integer' ), 'name' => array( 'type' => 'string' ), 'avatar' => array( 'type' => 'string', 'format' => 'uri', ), ), ), ) ); } /** * The last editor's ID, display name and avatar. * * @param array $post Page as prepared for the response. * @return array{id: int, name: string, avatar: string}|null */ public function get_field( array $post ): ?array { $user_id = (int) get_post_meta( $post['id'], '_edit_last', true ); $user = get_userdata( $user_id ? $user_id : (int) get_post_field( 'post_author', $post['id'] ) ); return $user ? array( 'id' => $user->ID, 'name' => $user->display_name, 'avatar' => (string) get_avatar_url( $user->ID, array( 'size' => 40 ) ), ) : null; } }