Helper = ( function_exists( 'AD' ) && is_object( AD() ) && isset( AD()->Helper ) ) ? AD()->Helper : null; parent::__construct( 'adqs_locations', // Base ID esc_html__( 'AD : Locations', 'adirectory' ), // Name [ 'description' => esc_html__( 'You can display listings locations.', 'adirectory' ) ] // Args ); } /** * Widget Merkup and display * * @param /args and instance */ public function widget( $w_args, $instance ) { wp_enqueue_style( 'adqs_main' ); wp_enqueue_style( 'tabler' ); $has_children = $instance['has_children'] ?? ''; $see_more = $instance['see_more'] ?? ''; $order_by = $instance['order_by'] ?? false; $order = $instance['order'] ?? false; $display_number = isset( $instance['display_number'] ) ? (int) $instance['display_number'] : -1; $widget_id = $this->id_base; static $tax_w_id = 1; // Store instance for use in helper methods $this->w_instance = $instance; echo wp_kses_post( $w_args['before_widget'] ); if ( $widget_id === 'adqs_locations' ) { $options = [ 'has_input' => false, 'has_children' => false, // 'display_count' => false, ]; if ( $has_children == '1' ) { $options = [ 'has_children' => true, ]; } // Determine current directory type automatically $current_directory_id = $this->get_current_directory(); if ( $current_directory_id ) { $options['has_directory'] = true; $options['directory_id'] = $current_directory_id; } // Get the location taxonomy for the current directory $location_taxonomy = ''; if ( $current_directory_id ) { $location_taxonomy = $this->get_location_taxonomy_for_directory( $current_directory_id ); } // Fallback: try to get from current context if ( empty( $location_taxonomy ) ) { $location_taxonomy = $this->get_location_taxonomy_from_context(); } // Final fallback: if we still don't have a valid taxonomy, try to get any available location taxonomy if ( empty( $location_taxonomy ) ) { $directory_types = adqs_get_directories(); if ( ! empty( $directory_types ) && ! is_wp_error( $directory_types ) ) { foreach ( $directory_types as $dir_type ) { $post_type = adqs_get_post_type_by_directory( $dir_type->term_id ); if ( $post_type ) { $fallback_taxonomy = adqs_get_taxonomy_for_post_type( $post_type, 'locations' ); if ( $fallback_taxonomy && taxonomy_exists( $fallback_taxonomy ) ) { $location_taxonomy = $fallback_taxonomy; break; } } } } } // If we still don't have a taxonomy, return empty if ( empty( $location_taxonomy ) || ! taxonomy_exists( $location_taxonomy ) ) { return ''; } $args = [ 'taxonomy' => $location_taxonomy, 'hide_empty' => true, 'order' => $order, ]; if ( ! empty( $order_by ) ) { $args['orderby'] = $order_by; } if ( ! empty( $display_number ) && ( $see_more !== '1' ) ) { $args['number'] = $display_number; } if ( $see_more === '1' ) : ?> '; // Get title with fallback to default $title = isset( $instance['title'] ) && ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Locations', 'adirectory' ); // If title is "1", use default if ( $title === '1' ) { $title = esc_html__( 'Locations', 'adirectory' ); } if ( ! empty( $title ) ) { echo wp_kses_post( $w_args['before_title'] . apply_filters( 'widget_title', $title ) . $w_args['after_title'] ); } echo '
'; echo ''; echo '
'; if ( $see_more === '1' ) { echo '' . esc_html__( 'Show More +', 'adirectory' ) . ''; } echo ''; } echo wp_kses_post( $w_args['after_widget'] ); ++$tax_w_id; } /** * Get current directory type * * @return int|false Directory term ID or false if not found */ private function get_current_directory() { // Check if we're on a specific directory type page if ( is_singular() && adqs_is_directory_post_type( get_post_type() ) ) { return adqs_get_directory_id_by_post_type( get_post_type() ); } // Check if we're on a taxonomy archive if ( is_tax() ) { $queried_object = get_queried_object(); if ( $queried_object && isset( $queried_object->taxonomy ) ) { // Try to find which directory this taxonomy belongs to $taxonomy_obj = get_taxonomy( $queried_object->taxonomy ); if ( $taxonomy_obj && ! empty( $taxonomy_obj->object_type ) ) { $post_type = $taxonomy_obj->object_type[0]; if ( adqs_is_directory_post_type( $post_type ) ) { return adqs_get_directory_id_by_post_type( $post_type ); } } } } // Check URL parameters for directory type if ( ! empty( $_GET['directory_type'] ) ) { $directory_type = sanitize_text_field( $_GET['directory_type'] ); if ( is_numeric( $directory_type ) ) { return absint( $directory_type ); } } // With shared taxonomies, we can no longer infer directory from URL patterns // Check if widget has a default directory set if ( ! empty( $this->w_instance['default_directory'] ) ) { return absint( $this->w_instance['default_directory'] ); } // Return default (first) directory if no current directory found return $this->get_default_directory(); } /** * Get default directory (first available) * * @return int|false Directory term ID or false if not found */ private function get_default_directory() { $directory_types = adqs_get_directories(); if ( ! empty( $directory_types ) && ! is_wp_error( $directory_types ) ) { return $directory_types[0]->term_id; } return false; } /** * Get location taxonomy for a directory * * @param int $directory_id Directory term ID * @return string|false Taxonomy name or false if not found */ private function get_location_taxonomy_for_directory( $directory_id ) { if ( ! $directory_id ) { return false; } // Get post type from directory type $post_type = adqs_get_post_type_by_directory( $directory_id ); if ( $post_type ) { return adqs_get_taxonomy_for_post_type( $post_type, 'locations' ); } return false; } /** * Get location taxonomy from current context * * @return string|false Taxonomy name or false if not found */ private function get_location_taxonomy_from_context() { // Try to get from current post type $current_post_type = get_post_type(); if ( empty( $current_post_type ) ) { // Try to get from global post if ( ! empty( $GLOBALS['post'] ) ) { $current_post_type = $GLOBALS['post']->post_type; } // Try to get from query vars elseif ( ! empty( $GLOBALS['wp_query']->query_vars['post_type'] ) ) { $current_post_type = $GLOBALS['wp_query']->query_vars['post_type']; } // If we're on a taxonomy archive, try to determine post type from taxonomy elseif ( is_tax() ) { $queried_object = get_queried_object(); if ( $queried_object && isset( $queried_object->taxonomy ) ) { $taxonomy_obj = get_taxonomy( $queried_object->taxonomy ); if ( $taxonomy_obj && ! empty( $taxonomy_obj->object_type ) ) { $current_post_type = $taxonomy_obj->object_type[0]; } } } } if ( $current_post_type ) { return adqs_get_taxonomy_for_post_type( $current_post_type, 'locations' ); } return false; } /** * form input fields * * @param /instance field data * @return void */ public function form( $instance ) { $title = isset( $instance['title'] ) ? $instance['title'] : ''; $has_children = isset( $instance['has_children'] ) ? $instance['has_children'] : '0'; $see_more = isset( $instance['see_more'] ) ? $instance['see_more'] : '1'; $order_by = isset( $instance['order_by'] ) ? $instance['order_by'] : ''; $order = isset( $instance['order'] ) ? $instance['order'] : ''; $display_number = isset( $instance['display_number'] ) ? $instance['display_number'] : ''; $default_directory = isset( $instance['default_directory'] ) ? $instance['default_directory'] : ''; ?>