Making WordPress.org


Ignore:
Timestamp:
09/01/2024 11:46:40 PM (17 months ago)
Author:
adamwood
Message:

Learn: Sync with git WordPress/learn@daa4713

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-learn/inc/post-meta.php

    r13981 r14012  
    2323add_action( 'admin_footer', __NAMESPACE__ . '\render_locales_list' );
    2424add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_editor_assets' );
     25add_action( 'wp_insert_post', __NAMESPACE__ . '\set_default_lesson_preview', 10, 3 );
    2526
    2627/**
     
    5253        ),
    5354    );
     55}
     56
     57/**
     58 * Set public preview to be enabled on lessons created within a course by default.
     59 * This post meta is registered by Sensei with no default value, so we set it here on lesson creation.
     60 *
     61 * @param int     $post_ID Post ID.
     62 * @param WP_Post $post    Post object.
     63 * @param bool    $update  Whether this is an existing post being updated.
     64 */
     65function set_default_lesson_preview( $post_ID, $post, $update ) {
     66    // Only run for new lessons.
     67    if ( $update || 'lesson' !== $post->post_type ) {
     68        return;
     69    }
     70
     71    // Check if the lesson belongs to a course.
     72    $course_id = get_post_meta( $post_ID, '_lesson_course', true );
     73
     74    if ( empty( $course_id ) ) {
     75        return;
     76    }
     77
     78    $existing_value = get_post_meta( $post_ID, '_lesson_preview', true );
     79
     80    if ( '' === $existing_value ) {
     81        update_post_meta( $post_ID, '_lesson_preview', 'preview' );
     82    }
    5483}
    5584
Note: See TracChangeset for help on using the changeset viewer.