PluginProbe ʕ •ᴥ•ʔ
Search Regex / trunk
Search Regex vtrunk
trunk 1.4.12 1.4.13 1.4.14 1.4.15 1.4.16 2.0 2.0.1 2.1 2.2 2.2.1 2.3 2.3.1 2.3.2 2.3.3 2.4 2.4.1 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.1 3.1.1 3.1.2 3.2 3.3 3.3.0 3.3.1 3.4 3.4.1 3.4.2
search-regex / includes / sql / join / class-term.php
search-regex / includes / sql / join Last commit date
class-comment.php 5 months ago class-join.php 4 months ago class-meta.php 5 months ago class-post.php 5 months ago class-taxonomy.php 5 months ago class-term-description.php 4 months ago class-term.php 5 months ago class-user.php 5 months ago
class-term.php
135 lines
1 <?php
2
3 namespace SearchRegex\Sql\Join;
4
5 use SearchRegex\Sql;
6 use WP_Error;
7
8 /**
9 * Join on terms
10 */
11 class Term extends Join {
12 /**
13 * Constructor
14 *
15 * @param string $term_type Type of taxonomy.
16 */
17 public function __construct( $term_type ) {
18 $this->column = $term_type;
19 }
20
21 public function get_where() {
22 if ( $this->is_matching ) {
23 return new Sql\Where\Where_String( new Sql\Select\Select( Sql\Value::column( 'tt' ), Sql\Value::column( 'taxonomy' ) ), '=', $this->column );
24 }
25
26 return false;
27 }
28
29 public function get_select() {
30 return new Sql\Select\Select( Sql\Value::column( 'tt' ), Sql\Value::column( '0' ), Sql\Value::column( $this->column ) );
31 }
32
33 public function get_group() {
34 global $wpdb;
35
36 return new Sql\Group( Sql\Value::column( "{$wpdb->posts}.ID" ) );
37 }
38
39 public function get_from() {
40 global $wpdb;
41
42 if ( $this->is_matching ) {
43 return new Sql\From( Sql\Value::safe_raw( sprintf( 'INNER JOIN %sterm_relationships AS tr ON (%s.ID = tr.object_id) INNER JOIN %sterm_taxonomy AS tt ON tt.term_taxonomy_id=tr.term_taxonomy_id', $wpdb->prefix, $wpdb->posts, $wpdb->prefix ) ) );
44 }
45
46 return false;
47 }
48
49 public function get_join_column() {
50 return 'tr.term_taxonomy_id';
51 }
52
53 public function get_join_value( $value ) {
54 $term = get_term( intval( $value, 10 ) );
55 if ( $term instanceof WP_Error ) {
56 return "$value";
57 }
58
59 if ( is_object( $term ) && $term->taxonomy === $this->column ) {
60 return $term->name;
61 }
62
63 // @phpstan-ignore cast.useless
64 return (string) $value;
65 }
66
67 /**
68 * Get all term values
69 *
70 * @param integer $row_id Row ID.
71 * @return integer[]
72 */
73 public function get_all_values( $row_id ) {
74 $terms = wp_get_post_terms( $row_id, $this->column, [ 'fields' => 'ids' ] );
75
76 if ( is_wp_error( $terms ) ) {
77 return [];
78 }
79
80 return $terms;
81 }
82
83 public function get_table() {
84 return '';
85 }
86
87 /**
88 * Get the value
89 *
90 * @param integer $row_id Row ID.
91 * @param string $type Term type.
92 * @param string $seperator How to seperate the terms.
93 * @return string
94 */
95 public function get_value( $row_id, $type, $seperator ) {
96 $terms = wp_get_post_terms( $row_id, $this->column, [ 'fields' => 'all' ] );
97 if ( $terms instanceof WP_Error ) {
98 return '';
99 }
100
101 $group = array_map(
102 function ( $term ) use ( $type ) {
103 if ( $type === 'slug' ) {
104 return $term->slug;
105 }
106
107 if ( $type === 'url' ) {
108 $link = get_term_link( $term );
109 if ( is_wp_error( $link ) ) {
110 return '';
111 }
112 return $link;
113 }
114
115 if ( $type === 'link' ) {
116 $link = get_term_link( $term );
117 if ( $link instanceof WP_Error ) {
118 return '';
119 }
120
121 return '<a href="' . esc_url( $link ) . '">' . $term->name . '</a>';
122 }
123
124 if ( $type === 'description' ) {
125 return $term->description;
126 }
127
128 return $term->name;
129 }, $terms
130 );
131
132 return implode( $seperator, $group );
133 }
134 }
135