Skip to content

Instantly share code, notes, and snippets.

@saqibsarwar
Created September 7, 2022 18:02
Show Gist options
  • Save saqibsarwar/bebf4bafa67168591233f5133cbc7016 to your computer and use it in GitHub Desktop.
Save saqibsarwar/bebf4bafa67168591233f5133cbc7016 to your computer and use it in GitHub Desktop.
Download Monitor and WP ALL Import - Attach PDF files to download CPT to make them downloadable.
<?php
// Start preparing an array to insert Download Monitor's Download Version (which is a CPT with Download as parent post).
$dlm_download_version = array(
'post_title' => 'Download #' . $download_id . ' File Version',
'post_type' => 'dlm_download_version',
'post_status' => 'publish',
'post_parent' => $download_id,
'post_author' => $attachment->post_author,
);
// Insert if no download version already exists.
$dlm_download_version_id = wp_insert_post( $dlm_download_version );
if ( $dlm_download_version_id ) {
// Initialise version field with an empty value.
update_post_meta( $dlm_download_version_id, '_version', '' );
// Attachment full path in an array.
$pdf_files = array( $filepath );
// We need to process the PDF files array using JSON encode as per Download Monitor's data structure requirements.
$encoded_files = json_encode( $pdf_files, JSON_UNESCAPED_UNICODE );
update_post_meta( $dlm_download_version_id, '_files', $encoded_files );
// I have used Download Monitor's file manager service to get file size.
$filesize = download_monitor()->service( 'file_manager' )->get_file_size( $pdf_files[0] );
update_post_meta( $dlm_download_version_id, '_filesize', $filesize );
// Saving empty values in other fields.
update_post_meta( $dlm_download_version_id, '_md5', '' );
update_post_meta( $dlm_download_version_id, '_sha1', '' );
update_post_meta( $dlm_download_version_id, '_sha256', '' );
update_post_meta( $dlm_download_version_id, '_crc32', '' );
update_post_meta( $dlm_download_version_id, '_download_count', 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment