Skip to content

Commit

Permalink
Make course lessons publicly previewable by default (#2866)
Browse files Browse the repository at this point in the history
Closes #2807
  • Loading branch information
adamwoodnz authored Sep 1, 2024
1 parent 77218e6 commit 84cc7ce
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions wp-content/plugins/wporg-learn/inc/post-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
add_action( 'save_post_meeting', __NAMESPACE__ . '\save_meeting_metabox_fields' );
add_action( 'admin_footer', __NAMESPACE__ . '\render_locales_list' );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_editor_assets' );
add_action( 'wp_insert_post', __NAMESPACE__ . '\set_default_lesson_preview', 10, 3 );

/**
* Register all post meta keys.
Expand Down Expand Up @@ -53,6 +54,34 @@ function register_lesson_meta() {
);
}

/**
* Set public preview to be enabled on lessons created within a course by default.
* This post meta is registered by Sensei with no default value, so we set it here on lesson creation.
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*/
function set_default_lesson_preview( $post_ID, $post, $update ) {
// Only run for new lessons.
if ( $update || 'lesson' !== $post->post_type ) {
return;
}

// Check if the lesson belongs to a course.
$course_id = get_post_meta( $post_ID, '_lesson_course', true );

if ( empty( $course_id ) ) {
return;
}

$existing_value = get_post_meta( $post_ID, '_lesson_preview', true );

if ( '' === $existing_value ) {
update_post_meta( $post_ID, '_lesson_preview', 'preview' );
}
}

/**
* Register post meta keys for lesson plans.
*/
Expand Down

0 comments on commit 84cc7ce

Please sign in to comment.