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
<strong>public</strong> <strong>function</strong> <strong>persist</strong>( $download );Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
$download | DLM_Download | n/a | The 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.
<em>// create new download</em>
<em>/** @var DLM_Download $download */</em>
$download = <strong>new</strong> DLM_Download();
$download->set_title( "Dummy Download" );
$download->set_description( "This is just a test download" );
$download->set_author( 1 );
$download->set_status( "publish" );
<em>// store new download</em>
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:
- Version Repository – retrieve_single
- Version Repository – retrieve
- Version Repository – persist
- Version Repository – num_rows
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.
<strong>try</strong> {
<em>// retrieve download with ID 1</em>
<em>/** @var DLM_Download $download */</em>
$download = download_monitor()->service( 'download_repository' )->retrieve_single( 1 );
<em>// set new title</em>
$download->set_title( "A way better title than it had before" );
<em>// update download in database</em>
download_monitor()->service( 'download_repository' )->persist( $download );
} <strong>catch</strong> ( <strong>Exception</strong> $exception ) {
<em>// download with ID 1 not found</em>
}