%s

", esc_html( $options_txt ) ); $premium_screens_displayed = function_exists( 'relevanssi_handle_insights_screens' ) ? relevanssi_handle_insights_screens( $_REQUEST ) : false; if ( ! $premium_screens_displayed ) { if ( 'on' === get_option( 'relevanssi_log_queries' ) ) { relevanssi_query_log(); } else { printf( '

%s

', esc_html__( 'Enable query logging to see stats here.', 'relevanssi' ) ); } } } /** * 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() { global $wpdb, $relevanssi_variables; $data = $wpdb->get_results( 'SELECT LEFT( `time`, 10 ) as `day`, count(*) as `count` ' . "FROM {$relevanssi_variables['log_table']} " . // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared 'GROUP BY LEFT( `time`, 10 )' ); $labels = array(); $values = array(); $from = gmdate( 'Y-m-d' ); foreach ( $data as $point ) { if ( $point->day < $from ) { $from = $point->day; } } wp_verify_nonce( '_relevanssi_nonce', 'relevanssi_user_searches' ); $from_and_to = relevanssi_from_and_to( $_REQUEST, $from ); $to = $from_and_to['to']; $from = $from_and_to['from']; foreach ( $data as $point ) { if ( $point->day >= $from && $point->day <= $to ) { $labels[] = gmdate( 'M j', strtotime( $point->day ) ); $values[] = $point->count; } } ?>

$values, ) ); $total_queries = relevanssi_total_queries( $from, $to ); ?>

'; printf( '

%s

', esc_html__( 'Reset Logs', 'relevanssi' ) ); print( "
" ); wp_nonce_field( 'relevanssi_reset_logs', '_relresnonce', true, true ); // Translators: do not translate "reset". $message = esc_html__( 'To reset the logs, type "reset" into the box here and click the Reset button', 'relevanssi' ); if ( RELEVANSSI_PREMIUM ) { // Translators: do not translate "reset". $message = esc_html__( 'To reset the logs, type "reset" into the box here and click the Reset button. This will reset both the search log and the click tracking log.', 'relevanssi' ); } printf( '

', $message, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- already escaped. esc_html__( 'Reset', 'relevanssi' ) ); } echo ''; } /** * Shows the total number of searches on 'User searches' page. * * @global object $wpdb The WP database interface. * @global array $relevanssi_variables The global Relevanssi variables array. * * @param string $from The start date. * @param string $to The end date. * * @return int The number of searches. */ function relevanssi_total_queries( string $from, string $to ) { global $wpdb, $relevanssi_variables; $log_table = $relevanssi_variables['log_table']; $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM $log_table " // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared . 'WHERE time >= %s AND time <= %s', $from . ' 00:00:00', $to . ' 23:59:59' ) ); return $count; } /** * Shows the total number of searches on 'User searches' page. * * @global object $wpdb The WP database interface. * @global array $relevanssi_variables The global Relevanssi variables array. * * @param string $from The start date. * @param string $to The end date. */ function relevanssi_nothing_found_queries( string $from, string $to ) { global $wpdb, $relevanssi_variables; $log_table = $relevanssi_variables['log_table']; $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM $log_table " // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared . 'WHERE time >= %s AND time <= %s AND hits = 0', $from . ' 00:00:00', $to . ' 23:59:59' ) ); return $count; } /** * 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 string $from The beginning date. * @param string $to The ending date. * @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( string $from, string $to, string $version = 'good' ) { global $wpdb, $relevanssi_variables; $log_table = $relevanssi_variables['log_table']; /** * Filters the number of most common queries to show. * * @param int The number of most common queries to show, default 100. */ $limit = apply_filters( 'relevanssi_user_searches_limit', 100 ); if ( 'good' === $version ) { $queries = $wpdb->get_results( $wpdb->prepare( 'SELECT COUNT(DISTINCT(id)) as cnt, query, AVG(hits) AS hits ' . "FROM $log_table " . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared 'WHERE time >= %s AND time <= %s AND hits > 0 GROUP BY query ORDER BY cnt DESC LIMIT %d', $from . ' 00:00:00', $to . ' 23:59:59', $limit ) ); } if ( 'bad' === $version ) { $queries = $wpdb->get_results( $wpdb->prepare( 'SELECT COUNT(DISTINCT(id)) as cnt, query, hits ' . "FROM $log_table " . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared 'WHERE time >= %s AND time <= %s AND hits = 0 GROUP BY query ORDER BY cnt DESC LIMIT %d', $from . ' 00:00:00', $to . ' 23:59:59', $limit ) ); } if ( count( $queries ) > 0 ) { if ( 'good' === $version ) { printf( "", esc_html__( 'Query', 'relevanssi' ), esc_html__( 'Hits', 'relevanssi' ), esc_html__( 'Clicks', 'relevanssi' ) ); } else { printf( "
%s # %s %s
", esc_html__( 'Query', 'relevanssi' ) ); } $url = get_bloginfo( 'url' ); foreach ( $queries as $query ) { if ( 'good' === $version && function_exists( 'relevanssi_get_query_clicks' ) ) { $clicks = intval( relevanssi_get_query_clicks( $query->query ) ); } else { $clicks = '-'; } $search_parameter = rawurlencode( $query->query ); /** * Filters the query URL for the user searches page. * * @param string Query URL. */ $query_url = apply_filters( 'relevanssi_user_searches_query_url', $url . '/?s=' . $search_parameter ); if ( function_exists( 'relevanssi_insights_link' ) ) { $query_link = relevanssi_insights_link( $query ); } else { $query_link = wp_kses( relevanssi_hyphenate( $query->query ), 'strip' ); } if ( 'good' === $version ) { printf( "", $query_link, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped esc_attr( $query_url ), intval( $query->cnt ), intval( $query->hits ), $clicks // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ); } else { printf( "", $query_link, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped esc_attr( $query_url ), intval( $query->cnt ) ); } } echo '
%s #
%s %d %d %s
%s %d
'; } }