-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into logo-aria-label
- Loading branch information
Showing
13 changed files
with
728 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
jQuery(document).ready(function ($) { | ||
|
||
// Get the taxonomies and their terms from the localized script object | ||
const taxonomies = listing_page_object.taxonomies; | ||
|
||
// Function to handle changes in the parent topic dropdown | ||
function handleTopicChange(parentClass, childClass, selected_topic) { | ||
if (selected_topic > 0) { | ||
const termsWithParents = getTermsWithMatchingParents(selected_topic); | ||
|
||
// Update subtopics dropdown based on whether any matching subtopics were found | ||
if (termsWithParents.length > 0) { | ||
populateSubTopics(childClass, termsWithParents); | ||
$(childClass).prop('disabled', false); | ||
} else { | ||
resetSubTopics(childClass); | ||
} | ||
} else { | ||
resetSubTopics(childClass); | ||
} | ||
} | ||
|
||
// Function to find terms with parents matching the selected topic | ||
function getTermsWithMatchingParents(selected_topic) { | ||
const termsWithParents = []; | ||
|
||
// Loop through each taxonomy and its terms | ||
Object.keys(taxonomies).forEach(taxonomy => { | ||
if (Array.isArray(taxonomies[taxonomy])) { // Ensure the terms are in array form | ||
taxonomies[taxonomy].forEach(termData => { | ||
if (termData.parent && termData.parent == selected_topic) { | ||
termsWithParents.push(termData); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return termsWithParents; | ||
} | ||
|
||
// Populate subtopics dropdown with matching terms | ||
function populateSubTopics(childClass, termsWithParents) { | ||
resetSubTopics(childClass); | ||
|
||
termsWithParents.forEach(term => { | ||
$(childClass).append(new Option(term.name, term.term_id)); | ||
}); | ||
} | ||
|
||
// Reset the subtopics dropdown | ||
function resetSubTopics(childClass) { | ||
$(childClass).empty(); | ||
$(childClass).append(new Option("Select option", "")); | ||
$(childClass).prop('disabled', true); // Disable the dropdown by default | ||
} | ||
|
||
// Attach change event listeners to each parent topic dropdown | ||
Object.keys(taxonomies).forEach(taxonomy => { | ||
const parentClass = `#${taxonomy}-filter-topic`; | ||
const childClass = `#${taxonomy}-filter-subtopic`; | ||
|
||
// Check if the parent dropdown exists on the page | ||
if ($(parentClass).length > 0) { | ||
$(parentClass).on('change', function () { | ||
const selected_topic = this.value; | ||
handleTopicChange(parentClass, childClass, selected_topic); | ||
}); | ||
} else { | ||
console.warn(`Parent dropdown with class ${parentClass} not found.`); | ||
} | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
//Adds new labels to taxonomy | ||
add_action('acf/taxonomy/render_settings_tab/labels', function ($acf_taxonomy) { | ||
|
||
acf_render_field_wrap( | ||
array( | ||
'type' => 'text', | ||
'key' => 'listing_page_filter', | ||
'name' => 'listing_page_filter', | ||
'prefix' => 'acf_taxonomy[labels]', | ||
'value' => $acf_taxonomy['labels']['listing_page_filter'], | ||
'data' => array( | ||
/* translators: %s Singular form of taxonomy name */ | ||
'label' => __( '%s', 'acf' ), | ||
'replace' => 'singular', | ||
), | ||
'label' => __( 'Listing Page Filter', 'acf' ), | ||
'instructions' => __( 'Displayed on listing page with a taxonomy filter', 'acf' ), | ||
'placeholder' => __( 'Tag', 'acf' ), | ||
), | ||
'div', | ||
'field' | ||
); | ||
|
||
acf_render_field_wrap( | ||
array( | ||
'type' => 'text', | ||
'key' => 'listing_page_subfilter', | ||
'name' => 'listing_page_subfilter', | ||
'prefix' => 'acf_taxonomy[labels]', | ||
'value' => $acf_taxonomy['labels']['listing_page_subfilter'], | ||
'data' => array( | ||
/* translators: %s Singular form of taxonomy name */ | ||
'label' => __( 'Sub %s', 'acf' ), | ||
'replace' => 'singular', | ||
), | ||
'label' => __( 'Listing Page Subfilter', 'acf' ), | ||
'instructions' => __( 'Displayed on listing page when a taxonomy filter has sub terms', 'acf' ), | ||
'placeholder' => __( 'Subtag', 'acf' ), | ||
), | ||
'div', | ||
'field' | ||
); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
/** | ||
* Adds a filter term and its subtopic to the active filters array if they exist. | ||
* | ||
* This function checks if a given filter term and its associated subtopic term exist in the specified taxonomy. | ||
* If they do, they are added to the `$listing_active_filters` array, which tracks active filters applied to a listing. | ||
* | ||
* @param string $filter The base name of the filter (taxonomy) to check. | ||
* This string is also used to generate the subtopic filter key by appending '_subtopic'. | ||
* @param array &$listing_active_filters An array that holds active filters. This array is passed by reference | ||
* and will be updated with the filter terms that exist. | ||
* | ||
* @return void | ||
* | ||
* @example | ||
* // Example usage: | ||
* $listing_active_filters = []; | ||
* hale_add_filter_term_if_exists('genre', $listing_active_filters); | ||
* // If 'genre' and 'genre_subtopic' query variables exist and correspond to valid terms, | ||
* // they will be added to $listing_active_filters. | ||
*/ | ||
function hale_add_filter_term_if_exists($filter, &$listing_active_filters) { | ||
|
||
|
||
$taxonomy = get_taxonomy($filter); | ||
|
||
if (!$taxonomy) { | ||
return; | ||
} | ||
|
||
// Generate the subtopic filter key | ||
$filter_term_id_subtopic = $taxonomy->query_var . '_subtopic'; | ||
|
||
// Retrieve the value of the main filter and subtopic query variables | ||
$filter_term_id = get_query_var($taxonomy->query_var); | ||
$filter_term_id_subtopic_value = get_query_var($filter_term_id_subtopic); | ||
|
||
// Combine them into an associative array | ||
$filter_terms = [ | ||
'term_id' => $filter_term_id, | ||
'subtopic_term_id' => $filter_term_id_subtopic_value | ||
]; | ||
|
||
// Check if the main filter term ID is numeric and exists | ||
if (is_numeric($filter_terms['term_id'])) { | ||
$filter_terms['term_id'] = intval($filter_terms['term_id']); | ||
|
||
if (term_exists($filter_terms['term_id'], $filter)) { | ||
$listing_active_filters[] = array( | ||
'taxonomy' => $filter, | ||
'value' => $filter_terms['term_id'] | ||
); | ||
} | ||
} | ||
|
||
// Check if the subtopic term ID is numeric and exists in the main taxonomy | ||
if (is_numeric($filter_terms['subtopic_term_id'])) { | ||
$filter_terms['subtopic_term_id'] = intval($filter_terms['subtopic_term_id']); | ||
|
||
if (term_exists($filter_terms['subtopic_term_id'], $filter)) { | ||
$listing_active_filters[] = array( | ||
'taxonomy' => $filter, | ||
'value' => $filter_terms['subtopic_term_id'] | ||
); | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Retrieve term IDs for specified taxonomies. | ||
* | ||
* This function accepts an array of taxonomy names, retrieves all terms associated with each taxonomy, | ||
* and returns an associative array where the keys are the taxonomy names and the values are arrays | ||
* of term IDs belonging to those taxonomies. | ||
* | ||
* @param array $taxonomies An array of taxonomy names. Each element should be a string representing a valid taxonomy. | ||
* | ||
* @return array|null An associative array where keys are taxonomy names and values are arrays of term IDs. | ||
* Returns null if the input is not an array. | ||
* | ||
*/ | ||
function get_taxonomy_term_ids($taxonomies) { | ||
// Initialize the arrays | ||
$taxonomy_term_ids = []; | ||
$term_ids = []; | ||
|
||
if (!is_array($taxonomies)) { | ||
return; | ||
} | ||
|
||
foreach ($taxonomies as $taxonomy) { | ||
// Get terms associated with the current taxonomy | ||
$terms = get_terms([ | ||
'taxonomy' => $taxonomy, | ||
'hide_empty' => false, | ||
]); | ||
|
||
if (!empty($terms) && !is_wp_error($terms)) { | ||
foreach ($terms as $term) { | ||
// Add the term ID to the array | ||
$term_ids[] = $term->term_id; | ||
} | ||
} | ||
$taxonomy_term_ids[$taxonomy] = $term_ids; | ||
} | ||
|
||
// Return the array containing term IDs for each taxonomy | ||
return $taxonomy_term_ids; | ||
} |
Oops, something went wrong.