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-meta.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-meta.php
152 lines
1 <?php
2
3 namespace SearchRegex\Sql\Join;
4
5 use SearchRegex\Sql;
6
7 /**
8 * Join on meta table
9 *
10 * @phpstan-type MetaRow object{
11 * meta_key: string,
12 * meta_value: string
13 * }
14 */
15 class Meta extends Join {
16 /**
17 * Meta table name
18 */
19 private string $meta_table;
20
21 /**
22 * Source table name
23 */
24 private string $source_table;
25
26 /**
27 * ID column to join on
28 */
29 private string $join_id;
30
31 /**
32 * Table to join on
33 */
34 private string $table_id;
35
36 /**
37 * Column to group by
38 */
39 private string $group_id;
40
41 /**
42 * ID in meta table to join on
43 */
44 private string $meta_id;
45
46 /**
47 * Constructor
48 *
49 * @param string $meta_type Meta type.
50 * @param string $source Source.
51 */
52 public function __construct( $meta_type, $source ) {
53 global $wpdb;
54
55 $this->column = $meta_type;
56 $this->source_table = '';
57 $this->meta_id = 'meta_id';
58 $this->group_id = '';
59 $this->table_id = '';
60 $this->meta_table = '';
61 $this->table_id = 'ID';
62 $this->join_id = '';
63
64 if ( $source === 'postmeta' ) {
65 $this->meta_table = $wpdb->postmeta;
66 $this->source_table = $wpdb->posts;
67 $this->join_id = 'post_id';
68 $this->group_id = $wpdb->posts . '.ID';
69 } elseif ( $source === 'commentmeta' ) {
70 $this->meta_table = $wpdb->commentmeta;
71 $this->source_table = $wpdb->comments;
72 $this->join_id = 'comment_id';
73 $this->group_id = $wpdb->comments . '.comment_ID';
74 $this->table_id = 'comment_id';
75 } elseif ( $source === 'usermeta' ) {
76 $this->meta_table = $wpdb->usermeta;
77 $this->source_table = $wpdb->users;
78 $this->join_id = 'user_id';
79 $this->group_id = $wpdb->users . '.ID';
80 $this->meta_id = 'umeta_id';
81 }
82 }
83
84 public function get_select() {
85 return new Sql\Select\Select( Sql\Value::table( $this->meta_table ), Sql\Value::column( '0' ), Sql\Value::column( 'meta_id' ) );
86 }
87
88 public function get_group() {
89 return new Sql\Group( Sql\Value::column( $this->group_id ) );
90 }
91
92 public function get_from() {
93 if ( $this->is_matching ) {
94 return new Sql\From( Sql\Value::safe_raw( sprintf( 'LEFT JOIN %s AS meta ON %s.%s = meta.%s', $this->meta_table, $this->source_table, $this->table_id, $this->join_id ) ) );
95 }
96
97 return false;
98 }
99
100 public function get_join_column() {
101 return 'meta.' . $this->column;
102 }
103
104 public function get_join_value( $meta_id ) {
105 global $wpdb;
106
107 if ( $this->column === 'meta_key' ) {
108 // phpcs:ignore
109 return $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM {$this->meta_table} WHERE {$this->meta_id}=%d", $meta_id ) );
110 }
111
112 // phpcs:ignore
113 return $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$this->meta_table} WHERE {$this->meta_id}=%d", $meta_id ) );
114 }
115
116 /**
117 * Get meta values
118 *
119 * @param list<int> $values Meta ID values.
120 * @return list<MetaRow>
121 */
122 public function get_values( $values ) {
123 global $wpdb;
124
125 $in = new Sql\Where\Where_In( new Sql\Select\Select( Sql\Value::table( $this->meta_table ), Sql\Value::column( $this->column ) ), 'IN', $values );
126
127 // phpcs:ignore
128 return $wpdb->get_results( "SELECT meta_key,meta_value FROM {$this->meta_table} WHERE {$this->meta_id} IN ". $in->get_value() );
129 }
130
131 public function get_column() {
132 return 'meta';
133 }
134
135 /**
136 * Get all the values for this join
137 *
138 * @param int $row_id Row ID.
139 * @return list<string>
140 */
141 public function get_all_values( $row_id ) {
142 global $wpdb;
143
144 // phpcs:ignore
145 return $wpdb->get_col( $wpdb->prepare( "SELECT {$this->meta_id} FROM {$this->get_table()} WHERE {$this->join_id} = %d", $row_id ) );
146 }
147
148 public function get_table() {
149 return $this->meta_table;
150 }
151 }
152