", esc_html__( 'Clearing the logs failed.', 'relevanssi' ) );
}
}
return $result;
}
/**
* Shows the query log with the most common queries
*
* Uses relevanssi_total_queries() and relevanssi_date_queries() to fetch the data.
*/
function relevanssi_query_log() {
/**
* Adjusts the number of days to show the logs in User searches page.
*
* @param int Number of days, default 1.
*/
$days1 = apply_filters( 'relevanssi_1day', 1 );
/**
* Adjusts the number of days to show the logs in User searches page.
*
* @param int Number of days, default 7.
*/
$days7 = apply_filters( 'relevanssi_7days', 7 );
/**
* Adjusts the number of days to show the logs in User searches page.
*
* @param int Number of days, default 30.
*/
$days30 = apply_filters( 'relevanssi_30days', 30 );
printf( '
', esc_html__( 'Common Queries', 'relevanssi' ) );
/**
* Filters the number of rows to show.
*
* @param int Number of top results to show, default 20.
*/
$limit = apply_filters( 'relevanssi_user_searches_limit', 20 );
// Translators: %d is the number of queries to show.
printf( '
%s
', esc_html( sprintf( __( 'Here you can see the %d most common user search queries, how many times those queries were made and how many results were found for those queries.', 'relevanssi' ), $limit ) ) );
echo "
";
if ( 1 === $days1 ) {
relevanssi_date_queries( $days1, __( 'Today and yesterday', 'relevanssi' ) );
} else {
// Translators: number of days to show.
relevanssi_date_queries( $days1, sprintf( __( 'Last %d days', 'relevanssi' ), $days1 ) );
}
echo '
';
echo "
";
// Translators: number of days to show.
relevanssi_date_queries( $days7, sprintf( __( 'Last %d days', 'relevanssi' ), $days7 ) );
echo '
';
echo "
";
// Translators: number of days to show.
relevanssi_date_queries( $days30, sprintf( __( 'Last %d days', 'relevanssi' ), $days30 ) );
echo '
';
}
/**
* Shows the most common search queries on different time periods.
*
* @global object $wpdb The WP database interface.
* @global array $relevanssi_variables The global Relevanssi variables array.
*
* @param int $days The number of days to show.
* @param string $title The title that is printed out on top of the results.
* @param string $version If 'good', show the searches that found something; if
* 'bad', show the searches that didn't find anything. Default 'good'.
*/
function relevanssi_date_queries( $days, $title, $version = 'good' ) {
global $wpdb, $relevanssi_variables;
$log_table = $relevanssi_variables['log_table'];
/** Documented in lib/interface.php. */
$limit = apply_filters( 'relevanssi_user_searches_limit', 20 );
if ( 'good' === $version ) {
$queries = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(id)) as cnt, query, hits
FROM $log_table
WHERE TIMESTAMPDIFF(DAY, time, NOW()) <= %d
GROUP BY query
ORDER BY cnt DESC
LIMIT %d",
$days, $limit
)
); // WPCS: unprepared SQL ok, Relevanssi table name.
}
if ( 'bad' === $version ) {
$queries = $wpdb->get_results(
$wpdb->prepare(
"SELECT COUNT(DISTINCT(id)) as cnt, query, hits
FROM $log_table
WHERE TIMESTAMPDIFF(DAY, time, NOW()) <= %d AND hits = 0
GROUP BY query
ORDER BY cnt DESC
LIMIT %d",
$days, $limit
)
); // WPCS: unprepared SQL ok, Relevanssi table name.
}
if ( count( $queries ) > 0 ) {
printf( "