PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.1.2
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.1.2
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-openstation-term-file.php

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

125 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 * OpenStation — `term` file type. Covers any taxonomy term
4 * (category, tag, custom). Reference shape: `"<taxonomy>:<term_id>"`.
5 *
6 * @package OpenStation
7 */
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * The `term` desktop file type.
13 */
14 class OpenStation_Term_File extends OpenStation_File {
15
16 /**
17 * Get the file type identifier.
18 *
19 * @return string
20 */
21 public static function type(): string {
22 return 'term';
23 }
24
25 /**
26 * Whether the term still exists.
27 *
28 * @return bool
29 */
30 public function exists(): bool {
31 return $this->term() instanceof WP_Term;
32 }
33
34 /**
35 * Get the term name.
36 *
37 * @return string
38 */
39 public function title(): string {
40 $term = $this->term();
41 if ( ! $term ) {
42 return __( '(missing term)', 'desktop-mode' );
43 }
44 return wp_strip_all_tags( $term->name );
45 }
46
47 /**
48 * Resolve a Dashicon class based on the taxonomy.
49 *
50 * @return string
51 */
52 public function icon(): string {
53 $term = $this->term();
54 if ( ! $term ) {
55 return 'dashicons-warning';
56 }
57 switch ( $term->taxonomy ) {
58 case 'category':
59 return 'dashicons-category';
60 case 'post_tag':
61 return 'dashicons-tag';
62 default:
63 return 'dashicons-tagcloud';
64 }
65 }
66
67 /**
68 * Check whether the user can edit terms in this taxonomy or
69 * has the base `read` capability.
70 *
71 * @param int $user_id
72 * @return bool
73 */
74 public function can_read( int $user_id ): bool {
75 $term = $this->term();
76 if ( ! $term ) {
77 return false;
78 }
79 $tax = get_taxonomy( $term->taxonomy );
80 if ( ! $tax ) {
81 return false;
82 }
83 return user_can( $user_id, $tax->cap->edit_terms ) || user_can( $user_id, 'read' );
84 }
85
86 /**
87 * Augment the base serialized shape with taxonomy and post
88 * count.
89 *
90 * @return array
91 */
92 public function serialize(): array {
93 $shape = parent::serialize();
94 $term = $this->term();
95 $shape['taxonomy'] = $term ? (string) $term->taxonomy : '';
96 $shape['count'] = $term ? (int) $term->count : 0;
97 return $shape;
98 }
99
100 /**
101 * Lazily resolve the underlying WP_Term from the stored ref.
102 *
103 * @return WP_Term|null Null when the term is gone or the ref is invalid.
104 */
105 private function term(): ?WP_Term {
106 [ $taxonomy, $term_id ] = $this->parse_ref();
107 if ( '' === $taxonomy || $term_id <= 0 ) {
108 return null;
109 }
110 $term = get_term( $term_id, $taxonomy );
111 return $term instanceof WP_Term ? $term : null;
112 }
113
114 /**
115 * @return array{0: string, 1: int}
116 */
117 private function parse_ref(): array {
118 $parts = explode( ':', $this->ref, 2 );
119 if ( count( $parts ) !== 2 ) {
120 return array( '', 0 );
121 }
122 return array( (string) $parts[0], (int) $parts[1] );
123 }
124 }
125