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