PluginProbe
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin / 1.0.1
OpenStation: Desktop Windows, Dock & Virtual Desktops for WP Admin v1.0.1
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.0.1, at includes/desktop-files/types/class-openstation-term-file.php

87 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 * 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 public static function type(): string {
17 return 'term';
18 }
19
20 public function exists(): bool {
21 return $this->term() instanceof WP_Term;
22 }
23
24 public function title(): string {
25 $term = $this->term();
26 if ( ! $term ) {
27 return __( '(missing term)', 'desktop-mode' );
28 }
29 return wp_strip_all_tags( $term->name );
30 }
31
32 public function icon(): string {
33 $term = $this->term();
34 if ( ! $term ) {
35 return 'dashicons-warning';
36 }
37 switch ( $term->taxonomy ) {
38 case 'category':
39 return 'dashicons-category';
40 case 'post_tag':
41 return 'dashicons-tag';
42 default:
43 return 'dashicons-tagcloud';
44 }
45 }
46
47 public function can_read( int $user_id ): bool {
48 $term = $this->term();
49 if ( ! $term ) {
50 return false;
51 }
52 $tax = get_taxonomy( $term->taxonomy );
53 if ( ! $tax ) {
54 return false;
55 }
56 return user_can( $user_id, $tax->cap->edit_terms ) || user_can( $user_id, 'read' );
57 }
58
59 public function serialize(): array {
60 $shape = parent::serialize();
61 $term = $this->term();
62 $shape['taxonomy'] = $term ? (string) $term->taxonomy : '';
63 $shape['count'] = $term ? (int) $term->count : 0;
64 return $shape;
65 }
66
67 private function term(): ?WP_Term {
68 [ $taxonomy, $term_id ] = $this->parse_ref();
69 if ( '' === $taxonomy || $term_id <= 0 ) {
70 return null;
71 }
72 $term = get_term( $term_id, $taxonomy );
73 return $term instanceof WP_Term ? $term : null;
74 }
75
76 /**
77 * @return array{0: string, 1: int}
78 */
79 private function parse_ref(): array {
80 $parts = explode( ':', $this->ref, 2 );
81 if ( count( $parts ) !== 2 ) {
82 return array( '', 0 );
83 }
84 return array( (string) $parts[0], (int) $parts[1] );
85 }
86 }
87