Filter tin-canny-learndash-reporting

tincanny_csv_row_data

Filters the data for a single row within the CSV export, allowing modification before it's added to the output.

add_filter( 'tincanny_csv_row_data', $callback, 10, 1 );

Description

Fires for each row of CSV data before it's written to the file. Developers can use this filter to modify or add to individual row data. Ensure the returned value is an array.


Usage

add_filter( 'tincanny_csv_row_data', 'your_function_name', 10, 1 );

Parameters

$value (mixed)
This parameter contains the data for a single row in the CSV file, which is then passed to the filter for modification.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'tincanny_csv_row_data' filter to modify row data before it's outputted to a CSV.
 *
 * In this example, we'll assume the CSV is for Uncanny Toolkit reporting.
 * We might want to format a date field or convert a boolean to a more human-readable string.
 */
add_filter( 'tincanny_csv_row_data', function( $row_data ) {
	// Ensure $row_data is an array. The hook already casts it, but good practice to check.
	if ( ! is_array( $row_data ) ) {
		return $row_data;
	}

	// Example: If a 'completion_date' key exists, reformat it.
	if ( isset( $row_data['completion_date'] ) && ! empty( $row_data['completion_date'] ) ) {
		try {
			// Assuming completion_date is in a standard database format like 'YYYY-MM-DD HH:MM:SS'
			$date_object = new DateTime( $row_data['completion_date'] );
			// Format it to something more user-friendly for a CSV, e.g., 'MM/DD/YYYY'
			$row_data['completion_date'] = $date_object->format( 'm/d/Y' );
		} catch ( Exception $e ) {
			// Handle potential errors if the date string is invalid.
			// For this example, we'll just log the error and leave the original data.
			error_log( 'Uncanny CSV: Error formatting completion_date: ' . $e->getMessage() );
		}
	}

	// Example: Convert a boolean 'is_active' field to 'Yes' or 'No'.
	if ( isset( $row_data['is_active'] ) ) {
		$row_data['is_active'] = ( $row_data['is_active'] ) ? __( 'Yes', 'your-text-domain' ) : __( 'No', 'your-text-domain' );
	}

	// Always return the modified (or original) data.
	return $row_data;
}, 10, 1 ); // Priority 10, accepts 1 argument ($row_data).

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/uncanny-tincan/classes/Admin/CSV.php:88

private function print_csv() {
		foreach( $this->data as $value ) {
			fputcsv( $this->output , (array) apply_filters( 'tincanny_csv_row_data', (array) $value ) );
		}
	}

Scroll to Top