Skip to content

Commit

Permalink
Add a taxonomy for hiding lessons (#2830)
Browse files Browse the repository at this point in the history
* Add a taxonomy for hiding lessons

* Add deprecation label to post meta setting

* Add taxonomy based visibility settings panel

* Adjust tax capabilities

* Add a new taxonomy based lesson visibility panel

* Filter the lessons by taxonomy on the frontend

* Use a pre-existing Jetpack Search tax

* Use standard taxonomy UI
  • Loading branch information
adamwoodnz authored Aug 8, 2024
1 parent 373568a commit bbe9ed4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
52 changes: 52 additions & 0 deletions wp-content/plugins/wporg-learn/inc/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function register() {
register_lesson_instruction_type();
register_lesson_level();
register_lesson_plan_series();
register_lesson_visibility();
register_workshop_series();
register_workshop_type();
register_wp_version();
Expand Down Expand Up @@ -260,6 +261,57 @@ function register_lesson_level() {
register_taxonomy( 'level', array( 'lesson-plan', 'lesson', 'course' ), $args );
}

/**
* Register the Lesson visibility taxonomy, used to hide lessons from the archive and search views.
*/
function register_lesson_visibility() {
$labels = array(
'name' => _x( 'Visibility', 'Taxonomy General Name', 'wporg-learn' ),
'singular_name' => _x( 'Visibility', 'Taxonomy Singular Name', 'wporg-learn' ),
'menu_name' => __( 'Visibility', 'wporg-learn' ),
'all_items' => __( 'All Visibilities', 'wporg-learn' ),
'parent_item' => __( 'Parent Visibility', 'wporg-learn' ),
'parent_item_colon' => __( 'Parent Visibility:', 'wporg-learn' ),
'new_item_name' => __( 'New Visibility Name', 'wporg-learn' ),
'add_new_item' => __( 'Add New Visibility', 'wporg-learn' ),
'edit_item' => __( 'Edit Visibility', 'wporg-learn' ),
'update_item' => __( 'Update Visibility', 'wporg-learn' ),
'view_item' => __( 'View Visibility', 'wporg-learn' ),
'separate_items_with_commas' => __( 'Separate visibilities with commas', 'wporg-learn' ),
'add_or_remove_items' => __( 'Add or remove visibilities', 'wporg-learn' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wporg-learn' ),
'popular_items' => __( 'Popular Visibilities', 'wporg-learn' ),
'search_items' => __( 'Search Visibilities', 'wporg-learn' ),
'not_found' => __( 'No Visibilities Found', 'wporg-learn' ),
'no_terms' => __( 'No visibilities', 'wporg-learn' ),
'items_list' => __( 'Visibilities list', 'wporg-learn' ),
'items_list_navigation' => __( 'Visibilities list navigation', 'wporg-learn' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'query_var' => 'wporg_lesson_visibility', // Prevent collisions with query params in the archive filter.
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'show_in_rest' => true,
'rewrite' => false,
'capabilities' => array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_lessons',
),
);

// Use an existing taxonomy registered with Jetpack Search
// See https://github.com/Automattic/jetpack/blob/36b2d5232e2ec3a1ad14034ab673fddce3acab97/projects/packages/sync/src/modules/class-search.php#L1479
register_taxonomy( 'show', array( 'lesson' ), $args );
}

/**
* Register the Lesson Plan Series taxonomy.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const LessonArchiveExcludedMeta = () => {
const [ lessonExcluded, setLessonFeatured ] = useState( postMetaData?._lesson_archive_excluded === EXCLUDED );

return (
<PluginDocumentSettingPanel title={ __( 'Hidden Lesson', 'wporg-learn' ) }>
<PluginDocumentSettingPanel title={ __( 'Hidden Lesson (deprecated)', 'wporg-learn' ) }>
<PanelRow>
<CheckboxControl
label={ __( 'Exclude this lesson from the archive', 'wporg-learn' ) }
Expand Down
23 changes: 20 additions & 3 deletions wp-content/themes/pub/wporg-learn-2024/inc/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ function add_excluded_to_lesson_archive_query( $query ) {
// Ensure this code runs only for the main query on lesson archive pages and search results.
if ( ! is_admin() && $query->is_main_query() && ( $query->is_archive( 'lesson' ) || $query->is_search() ) ) {
$meta_query = $query->get( 'meta_query', array() );
$tax_query = $query->get( 'tax_query', array() );

$exclude_lesson_query = array(
$exclude_lessons_by_post_meta = array(
'relation' => 'OR',
array(
'key' => '_lesson_archive_excluded',
Expand All @@ -71,13 +72,29 @@ function add_excluded_to_lesson_archive_query( $query ) {
$meta_query = array(
'relation' => 'AND',
$meta_query,
$exclude_lesson_query,
$exclude_lessons_by_post_meta,
);
} else {
$meta_query = $exclude_lesson_query;
$meta_query = $exclude_lessons_by_post_meta;
}

$exclude_lessons_by_taxonomy = array(
'taxonomy' => 'show',
'field' => 'slug',
'terms' => 'hidden',
'operator' => 'NOT IN',
);

// If there's an existing tax query, add the new condition
if ( ! empty( $tax_query ) ) {
$tax_query['relation'] = 'AND';
$tax_query[] = $exclude_lessons_by_taxonomy;
} else {
$tax_query = array( $exclude_lessons_by_taxonomy );
}

$query->set( 'meta_query', $meta_query );
$query->set( 'tax_query', $tax_query );
}
}

Expand Down

0 comments on commit bbe9ed4

Please sign in to comment.