Skip to content

Commit

Permalink
Remove post meta for excluding lessons from archive and search (#2845)
Browse files Browse the repository at this point in the history
* Remove post meta for excluding lessons from archive and search

This has been replaced with a taxonomy for compatibility with Jetpack classic search.

See #2735

* Update naming and doc
  • Loading branch information
adamwoodnz authored Aug 19, 2024
1 parent efc2415 commit 3128a62
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 113 deletions.
41 changes: 0 additions & 41 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,6 @@ function register_lesson_meta() {
},
),
);

register_post_meta(
'lesson',
'_lesson_archive_excluded',
array(
'description' => __( 'Whether the lesson should be excluded from archive views.', 'wporg-learn' ),
'type' => 'string',
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
'show_in_rest' => true,
'auth_callback' => function( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
},
),
);
}

/**
Expand Down Expand Up @@ -639,7 +624,6 @@ function render_locales_list() {
function enqueue_editor_assets() {
enqueue_expiration_date_assets();
enqueue_language_meta_assets();
enqueue_lesson_archive_excluded_meta_assets();
enqueue_lesson_featured_meta_assets();
enqueue_duration_meta_assets();
}
Expand Down Expand Up @@ -697,31 +681,6 @@ function enqueue_language_meta_assets() {
}
}

/**
* Enqueue scripts for the archive excluded lesson meta block.
*/
function enqueue_lesson_archive_excluded_meta_assets() {
global $typenow;

if ( 'lesson' === $typenow ) {
$script_asset_path = get_build_path() . 'lesson-archive-excluded-meta.asset.php';
if ( ! file_exists( $script_asset_path ) ) {
wp_die( 'You need to run `yarn start` or `yarn build` to build the required assets.' );
}

$script_asset = require( $script_asset_path );
wp_enqueue_script(
'wporg-learn-lesson-archive-excluded-meta',
get_build_url() . 'lesson-archive-excluded-meta.js',
$script_asset['dependencies'],
$script_asset['version'],
true
);

wp_set_script_translations( 'wporg-learn-lesson-archive-excluded-meta', 'wporg-learn' );
}
}

/**
* Enqueue scripts for the featured lesson meta block.
*/
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion wp-content/plugins/wporg-learn/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ config.entry = {
'course-status': './js/course-status/src/index.js',
'duration-meta': './js/duration-meta/index.js',
'expiration-date': './js/expiration-date/index.js',
'lesson-archive-excluded-meta': './js/lesson-archive-excluded-meta/index.js',
'lesson-count': './js/lesson-count/src/index.js',
'lesson-featured-meta': './js/lesson-featured-meta/index.js',
'workshop-application-form': './js/workshop-application-form/src/index.js',
Expand Down
32 changes: 3 additions & 29 deletions wp-content/themes/pub/wporg-learn-2024/inc/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace WordPressdotorg\Theme\Learn_2024\Query;

add_action( 'pre_get_posts', __NAMESPACE__ . '\add_language_to_archive_queries' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\add_excluded_to_lesson_archive_query' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_hidden_lessons_from_archive_and_search' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_search_queries_by_post_type' );
add_filter( 'request', __NAMESPACE__ . '\handle_all_level_query' );
add_filter( 'jetpack_search_es_wp_query_args', __NAMESPACE__ . '\filter_jetpack_wp_search_query', 10, 2 );
Expand Down Expand Up @@ -46,40 +46,15 @@ function add_language_to_archive_queries( $query ) {
}

/**
* Modify the query by adding meta query for excluding the lesson from the archive if set.
* Modify lessons archive and search queries by adding a taxonomy query for filtering out hidden lessons.
*
* @param WP_Query $query The query object.
*/
function add_excluded_to_lesson_archive_query( $query ) {
function filter_hidden_lessons_from_archive_and_search( $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_lessons_by_post_meta = array(
'relation' => 'OR',
array(
'key' => '_lesson_archive_excluded',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_lesson_archive_excluded',
'value' => 'excluded',
'compare' => '!=',
),
);

// If there's an existing meta query, wrap both in an AND relation
if ( ! empty( $meta_query ) ) {
$meta_query = array(
'relation' => 'AND',
$meta_query,
$exclude_lessons_by_post_meta,
);
} else {
$meta_query = $exclude_lessons_by_post_meta;
}

$exclude_lessons_by_taxonomy = array(
'taxonomy' => 'show',
'field' => 'slug',
Expand All @@ -95,7 +70,6 @@ function add_excluded_to_lesson_archive_query( $query ) {
$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 3128a62

Please sign in to comment.