array( 'sequential', 'free' ), '_slms_enrollment_mode' => array( 'self', 'admin', 'both' ), '_slms_content_change_mode' => array( 'off', 'lesson', 'quiz', 'both' ), ); public static function init() { add_action( 'save_post_slms_course', array( __CLASS__, 'save' ), 10, 2 ); } /** * Return $value if it is an allowed choice for $key, otherwise null. * * @param string $key Meta key. * @param mixed $value Submitted value. * @return string|null */ public static function sanitize_choice( $key, $value ) { if ( '_slms_course_language' === $key ) { return is_string( $value ) && isset( I18n::get_supported_languages()[ $value ] ) ? $value : null; } return isset( self::CHOICES[ $key ] ) && in_array( $value, self::CHOICES[ $key ], true ) ? $value : null; } public static function save( $post_id, $post ) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } if ( ! isset( $_POST['accessnow_slms_course_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['accessnow_slms_course_nonce'] ) ), 'accessnow_slms_save_course' ) ) { return; } foreach ( array( '_slms_progression_mode', '_slms_enrollment_mode', '_slms_content_change_mode', '_slms_course_language' ) as $key ) { if ( ! isset( $_POST[ $key ] ) ) { continue; } $value = self::sanitize_choice( $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) ); if ( null !== $value ) { update_post_meta( $post_id, $key, $value ); } } Lesson_Header::save_field( $post_id ); // Checkbox: an unticked box is not submitted, so absence means "not featured". update_post_meta( $post_id, '_slms_featured', empty( $_POST['_slms_featured'] ) ? 0 : 1 ); } public static function get( $post_id, $key, $fallback = '' ) { $value = get_post_meta( $post_id, $key, true ); return '' !== $value ? $value : $fallback; } }