Filter tin-canny-learndash-reporting

uo_tincanny_reporting_cached_query

Filters the cached query before it's used for Tincanny reporting to allow modification.

add_filter( 'uo_tincanny_reporting_cached_query', $callback, 10, 2 );

Description

Allows developers to modify the cached query used for Uncanny Tin Can reporting data. This filter intercepts the query before it's executed, enabling custom filtering, sorting, or data retrieval logic. Developers can use this to fine-tune the reporting output for specific needs.


Usage

add_filter( 'uo_tincanny_reporting_cached_query', 'your_function_name', 10, 2 );

Parameters

$query (mixed)
This parameter indicates whether or not the query results should be cached.
$cached_query (mixed)
This parameter contains the SQL query string that will be executed to retrieve Tincan data.

Return Value

The filtered value.


Examples

add_filter( 'uo_tincanny_reporting_cached_query', 'my_custom_tincanny_cache_logic', 10, 3 );

/**
 * Custom logic to decide whether to use the cached query for Tincanny reports.
 *
 * This example demonstrates how to conditionally bypass the cache based on
 * specific query parameters, for instance, if the query includes a date range
 * that is too volatile to reliably cache.
 *
 * @param bool   $use_cache      The default decision (true if the cache is valid).
 * @param string $query          The SQL query string being executed.
 * @param array  $cached_query   The cached results array.
 *
 * @return bool Whether to proceed with using the cached query.
 */
function my_custom_tincanny_cache_logic( $use_cache, $query, $cached_query ) {
	// If the default logic determined the cache is invalid, don't override it.
	if ( false === $use_cache ) {
		return false;
	}

	// Example: If the query contains 'DATE(' or 'BETWEEN', assume it's a date-sensitive
	// query and bypass the cache. In a real scenario, you'd likely parse the query
	// more robustly or have a more specific way to identify cacheable queries.
	if ( stripos( $query, 'DATE(' ) !== false || stripos( $query, 'BETWEEN' ) !== false ) {
		// Bypass cache for date-specific queries.
		return false;
	}

	// If we haven't bypassed the cache, return the default decision.
	return $use_cache;
}

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/Database/Admin.php:184

public function get_tincan_data( $start, $length, $count = false ) {
		if ( ! $this->set_user_id() ) {
			return false;
		}

		if ( ! current_user_can( apply_filters( 'uo_tincanny_reporting_capability', 'tincanny_reporting' ) ) ) {
			return false;
		}

		global $wpdb;

		$query = $this->get_tincan_data_query();

		$sort_direction = strtoupper( $this->filters['order'] ?? 'DESC' ); // Default to 'DESC'

		// Validate the sorting direction
		$valid_sort_directions = array( 'ASC', 'DESC' );
		if ( ! in_array( $sort_direction, $valid_sort_directions, true ) ) {
			$sort_direction = 'DESC';
		}

		// Get the column to order by from the filters
		$order_by_column = $this->sorting_columns_mapping();

		$orderby = " ORDER BY $order_by_column $sort_direction ";

		if ( false === $count ) {

			if ( intval( '-1' ) !== intval( $length ) ) {

				$orderby .= " LIMIT $start, $length";
			}
		}

		$query = $query . $orderby;

		// Lets cache the query
		$cache_key = md5( $query );

		$cached_query = wp_cache_get( $cache_key, 'tin-canny-reporting' );

		if ( false !== $cached_query && intval( '-1' ) !== intval( $length ) && true === apply_filters( 'uo_tincanny_reporting_cached_query', true, $query, $cached_query ) ) {
			return true === $count ? count( $cached_query ) : $cached_query;
		}

		$results = $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared

		wp_cache_set( $cache_key, $results, 'tin-canny-reporting', 300 );

		return true === $count ? count( $results ) : $this->build_tincan_report_data( $results );
	}

Scroll to Top