llms_update_300_migrate_course_postmeta()

Update keys of course meta fields for consistency


Return Return

(void)


Top ↑

Source Source

File: includes/functions/updates/llms-functions-updates-300.php

function llms_update_300_migrate_course_postmeta() {

	global $wpdb;

	// Rekey meta fields.
	llms_update_util_rekey_meta( 'course', '_llms_audio_embed', '_audio_embed' );
	llms_update_util_rekey_meta( 'course', '_llms_video_embed', '_video_embed' );
	llms_update_util_rekey_meta( 'course', '_llms_has_prerequisite', '_has_prerequisite' );
	llms_update_util_rekey_meta( 'course', '_llms_length', '_lesson_length' );
	llms_update_util_rekey_meta( 'course', '_llms_capacity', '_lesson_max_user' );
	llms_update_util_rekey_meta( 'course', '_llms_prerequisite', '_prerequisite' );
	llms_update_util_rekey_meta( 'course', '_llms_prerequisite_track', '_prerequisite_track' );

	llms_update_util_rekey_meta( 'course', '_llms_start_date', '_course_dates_from' );
	llms_update_util_rekey_meta( 'course', '_llms_end_date', '_course_dates_to' );

	// Updates course enrollment settings and reformats existing dates.
	$dates = $wpdb->get_results(
		"SELECT m.meta_id, m.post_id, m.meta_value
		 FROM {$wpdb->postmeta} AS m
		 INNER JOIN {$wpdb->posts} AS p ON p.ID = m.post_ID
	 	 WHERE p.post_type = 'course' AND ( m.meta_key = '_llms_start_date' OR m.meta_key = '_llms_end_date' );"
	); // db call ok; no-cache ok.
	foreach ( $dates as $r ) {
		// If no value in the field skip it otherwise we end up with start of the epoch.
		if ( ! $r->meta_value ) {
			continue; }
		$wpdb->update(
			$wpdb->postmeta,
			array(
				'meta_value' => date( 'm/d/Y', strtotime( $r->meta_value ) ),
			),
			array(
				'meta_id' => $r->meta_id,
			)
		); // db call ok; no-cache ok.
		add_post_meta( $r->post_id, '_llms_time_period', 'yes' );
		add_post_meta( $r->post_id, '_llms_course_opens_message', sprintf( __( 'This course opens on [lifterlms_course_info id="%d" key="start_date"].', 'lifterlms' ), $r->post_id ) );
		add_post_meta( $r->post_id, '_llms_course_closed_message', sprintf( __( 'This course closed on [lifterlms_course_info id="%d" key="end_date"].', 'lifterlms' ), $r->post_id ) );
	}

	// Update course capacity bool and related settings.
	$capacity = $wpdb->get_results(
		"SELECT m.post_id, m.meta_value
		 FROM {$wpdb->postmeta} AS m
		 INNER JOIN {$wpdb->posts} AS p ON p.ID = m.post_ID
	 	 WHERE p.post_type = 'course' AND m.meta_key = '_llms_capacity';"
	); // db call ok; no-cache ok.
	foreach ( $capacity as $r ) {
		if ( $r->meta_value ) {
			add_post_meta( $r->post_id, '_llms_enable_capacity', 'yes' );
			add_post_meta( $r->post_id, '_llms_capacity_message', __( 'Enrollment has closed because the maximum number of allowed students has been reached.', 'lifterlms' ) );
		}
	}

	// Convert numeric has_preqeq to "yes".
	$prereq = $wpdb->query(
		"UPDATE {$wpdb->prefix}postmeta AS m
		 INNER JOIN {$wpdb->prefix}posts AS p ON p.ID = m.post_ID
		 SET m.meta_value = 'yes'
	 	 WHERE p.post_type = 'course' AND m.meta_key = '_llms_has_prerequisite' AND m.meta_value = 1;"
	); // db call ok; no-cache ok.

	// Convert empty has_prereq to "no".
	$prereq = $wpdb->query(
		"UPDATE {$wpdb->prefix}postmeta AS m
		 INNER JOIN {$wpdb->prefix}posts AS p ON p.ID = m.post_ID
		 SET m.meta_value = 'no'
	 	 WHERE p.post_type = 'course' AND m.meta_key = '_llms_has_prerequisite' AND m.meta_value = '';"
	); // db call ok; no-cache ok.

}


Top ↑

Changelog Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.