| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `post` file type. |
| 4 |
* |
| 5 |
* Adapts any public post type (post, page, CPT) to the file |
| 6 |
* surface. The concrete post type is read from `get_post_type()` |
| 7 |
* and exposed in the serialized shape so plugins can theme tiles |
| 8 |
* differently per post type via `openstation_file_serialize`. |
| 9 |
* |
| 10 |
* @package OpenStation |
| 11 |
*/ |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* The `post` desktop file type. |
| 17 |
*/ |
| 18 |
class OpenStation_Post_File extends OpenStation_File { |
| 19 |
|
| 20 |
/** |
| 21 |
* Get the file type identifier. |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public static function type(): string { |
| 26 |
return 'post'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Whether the underlying post still exists. |
| 31 |
* |
| 32 |
* @return bool |
| 33 |
*/ |
| 34 |
public function exists(): bool { |
| 35 |
return $this->post() instanceof WP_Post; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get a human-readable label, falling back to a placeholder |
| 40 |
* when the post is missing or has no title. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function title(): string { |
| 45 |
$post = $this->post(); |
| 46 |
if ( ! $post ) { |
| 47 |
return __( '(missing post)', 'desktop-mode' ); |
| 48 |
} |
| 49 |
$title = wp_strip_all_tags( get_the_title( $post ) ); |
| 50 |
// Auto-drafts and brand-new posts often have empty titles. |
| 51 |
// Surface a placeholder so the tile shows something readable |
| 52 |
// instead of an icon with no label under it. |
| 53 |
return '' !== $title ? $title : __( '(no title)', 'desktop-mode' ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Resolve a Dashicon class for the post, preferring the |
| 58 |
* post-type's registered menu icon. |
| 59 |
* |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public function icon(): string { |
| 63 |
$post = $this->post(); |
| 64 |
if ( ! $post ) { |
| 65 |
return 'dashicons-warning'; |
| 66 |
} |
| 67 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 68 |
if ( $post_type_object && ! empty( $post_type_object->menu_icon ) ) { |
| 69 |
return (string) $post_type_object->menu_icon; |
| 70 |
} |
| 71 |
return 'page' === $post->post_type ? 'dashicons-page' : 'dashicons-admin-post'; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get the post's featured-image thumbnail URL, if any. |
| 76 |
* |
| 77 |
* @return string Empty string when no thumbnail is set. |
| 78 |
*/ |
| 79 |
public function preview_url(): string { |
| 80 |
$post = $this->post(); |
| 81 |
if ( ! $post ) { |
| 82 |
return ''; |
| 83 |
} |
| 84 |
$thumb_id = get_post_thumbnail_id( $post ); |
| 85 |
if ( ! $thumb_id ) { |
| 86 |
return ''; |
| 87 |
} |
| 88 |
$src = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); |
| 89 |
return is_array( $src ) ? (string) $src[0] : ''; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Check whether the given user can read this post. |
| 94 |
* |
| 95 |
* @param int $user_id |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
public function can_read( int $user_id ): bool { |
| 99 |
$post = $this->post(); |
| 100 |
if ( ! $post ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
return user_can( $user_id, 'read_post', $post->ID ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Augment the base serialized shape with post type, status, |
| 108 |
* and permalink for cross-frame drag handlers. |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function serialize(): array { |
| 113 |
$shape = parent::serialize(); |
| 114 |
$post = $this->post(); |
| 115 |
$shape['postType'] = $post ? (string) $post->post_type : ''; |
| 116 |
$shape['status'] = $post ? (string) $post->post_status : ''; |
| 117 |
// Permalink — surfaced on the serialized shape so cross-frame |
| 118 |
// drag handlers can build a `<a href>` block on drop without |
| 119 |
// a synchronous REST roundtrip. Empty string when the post is |
| 120 |
// gone or has no permalink (drafts of certain post types). |
| 121 |
$shape['link'] = $post ? (string) get_permalink( $post ) : ''; |
| 122 |
return $shape; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Lazily resolve the underlying WP_Post from the stored ref. |
| 127 |
* |
| 128 |
* @return WP_Post|null Null when the post is gone or the ref is invalid. |
| 129 |
*/ |
| 130 |
private function post(): ?WP_Post { |
| 131 |
$id = (int) $this->ref; |
| 132 |
if ( $id <= 0 ) { |
| 133 |
return null; |
| 134 |
} |
| 135 |
$post = get_post( $id ); |
| 136 |
return $post instanceof WP_Post ? $post : null; |
| 137 |
} |
| 138 |
} |
| 139 |
|