Filter uncanny-toolkit-pro

uo_download_certificates_in_bulk_filename

Filters the filename for bulk downloaded certificates, allowing modification before saving.

add_filter( 'uo_download_certificates_in_bulk_filename', $callback, 10, 5 );

Description

Filters the filename used when downloading certificates in bulk. Modify the $file_name parameter to customize the naming convention for downloaded certificate files. This hook provides the user ID, object ID, certificate post, and a timestamp, allowing for dynamic filename generation based on these elements.


Usage

add_filter( 'uo_download_certificates_in_bulk_filename', 'your_function_name', 10, 5 );

Parameters

$file_name (mixed)
This parameter contains the filename for the certificate download.
$user (mixed)
This parameter represents the name of the file for the downloaded certificates.
$object_id (mixed)
This parameter contains the WordPress user object for whom the certificate is being generated.
$certificate_post (mixed)
This parameter contains the ID of the object associated with the certificate.
$current_time_stamp (mixed)
This parameter provides the current Unix timestamp, likely used for generating unique filenames or for time-based operations related to the download.

Return Value

The filtered value.


Examples

/**
 * Filter hook: uo_download_certificates_in_bulk_filename
 *
 * Modifies the filename for bulk downloaded certificates.
 * This example prepends a prefix to the filename based on the certificate post type.
 *
 * @param string $file_name        The original generated filename.
 * @param WP_User $user            The user object for whom the certificate is generated.
 * @param int $object_id          The ID of the object associated with the certificate (e.g., course, quiz).
 * @param WP_Post $certificate_post The post object for the certificate template.
 * @param int $current_time_stamp The timestamp of when the certificate generation started.
 * @return string The modified filename.
 */
add_filter(
	'uo_download_certificates_in_bulk_filename',
	function( $file_name, $user, $object_id, $certificate_post, $current_time_stamp ) {
		// Get the slug of the certificate post type to use as a prefix.
		$certificate_type_slug = $certificate_post->post_type;

		// Construct the new filename by prepending the type slug and a separator.
		$new_file_name = sanitize_title( $certificate_type_slug . '-' . $file_name );

		return $new_file_name;
	},
	10, // Priority
	5  // Number of accepted arguments
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/download-certificates-in-bulk.php:726

public static function generate_group_certificate( $user_id, $object_id, $batch_id, $type = 'group' ) {

		$user = new WP_User( $user_id );

		$setup_parameters = self::setup_parameters( $object_id, $user_id, $type );
		if ( 1 !== (int) $setup_parameters['print-certificate'] ) {
			return;
		}

		self::$current_time_stamp = time();
		$new_folder               = '/bulk_export_' . $type . '_' . date_i18n( 'Y-m-d' ) . '_' . $batch_id . '/';

		$certificate_post = $setup_parameters['certificate-post'];
		/* Save Path on Server under Upload & allow overwrite */
		$save_path = WP_CONTENT_DIR . apply_filters( 'uo_download_certificates_in_bulk_folder', '/uploads/bulk-certificates' ) . $new_folder;

		$uo_dir = WP_CONTENT_DIR . apply_filters( 'uo_download_certificates_in_bulk_folder', '/uploads/bulk-certificates' );
		if ( ! is_dir( $uo_dir ) ) {
			mkdir( $uo_dir, 0755 );
		}

		/**
		 * New filter added so that arguments can be passed. Adding arguments
		 * to previous filter above might break sites since
		 * there might be no argument supplied with override function
		 *
		 * @since  3.6.4
		 * @author Saad
		 * @var $save_path
		 */
		$save_path = apply_filters( 'uo_download_certificates_in_bulk_upload_dir', $save_path, $user, $object_id, $certificate_post, self::$current_time_stamp );

		/* Creating a fileName that is going to be stored on the server. Certificate-QUIZID-USERID-NONCE_String */
		$file_name = sanitize_title( $user->user_email . '-' . $object_id . '-' . $certificate_post . '-' . date_i18n( 'Y-m-d', self::$current_time_stamp ) . '-' . wp_create_nonce( self::$current_time_stamp ) );

		//Allow overwrite of custom filename
		$file_name = apply_filters( 'uo_download_certificates_in_bulk_filename', $file_name, $user, $object_id, $certificate_post, self::$current_time_stamp );
		if ( ! file_exists( $save_path ) && ! mkdir( $save_path, 0755 ) && ! is_dir( $save_path ) ) { // phpcs:ignore
			throw new RuntimeException( sprintf( 'Directory "%s" was not created', $save_path ) );
		}

		$full_path          = $save_path . $file_name;
		self::$pdf_filename = $full_path;

		//Allow PDF args to be modified
		$generate_pdf_args = apply_filters(
			'uo_download_certificates_in_bulk_pdf_args',
			array(
				'certificate_post' => $certificate_post,
				'save_path'        => $save_path,
				'user'             => $user,
				'file_name'        => $file_name,
				'parameters'       => $setup_parameters,
				'type'             => $type,
				'bulk_generator'   => 'yes',
			),
			$object_id,
			$user_id
		);
		self::generate_pdf( $generate_pdf_args, $type );
	}


Scroll to Top