PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 0.8.7
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v0.8.7
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-term-file.php

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

88 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Desktop Mode — `term` file type. Covers any taxonomy term
4 * (category, tag, custom). Reference shape: `"<taxonomy>:<term_id>"`.
5 *
6 * @package WPDesktopMode
7 * @since 0.9.0
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * @since 0.9.0
14 */
15 class Desktop_Mode_Term_File extends Desktop_Mode_File {
16
17 public static function type(): string {
18 return 'term';
19 }
20
21 public function exists(): bool {
22 return $this->term() instanceof WP_Term;
23 }
24
25 public function title(): string {
26 $term = $this->term();
27 if ( ! $term ) {
28 return __( '(missing term)', 'desktop-mode' );
29 }
30 return wp_strip_all_tags( $term->name );
31 }
32
33 public function icon(): string {
34 $term = $this->term();
35 if ( ! $term ) {
36 return 'dashicons-warning';
37 }
38 switch ( $term->taxonomy ) {
39 case 'category':
40 return 'dashicons-category';
41 case 'post_tag':
42 return 'dashicons-tag';
43 default:
44 return 'dashicons-tagcloud';
45 }
46 }
47
48 public function can_read( int $user_id ): bool {
49 $term = $this->term();
50 if ( ! $term ) {
51 return false;
52 }
53 $tax = get_taxonomy( $term->taxonomy );
54 if ( ! $tax ) {
55 return false;
56 }
57 return user_can( $user_id, $tax->cap->edit_terms ) || user_can( $user_id, 'read' );
58 }
59
60 public function serialize(): array {
61 $shape = parent::serialize();
62 $term = $this->term();
63 $shape['taxonomy'] = $term ? (string) $term->taxonomy : '';
64 $shape['count'] = $term ? (int) $term->count : 0;
65 return $shape;
66 }
67
68 private function term(): ?WP_Term {
69 [ $taxonomy, $term_id ] = $this->parse_ref();
70 if ( '' === $taxonomy || $term_id <= 0 ) {
71 return null;
72 }
73 $term = get_term( $term_id, $taxonomy );
74 return $term instanceof WP_Term ? $term : null;
75 }
76
77 /**
78 * @return array{0: string, 1: int}
79 */
80 private function parse_ref(): array {
81 $parts = explode( ':', $this->ref, 2 );
82 if ( count( $parts ) !== 2 ) {
83 return array( '', 0 );
84 }
85 return array( (string) $parts[0], (int) $parts[1] );
86 }
87 }
88