Skip to content

Commit

Permalink
Merge branch 'main' into logo-aria-label
Browse files Browse the repository at this point in the history
  • Loading branch information
brown-a2 authored Sep 13, 2024
2 parents cc0f241 + 747408a commit f4569e0
Show file tree
Hide file tree
Showing 13 changed files with 728 additions and 282 deletions.
73 changes: 73 additions & 0 deletions assets/js/page-listing.js
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.`);
}
});

});
16 changes: 10 additions & 6 deletions assets/scss/listing.scss
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
.page-template-page-listing {

.page-template-page-listing
{

#primary {
#primary {
.listing-search-section {

.listing-search-form {

p {
font-weight: bold;
margin-top: 20px;
Expand All @@ -20,8 +18,14 @@
width: 100%;
min-width: 100%;
}

.filter-subtopic {
width: 100%;
min-width: 100%;
}
}
}

.listing-item-count {
border-top: 1px solid $govuk-border-colour;
padding-bottom: 16px;
Expand All @@ -42,7 +46,7 @@
.published-date {
font-weight: normal;
}

}

.list-item-detail {
Expand Down
22 changes: 20 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function hale_scripts() {
if (!$browser_is_IE) wp_enqueue_style('hale-custom-branding', hale_mix_asset('/css/custom-branding.min.css'));

$t=time();

if (is_customize_preview()) {
$css_file_name = "/temp-colours.css?t=$t";
} else {
Expand Down Expand Up @@ -236,13 +237,24 @@ function hale_scripts() {

wp_enqueue_script('news-archive' );
}
if (is_singular() && comments_open() && get_option('thread_comments')) {
wp_enqueue_script('comment-reply');

// Load Listing template JS
if ( is_page_template('page-listing.php') ) {
$script_path = get_template_directory() . '/dist/js/page-listing.js';
$script_version = file_exists($script_path) ? filemtime($script_path) : false;
wp_register_script('page-listing', hale_mix_asset('/js/page-listing.js'), array(), $script_version, true);
wp_enqueue_script('page-listing');
}
}

add_action('wp_enqueue_scripts', 'hale_scripts');

/**
* Enqueue listing template JS and
* localize the script with data
*/
require get_template_directory() . '/inc/listing-template/localize-script-data.php';

/**
* Dequeue nuisance plugins and their scripts.
*
Expand Down Expand Up @@ -381,6 +393,7 @@ function hale_mix_asset($filename)
// Admin changes
require get_template_directory() . '/inc/acf/admin/settings.php';
require get_template_directory() . '/inc/acf/admin/post-display-settings.php';
require get_template_directory() . '/inc/acf/admin/taxonomy-settings.php';

// General utilities
require get_template_directory() . '/inc/acf/utilities.php';
Expand Down Expand Up @@ -547,3 +560,8 @@ function hale_add_module_tag( $tag, $handle, $src ) {
* Add options for lang attribute for footer menu links
*/
require get_template_directory() . '/inc/footer-language-attributes.php';

/**
* Utility functions to help with various tasks
*/
require get_template_directory() . '/inc/helper-functions.php';
46 changes: 46 additions & 0 deletions inc/acf/admin/taxonomy-settings.php
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'
);
});

44 changes: 43 additions & 1 deletion inc/flexible-cpts.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ function hale_add_taxonomy_acf_field($tax) {
)
);

//Hide sub terms
add_filter('acf/fields/taxonomy/query/name=restrict_by_' . $tax->name, 'restrict_taxonomy_terms_to_top', 10, 3);

}

function restrict_taxonomy_terms_to_top( $args, $field, $post_id ) {

// get only top level terms
$args['parent'] = 0;

return $args;
}

/**
* Adds an Dropdown (select) ACF Field for a specific post type. Which lists acf fields and taxonomies assigned to a post type. Currently only on listing page template.
Expand Down Expand Up @@ -205,7 +214,6 @@ function hale_add_custom_fields_select_acf_field($post_type, $field_key, $field_
}
}

//var_dump($choices);
acf_add_local_field(array(
'key' => 'field_' . $field_key,
'label' => $field_label,
Expand Down Expand Up @@ -256,6 +264,39 @@ function hale_flexible_post_types_add_query_vars_filter($vars)

add_filter('query_vars', 'hale_flexible_post_types_add_query_vars_filter');

/**
* Registers custom query variables for all public taxonomies.
*
* This function adds custom query variables for each public taxonomy and its corresponding
* subtopic to the list of recognized query variables.
*
* @param array $vars The existing array of query variables.
* @return array Modified array of query variables with added custom taxonomy and subtopic query variables.
*
* @example
* // If you have a taxonomy called 'genre', this function will register:
* // - 'genre' as the main query variable for the taxonomy.
* // - 'genre_subtopic' as the query variable for subtopics within the 'genre' taxonomy.
*
* @hooked 'query_vars' - The WordPress filter that allows customization of query variables.
*/
function hale_add_custom_query_vars_filter($vars) {

$taxonomies = get_taxonomies(array('public' => true), 'objects');

foreach ($taxonomies as $taxonomy) {
// Register the main taxonomy query var
$vars[] = $taxonomy->query_var;

// Register the subtopic query var
$vars[] = $taxonomy->query_var . '_subtopic';
}

return $vars;
}

add_filter('query_vars', 'hale_add_custom_query_vars_filter');

//Makes sure Flexible CPTS are on category and tag archive pages
function hale_flexible_post_types_pre_get_posts($query) {

Expand All @@ -280,3 +321,4 @@ function hale_flexible_post_types_pre_get_posts($query) {
return $query;
}
add_filter('pre_get_posts', 'hale_flexible_post_types_pre_get_posts');

110 changes: 110 additions & 0 deletions inc/helper-functions.php
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;
}
Loading

0 comments on commit f4569e0

Please sign in to comment.