| 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 |
public static function type(): string { |
| 21 |
return 'post'; |
| 22 |
} |
| 23 |
|
| 24 |
public function exists(): bool { |
| 25 |
return $this->post() instanceof WP_Post; |
| 26 |
} |
| 27 |
|
| 28 |
public function title(): string { |
| 29 |
$post = $this->post(); |
| 30 |
if ( ! $post ) { |
| 31 |
return __( '(missing post)', 'desktop-mode' ); |
| 32 |
} |
| 33 |
$title = wp_strip_all_tags( get_the_title( $post ) ); |
| 34 |
// Auto-drafts and brand-new posts often have empty titles. |
| 35 |
// Surface a placeholder so the tile shows something readable |
| 36 |
// instead of an icon with no label under it. |
| 37 |
return '' !== $title ? $title : __( '(no title)', 'desktop-mode' ); |
| 38 |
} |
| 39 |
|
| 40 |
public function icon(): string { |
| 41 |
$post = $this->post(); |
| 42 |
if ( ! $post ) { |
| 43 |
return 'dashicons-warning'; |
| 44 |
} |
| 45 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 46 |
if ( $post_type_object && ! empty( $post_type_object->menu_icon ) ) { |
| 47 |
return (string) $post_type_object->menu_icon; |
| 48 |
} |
| 49 |
return 'page' === $post->post_type ? 'dashicons-page' : 'dashicons-admin-post'; |
| 50 |
} |
| 51 |
|
| 52 |
public function preview_url(): string { |
| 53 |
$post = $this->post(); |
| 54 |
if ( ! $post ) { |
| 55 |
return ''; |
| 56 |
} |
| 57 |
$thumb_id = get_post_thumbnail_id( $post ); |
| 58 |
if ( ! $thumb_id ) { |
| 59 |
return ''; |
| 60 |
} |
| 61 |
$src = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); |
| 62 |
return is_array( $src ) ? (string) $src[0] : ''; |
| 63 |
} |
| 64 |
|
| 65 |
public function can_read( int $user_id ): bool { |
| 66 |
$post = $this->post(); |
| 67 |
if ( ! $post ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
return user_can( $user_id, 'read_post', $post->ID ); |
| 71 |
} |
| 72 |
|
| 73 |
public function serialize(): array { |
| 74 |
$shape = parent::serialize(); |
| 75 |
$post = $this->post(); |
| 76 |
$shape['postType'] = $post ? (string) $post->post_type : ''; |
| 77 |
$shape['status'] = $post ? (string) $post->post_status : ''; |
| 78 |
// Permalink — surfaced on the serialized shape so cross-frame |
| 79 |
// drag handlers can build a `<a href>` block on drop without |
| 80 |
// a synchronous REST roundtrip. Empty string when the post is |
| 81 |
// gone or has no permalink (drafts of certain post types). |
| 82 |
$shape['link'] = $post ? (string) get_permalink( $post ) : ''; |
| 83 |
return $shape; |
| 84 |
} |
| 85 |
|
| 86 |
private function post(): ?WP_Post { |
| 87 |
$id = (int) $this->ref; |
| 88 |
if ( $id <= 0 ) { |
| 89 |
return null; |
| 90 |
} |
| 91 |
$post = get_post( $id ); |
| 92 |
return $post instanceof WP_Post ? $post : null; |
| 93 |
} |
| 94 |
} |
| 95 |
|