PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.9.6
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.9.6
1.1.10 1.1.9 1.1.8 1.1.7 1.1.6 1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.1 1.0.0 0.9.8 0.9.7 0.9.6 0.9.4 0.9.5 0.9.3 0.9.2 0.9.1 0.9.0 0.8.9 0.8.8 0.8.7 All 34 releases
desktop-mode / includes / desktop-files / types / class-desktop-mode-post-file.php

class-desktop-mode-post-file.php in OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin 0.9.6, at includes/desktop-files/types/class-desktop-mode-post-file.php

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