Skip to content

Commit

Permalink
Merge pull request #466 from lightspeedwp/2.0.1-parents-only-query-args
Browse files Browse the repository at this point in the history
2.0.1 Parents only query args
  • Loading branch information
krugazul authored Jan 8, 2025
2 parents 8ab6e71 + c972c84 commit e33fc9b
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 60 deletions.
24 changes: 24 additions & 0 deletions assets/js/blocks/slider-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
filterByOnsale = props.attributes.filterByOnsale;
}

var parentsOnly = props.attributes.parentsOnly || false;
if ( undefined === props.attributes.parentsOnly ) {
if ( props.attributes.className && props.attributes.className.includes( 'parents-only' ) ) {
parentsOnly = true;
}
} else {
parentsOnly = props.attributes.parentsOnly;
}

return el(
element.Fragment,
{},
Expand All @@ -52,6 +61,15 @@
filterByOnsale: value
});
}
}),
el(CheckboxControl, {
label: 'Parents Only',
checked: parentsOnly,
onChange: function (value) {
props.setAttributes({
parentsOnly: value
});
}
})
)
)
Expand Down Expand Up @@ -87,6 +105,12 @@
extraProps.className = extraProps.className.replace(/\bon-sale\b\s*/g, '').trim();
}

if ( true === attributes.parentsOnly ) {
extraProps.className = (extraProps.className || '') + ' parents-only';
} else if ( false === attributes.parentsOnly && extraProps.className ) {
extraProps.className = extraProps.className.replace(/\bparents-only\b\s*/g, '').trim();
}

}
return extraProps;
}
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [[2.0.1]](https://github.com/lightspeeddevelopment/tour-operator/releases/tag/2.0.1) -

### Added
- A "Parents Only" checkbox to the TO query block settings, allowing you to select only the parent destinations.

## [[2.0.0]](https://github.com/lightspeeddevelopment/tour-operator/releases/tag/2.0.0) - 20-12-2024

### Enhancements
Expand Down
11 changes: 0 additions & 11 deletions includes/classes/admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,10 @@ class Admin {
* LSX Tour Operator Admin constructor.
*/
public function __construct() {
add_filter( 'upload_mimes', array( $this, 'allow_svgimg_types' ) );
add_filter( 'type_url_form_media', array( $this, 'change_attachment_field_button' ), 20, 1 );
add_filter( 'plugin_action_links_' . plugin_basename( LSX_TO_CORE ), array( $this, 'add_action_links' ) );
}

/**
* Allow SVG files for upload
*/
public function allow_svgimg_types( $mimes ) {
$mimes['svg'] = 'image/svg+xml';
$mimes['kml'] = 'image/kml+xml';

return $mimes;
}

/**
* Change the "Insert into Post" button text when media modal is used for
* feature images
Expand Down
57 changes: 38 additions & 19 deletions includes/classes/blocks/class-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Registration {
*/
protected $onsale = false;

/**
* True if the current query outputting needs only the parent outputs.
*
* @var boolean
*/
protected $parents_only = false;

/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
Expand All @@ -37,9 +44,8 @@ public function __construct() {
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( 'render_block_data', array( $this, 'save_checkbox_queries' ), 10, 1 );
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 @@ -108,11 +114,31 @@ public function enqueue_block_variations_script() {
public function query_args_filter( $query, $block ) {
$block = $block->parsed_block;

// These are for all query blocks.
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;
}

if ( true === $this->parents_only ) {
$query['post_parent'] = 0;
}


// Determine if this is the custom block variation.
if ( ! isset( $block['attrs']['className'] ) ) {
return $query;
}


// Add our specific query args to the query for our variations.
$pattern = "/(lsx|facts)-(.*?)-query/";
preg_match( $pattern, $block['attrs']['className'], $matches );

Expand Down Expand Up @@ -258,20 +284,6 @@ 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;
}

return $query;
}

Expand Down Expand Up @@ -477,7 +489,7 @@ public function find_featured_items( $query ) {
* @param array $parsed_block
* @return array
*/
public function save_onsale_queries( $parsed_block ) {
public function save_checkbox_queries( $parsed_block ) {
if ( ! isset( $parsed_block['blockName'] ) || ! isset( $parsed_block['attrs'] ) ) {
return $parsed_block;
}
Expand All @@ -488,15 +500,22 @@ public function save_onsale_queries( $parsed_block ) {
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;
}

$this->parents_only = false;
if ( false !== stripos( $parsed_block['attrs']['className'], 'parents-only' ) ) {
$this->parents_only = true;
}
do_action( 'qm/debug', $this->parents_only );

return $parsed_block;
}
}
18 changes: 1 addition & 17 deletions includes/classes/blocks/class-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,7 @@ public function register_post_type_templates() {
'search-results' => [
'title' => __( 'Search Results', 'tour-operator' ),
'description' => __( 'Displays when a visitor performs a search on your website.', 'tour-operator' ),
],
'index' => [
'title' => __( 'Index', 'tour-operator' ),
'description' => __( 'Used as a fallback template for all pages when a more specific template is not defined.', 'tour-operator' ),
],
'no-title' => [
'title' => __( 'No Title', 'tour-operator' ),
'description' => __( 'A generic page template with no page title displayed', 'tour-operator' ),
],
'pages' => [
'title' => __( 'Pages', 'tour-operator' ),
'description' => __( 'A generic page template with a page title displayed', 'tour-operator' ),
],
'archive' => [
'title' => __( 'All Archives', 'tour-operator' ),
'description' => __( 'Displays any archive, including posts by a single author, category, tag, taxonomy, custom post type, and date. This template will serve as a fallback when more specific templates (e.g., Category or Tag) cannot be found.', 'tour-operator' ),
],
]
];

foreach ( $post_types as $key => $labels ) {
Expand Down
37 changes: 37 additions & 0 deletions includes/classes/legacy/class-destination.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ private function __construct() {
add_filter( 'lsx_to_parents_only', array( $this, 'filter_countries' ) );

add_filter( 'lsx_to_custom_field_query', array( $this, 'travel_information_excerpt' ), 5, 10 );
add_filter( 'facetwp_query_args', [ $this, 'facet_wp_filter' ] , 10, 2 );
add_action( 'pre_get_posts', [ $this, 'only_parent_destinations' ] );

add_action( 'wp_footer', array( $this, 'output_modals' ) );

}

/**
Expand Down Expand Up @@ -114,6 +117,40 @@ public function filter_countries( $countries = array() ) {
return $countries;
}

public function only_parent_destinations( $query ) {
// Only run on the front end and for the main query
if ( ! is_admin() && $query->is_main_query() && $query->is_post_type_archive( 'destination' ) ) {
// Show only top-level
$query->set( 'post_parent', 0 );

// Alphabetical by title
$query->set( 'orderby', 'title' );
$query->set( 'order', 'ASC' );

// Make sure pagination is not disabled
$query->set( 'posts_per_page', 12 ); // or your desired number
$query->set( 'paged', get_query_var( 'paged' ) );
$query->set( 'nopaging', false );
}
}

/**
* Sets the destination archive to only show top-level destinations
*
* @param array $args
* @param array $facet
* @return array
*/
public function facet_wp_filter( $args, $facet ) {
if ( is_post_type_archive( 'destination' ) ) {
$args['post_parent'] = 0;
$args['orderby'] = 'title';
$args['order'] = 'ASC';
$args['posts_per_page'] = 12;
}
return $args;
}

/**
* Filter the travel information and return a shortened version.
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/legacy/class-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct() {
}

// add_filter( 'the_terms', array( $this, 'links_new_window' ), 10, 2 );
$this->maps = Maps::get_instance();
//$this->maps = Maps::get_instance();

add_filter( 'get_the_archive_title', array( $this, 'get_the_archive_title' ), 100 );

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tour-operator",
"version": "1.4.9",
"version": "2.0.1",
"description": "Tour Operators for LSX",
"main": "gulpfile.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: lsx, tour operator, travel, tourism, itinerary
Requires at least: 6.7
Tested up to: 6.7
Requires PHP: 8.0
Stable tag: 2.0.0
Stable tag: 2.0.1
License: GPLv3 or later
License URI: https://www.gnu.org/licenses/gpl-3.0.html

Expand Down
7 changes: 5 additions & 2 deletions templates/archive-destination.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
<!-- /wp:group -->

<!-- wp:group {"metadata":{"name":"Archive Content"},"align":"wide","style":{"spacing":{"padding":{"top":"var:preset|spacing|medium","bottom":"var:preset|spacing|medium","left":"var:preset|spacing|x-small","right":"var:preset|spacing|x-small"},"margin":{"top":"0","bottom":"0"}}},"layout":{"type":"constrained"}} -->
<div class="wp-block-group alignwide" style="margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--medium);padding-right:var(--wp--preset--spacing--x-small);padding-bottom:var(--wp--preset--spacing--medium);padding-left:var(--wp--preset--spacing--x-small)"><!-- wp:query {"queryId":1,"query":{"perPage":6,"pages":"3","offset":"0","postType":"destination","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true,"parents":[]},"align":"wide","layout":{"type":"constrained","contentSize":""}} -->
<div class="wp-block-query alignwide"><!-- wp:post-template {"lock":{"move":false,"remove":false},"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|small"}},"layout":{"type":"grid","columnCount":3,"minimumColumnWidth":null}} -->
<div class="wp-block-group alignwide" style="margin-top:0;margin-bottom:0;padding-top:var(--wp--preset--spacing--medium);padding-right:var(--wp--preset--spacing--x-small);padding-bottom:var(--wp--preset--spacing--medium);padding-left:var(--wp--preset--spacing--x-small)">

<!-- wp:query {"queryId":1,"query":{"perPage":"9","pages":"3","offset":"0","postType":"tour","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":false,"parents":[]},"align":"wide","layout":{"type":"constrained","contentSize":""}} -->

<div class="wp-block-query alignwide parents-only"><!-- wp:post-template {"lock":{"move":false,"remove":false},"align":"wide","style":{"spacing":{"blockGap":"var:preset|spacing|small"}},"layout":{"type":"grid","columnCount":3,"minimumColumnWidth":null}} -->
<!-- wp:group {"metadata":{"name":"Destination Card"},"className":"is-style-shadow-sm","style":{"spacing":{"blockGap":"0px","padding":{"top":"0px","bottom":"0px","left":"0px","right":"0px"}},"border":{"radius":"8px"}},"backgroundColor":"base","layout":{"type":"constrained"}} -->
<div class="wp-block-group is-style-shadow-sm has-base-background-color has-background" style="border-radius:8px;padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px"><!-- wp:post-featured-image {"isLink":true,"aspectRatio":"3/2","linkTarget":"_blank","style":{"spacing":{"padding":{"top":"0","bottom":"0"},"margin":{"top":"0","bottom":"0"}},"border":{"radius":{"topLeft":"8px","topRight":"8px"}}}} /-->

Expand Down
10 changes: 2 additions & 8 deletions tour-operator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Showcase tours, destinations, and accommodations with digital itineraries, galleries, and integrated maps.
* Author: lightspeedwp
* Author URI: https://lightspeedwp.agency/
* Version: 2.0.0
* Version: 2.0.1
* Requires at least: 6.7
* Tested up to: 6.7
* Requires PHP: 8.0
Expand All @@ -24,17 +24,11 @@
define( 'LSX_TO_PATH', plugin_dir_path( __FILE__ ) );
define( 'LSX_TO_CORE', __FILE__ );
define( 'LSX_TO_URL', plugin_dir_url( __FILE__ ) );
define( 'LSX_TO_VER', '2.0.0' );
define( 'LSX_TO_VER', '2.0.1' );

// Post Expirator.
define( 'LSX_TO_POSTEXPIRATOR_DATEFORMAT', esc_html__( 'l F jS, Y', 'tour-operator' ) );
define( 'LSX_TO_POSTEXPIRATOR_TIMEFORMAT', esc_html__( 'g:ia', 'tour-operator' ) );

// Include bootstrapper and start plugin.
require_once LSX_TO_PATH . 'tour-operator-bootstrap.php';

// Register activation hook.
/*register_activation_hook( LSX_TO_CORE, array(
'Tour_Operator',
'register_activation_hook',
) );*/

0 comments on commit e33fc9b

Please sign in to comment.