diff --git a/includes/model/Listings.php b/includes/model/Listings.php index d63a853cc6..3123c761c0 100644 --- a/includes/model/Listings.php +++ b/includes/model/Listings.php @@ -13,6 +13,8 @@ class Directorist_Listings { protected $thumbnails_cached = false; + protected $search_form = null; + public $query_args = []; public $query_results = []; public $options = []; @@ -1137,15 +1139,6 @@ public function render_shortcode( $atts = [] ) { Helper::add_shortcode_comment( $atts['shortcode'] ); } - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); - - $args = array( - 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( $this->type, $this->current_listing_type, $search_field_atts ), - ); - switch ( $this->sidebar ) { case 'left_sidebar': $template = 'sidebar-archive-contents'; @@ -1164,9 +1157,9 @@ public function render_shortcode( $atts = [] ) { Helper::get_template( $template, array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( $this->type, $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ), - 'listings_archive', + 'listings_archive' ); return ob_get_clean(); @@ -2142,34 +2135,29 @@ public function render_badge_template( $field ) { switch ( $field['widget_key'] ) { case 'popular_badge': + $field['class'] = 'popular'; + $field['icon'] = 'fas fa-fire'; + $field['tooltip_class'] = 'directorist-badge-tooltip__popular'; + $field['label'] = Helper::popular_badge_text(); - $field['class'] = 'popular'; - $field['icon'] = 'fas fa-fire'; - $field['tooltip_class'] = 'directorist-badge-tooltip__popular'; - $field['label'] = Helper::popular_badge_text(); - - if ( Helper::is_popular( $id ) ) { - Helper::get_template( 'archive/fields/badge', $field ); - } - - break; + if ( Helper::is_popular( $id ) ) { + Helper::get_template( 'archive/fields/badge', $field ); + } + break; case 'featured_badge': + $field['class'] = 'featured'; + $field['icon'] = 'fas fa-star'; + $field['tooltip_class'] = 'directorist-badge-tooltip__featured'; + $field['label'] = Helper::featured_badge_text(); + $field['featured_badge_type'] = get_directorist_option( 'feature_badge_type', 'icon_badge'); + + if ( Helper::is_featured( $id ) ) { + Helper::get_template( 'archive/fields/badge', apply_filters( 'directorist_featured_badge_field_data', $field ) ); + } + break; - $field['class'] = 'featured'; - $field['icon'] = 'fas fa-star'; - $field['tooltip_class'] = 'directorist-badge-tooltip__featured'; - $field['label'] = Helper::featured_badge_text(); - $field['featured_badge_type'] = get_directorist_option( 'feature_badge_type', 'icon_badge'); - - if ( Helper::is_featured( $id ) ) { - Helper::get_template( 'archive/fields/badge', apply_filters( 'directorist_featured_badge_field_data', $field ) ); - } - - break; - - case 'new_badge': - + case 'new_badge': $field['class'] = 'new'; $field['icon'] = 'fas fa-bolt'; $field['tooltip_class'] = 'directorist-badge-tooltip__new'; @@ -2180,11 +2168,8 @@ public function render_badge_template( $field ) { if ( Helper::is_new( $id ) ) { Helper::get_template( 'archive/fields/badge', $field ); } - break; - } - } public function listing_wrapper_class() { @@ -2199,17 +2184,17 @@ public function grid_container_fluid() { public function sidebar_class() { $class = 'no-sidebar-contents'; - if ( $this->sidebar ) { - - switch ( $this->sidebar ) { - case 'left_sidebar': - $class = 'left-sidebar-contents'; - break; - case 'right_sidebar': - $class = 'right-sidebar-contents'; - break; - } + if ( ! $this->sidebar ) { + return $class; + } + switch ( $this->sidebar ) { + case 'left_sidebar': + $class = 'left-sidebar-contents'; + break; + case 'right_sidebar': + $class = 'right-sidebar-contents'; + break; } return $class; @@ -2244,41 +2229,36 @@ public function display_search_button() { } public function search_form_template() { - // only catch atts with the prefix 'filter_' - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); - $args = array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( $this->type, $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ); Helper::get_template( 'archive/search-form', $args ); } public function basic_search_form_template() { - // only catch atts with the prefix 'filter_' - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); + if ( ! $this->get_search_form()->have_basic_fields() ) { + return; + } $args = array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( 'search_result', $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ); + Helper::get_template( 'archive/basic-search-form', $args ); } public function advance_search_form_template() { - // only catch atts with the prefix 'filter_' - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); + if ( ! $this->get_search_form()->have_advance_fields() ) { + return; + } $args = array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( 'search_result', $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ); + Helper::get_template( 'archive/advance-search-form', $args ); } @@ -2292,14 +2272,9 @@ public function filter_btn_html() { } public function mobile_view_filter_template() { - // only catch atts with the prefix 'filter_' - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); - $args = array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( $this->type, $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ); Helper::get_template( 'archive/mobile-search-form', $args ); } @@ -2326,14 +2301,9 @@ public function listings_header_title() { } public function full_search_form_template() { - // only catch atts with the prefix 'filter_' - $search_field_atts = array_filter( $this->atts, function( $key ) { - return substr( $key, 0, 7 ) == 'filter_'; - }, ARRAY_FILTER_USE_KEY ); - $args = array( 'listings' => $this, - 'searchform' => new Directorist_Listing_Search_Form( $this->type, $this->current_listing_type, $search_field_atts ), + 'searchform' => $this->get_search_form(), ); Helper::get_template( 'archive/search-form', $args ); @@ -2470,4 +2440,38 @@ public static function mark_as_favourite_button() { _deprecated_function( __METHOD__, '7.3.1' ); } -} \ No newline at end of file + public function should_display_basic_search_form() { + return ( ! $this->hide_top_search_bar_on_sidebar_layout() && $this->get_search_form()->have_basic_fields() ); + } + + public function should_display_advance_search_form() { + return $this->get_search_form()->have_advance_fields(); + } + + protected function get_filter_attributes() { + return array_filter( + $this->atts, + function( $key ) { + return substr( $key, 0, 7 ) === 'filter_'; + }, + ARRAY_FILTER_USE_KEY + ); + } + + public function get_search_form() { + if ( is_null( $this->search_form ) ) { + $this->search_form = new Directorist_Listing_Search_Form( + $this->type, + $this->current_listing_type, + $this->get_filter_attributes() + ); + } + + return $this->search_form; + } + + public function wrapper_html_attributes() { + $this->wrapper_class(); + $this->data_atts(); + } +} diff --git a/includes/model/SearchForm.php b/includes/model/SearchForm.php index ee7a4dbff9..43755bd813 100644 --- a/includes/model/SearchForm.php +++ b/includes/model/SearchForm.php @@ -753,4 +753,26 @@ public function load_radius_search_scripts( $data ) { public function load_map_scripts() { _deprecated_function( __METHOD__, '7.3' ); } + + public function have_basic_fields() { + return ! empty( $this->get_basic_fields() ); + } + + public function have_advance_fields() { + return ! empty( $this->get_advance_fields() ); + } + + public function render_basic_search_fields() { + foreach ( $this->get_basic_fields() as $field ) { + $this->field_template( $field ); + } + } + + public function render_advance_search_fields() { + foreach ( $this->get_advance_fields() as $field ) : ?> +
+ field_template( $field ); ?> +
+ have_advance_fields() ) { + return; +} + +$title = $listings->options['sidebar_filter_text'] ?? __( 'Filters', 'directorist' ); +$reset_button_label = $searchform->options['reset_sidebar_filters_text'] ?? esc_html__( 'Clear All', 'directorist' ); ?> \ No newline at end of file + diff --git a/templates/archive/basic-search-form.php b/templates/archive/basic-search-form.php index 68e4c952a9..8bad398c03 100644 --- a/templates/archive/basic-search-form.php +++ b/templates/archive/basic-search-form.php @@ -2,40 +2,34 @@ /** * @author wpWax * @since 7.2.2 - * @version 8.0 + * @version 8.4.1 */ +if ( ! defined( 'ABSPATH' ) ) { + exit; +} -use \Directorist\Helper; - -if ( ! defined( 'ABSPATH' ) ) exit; +if ( ! $searchform->have_basic_fields() ) { + return; +} ?> - \ No newline at end of file + diff --git a/templates/sidebar-archive-contents.php b/templates/sidebar-archive-contents.php index f1f6cd6499..16f6c2c691 100644 --- a/templates/sidebar-archive-contents.php +++ b/templates/sidebar-archive-contents.php @@ -2,58 +2,46 @@ /** * @author wpWax * @since 6.6 - * @version 8.2 + * @version 8.4.1 */ -if ( ! defined( 'ABSPATH' ) ) exit; -?> +if ( ! defined( 'ABSPATH' ) ) { + exit; +} -
wrapper_class(); $listings->data_atts(); ?>> +?> +
wrapper_html_attributes(); ?>>
- directory_type_nav_template(); - ?> + directory_type_nav_template(); ?>
- hide_top_search_bar_on_sidebar_layout() ) : ?> - + should_display_basic_search_form() ) : ?>
- basic_search_form_template(); - ?> + basic_search_form_template(); ?>
- - header ) : ?> - + header ) : ?>
- header_bar_template(); - ?> + header_bar_template(); ?>
-
- form_data[1]['fields'] ) && ! empty( $searchform->form_data[1]['fields'] ) ) : ?> + should_display_advance_search_form() ) : ?> +
- archive_view_template(); - ?> + archive_view_template(); ?>
- -
\ No newline at end of file +