get_results( "SELECT `terms`, SUM(`count`) AS countsum FROM `{$wpdb->prefix}searchmeter` WHERE DATE_SUB( UTC_DATE( ) , INTERVAL 30 DAY ) <= `date` AND 0 < `last_hits` {$filter_term} GROUP BY `terms` ORDER BY countsum DESC, `terms` ASC LIMIT $count"); if (count($results)) { echo "$before\n
' . __('Powered by Search Meter', 'search-meter') . '
'; } } class SM_Recent_Searches_Widget extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widget_search_meter', 'description' => __( "A list of the most recent successful searches on your blog", 'search-meter')); parent::__construct('recent_searches', __('Recent Searches', 'search-meter'), $widget_ops); } function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', empty($instance['recent-searches-title']) ? __('Recent Searches', 'search-meter') : $instance['recent-searches-title']); $count = (int) (empty($instance['recent-searches-number']) ? 5 : $instance['recent-searches-number']); echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } sm_list_recent_searches('', '', sm_constrain_widget_search_count($count)); echo $after_widget; } function update($new_instance, $old_instance){ $instance = $old_instance; $instance['recent-searches-title'] = strip_tags(stripslashes($new_instance['recent-searches-title'])); $instance['recent-searches-number'] = (int) ($new_instance['recent-searches-number']); return $instance; } function form($instance){ //Defaults $instance = wp_parse_args((array) $instance, array('recent-searches-title' => __('Recent Searches', 'search-meter'), 'recent-searches-number' => 5)); $title = htmlspecialchars($instance['recent-searches-title']); $count = htmlspecialchars($instance['recent-searches-number']); # Output the options echo ''; echo ''; echo '' . __('Powered by Search Meter', 'search-meter') . '
'; } } function sm_constrain_widget_search_count($number) { return max(1, min((int)$number, 100)); } // Keep track of how many times this search has been saved. // The save function may be called many times; normally we only save the first time. $tguy_sm_save_count = 0; function tguy_sm_save_search($posts) { // Check if the request is a search, and if so then save details. // This is a filter but does not change the posts. global $wpdb, $wp_query, $tguy_sm_save_count; // The filter may get called more than once for a given request. We ignore these duplicates. // Recording duplicate searches can be enabled by adding this line to functions.php: // add_filter('search_meter_record_duplicates', function() { return true; }); // Setting to true will record duplicates (the fact that it's a dupe will be recorded in the // details). This will mess up the stats, but could be useful for troubleshooting. $record_duplicates = apply_filters('search_meter_record_duplicates', false); if (is_search() && !is_paged() // not the second or subsequent page of a previously-counted search && !tguy_is_admin_interface() // not using the administration console && (0 === $tguy_sm_save_count || $record_duplicates) && (tguy_sm_array_value($_SERVER, 'HTTP_REFERER')) // proper referrer (otherwise could be search engine, cache...) ) { $options = get_option('tguy_search_meter'); // Break out if we're supposed to ignore admin searches if (tguy_sm_array_value($options, 'sm_ignore_admin_search') && current_user_can("manage_options")) { return $posts; // EARLY EXIT } // Get all details of this search // search string is the raw query $search_string = $wp_query->query_vars['s']; if (get_magic_quotes_gpc()) { $search_string = stripslashes($search_string); } // search terms is the words in the query $search_terms = $search_string; $search_terms = preg_replace('/[," ]+/', ' ', $search_terms); $search_terms = trim($search_terms); $hit_count = $wp_query->found_posts; // Thanks to Will for this line // Other useful details of the search $details = ''; if (tguy_sm_array_value($options, 'sm_details_verbose')) { if ($record_duplicates) { $details .= __('Search Meter save count', 'search-meter') . ": $tguy_sm_save_count\n"; } foreach (array('REQUEST_URI','REQUEST_METHOD','QUERY_STRING','REMOTE_ADDR','HTTP_USER_AGENT','HTTP_REFERER') as $header) { $details .= $header . ': ' . tguy_sm_array_value($_SERVER, $header) . "\n"; } } // Save the individual search to the DB $success = $wpdb->query($wpdb->prepare(" INSERT INTO `{$wpdb->prefix}searchmeter_recent` (`terms`,`datetime`,`hits`,`details`) VALUES (%s, UTC_TIMESTAMP(), %d, %s)", $search_string, $hit_count, $details )); if ($success) { $rowcount = $wpdb->get_var( "SELECT count(`datetime`) as rowcount FROM `{$wpdb->prefix}searchmeter_recent`"); // History size can be overridden by a user by adding a line like this to functions.php: // add_filter('search_meter_history_size', function() { return 50000; }); $history_size = apply_filters('search_meter_history_size', 500); // Ensure history table never grows larger than (history size) + 100; truncate it // to (history size) when it gets too big. (This we way will only truncate the table // every 100 searches, rather than every time.) if ($history_size + 100 < $rowcount) { // find time of ($history_size)th entry; delete everything before that $dateZero = $wpdb->get_var($wpdb->prepare( "SELECT `datetime` FROM `{$wpdb->prefix}searchmeter_recent` ORDER BY `datetime` DESC LIMIT %d, 1", $history_size)); $query = "DELETE FROM `{$wpdb->prefix}searchmeter_recent` WHERE `datetime` < '$dateZero'"; $success = $wpdb->query($query); } } // Save search summary into the DB. Usually this will be a new row, so try to insert first // Temporarily suppress errors, as this query is expected to fail on duplicate searches in a single day. Thanks to James Collins. $suppress = $wpdb->suppress_errors(); $success = $wpdb->query($wpdb->prepare(" INSERT INTO `{$wpdb->prefix}searchmeter` (`terms`,`date`,`count`,`last_hits`) VALUES (%s, UTC_DATE(), 1, %d)", $search_terms, $hit_count )); $wpdb->suppress_errors($suppress); if (!$success) { $success = $wpdb->query($wpdb->prepare(" UPDATE `{$wpdb->prefix}searchmeter` SET `count` = `count` + 1, `last_hits` = %d WHERE `terms` = %s AND `date` = UTC_DATE()", $hit_count, $search_terms )); } ++$tguy_sm_save_count; } return $posts; }