| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `comment` file type. |
| 4 |
* |
| 5 |
* @package OpenStation |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* The `comment` desktop file type. |
| 12 |
*/ |
| 13 |
class OpenStation_Comment_File extends OpenStation_File { |
| 14 |
|
| 15 |
public static function type(): string { |
| 16 |
return 'comment'; |
| 17 |
} |
| 18 |
|
| 19 |
public function exists(): bool { |
| 20 |
return $this->comment() instanceof WP_Comment; |
| 21 |
} |
| 22 |
|
| 23 |
public function title(): string { |
| 24 |
$c = $this->comment(); |
| 25 |
if ( ! $c ) { |
| 26 |
return __( '(missing comment)', 'desktop-mode' ); |
| 27 |
} |
| 28 |
$author = '' !== $c->comment_author ? $c->comment_author : __( 'Anonymous', 'desktop-mode' ); |
| 29 |
$excerpt = wp_trim_words( wp_strip_all_tags( $c->comment_content ), 8, '…' ); |
| 30 |
return sprintf( '%s — %s', $author, $excerpt ); |
| 31 |
} |
| 32 |
|
| 33 |
public function icon(): string { |
| 34 |
return 'dashicons-admin-comments'; |
| 35 |
} |
| 36 |
|
| 37 |
public function can_read( int $user_id ): bool { |
| 38 |
$c = $this->comment(); |
| 39 |
if ( ! $c ) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
// Approved comments are public; non-approved gated on |
| 43 |
// moderation cap. |
| 44 |
if ( '1' === (string) $c->comment_approved ) { |
| 45 |
return true; |
| 46 |
} |
| 47 |
return user_can( $user_id, 'moderate_comments' ); |
| 48 |
} |
| 49 |
|
| 50 |
public function serialize(): array { |
| 51 |
$shape = parent::serialize(); |
| 52 |
$c = $this->comment(); |
| 53 |
$shape['postId'] = $c ? (int) $c->comment_post_ID : 0; |
| 54 |
$shape['approved'] = $c ? '1' === (string) $c->comment_approved : false; |
| 55 |
return $shape; |
| 56 |
} |
| 57 |
|
| 58 |
private function comment(): ?WP_Comment { |
| 59 |
$id = (int) $this->ref; |
| 60 |
if ( $id <= 0 ) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
$c = get_comment( $id ); |
| 64 |
return $c instanceof WP_Comment ? $c : null; |
| 65 |
} |
| 66 |
} |
| 67 |
|