uo_download_certificates_in_bulk_upload_dir
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 Filters the save path for bulk uploaded certificates, allowing customization based on user, object, and certificate details.
add_filter( 'uo_download_certificates_in_bulk_upload_dir', $callback, 10, 3 );
Description
Filters the directory path where bulk-downloaded certificates are saved. This allows developers to dynamically alter the save path based on user, object, or certificate details before the files are written.
Usage
add_filter( 'uo_download_certificates_in_bulk_upload_dir', 'your_function_name', 10, 3 );
Parameters
-
$save_path(mixed) - - **$user** `mixed`
-
$object_id(mixed) - - **$certificate_post** `mixed`
-
$current_time_stamp(mixed)
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the uo_download_certificates_in_bulk_upload_dir filter hook.
*
* This example demonstrates modifying the save path for bulk certificate uploads.
* It appends a user-specific subdirectory to the default save path.
*/
add_filter( 'uo_download_certificates_in_bulk_upload_dir', 'my_custom_bulk_certificate_save_path', 10, 5 );
/**
* Custom function to modify the bulk certificate save path.
*
* @param string $save_path The original save path.
* @param WP_User $user The WP_User object of the current user.
* @param int $object_id The ID of the object the certificate is associated with.
* @param int $certificate_post The ID of the certificate post type.
* @param int $current_time_stamp The current timestamp.
*
* @return string The modified save path.
*/
function my_custom_bulk_certificate_save_path( $save_path, $user, $object_id, $certificate_post, $current_time_stamp ) {
// Ensure the user object is valid and has an ID.
if ( ! $user instanceof WP_User || ! $user->exists() ) {
return $save_path;
}
// Construct a user-specific subdirectory.
// For example, we'll use the user's ID to create a folder like 'user_123/'.
$user_specific_folder = 'user_' . $user->ID . '/';
// Append the user-specific folder to the existing save path.
$modified_save_path = trailingslashit( $save_path ) . $user_specific_folder;
// It's good practice to ensure the directory exists if you're modifying it.
// This should ideally be handled by the core function as well, but double-checking is safe.
if ( ! wp_mkdir_p( $modified_save_path ) ) {
// If directory creation fails, fall back to the original save path or log an error.
// For this example, we'll just return the original path.
error_log( "Failed to create user-specific directory for bulk certificates: " . $modified_save_path );
return $save_path;
}
// Return the modified save path.
return $modified_save_path;
}
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:720
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 );
}