Skip to content

Commit

Permalink
Switch to archive based search (#2574)
Browse files Browse the repository at this point in the history
* Refactor query filter options

Rename keys and functions

* Pass search query with filter params

* Add filters to search template

* Add filter info to search results context

* Get all available languages for search language filter

* Add a post type filter to the search template

Enables clearing the post type if set from searching on an archive

* Use native labels for language options

* Show a header for Course and Lesson searches

* Fix post type filtering for 'all'

* Switch to archive based search

* Allow lesson plans to be searched

* Set the title of the Lesson plan archive

* Remove search post type limits

* Update filter option docs

* Fix archive language fetching
  • Loading branch information
adamwoodnz authored Jun 26, 2024
1 parent 30e5328 commit 66fe29e
Show file tree
Hide file tree
Showing 15 changed files with 419 additions and 333 deletions.
24 changes: 16 additions & 8 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,23 @@ function get_available_post_type_locales( $meta_key, $post_type, $post_status, $
$and_post_status = "AND posts.post_status = '$post_status'";
}

$and_post_type = '';
if ( isset( $post_type ) ) {
$public_post_types = get_post_types( array( 'public' => true ), 'names' );

if ( in_array( $post_type, $public_post_types ) ) {
$and_post_type = "AND posts.post_type = '$post_type'";
}
}

$results = $wpdb->get_col( $wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $and_post_status only includes $post_status if it matches an allowed string.
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id AND posts.post_type = %s $and_post_status
WHERE postmeta.meta_key = %s
",
$post_type,
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $and_post_status and $and_post_type only include $post_status and $post_type if they match an allowed string.
"SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id
$and_post_type
$and_post_status
WHERE postmeta.meta_key = %s",
$meta_key
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
) );
Expand Down
97 changes: 73 additions & 24 deletions wp-content/themes/pub/wporg-learn-2024/inc/block-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
use function WPOrg_Learn\Post_Meta\{get_available_post_type_locales};

add_filter( 'wporg_query_filter_options_language', __NAMESPACE__ . '\get_language_options' );
add_filter( 'wporg_query_filter_options_archive_language', __NAMESPACE__ . '\get_language_options_by_post_type' );

add_filter( 'wporg_query_filter_options_level', __NAMESPACE__ . '\get_level_options' );
add_filter( 'wporg_query_filter_options_taxonomy-level', __NAMESPACE__ . '\get_taxonomy_level_options' );
add_filter( 'wporg_query_filter_options_learning-pathway-level', __NAMESPACE__ . '\get_learning_pathway_level_options' );
add_filter( 'wporg_query_filter_options_archive_level', __NAMESPACE__ . '\get_level_options_by_post_type' );
add_filter( 'wporg_query_filter_options_learning_pathway_level', __NAMESPACE__ . '\get_learning_pathway_level_options' );

add_filter( 'wporg_query_filter_options_topic', __NAMESPACE__ . '\get_topic_options' );
add_filter( 'wporg_query_filter_options_taxonomy-topic', __NAMESPACE__ . '\get_taxonomy_topic_options' );
add_filter( 'wporg_query_filter_options_learning-pathway-topic', __NAMESPACE__ . '\get_learning_pathway_topic_options' );
add_filter( 'wporg_query_filter_options_archive_topic', __NAMESPACE__ . '\get_topic_options_by_post_type' );
add_filter( 'wporg_query_filter_options_learning_pathway_topic', __NAMESPACE__ . '\get_learning_pathway_topic_options' );

add_filter( 'query_vars', __NAMESPACE__ . '\add_student_course_filter_query_vars' );
add_filter( 'wporg_query_filter_options_student-course', __NAMESPACE__ . '\get_student_course_options' );
add_filter( 'wporg_query_filter_options_student_course', __NAMESPACE__ . '\get_student_course_options' );
add_action( 'wporg_query_filter_in_form', __NAMESPACE__ . '\inject_other_filters' );

/**
Expand All @@ -29,7 +33,7 @@ function get_current_url() {
}

/**
* Create level options.
* Create the options for a level filter.
*
* @param array $levels The filtered levels for a view.
* @return array The options for a level filter.
Expand Down Expand Up @@ -92,12 +96,13 @@ function ( $level ) use ( $selected_slug ) {
}

/**
* Get the list of levels for the course and lesson filters.
* Get the top 10 level options for a post type.
* Used for the archive filters.
*
* @param array $options The options for this filter.
* @return array New list of level options.
*/
function get_level_options( $options ) {
function get_level_options_by_post_type( $options ) {
global $wp_query;

if ( ! isset( $wp_query->query_vars['post_type'] ) ) {
Expand Down Expand Up @@ -128,12 +133,13 @@ function get_level_options( $options ) {
}

/**
* Get the list of levels for the taxonomy filters.
* Get the top 10 level options.
* Used for the taxonomy and search filters.
*
* @param array $options The options for this filter.
* @return array New list of level options.
*/
function get_taxonomy_level_options( $options ) {
function get_level_options( $options ) {
// Get top 10 levels ordered by count, not empty.
$levels = get_terms(
array(
Expand All @@ -149,7 +155,7 @@ function get_taxonomy_level_options( $options ) {
}

/**
* Get the list of levels for the learning pathway filters.
* Get the top 10 level options for a learning pathway.
*
* @param array $options The options for this filter.
* @return array New list of level options.
Expand Down Expand Up @@ -192,7 +198,7 @@ function get_learning_pathway_level_options( $options ) {
}

/**
* Create topic options.
* Create the options for a topic filter.
*
* @param array $topics The filtered topics for a view.
* @return array The options for a topic filter.
Expand Down Expand Up @@ -232,12 +238,13 @@ function ( $a, $b ) {
}

/**
* Get the list of topics for the course and lesson filters.
* Get the top 20 topic options for a post type.
* Used for the archive filters.
*
* @param array $options The options for this filter.
* @return array New list of topic options.
*/
function get_topic_options( $options ) {
function get_topic_options_by_post_type( $options ) {
global $wp_query;

if ( ! isset( $wp_query->query_vars['post_type'] ) ) {
Expand Down Expand Up @@ -266,12 +273,13 @@ function get_topic_options( $options ) {
}

/**
* Get the list of topics for the taxonomy filters.
* Get the top 20 topic options.
* Used for the taxonomy and search filters.
*
* @param array $options The options for this filter.
* @return array New list of topic options.
*/
function get_taxonomy_topic_options( $options ) {
function get_topic_options( $options ) {
// Get top 20 topics ordered by count, not empty.
$topics = get_terms(
array(
Expand All @@ -287,7 +295,7 @@ function get_taxonomy_topic_options( $options ) {
}

/**
* Get the list of topics for the learning pathway filters.
* Get the top 20 topic options for a learning pathway.
*
* @param array $options The options for this filter.
* @return array New list of topic options.
Expand Down Expand Up @@ -351,15 +359,14 @@ function get_meta_query_values_by_key( $query, $key ) {
}

/**
* Get the list of languages for the course and lesson filters.
* Create the options for a language filter.
*
* @param array $options The options for this filter.
* @return array New list of language options.
* @param array $languages The filtered languages for a view.
* @return array The options for a language filter.
*/
function get_language_options( $options ) {
function create_language_options( $languages ) {
global $wp_query;
$post_type = $wp_query->query_vars['post_type'];
$languages = get_available_post_type_locales( 'language', $post_type, 'publish' );

// If there are no languages, or the only language is en_US, don't show the filter.
if ( empty( $languages ) || ( 1 === count( $languages ) && isset( $languages['en_US'] ) ) ) {
return array();
Expand Down Expand Up @@ -388,6 +395,43 @@ function get_language_options( $options ) {
);
}

/**
* Get the full list of available languages that have content.
* Used for the taxonomy filters.
*
* @param array $options The options for this filter.
* @return array New list of language options.
*/
function get_language_options( $options ) {
$languages = get_available_post_type_locales( 'language', null, 'publish', 'native' );

return create_language_options( $languages );
}

/**
* Get the list of languages for a post_type.
* Used for the archive filters.
*
* @param array $options The options for this filter.
* @return array New list of language options.
*/
function get_language_options_by_post_type( $options ) {
global $wp_query;
$post_type = $wp_query->get( 'post_type' );
// Convert post type from array to string if possible.
if ( is_array( $post_type ) && count( $post_type ) === 1 ) {
$post_type = reset( $post_type );
}

if ( ! is_string( $post_type ) ) {
return array();
}

$languages = get_available_post_type_locales( 'language', $post_type, 'publish', 'native' );

return create_language_options( $languages );
}

/**
* Get the query variable name for the student course filter, used by Sensei to filter the My Courses page.
* This is the PARAM_KEY defined in the Sensei plugin + the query id on the query loop in my-courses-content.php
Expand Down Expand Up @@ -468,7 +512,7 @@ function ( $option, $slug ) use ( $selected_slug ) {
function inject_other_filters( $key ) {
global $wp_query;

$single_query_vars = array( 'wporg_lesson_level' );
$single_query_vars = array( 'wporg_lesson_level', 'wporg_learning_pathway', 'post_type' );
foreach ( $single_query_vars as $single_query_var ) {
if ( ! isset( $wp_query->query[ $single_query_var ] ) ) {
continue;
Expand Down Expand Up @@ -507,4 +551,9 @@ function inject_other_filters( $key ) {
printf( '<input type="hidden" name="%s[]" value="%s" />', esc_attr( $meta_query_var ), esc_attr( $value ) );
}
}

// Pass through search query.
if ( isset( $wp_query->query['s'] ) ) {
printf( '<input type="hidden" name="s" value="%s" />', esc_attr( $wp_query->query['s'] ) );
}
}
30 changes: 0 additions & 30 deletions wp-content/themes/pub/wporg-learn-2024/inc/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

add_action( 'pre_get_posts', __NAMESPACE__ . '\modify_archive_queries' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\modify_level_query' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\modify_search_query' );


/**
* Modify the query by adding meta query for language if set.
Expand Down Expand Up @@ -63,31 +61,3 @@ function modify_level_query( $query ) {

return $query;
}

/**
* Get a list of the searchable Learn post types.
*
* @return array The searchable post types.
*/
function get_searchable_post_types() {
return array( 'course', 'lesson', 'quiz', 'meeting', 'page', 'post', 'wporg_workshop' );
}

/**
* Modify the search query to filter to only Learn post types if no post type is set.
*
* @param WP_Query $query The search query.
*/
function modify_search_query( $query ) {
if ( is_admin() || ! $query->is_search() ) {
return;
}

if ( isset( $query->query_vars['post_type'] ) ) {
return $query;
}

$query->set( 'post_type', get_searchable_post_types() );

return $query;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"language"} /-->
<!-- wp:wporg/query-filter {"key":"topic"} /-->
<!-- wp:wporg/query-filter {"key":"level","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"archive_language"} /-->
<!-- wp:wporg/query-filter {"key":"archive_topic"} /-->
<!-- wp:wporg/query-filter {"key":"archive_level","multiple":false} /-->
</div>
<!-- /wp:group -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|50"}}},"layout":{"type":"constrained","justifyContent":"left","contentSize":"730px"}} -->
<div class="wp-block-group" style="margin-bottom:var(--wp--preset--spacing--50)">

<!-- wp:query-title {"type":"archive","showPrefix":false} /-->
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><?php esc_html_e( 'Courses', 'wporg-learn' ); ?></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><?php esc_html_e( 'WordPress provides limitless ways for people to craft and grow their online presence. The content in these courses is delivered in multiple formats, with a focus on text and video, working towards practical learning objectives to help you become a better WordPress developer, designer, user, and contributor.', 'wporg-learn' ); ?></p>
Expand All @@ -26,15 +28,16 @@

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"language"} /-->
<!-- wp:wporg/query-filter {"key":"topic"} /-->
<!-- wp:wporg/query-filter {"key":"level","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"archive_language"} /-->
<!-- wp:wporg/query-filter {"key":"archive_topic"} /-->
<!-- wp:wporg/query-filter {"key":"archive_level","multiple":false} /-->
</div>
<!-- /wp:group -->

</div>
<!-- /wp:group -->

<!-- wp:wporg-learn/search-results-context {"style":{"spacing":{"padding":{"bottom":"var:preset|spacing|20"}},"elements":{"link":{"color":{"text":"var:preset|color|charcoal-4"}}}},"textColor":"charcoal-4","fontSize":"small"} /-->

<!-- wp:query {"queryId":1,"query":{"perPage":12,"postType":"course","courseFeatured":false,"inherit":true},"namespace":"wporg-learn/course-grid","align":"wide","className":"wporg-learn-course-grid"} -->
<div class="wp-block-query alignwide wporg-learn-course-grid">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|50"}}},"layout":{"type":"constrained","justifyContent":"left","contentSize":"730px"}} -->
<div class="wp-block-group" style="margin-bottom:var(--wp--preset--spacing--50)">

<!-- wp:query-title {"type":"archive","showPrefix":false} /-->
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><?php esc_html_e( 'Lesson Plans', 'wporg-learn' ); ?></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><?php esc_html_e( 'Want to help others learn about WordPress? Read through, use, and remix these lesson plans.', 'wporg-learn' ); ?></p>
Expand All @@ -26,9 +28,9 @@

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"language"} /-->
<!-- wp:wporg/query-filter {"key":"topic"} /-->
<!-- wp:wporg/query-filter {"key":"level","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"archive_language"} /-->
<!-- wp:wporg/query-filter {"key":"archive_topic"} /-->
<!-- wp:wporg/query-filter {"key":"archive_level","multiple":false} /-->
</div>
<!-- /wp:group -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
<!-- wp:group {"style":{"spacing":{"margin":{"bottom":"var:preset|spacing|50"}}},"layout":{"type":"constrained","justifyContent":"left","contentSize":"730px"}} -->
<div class="wp-block-group" style="margin-bottom:var(--wp--preset--spacing--50)">

<!-- wp:query-title {"type":"archive","showPrefix":false} /-->
<!-- wp:heading {"level":1} -->
<h1 class="wp-block-heading"><?php esc_html_e( 'Lessons', 'wporg-learn' ); ?></h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p><?php esc_html_e( 'Lessons can be taken as part of a course or whenever you want to learn deeply about a specific subject or concept. They come in a range of formats, including how-to videos, quizzes, text-based content, and other activities.', 'wporg-learn' ); ?></p>
Expand All @@ -26,15 +28,17 @@

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"language"} /-->
<!-- wp:wporg/query-filter {"key":"topic"} /-->
<!-- wp:wporg/query-filter {"key":"level","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"archive_language"} /-->
<!-- wp:wporg/query-filter {"key":"archive_topic"} /-->
<!-- wp:wporg/query-filter {"key":"archive_level","multiple":false} /-->
</div>
<!-- /wp:group -->

</div>
<!-- /wp:group -->

<!-- wp:wporg-learn/search-results-context {"style":{"spacing":{"padding":{"bottom":"var:preset|spacing|20"}},"elements":{"link":{"color":{"text":"var:preset|color|charcoal-4"}}}},"textColor":"charcoal-4","fontSize":"small"} /-->

<!-- wp:query {"queryId":1,"query":{"perPage":12,"pages":0,"offset":0,"postType":"lesson","order":"desc","orderBy":"date","author":"","search":"","exclude":[],"sticky":"","inherit":true,"parents":[]}} -->
<div class="wp-block-query">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<!-- wp:group {"style":{"spacing":{"blockGap":"0"}},"layout":{"type":"flex","flexWrap":"nowrap"},"className":"wporg-query-filters"} -->
<div class="wp-block-group wporg-query-filters">
<!-- wp:wporg/query-filter {"key":"student-course","multiple":false} /-->
<!-- wp:wporg/query-filter {"key":"student_course","multiple":false} /-->
</div>
<!-- /wp:group -->

Expand Down
Loading

0 comments on commit 66fe29e

Please sign in to comment.