Download Repository – persist

Please note: Functionality described on this page requires Download Monitor version 4.0 or above.

Downloads in Download Monitor can be stored in the database via the Download Repository. persist is a method of Download Monitor’s download repository service. The method stores the given DLM_Download object in the WordPress database. This method can be used for inserting new and updating existing downloads. If no ID in the DLM_Download object is set, the download will be inserted as a new download. If an ID is set in the DLM_Download object, the download in the WordPress database with that ID will be updated.

Signature

public function persist( $download );

Parameters

ParameterTypeDefaultDescription
$downloadDLM_Downloadn/aThe download that will be saved in the WordPress database.

Examples

Inserting a new download

This snippet will create a new DLM_Download object and save it in the WordPress database.

// create new download
/** @var DLM_Download $download */
$download = new DLM_Download();
$download->set_title( "Dummy Download" );
$download->set_description( "This is just a test download" );
$download->set_author( 1 );
$download->set_status( "publish" );

// store new download
download_monitor()->service( 'download_repository' )->persist( $download );

Please note: The download created with this snippet contains no versions. The version can be created with the Version Repository, which you can read more about here:

Updating an existing download

This snippet will fetch download with ID 1, change its title and update it in the WordPress database. The retrieve_single call is wrapped in a try-catch block. You can read here why that is done.

try {
	// retrieve download with ID 1
	/** @var DLM_Download $download */
	$download = download_monitor()->service( 'download_repository' )->retrieve_single( 1 );

	// set new title
	$download->set_title( "A way better title than it had before" );

	// update download in database
	download_monitor()->service( 'download_repository' )->persist( $download );
} catch ( Exception $exception ) {
	// download with ID 1 not found
}
Was this article helpful?
Start Protecting your WordPress Downloads

Protect and track downloads on WordPress... What are you waiting for?