Filter Uncanny Redemption Codes

uncanny-codes-filter-csv-filename

Filters the filename for WooCommerce CSV exports, allowing modification of the default secure filename based on the order.

add_filter( 'uncanny-codes-filter-csv-filename', $callback, 10, 2 );

Description

Filter the automatically generated filename for WooCommerce CSV exports. Modify the `$secure_filename` parameter to customize the file naming convention, perhaps by including order details or date information. This hook offers flexibility in how WooCommerce CSV exports are named.


Usage

add_filter( 'uncanny-codes-filter-csv-filename', 'your_function_name', 10, 2 );

Parameters

$secure_filename (mixed)
This parameter contains the generated filename for the CSV, which can be modified.
$order (mixed)
This parameter holds the dynamically generated filename for the CSV file, allowing for modification.

Return Value

The filtered value.


Examples

add_filter( 'uncanny-codes-filter-csv-filename', 'my_custom_uncanny_codes_csv_filename', 10, 2 );

/**
 * Custom function to modify the CSV filename generated by Uncanny Codes.
 *
 * This function appends the order date to the filename for better organization.
 *
 * @param string $secure_filename The original, secure filename generated by the plugin.
 * @param WC_Order $order The WooCommerce order object.
 * @return string The modified CSV filename.
 */
function my_custom_uncanny_codes_csv_filename( $secure_filename, $order ) {
	// Get the order date.
	$order_date = $order->get_date_created();

	// If the order date exists, format it and append it to the filename.
	if ( $order_date ) {
		$formatted_date = $order_date->format( 'Y-m-d' );
		$secure_filename = str_replace( 'Codes-for-Order', 'Codes-' . $formatted_date . '-Order', $secure_filename );
	}

	// Ensure the filename remains secure and valid.
	return sanitize_file_name( $secure_filename );
}

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/integrations/woocommerce/class-woo-automator-codes.php:563
src/classes/integrations/woocommerce/class-woo-automator-codes.php:1061

public function generate_csv( $order_id ) {
		$order          = wc_get_order( $order_id );
		$items          = $order->get_items();
		$file_paths     = array();
		$directory_path = UNCANNYC_CODES_CSV_PATH;
		if ( ! file_exists( $directory_path ) ) {
			mkdir( $directory_path, 0755, true );
		}
		// create/update .htaccess to protect directory but allow CSV downloads
		if ( apply_filters( 'uncanny_codes_htaccess_folder_protection', true ) ) {
			$htaccess_path        = "{$directory_path}/.htaccess";
			$new_htaccess_content = "# Protect directory from browsingnOptions -Indexesnn# Allow CSV file downloadsn<Files "*.csv">ntOrder allow,denyntAllow from alln</Files>nn# Deny access to everything elsen<FilesMatch "^(?!.*\.csv$).*">ntOrder deny,allowntDeny from alln</FilesMatch>";

			// Check if .htaccess exists and has old content
			if ( ! file_exists( $htaccess_path ) || file_get_contents( $htaccess_path ) === 'deny from all' ) {
				$f_open = fopen( $htaccess_path, 'w' );
				fwrite( $f_open, $new_htaccess_content );
				fclose( $f_open );
			}
		}

		$base_filename   = sprintf( esc_html__( 'Codes-for-Order %d', 'uncanny-learndash-codes' ), $order_id );
		$secure_filename = $this->generate_secure_filename( $order_id, $base_filename );
		$file_name       = sanitize_file_name( apply_filters( 'uncanny-codes-filter-csv-filename', $secure_filename, $order ) );

		// Save file path separately.
		$file_path = $directory_path . DIRECTORY_SEPARATOR . $file_name;
		// check File permission for writing content.
		$file_path = "{$file_path}.csv";
		$this->render_csv( $order_id, $file_path );
		$file_paths[] = $file_path;

		return $file_paths;
	}


Scroll to Top