Skip to content

Commit

Permalink
Finally added in the query vars to execute the "onsale" arguments. #377
Browse files Browse the repository at this point in the history
  • Loading branch information
krugazul committed Dec 5, 2024
1 parent 24d69ed commit b661a36
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
62 changes: 61 additions & 1 deletion includes/classes/blocks/class-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ class Registration {

protected $disabled = [];

/**
* Holds the array of featured queries.
*
* @var array
*/
protected $featured = [];

/**
* True if the current query outputting needs to be onsale.
*
* @var boolean
*/
protected $onsale = false;

/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
Expand All @@ -22,10 +34,12 @@ class Registration {
*/
public function __construct() {
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_variations_script' ), 10 );
add_filter( 'query_loop_block_query_vars', array( $this, 'query_args_filter' ), 10, 2 );
add_filter( 'query_loop_block_query_vars', array( $this, 'query_args_filter' ), 1, 2 );
add_filter( 'render_block', array( $this, 'maybe_hide_varitaion' ), 10, 3 );

add_filter( 'posts_pre_query', array( $this, 'posts_pre_query' ), 10, 2 );

add_filter( 'render_block_data', array( $this, 'save_onsale_queries' ), 10, 1 );
}

/**
Expand Down Expand Up @@ -113,6 +127,7 @@ public function query_args_filter( $query, $block ) {
// We only restric this on the destination post type, in case the block is used on a landing page.
if ( 'destination' === get_post_type() ) {
$query['post_parent__in'] = [ get_the_ID() ];
$query['post__not_in'] = [ get_the_ID() ];
}
break;

Expand Down Expand Up @@ -240,6 +255,22 @@ public function query_args_filter( $query, $block ) {
break;
}

// Look for the "on sale" CSS class.
if ( true === $this->onsale ) {
if ( isset( $query['meta_query']['relation'] ) ) {
$query['meta_query']['relation'] = 'AND';
}
$query['meta_query'][] = array(
'key' => 'sale_price',
'compare' => 'EXISTS',
);

// reset this to false for the next query.
$this->onsale = false;
}

do_action( 'qm/debug', $query );

return $query;
}

Expand Down Expand Up @@ -432,4 +463,33 @@ public function find_featured_items( $query ) {
}
return $items;
}

/**
* This function looks at the query blocks CSS classes to determine if it is onsale.
*
* @param array $parsed_block
* @return array
*/
public function save_onsale_queries( $parsed_block ) {
if ( ! isset( $parsed_block['blockName'] ) || ! isset( $parsed_block['attrs'] ) ) {
return $parsed_block;
}
$allowed_blocks = array(
'core/query',
);

if ( ! in_array( $parsed_block['blockName'], $allowed_blocks, true ) ) {
return $parsed_block;
}
if ( ! isset( $parsed_block['attrs']['className'] ) || '' === $parsed_block['attrs']['className'] || false === $parsed_block['attrs']['className'] ) {
return $parsed_block;
}

$this->onsale = false;

if ( false !== stripos( $parsed_block['attrs']['className'], 'on-sale' ) ) {
$this->onsale = true;
}
return $parsed_block;
}
}
2 changes: 0 additions & 2 deletions includes/classes/legacy/class-accommodation.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ public function price_filter( $html = '', $meta_key = false, $value = false, $be
'single_supplement'
];

do_action( 'qm/debug', [ get_post_type(), $meta_key ] );

if ( get_post_type() === 'accommodation' && in_array( $meta_key, $currency_fields ) ) {
$price_type = get_post_meta( get_the_ID(), 'price_type', true );
$value = preg_replace( '/[^0-9,.]/', '', $value );
Expand Down

0 comments on commit b661a36

Please sign in to comment.