Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filtering of lesson plans by language #1500

Merged
merged 10 commits into from
May 3, 2023
21 changes: 11 additions & 10 deletions wp-content/plugins/wporg-learn/inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use WP_Query;
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_workshop_locales;
use function WPOrg_Learn\Post_Meta\get_available_post_type_locales;

defined( 'WPINC' ) || die();

Expand All @@ -24,8 +24,8 @@
add_filter( 'manage_' . $pt . '_posts_custom_column', __NAMESPACE__ . '\render_list_table_language_column', 10, 2 );
}
add_filter( 'manage_edit-wporg_workshop_sortable_columns', __NAMESPACE__ . '\add_workshop_list_table_sortable_columns' );
add_action( 'restrict_manage_posts', __NAMESPACE__ . '\add_workshop_list_table_filters', 10, 2 );
add_action( 'pre_get_posts', __NAMESPACE__ . '\handle_workshop_list_table_filters' );
add_action( 'restrict_manage_posts', __NAMESPACE__ . '\add_admin_list_table_filters', 10, 2 );
add_action( 'pre_get_posts', __NAMESPACE__ . '\handle_admin_list_table_filters' );
add_filter( 'display_post_states', __NAMESPACE__ . '\add_post_states', 10, 2 );
foreach ( array( 'lesson-plan', 'wporg_workshop', 'course', 'lesson' ) as $pt ) {
add_filter( 'views_edit-' . $pt, __NAMESPACE__ . '\list_table_views' );
Expand Down Expand Up @@ -223,19 +223,20 @@ function add_workshop_list_table_sortable_columns( $sortable_columns ) {
}

/**
* Add filtering controls for the workshops list table.
* Add filtering controls for the tutorial and lesson plan list tables.
*
* @param string $post_type
* @param string $which
*
* @return void
*/
function add_workshop_list_table_filters( $post_type, $which ) {
if ( 'wporg_workshop' !== $post_type || 'top' !== $which ) {
function add_admin_list_table_filters( $post_type, $which ) {
if ( ( 'wporg_workshop' !== $post_type && 'lesson-plan' !== $post_type ) || 'top' !== $which ) {
return;
}

$available_locales = get_available_workshop_locales( 'language', 'english', false );
$post_status = filter_input( INPUT_GET, 'post_status', FILTER_SANITIZE_STRING );
$available_locales = get_available_post_type_locales( 'language', $post_type, 'english', $post_status );
$language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );

?>
Expand All @@ -260,13 +261,13 @@ function add_workshop_list_table_filters( $post_type, $which ) {
}

/**
* Alter the query to include workshop list table filters.
* Alter the query to include tutorial and lesson plan list table filters.
*
* @param WP_Query $query
*
* @return void
*/
function handle_workshop_list_table_filters( WP_Query $query ) {
function handle_admin_list_table_filters( WP_Query $query ) {
if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
return;
}
Expand All @@ -277,7 +278,7 @@ function handle_workshop_list_table_filters( WP_Query $query ) {
return;
}

if ( 'edit-wporg_workshop' === $current_screen->id ) {
if ( 'edit-wporg_workshop' === $current_screen->id || 'edit-lesson-plan' === $current_screen->id ) {
$language = filter_input( INPUT_GET, 'language', FILTER_SANITIZE_STRING );

if ( $language ) {
Expand Down
56 changes: 35 additions & 21 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,35 +257,40 @@ function get_workshop_duration( WP_Post $workshop, $format = 'raw' ) {
}

/**
* Get a list of locales that are associated with at least one workshop.
*
* Optionally only published workshops.
* Get a list of locales that are associated with at least one post of the specified type.
*
* @param string $meta_key
* @param string $post_type
* @param string $label_language
* @param bool $published_only
* @param string $post_status
*
* @return array
*/
function get_available_workshop_locales( $meta_key, $label_language = 'english', $published_only = true ) {
function get_available_post_type_locales( $meta_key, $post_type, $label_language = 'english', $post_status ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this notice on PHP 8.0.28. We don't run that on prod yet, but will in the near future, so it'd be good to be prepared now IMO.

PHP Deprecated: Required parameter $post_status follows optional parameter $label_language in wp-content/plugins/wporg-learn/inc/post-meta.php on line 269

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 4cb6519

global $wpdb;

$and_post_status = '';
if ( $published_only ) {
$and_post_status = "AND posts.post_status = 'publish'";
}

$results = $wpdb->get_col( $wpdb->prepare(
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $and_post_status contains no user input.
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id $and_post_status
WHERE postmeta.meta_key = %s
",
$meta_key
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
) );
$results = isset( $post_status )
? $wpdb->get_col( $wpdb->prepare(
"
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 posts.post_status = %s
WHERE postmeta.meta_key = %s
",
$post_type,
$post_status,
$meta_key
) )
: $wpdb->get_col( $wpdb->prepare(
"
SELECT DISTINCT postmeta.meta_value
FROM {$wpdb->postmeta} postmeta
JOIN {$wpdb->posts} posts ON posts.ID = postmeta.post_id AND posts.post_type = %s
WHERE postmeta.meta_key = %s
",
$post_type,
$meta_key
) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it's a fair point. I was trying to remove the need for phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, but it seems to be common practice. Changed to something closer resembling the previous code, and updated the comment.


if ( empty( $results ) ) {
return array();
Expand Down Expand Up @@ -345,6 +350,15 @@ function save_lesson_plan_metabox_fields( $post_id ) {

$download_url = filter_input( INPUT_POST, 'slides-download-url', FILTER_VALIDATE_URL ) ?: '';
update_post_meta( $post_id, 'slides_download_url', $download_url );

// This language meta field is rendered in the editor sidebar using a PluginDocumentSettingPanel block,
// which won't save the field on publish if it has the default value.
// Our filtering by locale depends on it being set, so we force it to be updated after saving:
$language = get_post_meta( $post_id, 'language', true );
$language_default = 'en_US';
if ( ! isset( $language ) || $language_default === $language ) {
update_post_meta( $post_id, 'language', $language_default );
}
Comment on lines +349 to +356
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This follows the same approach that we had to take with Tutorials: https://github.com/WordPress/Learn/blob/trunk/wp-content/plugins/wporg-learn/inc/post-meta.php#L525

$language can be read as the default value of en_US even if it's not set, hence forcing save in this case as well, on line 362.

}

/**
Expand Down
11 changes: 10 additions & 1 deletion wp-content/themes/pub/wporg-learn-2020/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,16 @@ function wporg_learn_get_card_template_args( $post_id ) {
break;

case 'lesson-plan':
$args['meta'] = wporg_learn_get_lesson_plan_taxonomy_data( $post_id, 'archive' );
$args['meta'] = array_merge(
wporg_learn_get_lesson_plan_taxonomy_data( $post_id, 'archive' ),
array(
array(
'icon' => 'admin-site-alt3',
'label' => __( 'Language:', 'wporg-learn' ),
'value' => \WordPressdotorg\Locales\get_locale_name_from_code( $post->language, 'native' ),
),
)
);
break;

case 'wporg_workshop':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
'current' => filter_input( INPUT_GET, 'wp_version', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY ) ?: array(),
),
);

$locales = \WPOrg_Learn\Post_Meta\get_available_post_type_locales( 'language', 'lesson-plan', 'native', 'publish' );
?>

<form class="sidebar-filters col-3" method="get" action="<?php echo esc_url( get_post_type_archive_link( 'lesson-plan' ) ); ?>">
Expand All @@ -40,11 +42,32 @@
<button type="submit" class="button button-large button-secondary">
<?php esc_html_e( 'Apply Filters', 'wporg-learn' ); ?>
</button>
<a href="<?php echo esc_url( get_post_type_archive_link( 'lesson-plan' ) ); ?>" class="clear-filters">
<a href="<?php echo esc_url( get_post_type_archive_link( 'lesson-plan' ) . '?_view=all' ); ?>" class="clear-filters">
<?php esc_html_e( 'Clear All Filters', 'wporg-learn' ); ?>
</a>
</div>

<h4 class="h5" id="lp-filters-language-heading"><?php esc_html_e( 'Language', 'wporg-learn' ); ?></h4>
<div class="filter-group">
<select
id="<?php echo esc_attr( 'language' ); ?>"
class="filter-group-select"
name="<?php echo esc_attr( 'language' ); ?>"
data-placeholder="<?php esc_attr_e( 'Select', 'wporg-learn' ); ?>"
aria-labelledby="lp-filters-language-heading"
>
<option value=""><?php esc_html_e( 'Select', 'wporg-learn' ); ?></option>
<?php foreach ( $locales as $locale_value => $locale_label ) : ?>
<option
value="<?php echo esc_attr( $locale_value ); ?>"
<?php selected( $locale_value, filter_input( INPUT_GET, 'language' ) ); ?>
>
<?php printf( '%s [%s]', esc_html( $locale_label ), esc_html( $locale_value ) ); ?>
</option>
<?php endforeach; ?>
</select>
</div>

<?php foreach ( $taxonomies as $txnmy ) : ?>
<h4 class="h5"><?php echo esc_html( $txnmy['label'] ); ?></h4>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
array(
'label' => __( 'Language', 'wporg-learn' ),
'name' => 'language',
'items' => \WPOrg_Learn\Post_Meta\get_available_workshop_locales( 'language', 'native' ),
'items' => \WPOrg_Learn\Post_Meta\get_available_post_type_locales( 'language', 'wporg_workshop', 'native', 'publish' ),
),
array(
'label' => __( 'Subtitles', 'wporg-learn' ),
'name' => 'captions',
'items' => \WPOrg_Learn\Post_Meta\get_available_workshop_locales( 'video_caption_language', 'native' ),
'items' => \WPOrg_Learn\Post_Meta\get_available_post_type_locales( 'video_caption_language', 'wporg_workshop', 'native', 'publish' ),
),
array(
'label' => __( 'WordPress Version', 'wporg-learn' ),
Expand Down