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-user.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SearchRegex\Sql\Join; |
| 4 | |
| 5 | use SearchRegex\Sql; |
| 6 | |
| 7 | /** |
| 8 | * Undocumented class |
| 9 | */ |
| 10 | class User extends Join { |
| 11 | /** |
| 12 | * Join logic |
| 13 | */ |
| 14 | private string $logic = ''; |
| 15 | |
| 16 | /** |
| 17 | * Join table |
| 18 | */ |
| 19 | private string $join_table = ''; |
| 20 | |
| 21 | /** |
| 22 | * Join column |
| 23 | */ |
| 24 | private string $join_column; |
| 25 | |
| 26 | /** |
| 27 | * Join to |
| 28 | */ |
| 29 | private string $join_to; |
| 30 | |
| 31 | /** |
| 32 | * Join to column |
| 33 | */ |
| 34 | private string $join_to_column = ''; |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | * |
| 39 | * @param string $column Column. |
| 40 | * @param string $source Source. |
| 41 | */ |
| 42 | public function __construct( $column, $source ) { |
| 43 | global $wpdb; |
| 44 | |
| 45 | $this->column = $column; |
| 46 | $this->join_column = 'user_id'; |
| 47 | $this->join_to_column = 'ID'; |
| 48 | $this->join_to = $wpdb->users; |
| 49 | |
| 50 | if ( $source === 'comment' ) { |
| 51 | $this->join_table = $wpdb->comments; |
| 52 | $this->join_column = 'user_id'; |
| 53 | } elseif ( $source === 'posts' ) { |
| 54 | $this->join_table = $wpdb->posts; |
| 55 | $this->join_column = 'post_author'; |
| 56 | } elseif ( $source === 'users' ) { |
| 57 | $this->join_table = $wpdb->users; |
| 58 | $this->join_to_column = 'ID'; |
| 59 | } elseif ( $source === 'user-meta' ) { |
| 60 | $this->join_table = $wpdb->usermeta; |
| 61 | $this->join_column = 'user_id'; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Set the logic |
| 67 | * |
| 68 | * @param string $logic Logic. |
| 69 | * @return void |
| 70 | */ |
| 71 | public function set_logic( $logic ) { |
| 72 | $this->logic = $logic; |
| 73 | } |
| 74 | |
| 75 | public function get_select() { |
| 76 | return new Sql\Select\Select( Sql\Value::table( $this->join_table ), Sql\Value::column( $this->join_column ), null, true ); |
| 77 | } |
| 78 | |
| 79 | public function get_from() { |
| 80 | global $wpdb; |
| 81 | |
| 82 | $table = Sql\Value::table( $this->join_table ); |
| 83 | $column = Sql\Value::column( $this->join_column ); |
| 84 | return new Sql\From( Sql\Value::safe_raw( sprintf( "LEFT JOIN {$wpdb->users} ON {$wpdb->users}.ID=%s.%s", $table->get_value(), $column->get_value() ) ) ); |
| 85 | } |
| 86 | |
| 87 | public function get_join_column() { |
| 88 | return $this->join_column; |
| 89 | } |
| 90 | |
| 91 | public function get_where() { |
| 92 | return new Sql\Where\Where_Null( new Sql\Select\Select( Sql\Value::table( $this->join_to ), Sql\Value::column( $this->join_to_column ) ), $this->logic ); |
| 93 | } |
| 94 | |
| 95 | public function get_join_value( $value ) { |
| 96 | return "$value"; |
| 97 | } |
| 98 | |
| 99 | public function get_table() { |
| 100 | return $this->join_table; |
| 101 | } |
| 102 | } |
| 103 |