_migrate_awards( string $type )

Migrate deprecated meta values for user awards by type.


Description Description

Queries 50 earned awards at a time and migrates their data by moving meta data to the new location and then deleting the deprecated meta values.


Top ↑

Parameters Parameters

$type

(string) (Required) Award type, either "achievement" or "certificate".


Top ↑

Return Return

(boolean) Returns true if there are more results and false if there are no further results.


Top ↑

Source Source

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

function _migrate_awards( $type ) {

	$per_page = llms_update_util_get_items_per_page();

	$query_args = array(
		'orderby'        => array( 'ID' => 'ASC' ),
		'post_type'      => "llms_my_{$type}",
		'post_status'    => 'any',
		'posts_per_page' => $per_page,
		'no_found_rows'  => true, // We don't care about found rows since we'll run the query as many times as needed anyway.
		'fields'         => 'ids', // We just need the ID for the updates we'll perform.
		'meta_query'     => array(
			'relation' => 'OR',
			array(
				'key'     => "_llms_{$type}_title",
				'compare' => 'EXISTS',
			),
			array(
				'key'     => "_llms_{$type}_template",
				'compare' => 'EXISTS',
			),
			array(
				'key'     => "_llms_{$type}_image",
				'compare' => 'EXISTS',
			),
		),
	);

	if ( 'achievement' === $type ) {
		$query_args['meta_query'][] = array(
			'key'     => '_llms_achievement_content',
			'compare' => 'EXISTS',
		);
	}

	$query = new \WP_Query( $query_args );

	// Don't trigger deprecations.
	remove_filter( 'get_post_metadata', 'llms_engagement_handle_deprecated_meta_keys', 20, 3 );

	// Don't trigger save hooks.
	remove_action( "save_post_llms_my_{$type}", array( 'LLMS_Controller_Awards', 'on_save' ), 20 );

	$legacy_option_added = false;
	foreach ( $query->posts as $post_id ) {

		$image_migrated = _migrate_award( $post_id, $type );

		if ( ! $legacy_option_added && ! $image_migrated ) {
			_add_legacy_opt( $type );
			$legacy_option_added = true;
		}
	}
	// Re-enable deprecations.
	add_filter( 'get_post_metadata', 'llms_engagement_handle_deprecated_meta_keys', 20, 3 );

	// Re-enabled save hooks.
	add_action( "save_post_llms_my_{$type}", array( 'LLMS_Controller_Awards', 'on_save' ), 20 );

	// If there was 50 results assume there's another page and run again, otherwise we're done.
	return ( count( $query->posts ) === $per_page );

}

Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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