Filter tin-canny-learndash-reporting

uo_tincanny_tincan_xapi_report_date_range_last_type

Filters the last report date range type used in the Tincanny XAPI reporting.

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

Description

Filters the selected date range type for XAPI reports. Allows developers to modify or override the default 'last' or 'custom' date range filtering based on the 'tc_filter_date_range' parameter. This hook is used internally to handle date range selection logic for Tincanny reports.


Usage

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

Parameters

$request (mixed)
This parameter represents the raw date range filter value retrieved from the request.

Return Value

The filtered value.


Examples

<?php
/**
 * Filter to modify the 'last' date range for XAPI reports based on user selection.
 *
 * This function allows for finer-grained control over the 'last' date range,
 * such as specifying 'week', 'month', 'year', or a custom number of days.
 *
 * @param mixed $request The request object containing parameters.
 * @return mixed The modified request object with updated date range settings.
 */
add_filter( 'uo_tincanny_tincan_xapi_report_date_range_last_type', function( $date_range_last_type, $request ) {

	if ( ! $request->has_param( 'tc_filter_date_range_last' ) || empty( $request->get_param( 'tc_filter_date_range_last' ) ) ) {
		return $date_range_last_type; // Return original if parameter is not set
	}

	$date_range_last = sanitize_text_field( $request->get_param( 'tc_filter_date_range_last' ) );
	$current_time    = current_time( 'timestamp' ); // Current timestamp

	switch ( $date_range_last ) {
		case 'week':
			// Set date range to the last full week (Monday to Sunday)
			$dateStart = date( 'Y-m-d 00:00:00', strtotime( 'last week Monday', $current_time ) );
			$dateEnd   = date( 'Y-m-d 23:59:59', strtotime( 'last week Sunday', $current_time ) );
			break;
		case 'month':
			// Set date range to the first day of the last month to the last day of the last month
			$dateStart = date( 'Y-m-d 00:00:00', strtotime( 'first day of last month', $current_time ) );
			$dateEnd   = date( 'Y-m-d 23:59:59', strtotime( 'last day of last month', $current_time ) );
			break;
		case 'year':
			// Set date range to the first day of the last year to the last day of the last year
			$dateStart = date( 'Y-m-d 00:00:00', strtotime( 'first day of last year', $current_time ) );
			$dateEnd   = date( 'Y-m-d 23:59:59', strtotime( 'last day of last year', $current_time ) );
			break;
		default:
			// Attempt to parse as a number of days if not a predefined unit
			if ( is_numeric( $date_range_last ) ) {
				$days = intval( $date_range_last );
				if ( $days > 0 ) {
					$dateStart = date( 'Y-m-d 00:00:00', strtotime( "-{$days} days", $current_time ) );
					$dateEnd   = date( 'Y-m-d 23:59:59', $current_time );
				}
			}
			break;
	}

	// Assuming $request object has properties to store these dates, or we modify it in place
	// For demonstration, we'll simulate adding them to the request object.
	// In a real scenario, you might directly modify a global variable, a class property,
	// or return an updated object if that's how the original code is structured.
	if ( isset( $dateStart ) && isset( $dateEnd ) ) {
		// Example: If $request is a WP_REST_Request object or similar, you might set parameters.
		// If the original code directly modifies a class property like self::$tincan_database,
		// this filter would need to update that.
		// For this example, let's assume we're returning an array with the updated dates.
		// Adjust this based on the actual usage context.
		return array(
			'dateStart' => $dateStart,
			'dateEnd'   => $dateEnd,
		);
	}

	return $date_range_last_type; // Return original if no valid date range was set
}, 10, 2 ); // Priority 10, accepts 2 arguments ($date_range_last_type, $request)

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/shortcode-tincanny/shortcode-tincanny.php:559

if ( $request->has_param( 'tc_filter_date_range' ) && ! empty( $request->get_param( 'tc_filter_date_range' ) ) ) {
			$date_range   = apply_filters( 'uo_tincanny_tincan_xapi_report_date_range_type', sanitize_text_field( $request->get_param( 'tc_filter_date_range' ) ) );
			$current_time = current_time( 'timestamp' ); // Current timestamp

			switch ( $date_range ) {
				case 'last':
					if ( $request->has_param( 'tc_filter_date_range_last' ) && ! empty( $request->get_param( 'tc_filter_date_range_last' ) ) ) {
						$date_range_last = apply_filters( 'uo_tincanny_tincan_xapi_report_date_range_last_type', sanitize_text_field( $request->get_param( 'tc_filter_date_range_last' ) ) );
						switch ( $date_range_last ) {
							case 'week':
								self::$tincan_database->dateStart = date( 'Y-m-d 00:00:00', strtotime( 'last week Monday', $current_time ) );
								self::$tincan_database->dateEnd   = date( 'Y-m-d 23:59:59', strtotime( 'last week Sunday', $current_time ) );
								break;
							case 'month':
								self::$tincan_database->dateStart = date( 'Y-m-d 00:00:00', strtotime( 'first day of last month', $current_time ) );


Scroll to Top