diff --git a/wp-content/plugins/wporg-learn/inc/post-meta.php b/wp-content/plugins/wporg-learn/inc/post-meta.php index 97e966a55..2421c75a0 100644 --- a/wp-content/plugins/wporg-learn/inc/post-meta.php +++ b/wp-content/plugins/wporg-learn/inc/post-meta.php @@ -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 ) ); diff --git a/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php b/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php index 33be79680..340abda0e 100644 --- a/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php +++ b/wp-content/themes/pub/wporg-learn-2024/inc/block-config.php @@ -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' ); /** @@ -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. @@ -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'] ) ) { @@ -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( @@ -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. @@ -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. @@ -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'] ) ) { @@ -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( @@ -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. @@ -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(); @@ -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 @@ -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; @@ -507,4 +551,9 @@ function inject_other_filters( $key ) { printf( '', esc_attr( $meta_query_var ), esc_attr( $value ) ); } } + + // Pass through search query. + if ( isset( $wp_query->query['s'] ) ) { + printf( '', esc_attr( $wp_query->query['s'] ) ); + } } diff --git a/wp-content/themes/pub/wporg-learn-2024/inc/query.php b/wp-content/themes/pub/wporg-learn-2024/inc/query.php index 0e5523107..d28ae030b 100644 --- a/wp-content/themes/pub/wporg-learn-2024/inc/query.php +++ b/wp-content/themes/pub/wporg-learn-2024/inc/query.php @@ -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. @@ -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; -} diff --git a/wp-content/themes/pub/wporg-learn-2024/patterns/archive-content.php b/wp-content/themes/pub/wporg-learn-2024/patterns/archive-content.php index 084eda5e6..97158e32a 100644 --- a/wp-content/themes/pub/wporg-learn-2024/patterns/archive-content.php +++ b/wp-content/themes/pub/wporg-learn-2024/patterns/archive-content.php @@ -22,9 +22,9 @@