/** * Get the Search Heatmap. * * @since 1.2 * * @param array|string $args Heatmap Parameters * @return string Search heatmap */ function get_bsearch_heatmap($args = array()) { global $wpdb, $bsearch_url, $bsearch_settings; $defaults = array('daily' => false, 'smallest' => intval($bsearch_settings['heatmap_smallest']), 'largest' => intval($bsearch_settings['heatmap_largest']), 'unit' => $bsearch_settings['heatmap_unit'], 'cold' => $bsearch_settings['heatmap_cold'], 'hot' => $bsearch_settings['heatmap_hot'], 'before' => $bsearch_settings['heatmap_before'], 'after' => $bsearch_settings['heatmap_after'], 'heatmap_limit' => intval($bsearch_settings['heatmap_limit']), 'daily_range' => intval($bsearch_settings['daily_range'])); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); $table_name = $wpdb->prefix . 'bsearch'; if ($args['daily']) { $table_name .= '_daily'; // If we're viewing daily posts, set this to true } $output = ''; if (!$args['daily']) { $sargs = array($args['heatmap_limit']); $sql = "SELECT searchvar, cntaccess FROM {$table_name} WHERE accessedid IN (SELECT accessedid FROM {$table_name} WHERE searchvar <> '' ORDER BY cntaccess DESC, searchvar ASC) ORDER by accessedid LIMIT %d"; } else { $current_time = current_time('timestamp', 0); $current_time = $current_time - ($args['daily_range'] - 1) * 3600 * 24; $current_date = date_i18n('Y-m-j', $current_time); $sargs = array($current_date, $args['heatmap_limit']); $sql = "\n\t\t\tSELECT DISTINCT wp1.searchvar, wp2.sumCount\n\t\t\tFROM {$table_name} wp1,\n\t\t\t\t\t(SELECT searchvar, SUM(cntaccess) as sumCount\n\t\t\t\t\tFROM {$table_name}\n\t\t\t\t\tWHERE dp_date >= '%s'\n\t\t\t\t\tGROUP BY searchvar\n\t\t\t\t\tORDER BY sumCount DESC LIMIT %d) wp2\n\t\t\t\t\tWHERE wp1.searchvar = wp2.searchvar\n\t\t\tORDER by wp1.searchvar ASC\n\t\t"; } $results = $wpdb->get_results($wpdb->prepare($sql, $sargs)); if ($results) { foreach ($results as $result) { if (!$args['daily']) { $cntaccesss[] = $result->cntaccess; } else { $cntaccesss[] = $result->sumCount; } } $min = min($cntaccesss); $max = max($cntaccesss); $spread = $max - $min; // Calculate various font sizes if ($args['largest'] != $args['smallest']) { $fontspread = $args['largest'] - $args['smallest']; if (0 != $spread) { $fontstep = $fontspread / $spread; } else { $fontstep = 0; } } // Calculate colors if ($args['hot'] != $args['cold']) { $hotdec = bsearch_html2rgb($args['hot']); $colddec = bsearch_html2rgb($args['cold']); for ($i = 0; $i < 3; $i++) { $coldval[] = $colddec[$i]; $hotval[] = $hotdec[$i]; $colorspread[] = $hotdec[$i] - $colddec[$i]; if (0 != $spread) { $colorstep[] = ($hotdec[$i] - $colddec[$i]) / $spread; } else { $colorstep[] = 0; } } } foreach ($results as $result) { if (!$args['daily']) { $cntaccess = $result->cntaccess; } else { $cntaccess = $result->sumCount; } $textsearchvar = esc_attr($result->searchvar); $url = home_url() . '/?s=' . $textsearchvar; $fraction = $cntaccess - $min; $fontsize = $args['smallest'] + $fontstep * $fraction; $color = ''; for ($i = 0; $i < 3; $i++) { $color .= dechex($coldval[$i] + $colorstep[$i] * $fraction); } $style = 'style="'; if ($args['largest'] != $args['smallest']) { $style .= 'font-size:' . round($fontsize) . $args['unit'] . ';'; } if ($args['hot'] != $args['cold']) { $style .= 'color:#' . $color . ';'; } $style .= '"'; $output .= $args['before'] . '<a href="' . $url . '" title="'; $output .= sprintf(_n('Search for %1$s (%2$s search)', 'Search for %1$s (%2$s searches)', $cntaccess, 'better-search'), $textsearchvar, $cntaccess); $output .= '" ' . $style; if ($bsearch_settings['link_nofollow']) { $output .= ' rel="nofollow" '; } if ($bsearch_settings['link_new_window']) { $output .= ' target="_blank" '; } $output .= '>' . $textsearchvar . '</a>' . $args['after'] . ' '; } } else { $output = __('No searches made yet', 'better-search'); } /** * Filter formatted string with the search heatmap * * @since 1.2 * * @param string $output Formatted excerpt * @param string|array $args Arguments */ return apply_filters('get_bsearch_heatmap', $output, $args); }
/** * Get the Search Heatmap. * * @access public * @param array|string $args Heatmap Parameters * @return string */ function get_bsearch_heatmap($args = array()) { global $wpdb, $bsearch_url, $bsearch_settings; $defaults = array('daily' => FALSE, 'smallest' => intval($bsearch_settings['heatmap_smallest']), 'largest' => intval($bsearch_settings['heatmap_largest']), 'unit' => $bsearch_settings['heatmap_unit'], 'cold' => $bsearch_settings['heatmap_cold'], 'hot' => $bsearch_settings['heatmap_hot'], 'before' => $bsearch_settings['heatmap_before'], 'after' => $bsearch_settings['heatmap_after'], 'heatmap_limit' => intval($bsearch_settings['heatmap_limit']), 'daily_range' => intval($bsearch_settings['daily_range'])); // Parse incomming $args into an array and merge it with $defaults $args = wp_parse_args($args, $defaults); // OPTIONAL: Declare each item in $args as its own variable i.e. $type, $before. extract($args, EXTR_SKIP); $table_name = $wpdb->prefix . "bsearch"; if ($daily) { $table_name .= "_daily"; // If we're viewing daily posts, set this to true } $output = ''; if (!$daily) { $args = array($heatmap_limit); $sql = "SELECT searchvar, cntaccess FROM {$table_name} WHERE accessedid IN (SELECT accessedid FROM {$table_name} WHERE searchvar <> '' ORDER BY cntaccess DESC, searchvar ASC) ORDER by accessedid LIMIT %d"; } else { $current_time = current_time('timestamp', 0); $current_time = $current_time - ($daily_range - 1) * 3600 * 24; $current_date = date('Y-m-j', $current_time); $args = array($current_date, $heatmap_limit); $sql = "\r\n\t\t\tSELECT DISTINCT wp1.searchvar, wp2.sumCount\r\n\t\t\tFROM {$table_name} wp1,\r\n\t\t\t\t\t(SELECT searchvar, SUM(cntaccess) as sumCount\r\n\t\t\t\t\tFROM {$table_name}\r\n\t\t\t\t\tWHERE dp_date >= '%s'\r\n\t\t\t\t\tGROUP BY searchvar\r\n\t\t\t\t\tORDER BY sumCount DESC LIMIT %d) wp2\r\n\t\t\t\t\tWHERE wp1.searchvar = wp2.searchvar\r\n\t\t\tORDER by wp1.searchvar ASC\r\n\t\t"; } $results = $wpdb->get_results($wpdb->prepare($sql, $args)); if ($results) { foreach ($results as $result) { if (!$daily) { $cntaccesss[] = $result->cntaccess; } else { $cntaccesss[] = $result->sumCount; } } $min = min($cntaccesss); $max = max($cntaccesss); $spread = $max - $min; // Calculate various font sizes if ($largest != $smallest) { $fontspread = $largest - $smallest; if (0 != $spread) { $fontstep = $fontspread / $spread; } else { $fontstep = 0; } } // Calculate colors if ($hot != $cold) { $hotdec = bsearch_html2rgb($hot); $colddec = bsearch_html2rgb($cold); for ($i = 0; $i < 3; $i++) { $coldval[] = $colddec[$i]; $hotval[] = $hotdec[$i]; $colorspread[] = $hotdec[$i] - $colddec[$i]; if (0 != $spread) { $colorstep[] = ($hotdec[$i] - $colddec[$i]) / $spread; } else { $colorstep[] = 0; } } } foreach ($results as $result) { if (!$daily) { $cntaccess = $result->cntaccess; } else { $cntaccess = $result->sumCount; } $textsearchvar = esc_attr($result->searchvar); $url = home_url() . '/?s=' . $textsearchvar; $fraction = $cntaccess - $min; $fontsize = $smallest + $fontstep * $fraction; $color = ""; for ($i = 0; $i < 3; $i++) { $color .= dechex($coldval[$i] + $colorstep[$i] * $fraction); } $style = 'style="'; if ($largest != $smallest) { $style .= "font-size:" . round($fontsize) . $unit . ";"; } if ($hot != $cold) { $style .= "color:#" . $color . ";"; } $style .= '"'; $output .= $before . '<a href="' . $url . '" title="'; $output .= sprintf(_n('Search for %1$s (%2$s search)', 'Search for %1$s (%2$s searches)', $cntaccess, BSEARCH_LOCAL_NAME), $textsearchvar, $cntaccess); $output .= '" ' . $style; if ($bsearch_settings['link_nofollow']) { $output .= ' rel="nofollow" '; } if ($bsearch_settings['link_new_window']) { $output .= ' target="_blank" '; } $output .= '>' . $textsearchvar . '</a>' . $after . ' '; } } else { $output = __('No searches made yet', BSEARCH_LOCAL_NAME); } return apply_filters('get_bsearch_heatmap', $output); }