Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function tguy_sm_stats_css() {
function tguy_sm_init() {
tguy_sm_create_summary_table();
tguy_sm_create_recent_table();
tguy_sm_create_word_table();
}

function tguy_sm_create_summary_table() {
Expand Down Expand Up @@ -148,6 +149,25 @@ function tguy_sm_create_recent_table() {
}
}

function tguy_sm_create_word_table() {
// Create the table if not already there.
global $wpdb;
$table_name = $wpdb->prefix . "searchmeter_word";
if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
dbDelta("
CREATE TABLE `{$table_name}` (
`word` VARCHAR(50) NOT NULL,
`date` DATE NOT NULL,
`count` INT(11) NOT NULL,
PRIMARY KEY (`word`,`date`)
)
{$charset_collate};
");
}
}


//////// Permissions

Expand Down Expand Up @@ -231,6 +251,11 @@ function tguy_sm_summary_page() {
"DELETE FROM `{$wpdb->prefix}searchmeter`
WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)");
echo "<!-- Search Meter: deleted $result old rows -->\n";

$result = $wpdb->query(
"DELETE FROM `{$wpdb->prefix}searchmeter_word`
WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)");
echo "<!-- Search Meter: deleted $result old rows from word -->\n";
?>
<div class="wrap">

Expand Down Expand Up @@ -267,6 +292,21 @@ function tguy_sm_summary_page() {
</div>
<div class="sm-stats-clear"></div>

<h2><?php _e('Search summary words', 'search-meter') ?></h2>
<div class="sm-stats-table">
<h3><?php _e('Yesterday and today', 'search-meter') ?></h3>
<?php tguy_sm_summary_word_table(1); ?>
</div>
<div class="sm-stats-table">
<h3><?php _e('Last 7 days', 'search-meter') ?></h3>
<?php tguy_sm_summary_word_table(7); ?>
</div>
<div class="sm-stats-table">
<h3><?php _e('Last 30 days', 'search-meter') ?></h3>
<?php tguy_sm_summary_word_table(30); ?>
</div>
<div class="sm-stats-clear"></div>

<h2><?php _e('Unsuccessful search summary', 'search-meter') ?></h2>

<p><?php _e('These tables show only the search terms for which the last search yielded no results. People are searching your blog for these terms; maybe you should give them what they want.', 'search-meter') ?></p>
Expand Down Expand Up @@ -374,6 +414,40 @@ function tguy_sm_summary_table($days, $do_include_successes = true) {
}
}

function tguy_sm_summary_word_table($days) {
global $wpdb;
$results = $wpdb->get_results(
"SELECT `word`,
SUM( `count` ) AS countsum
FROM `{$wpdb->prefix}searchmeter_word`
WHERE DATE_SUB( CURDATE( ) , INTERVAL $days DAY ) <= `date`
GROUP BY `word`
ORDER BY countsum DESC, `word` ASC
LIMIT 20");
if (count($results)) {
?>
<table cellpadding="3" cellspacing="2">
<tbody>
<tr class="alternate"><th class="sm-text"><?php _e('Word', 'search-meter') ?></th><th><?php _e('Searches', 'search-meter') ?></th></tr>
<?php
$class= '';
foreach ($results as $result) {
?>
<tr class="<?php echo $class ?>">
<td><a href="<?php echo get_bloginfo('wpurl').'/wp-admin/edit.php?s='.urlencode($result->word).'&submit=Search' ?>"><?php echo htmlspecialchars($result->word) ?></a></td>
<td class="sm-number"><?php echo $result->countsum ?></td></tr>
<?php
$class = ($class == '' ? 'alternate' : '');
}
?>
</tbody>
</table>
<?php
} else {
?><p><em><?php _e('No searches recorded for this period.', 'search-meter') ?></em></p><?php
}
}

function tguy_sm_recent_page($max_lines, $do_show_details) {
global $wpdb;

Expand Down
23 changes: 23 additions & 0 deletions search-meter.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,29 @@ function tguy_sm_save_search($posts) {
));
}
++$tguy_sm_save_count;

// Save search word into the DB. Usually this will be a new row, so try to insert first
$exploded = explode(' ', $search_terms);
foreach($exploded as $word) {
if($word === '') {
continue;
}
$suppress = $wpdb->suppress_errors();
$success = $wpdb->query($wpdb->prepare("
INSERT INTO `{$wpdb->prefix}searchmeter_word` (`word`,`date`,`count`)
VALUES (%s, UTC_DATE(), 1)",
$word
));
$wpdb->suppress_errors($suppress);
if (!$success) {
$success = $wpdb->query($wpdb->prepare("
UPDATE `{$wpdb->prefix}searchmeter_word` SET
`count` = `count` + 1
WHERE `word` = %s AND `date` = UTC_DATE()",
$word
));
}
}
}
return $posts;
}