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
Conversation
948c26f
to
226c779
Compare
// 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 ); | ||
} |
There was a problem hiding this comment.
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.
019928f
to
d181ba8
Compare
d181ba8
to
ca29dbd
Compare
It should stay on the archive page rather than redirecting to the landing page
Enables admin page post type filtering to also filter the locale list
ca29dbd
to
4c1afee
Compare
Nice work @adamwoodnz I've made a note to review this ASAP. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I left a minor suggestion, but I don't feel strongly about it.
$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 | ||
) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I worry that duplicating the query here could lead to them accidentally getting out of sync, and it seems less readable to me.
What do you think about integrating $post_status
into the query conditionally? That seems like the common approach to me. Here's a few examples:
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functionality works great, nice one @adamwoodnz
32b84ce
to
b36d679
Compare
b36d679
to
6f881b8
Compare
global $wpdb; | ||
|
||
$and_post_status = ''; | ||
if ( $published_only ) { | ||
$and_post_status = "AND posts.post_status = 'publish'"; | ||
if ( in_array( $post_status, get_post_stati(), true ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
post_status
is also only sourced from INPUT_GET
in the admin. On the frontend post_status
is always hard coded to publish
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that looks like a great way to guarantee it's a valid value 👍🏻
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
* | ||
* @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 ) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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'"; | ||
if ( in_array( $post_status, get_post_stati(), true ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that looks like a great way to guarantee it's a valid value 👍🏻
Optional parameter should be last in PHP 8
Closes #874
This PR adds admin and frontend filtering of Lesson Plans, expanding on the functionality previously added for Tutorials.
Screenshots
How to test
IMPORTANT before you start
In the Lesson Plan list if the Language field displays
English [en_US]
it is most likely that the Lesson Plan actually has no meta language set and the column is displaying the default value. The easiest way to fix this is to bulk edit all these entries and set them to English. This has already been done in prod, so if you're testing in sandbox things should just work.Admin
Frontend
English [en_US]
and check that the Lesson Plan you created in step 4 above is displayed