uo_tincanny_xapi_report_cached_query
Filters the XAPI report query, allowing modification of cached query results before they are returned.
add_filter( 'uo_tincanny_xapi_report_cached_query', $callback, 10, 2 );
Description
Allows modification of the Xapi query args before fetching cached Xapi data. Developers can alter the query parameters to customize the data retrieval process, potentially influencing sorting, filtering, or selecting specific Xapi statements.
Usage
add_filter( 'uo_tincanny_xapi_report_cached_query', 'your_function_name', 10, 2 );
Parameters
-
$query(mixed) - This parameter appears to be a boolean flag, likely indicating whether the XAPI data should be retrieved from a cache.
-
$cached_query(mixed) - This parameter contains the SQL query string that will be used to retrieve Xapi data from the database.
Return Value
The filtered value.
Examples
/**
* Example of how to use the uo_tincanny_xapi_report_cached_query filter.
* This example will always return false for the filter, effectively disabling caching
* for XAPI reports, which might be useful for debugging or forcing fresh data.
*
* @param bool $use_cache Whether to use the cached query result.
* @param string $query The generated SQL query string.
* @param array $cached_query The cached query result (if found).
* @return bool Always returns false to disable caching.
*/
add_filter(
'uo_tincanny_xapi_report_cached_query',
function ( $use_cache, $query, $cached_query ) {
// For demonstration purposes, we'll always return false to disable caching.
// In a real-world scenario, you might want to inspect $query or $cached_query
// to decide whether to use the cache or not based on specific conditions.
return false;
},
10, // Priority
3 // Number of accepted arguments
);
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:421
public function get_xapi_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_xapi_query_string();
$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, 'xapi-quiz-reporting' );
if ( false !== $cached_query && '-1' !== intval( $length ) && true === apply_filters( 'uo_tincanny_xapi_report_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_xapi_quiz_report_data( $results );
}