Skip to content

Commit

Permalink
Early return.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Mar 12, 2024
1 parent b85a90e commit e4d3fa9
Showing 1 changed file with 53 additions and 51 deletions.
104 changes: 53 additions & 51 deletions classes/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,71 +121,73 @@ public function posts_clauses( $pieces, $query ) {

$post_type = $query->get( 'post_type' );

if ( 'orbis_project' === $post_type ) {
// Fields
$fields = ',
project.id AS project_id,
project.number_seconds AS project_number_seconds,
project.finished AS project_is_finished,
project.invoiced AS project_is_invoiced
';

// Join
$join = "
if ( 'orbis_project' !== $post_type ) {
return $pieces;
}

// Fields
$fields = ',
project.id AS project_id,
project.number_seconds AS project_number_seconds,
project.finished AS project_is_finished,
project.invoiced AS project_is_invoiced
';

// Join
$join = "
LEFT JOIN
$wpdb->orbis_projects AS project
ON $wpdb->posts.ID = project.post_id
";

if ( property_exists( $wpdb, 'orbis_companies' ) ) {
$fields .= ',
principal.id AS principal_id,
principal.name AS principal_name,
principal.post_id AS principal_post_id
';

$join .= "
LEFT JOIN
$wpdb->orbis_projects AS project
ON $wpdb->posts.ID = project.post_id
$wpdb->orbis_companies AS principal
ON project.principal_id = principal.id
";
}

if ( property_exists( $wpdb, 'orbis_companies' ) ) {
$fields .= ',
principal.id AS principal_id,
principal.name AS principal_name,
principal.post_id AS principal_post_id
';

$join .= "
LEFT JOIN
$wpdb->orbis_companies AS principal
ON project.principal_id = principal.id
";
}

// Where
$where = '';

$principal = $query->get( 'orbis_project_principal' );
// Where
$where = '';

if ( ! empty( $principal ) ) {
$where .= $wpdb->prepare( ' AND principal.name LIKE %s', '%' . $wpdb->esc_like( $principal ) . '%' );
}
$principal = $query->get( 'orbis_project_principal' );

$client_id = $query->get( 'orbis_project_client_id' );
if ( ! empty( $principal ) ) {
$where .= $wpdb->prepare( ' AND principal.name LIKE %s', '%' . $wpdb->esc_like( $principal ) . '%' );
}

if ( ! empty( $client_id ) ) {
$where .= $wpdb->prepare( ' AND principal.post_id LIKE %d ', $client_id );
}
$client_id = $query->get( 'orbis_project_client_id' );

$invoice_number = $query->get( 'orbis_project_invoice_number' );
if ( ! empty( $client_id ) ) {
$where .= $wpdb->prepare( ' AND principal.post_id LIKE %d ', $client_id );
}

if ( ! empty( $invoice_number ) ) {
$where .= $wpdb->prepare( ' AND project.invoice_number LIKE %s', '%' . $wpdb->esc_like( $invoice_number ) . '%' );
}
$invoice_number = $query->get( 'orbis_project_invoice_number' );

$is_finished = $query->get( 'orbis_project_is_finished', null );
if ( ! empty( $invoice_number ) ) {
$where .= $wpdb->prepare( ' AND project.invoice_number LIKE %s', '%' . $wpdb->esc_like( $invoice_number ) . '%' );
}

if ( null !== $is_finished ) {
$is_finished = filter_var( $is_finished, FILTER_VALIDATE_BOOLEAN );
$is_finished = $query->get( 'orbis_project_is_finished', null );

$where .= $wpdb->prepare( ' AND project.finished = %d', $is_finished );
}
if ( null !== $is_finished ) {
$is_finished = filter_var( $is_finished, FILTER_VALIDATE_BOOLEAN );

// Pieces
$pieces['join'] .= $join;
$pieces['fields'] .= $fields;
$pieces['where'] .= $where;
$where .= $wpdb->prepare( ' AND project.finished = %d', $is_finished );
}

// Pieces
$pieces['join'] .= $join;
$pieces['fields'] .= $fields;
$pieces['where'] .= $where;

return $pieces;
}

Expand Down

0 comments on commit e4d3fa9

Please sign in to comment.