Skip to content

Commit

Permalink
Extend Audience and Level taxonomies to Courses and Lessons (#2435)
Browse files Browse the repository at this point in the history
* Extend existing Audience and Level taxonomies

Remove new taxonomies added to 2024 theme
Remove helper functions mainly used for includes

* Apply the admin table filters to the query

* Update doc

* Make get_available_taxonomy_terms taxonomy agnostic
  • Loading branch information
adamwoodnz authored May 15, 2024
1 parent 9dd187b commit a6877da
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 338 deletions.
125 changes: 101 additions & 24 deletions wp-content/plugins/wporg-learn/inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use function WordPressdotorg\Locales\get_locales_with_english_names;
use function WordPressdotorg\Locales\get_locale_name_from_code;
use function WPOrg_Learn\Post_Meta\get_available_post_type_locales;
use function WPOrg_Learn\Taxonomy\get_available_taxonomy_terms;

defined( 'WPINC' ) || die();

Expand Down Expand Up @@ -224,45 +225,84 @@ function add_workshop_list_table_sortable_columns( $sortable_columns ) {
}

/**
* Add filtering controls for the tutorial and lesson plan list tables.
* Add filtering controls for the tutorial, lesson plan, lesson and course list tables.
*
* @param string $post_type
* @param string $which
*
* @return void
*/
function add_admin_list_table_filters( $post_type, $which ) {
if ( ( 'wporg_workshop' !== $post_type && 'lesson-plan' !== $post_type ) || 'top' !== $which ) {
if (
(
'wporg_workshop' !== $post_type &&
'lesson-plan' !== $post_type &&
'lesson' !== $post_type &&
'course' !== $post_type
)
|| 'top' !== $which
) {
return;
}

$post_status = filter_input( INPUT_GET, 'post_status', FILTER_SANITIZE_STRING );
$available_locales = get_available_post_type_locales( 'language', $post_type, $post_status );
$language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );
$audience = filter_input( INPUT_GET, 'wporg_audience', FILTER_SANITIZE_STRING );
$language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );
$level = filter_input( INPUT_GET, 'wporg_experience_level', FILTER_SANITIZE_STRING );
$post_status = filter_input( INPUT_GET, 'post_status', FILTER_SANITIZE_STRING );

$available_audiences = get_available_taxonomy_terms( 'audience', $post_type, $post_status );
$available_levels = get_available_taxonomy_terms( 'level', $post_type, $post_status );
$available_locales = get_available_post_type_locales( 'language', $post_type, $post_status );

?>
<label for="filter-by-language" class="screen-reader-text">
<?php esc_html_e( 'Filter by language', 'wporg-learn' ); ?>
</label>
<select id="filter-by-language" name="language">
<option value=""<?php selected( ! $language ); ?>><?php esc_html_e( 'Any language', 'wporg-learn' ); ?></option>
<?php foreach ( $available_locales as $code => $name ) : ?>
<option value="<?php echo esc_attr( $code ); ?>"<?php selected( $code, $language ); ?>>
<?php
printf(
'%s [%s]',
esc_html( $name ),
esc_html( $code )
);
?>
</option>
<?php endforeach; ?>
</select>

<label for="filter-by-language" class="screen-reader-text">
<?php esc_html_e( 'Filter by language', 'wporg-learn' ); ?>
</label>
<select id="filter-by-language" name="language">
<option value=""<?php selected( ! $language ); ?>><?php esc_html_e( 'Any language', 'wporg-learn' ); ?></option>
<?php foreach ( $available_locales as $code => $name ) : ?>
<option value="<?php echo esc_attr( $code ); ?>"<?php selected( $code, $language ); ?>>
<?php
printf(
'%s [%s]',
esc_html( $name ),
esc_html( $code )
);
?>
</option>
<?php endforeach; ?>
</select>

<label for="filter-by-audience" class="screen-reader-text">
<?php esc_html_e( 'Filter by audience', 'wporg-learn' ); ?>
</label>
<select id="filter-by-audience" name="wporg_audience">
<option value=""<?php selected( ! $audience ); ?>><?php esc_html_e( 'Any audience', 'wporg-learn' ); ?></option>
<?php foreach ( $available_audiences as $code => $name ) : ?>
<option value="<?php echo esc_attr( $code ); ?>"<?php selected( $code, $audience ); ?>>
<?php echo esc_html( $name ); ?>
</option>
<?php endforeach; ?>
</select>

<label for="filter-by-level" class="screen-reader-text">
<?php esc_html_e( 'Filter by level', 'wporg-learn' ); ?>
</label>
<select id="filter-by-level" name="wporg_experience_level">
<option value=""<?php selected( ! $level ); ?>><?php esc_html_e( 'Any level', 'wporg-learn' ); ?></option>
<?php foreach ( $available_levels as $code => $name ) : ?>
<option value="<?php echo esc_attr( $code ); ?>"<?php selected( $code, $level ); ?>>
<?php echo esc_html( $name ); ?>
</option>
<?php endforeach; ?>
</select>

<?php
}

/**
* Alter the query to include tutorial and lesson plan list table filters.
* Alter the query to include tutorial, lesson plan, lesson and course list table filters.
*
* @param WP_Query $query
*
Expand All @@ -279,9 +319,46 @@ function handle_admin_list_table_filters( WP_Query $query ) {
return;
}

if ( 'edit-wporg_workshop' === $current_screen->id || 'edit-lesson-plan' === $current_screen->id ) {
if (
'edit-wporg_workshop' === $current_screen->id ||
'edit-lesson-plan' === $current_screen->id ||
'edit-lesson' === $current_screen->id ||
'edit-course' === $current_screen->id
) {
$audience = filter_input( INPUT_GET, 'wporg_audience', FILTER_SANITIZE_STRING );
$language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );
$level = filter_input( INPUT_GET, 'wporg_experience_level', FILTER_SANITIZE_STRING );

// Tax queries
$tax_query = $query->get( 'tax_query', array() );

if ( $audience ) {
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'audience',
'field' => 'slug',
'terms' => $audience,
),
);
}

if ( $level ) {
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'level',
'field' => 'slug',
'terms' => $level,
),
);
}

if ( ! empty( $tax_query ) ) {
$query->set( 'tax_query', $tax_query );
}

// Meta queries
if ( $language ) {
$meta_query = $query->get( 'meta_query', array() );

Expand Down
54 changes: 50 additions & 4 deletions wp-content/plugins/wporg-learn/inc/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function register_lesson_audience() {
'items_list_navigation' => __( 'Audiences list navigation', 'wporg-learn' ),
);

$args = array(
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
Expand All @@ -77,7 +77,7 @@ function register_lesson_audience() {
),
);

register_taxonomy( 'audience', array( 'lesson-plan' ), $args );
register_taxonomy( 'audience', array( 'lesson-plan', 'lesson', 'course' ), $args );
}

/**
Expand Down Expand Up @@ -241,7 +241,7 @@ function register_lesson_level() {
'items_list_navigation' => __( 'Experience Levels list navigation', 'wporg-learn' ),
);

$args = array(
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
Expand All @@ -256,7 +256,7 @@ function register_lesson_level() {
),
);

register_taxonomy( 'level', array( 'lesson-plan' ), $args );
register_taxonomy( 'level', array( 'lesson-plan', 'lesson', 'course' ), $args );
}

/**
Expand Down Expand Up @@ -649,3 +649,49 @@ function tax_save_term_fields( $term_id ) {
rest_sanitize_boolean( $is_sticky )
);
}

/**
* Get available taxonomy terms for a post type.
*
* @param string $taxonomy The taxonomy.
* @param string $post_type The post type.
* @param string $post_status The post status.
* @return array The available taxonomy terms.
*/
function get_available_taxonomy_terms( $taxonomy, $post_type, $post_status = null ) {
$posts = get_posts( array(
'post_status' => $post_status ?? 'any',
'post_type' => $post_type,
'posts_per_page' => -1,
) );

if ( empty( $posts ) ) {
return array();
}

$term_ids = array();
foreach ( $posts as $post ) {
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

if ( ! is_wp_error( $post_terms ) ) {
$term_ids = array_merge( $term_ids, $post_terms );
}
}

if ( empty( $term_ids ) ) {
return array();
}

$term_ids = array_unique( $term_ids );

$term_objects = get_terms( array(
'taxonomy' => $taxonomy,
'include' => $term_ids,
'hide_empty' => false,
) );

return array_reduce( $term_objects, function( $terms, $term_object ) {
$terms[ $term_object->slug ] = $term_object->name;
return $terms;
}, array());
}
55 changes: 2 additions & 53 deletions wp-content/themes/pub/wporg-learn-2024/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,6 @@

namespace WordPressdotorg\Theme\Learn_2024;

/**
* Shortcut to the build directory.
*
* @return string
*/
function get_build_path() {
return get_stylesheet_directory() . '/build/';
}

/**
* Shortcut to the build URL.
*
* @return string
*/
function get_build_url() {
return get_stylesheet_directory_uri() . '/build/';
}

/**
* Shortcut to the includes directory.
*
* @return string
*/
function get_includes_path() {
return get_stylesheet_directory() . '/inc/';
}

/**
* Shortcut to the views directory.
*
* @return string
*/
function get_views_path() {
return get_stylesheet_directory() . '/views/';
}

/**
* Admin.
*/
require get_includes_path() . 'admin.php';

/**
* Capabilities.
*/
require get_includes_path() . 'capabilities.php';

/**
* Taxonomies.
*/
require get_includes_path() . 'taxonomy.php';

/**
* Actions and filters.
*/
Expand Down Expand Up @@ -111,9 +60,9 @@ function enqueue_assets() {
// stylesheet as a dependency.
wp_enqueue_style(
'wporg-learn-2024-style',
get_build_url() . 'style/style-index.css',
get_stylesheet_directory_uri() . '/build/style/style-index.css',
array( 'wporg-parent-2021-style', 'wporg-global-fonts' ),
filemtime( get_build_path() . 'style/style-index.css' )
filemtime( get_stylesheet_directory() . '/build/style/style-index.css' )
);

// Preload the heading font(s).
Expand Down
59 changes: 0 additions & 59 deletions wp-content/themes/pub/wporg-learn-2024/inc/admin.php

This file was deleted.

Loading

0 comments on commit a6877da

Please sign in to comment.