From a6877da0b7b68ebd9654bf97399e0ca45b3d35cb Mon Sep 17 00:00:00 2001
From: Adam Wood <1017872+adamwoodnz@users.noreply.github.com>
Date: Wed, 15 May 2024 12:48:58 +1200
Subject: [PATCH] Extend Audience and Level taxonomies to Courses and Lessons
(#2435)
* 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
---
wp-content/plugins/wporg-learn/inc/admin.php | 125 +++++++++++---
.../plugins/wporg-learn/inc/taxonomy.php | 54 +++++-
.../themes/pub/wporg-learn-2024/functions.php | 55 +-----
.../themes/pub/wporg-learn-2024/inc/admin.php | 59 -------
.../pub/wporg-learn-2024/inc/capabilities.php | 42 -----
.../pub/wporg-learn-2024/inc/taxonomy.php | 156 ------------------
6 files changed, 153 insertions(+), 338 deletions(-)
delete mode 100644 wp-content/themes/pub/wporg-learn-2024/inc/admin.php
delete mode 100644 wp-content/themes/pub/wporg-learn-2024/inc/capabilities.php
delete mode 100644 wp-content/themes/pub/wporg-learn-2024/inc/taxonomy.php
diff --git a/wp-content/plugins/wporg-learn/inc/admin.php b/wp-content/plugins/wporg-learn/inc/admin.php
index 168f02940..a631ed2f6 100644
--- a/wp-content/plugins/wporg-learn/inc/admin.php
+++ b/wp-content/plugins/wporg-learn/inc/admin.php
@@ -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();
@@ -224,7 +225,7 @@ 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
@@ -232,37 +233,76 @@ function add_workshop_list_table_sortable_columns( $sortable_columns ) {
* @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 );
?>
-
-
+
+
+
+
+
+
+
+
+
+
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() );
diff --git a/wp-content/plugins/wporg-learn/inc/taxonomy.php b/wp-content/plugins/wporg-learn/inc/taxonomy.php
index de121dc64..d14697548 100644
--- a/wp-content/plugins/wporg-learn/inc/taxonomy.php
+++ b/wp-content/plugins/wporg-learn/inc/taxonomy.php
@@ -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,
@@ -77,7 +77,7 @@ function register_lesson_audience() {
),
);
- register_taxonomy( 'audience', array( 'lesson-plan' ), $args );
+ register_taxonomy( 'audience', array( 'lesson-plan', 'lesson', 'course' ), $args );
}
/**
@@ -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,
@@ -256,7 +256,7 @@ function register_lesson_level() {
),
);
- register_taxonomy( 'level', array( 'lesson-plan' ), $args );
+ register_taxonomy( 'level', array( 'lesson-plan', 'lesson', 'course' ), $args );
}
/**
@@ -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());
+}
diff --git a/wp-content/themes/pub/wporg-learn-2024/functions.php b/wp-content/themes/pub/wporg-learn-2024/functions.php
index 07810c9ec..c325d0c72 100644
--- a/wp-content/themes/pub/wporg-learn-2024/functions.php
+++ b/wp-content/themes/pub/wporg-learn-2024/functions.php
@@ -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.
*/
@@ -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).
diff --git a/wp-content/themes/pub/wporg-learn-2024/inc/admin.php b/wp-content/themes/pub/wporg-learn-2024/inc/admin.php
deleted file mode 100644
index 4a2c92987..000000000
--- a/wp-content/themes/pub/wporg-learn-2024/inc/admin.php
+++ /dev/null
@@ -1,59 +0,0 @@
-
-
-
-
-
-
-
-
- cap->edit_posts ) ) {
- $required_caps[] = $object->cap->edit_posts;
- break 2; // Breaks out of the foreach and the switch.
- }
- }
-
- $required_caps[] = 'do_not_allow';
- break;
- }
-
- return $required_caps;
-}
diff --git a/wp-content/themes/pub/wporg-learn-2024/inc/taxonomy.php b/wp-content/themes/pub/wporg-learn-2024/inc/taxonomy.php
deleted file mode 100644
index 862889273..000000000
--- a/wp-content/themes/pub/wporg-learn-2024/inc/taxonomy.php
+++ /dev/null
@@ -1,156 +0,0 @@
- _x( 'Audiences', 'Taxonomy General Name', 'wporg-learn' ),
- 'singular_name' => _x( 'Audience', 'Taxonomy Singular Name', 'wporg-learn' ),
- 'menu_name' => __( 'Audience', 'wporg-learn' ),
- 'all_items' => __( 'All audiences', 'wporg-learn' ),
- 'parent_item' => __( 'Parent audience', 'wporg-learn' ),
- 'parent_item_colon' => __( 'Parent audience:', 'wporg-learn' ),
- 'new_item_name' => __( 'New audience Name', 'wporg-learn' ),
- 'add_new_item' => __( 'Add New audience', 'wporg-learn' ),
- 'edit_item' => __( 'Edit audience', 'wporg-learn' ),
- 'update_item' => __( 'Update audience', 'wporg-learn' ),
- 'view_item' => __( 'View audience', 'wporg-learn' ),
- 'separate_items_with_commas' => __( 'Separate audiences with commas', 'wporg-learn' ),
- 'add_or_remove_items' => __( 'Add or remove audiences', 'wporg-learn' ),
- 'choose_from_most_used' => __( 'Choose from the most used', 'wporg-learn' ),
- 'popular_items' => __( 'Popular audiences', 'wporg-learn' ),
- 'search_items' => __( 'Search audiences', 'wporg-learn' ),
- 'not_found' => __( 'No audience found', 'wporg-learn' ),
- 'no_terms' => __( 'No audiences', 'wporg-learn' ),
- 'items_list' => __( 'Audiences list', 'wporg-learn' ),
- 'items_list_navigation' => __( 'Audiences list navigation', 'wporg-learn' ),
- );
-
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => false,
- 'public' => true,
- 'query_var' => 'wporg_audience', // Prevent collisions with query params in the archive
- 'show_ui' => true,
- 'show_admin_column' => true,
- 'show_in_nav_menus' => true,
- 'show_tagcloud' => false,
- 'show_in_rest' => true,
- 'capabilities' => array(
- 'assign_terms' => 'edit_any_learn_content', // See WordPressdotorg\Theme\Learn_2024\Capabilities\map_meta_caps
- ),
- );
-
- register_taxonomy( 'audience', array( 'lesson', 'course' ), $args );
-}
-
-/**
- * Register the Experience Level taxonomy.
- */
-function register_experience_level() {
- $labels = array(
- 'name' => _x( 'Experience Levels', 'Taxonomy General Name', 'wporg-learn' ),
- 'singular_name' => _x( 'Experience Level', 'Taxonomy Singular Name', 'wporg-learn' ),
- 'menu_name' => __( 'Experience level', 'wporg-learn' ),
- 'all_items' => __( 'All experience levels', 'wporg-learn' ),
- 'parent_item' => __( 'Parent experience level', 'wporg-learn' ),
- 'parent_item_colon' => __( 'Parent experience level:', 'wporg-learn' ),
- 'new_item_name' => __( 'New experience level Name', 'wporg-learn' ),
- 'add_new_item' => __( 'Add New experience level', 'wporg-learn' ),
- 'edit_item' => __( 'Edit experience level', 'wporg-learn' ),
- 'update_item' => __( 'Update experience level', 'wporg-learn' ),
- 'view_item' => __( 'View experience level', 'wporg-learn' ),
- 'separate_items_with_commas' => __( 'Separate experience levels with commas', 'wporg-learn' ),
- 'add_or_remove_items' => __( 'Add or remove experience levels', 'wporg-learn' ),
- 'choose_from_most_used' => __( 'Choose from the most used', 'wporg-learn' ),
- 'popular_items' => __( 'Popular experience levels', 'wporg-learn' ),
- 'search_items' => __( 'Search experience levels', 'wporg-learn' ),
- 'not_found' => __( 'No experience level found', 'wporg-learn' ),
- 'no_terms' => __( 'No experience levels', 'wporg-learn' ),
- 'items_list' => __( 'Experience levels list', 'wporg-learn' ),
- 'items_list_navigation' => __( 'Experience levels list navigation', 'wporg-learn' ),
- );
-
- $args = array(
- 'labels' => $labels,
- 'hierarchical' => false,
- 'public' => true,
- 'query_var' => 'wporg_experience_level', // Prevent collisions with query params in the archive
- 'show_ui' => true,
- 'show_admin_column' => true,
- 'show_in_nav_menus' => true,
- 'show_tagcloud' => false,
- 'show_in_rest' => true,
- 'capabilities' => array(
- 'assign_terms' => 'edit_any_learn_content', // See WordPressdotorg\Theme\Learn_2024\Capabilities\map_meta_caps
- ),
- );
-
- register_taxonomy( 'level', array( 'lesson', 'course' ), $args );
-}
-
-/**
- * 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 );
-
- $terms = get_terms( array(
- 'taxonomy' => $taxonomy,
- 'include' => $term_ids,
- 'hide_empty' => false,
- ) );
-
- $levels = array();
- foreach ( $terms as $term ) {
- $levels[ $term->slug ] = $term->name;
- }
-
- return $levels;
-}