| 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 |
/** |
| 16 |
* Get the file type identifier. |
| 17 |
* |
| 18 |
* @return string |
| 19 |
*/ |
| 20 |
public static function type(): string { |
| 21 |
return 'comment'; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Whether the comment still exists. |
| 26 |
* |
| 27 |
* @return bool |
| 28 |
*/ |
| 29 |
public function exists(): bool { |
| 30 |
return $this->comment() instanceof WP_Comment; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a label combining author name and a content excerpt. |
| 35 |
* |
| 36 |
* @return string |
| 37 |
*/ |
| 38 |
public function title(): string { |
| 39 |
$c = $this->comment(); |
| 40 |
if ( ! $c ) { |
| 41 |
return __( '(missing comment)', 'desktop-mode' ); |
| 42 |
} |
| 43 |
$author = '' !== $c->comment_author ? $c->comment_author : __( 'Anonymous', 'desktop-mode' ); |
| 44 |
$excerpt = wp_trim_words( wp_strip_all_tags( $c->comment_content ), 8, '…' ); |
| 45 |
return sprintf( '%s — %s', $author, $excerpt ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the Dashicon class for comment tiles. |
| 50 |
* |
| 51 |
* @return string |
| 52 |
*/ |
| 53 |
public function icon(): string { |
| 54 |
return 'dashicons-admin-comments'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Approved comments are public; unapproved comments require |
| 59 |
* the `moderate_comments` capability. |
| 60 |
* |
| 61 |
* @param int $user_id |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public function can_read( int $user_id ): bool { |
| 65 |
$c = $this->comment(); |
| 66 |
if ( ! $c ) { |
| 67 |
return false; |
| 68 |
} |
| 69 |
// Approved comments are public; non-approved gated on |
| 70 |
// moderation cap. |
| 71 |
if ( '1' === (string) $c->comment_approved ) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
return user_can( $user_id, 'moderate_comments' ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Augment the base serialized shape with post id and approval |
| 79 |
* status. |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function serialize(): array { |
| 84 |
$shape = parent::serialize(); |
| 85 |
$c = $this->comment(); |
| 86 |
$shape['postId'] = $c ? (int) $c->comment_post_ID : 0; |
| 87 |
$shape['approved'] = $c ? '1' === (string) $c->comment_approved : false; |
| 88 |
return $shape; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Lazily resolve the underlying WP_Comment from the stored ref. |
| 93 |
* |
| 94 |
* @return WP_Comment|null Null when the comment is gone or the ref is invalid. |
| 95 |
*/ |
| 96 |
private function comment(): ?WP_Comment { |
| 97 |
$id = (int) $this->ref; |
| 98 |
if ( $id <= 0 ) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
$c = get_comment( $id ); |
| 102 |
return $c instanceof WP_Comment ? $c : null; |
| 103 |
} |
| 104 |
} |
| 105 |
|