Search Meter - Version 2.9.1

Version Description

  • Ensure Search Meter can save searches even if other plugins trigger a query before the main WordPress loop.
Download this release

Release Info

Developer bennettmcelwee
Plugin Icon 128x128 Search Meter
Version 2.9.1
Comparing to
See all releases

Code changes from version 2.9 to 2.9.1

Files changed (3) hide show
  1. admin.php +609 -609
  2. readme.txt +5 -2
  3. search-meter.php +10 -10
admin.php CHANGED
@@ -1,609 +1,609 @@
1
- <?php
2
- /*
3
- Copyright (C) 2005-12 Bennett McElwee (bennett at thunderguy dotcom)
4
-
5
- This program is free software; you can redistribute it and/or
6
- modify it under the terms of version 2 of the GNU General Public
7
- License as published by the Free Software Foundation.
8
-
9
- This program is distributed in the hope that it will be useful,
10
- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- GNU General Public License for more details, available at
13
- http://www.gnu.org/copyleft/gpl.html
14
- */
15
-
16
- //////// Parameters
17
-
18
-
19
- define('TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY', 'publish_posts');
20
- // Default capability users must have in order to see stats.
21
-
22
- define('TGUY_SM_OPTIONS_CAPABILITY', 'manage_options');
23
- // Capability users must have in order to set options.
24
-
25
-
26
- //////// General admin
27
-
28
-
29
- add_action('admin_head', 'tguy_sm_stats_css');
30
-
31
- function tguy_sm_stats_css() {
32
- ?>
33
- <style type="text/css">
34
- #search_meter_menu {
35
- line-height: 1.4em;
36
- margin: 5px 0 0 0;
37
- padding: 0;
38
- border-bottom: 1px solid #aaaaaa;
39
- }
40
- #search_meter_menu li {
41
- border: 1px solid #aaaaaa;
42
- border-bottom: none;
43
- line-height: 1.4em;
44
- display: inline-block;
45
- margin: 0 5px 0 0;
46
- padding: 0;
47
- list-style-type: none;
48
- list-style-image: none;
49
- list-style-position: outside;
50
- }
51
- #search_meter_menu li.current span {
52
- background-color: #ffffff;
53
- font-weight: bold;
54
- padding: 0 5px 3px 5px;
55
- }
56
- #search_meter_menu li a,
57
- #search_meter_menu li a:visited {
58
- padding: 0 5px;
59
- text-decoration: none;
60
- }
61
- #search_meter_menu li a:hover {
62
- background-color: #eaf2fa;
63
- }
64
- #search_meter_menu + .wrap {
65
- margin-top: 0;
66
- }
67
- div.sm-stats-table {
68
- float: left;
69
- padding-right: 3em;
70
- padding-bottom: 1.5em;
71
- }
72
- div.sm-stats-table th, div.sm-stats-table td {
73
- padding-right: 0.5em;
74
- }
75
- div.sm-stats-table h3 {
76
- margin-top: 0;
77
- margin-bottom: 0.5em;
78
- }
79
- div.sm-stats-table .left {
80
- text-align: left;
81
- }
82
- div.sm-stats-table .right {
83
- text-align: right;
84
- }
85
- div.sm-stats-clear {
86
- clear: both;
87
- }
88
-
89
- /* Dashboard widget overrides */
90
- #dashboard_search_meter h4 {
91
- line-height: 1.7em;
92
- }
93
- #dashboard_search_meter div.sm-stats-table {
94
- float: none;
95
- padding-bottom: 0;
96
- padding-right: 0;
97
- }
98
- #dashboard_search_meter div.sm-stats-table th {
99
- color: #8F8F8F;
100
- }
101
- #dashboard_search_meter ul.subsubsub {
102
- float: none;
103
- }
104
-
105
- </style>
106
- <?php
107
- }
108
-
109
-
110
- //////// Initialisation
111
-
112
-
113
- function tguy_sm_init() {
114
- tguy_sm_create_summary_table();
115
- tguy_sm_create_recent_table();
116
- }
117
-
118
- function tguy_sm_create_summary_table() {
119
- // Create the table if not already there.
120
- global $wpdb;
121
- $table_name = $wpdb->prefix . "searchmeter";
122
- if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
123
- require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
124
- dbDelta("
125
- CREATE TABLE `{$table_name}` (
126
- `terms` VARCHAR(50) NOT NULL,
127
- `date` DATE NOT NULL,
128
- `count` INT(11) NOT NULL,
129
- `last_hits` INT(11) NOT NULL,
130
- PRIMARY KEY (`terms`,`date`)
131
- )
132
- CHARACTER SET utf8 COLLATE utf8_general_ci;
133
- ");
134
- }
135
- }
136
-
137
- function tguy_sm_create_recent_table() {
138
- // Create the table if not already there.
139
- global $wpdb;
140
- $table_name = $wpdb->prefix . "searchmeter_recent";
141
- if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
142
- require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
143
- dbDelta("
144
- CREATE TABLE `{$table_name}` (
145
- `terms` VARCHAR(50) NOT NULL,
146
- `datetime` DATETIME NOT NULL,
147
- `hits` INT(11) NOT NULL,
148
- `details` TEXT NOT NULL,
149
- KEY `datetimeindex` (`datetime`)
150
- )
151
- CHARACTER SET utf8 COLLATE utf8_general_ci;
152
- ");
153
- }
154
- }
155
-
156
-
157
- //////// Permissions
158
-
159
-
160
- function smcln_sm_can_view_stats() {
161
- $options = get_option('tguy_search_meter');
162
- $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
163
- if ($view_stats_capability == '') {
164
- $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
165
- }
166
- return current_user_can($view_stats_capability);
167
- }
168
-
169
-
170
- //////// Dashboard widget
171
-
172
- add_action('wp_dashboard_setup', 'smcln_sm_dashboard');
173
-
174
- // Add the widget to the dashboard
175
- function smcln_sm_dashboard() {
176
- if (smcln_sm_can_view_stats()) {
177
- wp_add_dashboard_widget( 'dashboard_search_meter', 'Search Meter', 'smcln_sm_summary');
178
- }
179
- }
180
-
181
- // Render the summary widget
182
- function smcln_sm_summary() {
183
- ?>
184
- <div class="sm-stats-table">
185
- <h4>Searches in the Last 7 Days</h4>
186
- <?php tguy_sm_summary_table(7); ?>
187
- </div>
188
- <ul class="subsubsub">
189
- <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>">Full Dashboard</a> |</li>
190
- <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
191
- <li><a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Settings</a> |</li>
192
- <?php endif; ?>
193
- <li><a href="http://thunderguy.com/semicolon/donate/">Donate</a></li>
194
- </ul>
195
- <?php
196
- }
197
-
198
-
199
- //////// Admin pages
200
-
201
-
202
- add_action('admin_menu', 'tguy_sm_add_admin_pages');
203
-
204
- function tguy_sm_add_admin_pages() {
205
- $options = get_option('tguy_search_meter');
206
- $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
207
- if ($view_stats_capability == '') {
208
- $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
209
- }
210
- add_submenu_page('index.php', 'Search Meter', 'Search Meter', $view_stats_capability, __FILE__, 'tguy_sm_stats_page');
211
- add_options_page('Search Meter', 'Search Meter', TGUY_SM_OPTIONS_CAPABILITY, __FILE__, 'tguy_sm_options_page');
212
- }
213
-
214
-
215
- //////// Statistics pages
216
-
217
-
218
- function tguy_sm_stats_page() {
219
- if (array_key_exists('recent', $_GET)) {
220
- $recent_count = intval($_GET['recent']);
221
- $do_show_details = array_key_exists('details', $_GET) && $_GET['details'];
222
- tguy_sm_recent_page($recent_count, $do_show_details);
223
- } else {
224
- tguy_sm_summary_page();
225
- }
226
- }
227
-
228
- function tguy_sm_summary_page() {
229
- global $wpdb;
230
-
231
- $options = get_option('tguy_search_meter');
232
- $is_disable_donation = $options['sm_disable_donation'];
233
-
234
- // Delete old records
235
- $result = $wpdb->query(
236
- "DELETE FROM `{$wpdb->prefix}searchmeter`
237
- WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)");
238
- echo "<!-- Search Meter: deleted $result old rows -->\n";
239
- ?>
240
- <div class="wrap">
241
-
242
- <ul id="search_meter_menu">
243
- <li class="current"><span>Summary</span></li>
244
- <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>&amp;recent=100">Last 100 Searches</a></li>
245
- <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>&amp;recent=500">Last 500 Searches</a></li>
246
- </ul>
247
-
248
- <h2>Search summary</h2>
249
-
250
- <p>These tables show the most popular searches on your blog for the given time periods. <strong>Term</strong> is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) <strong>Searches</strong> is the number of times the term was searched for. <strong>Results</strong> is the number of posts that were returned from the <em>last</em> search for that term.</p>
251
-
252
- <div class="sm-stats-table">
253
- <h3>Yesterday and today</h3>
254
- <?php tguy_sm_summary_table(1); ?>
255
- </div>
256
- <div class="sm-stats-table">
257
- <h3>Last 7 days</h3>
258
- <?php tguy_sm_summary_table(7); ?>
259
- </div>
260
- <div class="sm-stats-table">
261
- <h3>Last 30 days</h3>
262
- <?php tguy_sm_summary_table(30); ?>
263
- </div>
264
- <div class="sm-stats-clear"></div>
265
-
266
- <h2>Unsuccessful search summary</h2>
267
-
268
- <p>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.</p>
269
-
270
- <div class="sm-stats-table">
271
- <h3>Yesterday and today</h3>
272
- <?php tguy_sm_summary_table(1, false); ?>
273
- </div>
274
- <div class="sm-stats-table">
275
- <h3>Last 7 days</h3>
276
- <?php tguy_sm_summary_table(7, false); ?>
277
- </div>
278
- <div class="sm-stats-table">
279
- <h3>Last 30 days</h3>
280
- <?php tguy_sm_summary_table(30, false); ?>
281
- </div>
282
- <div class="sm-stats-clear"></div>
283
-
284
- <h2>Notes</h2>
285
-
286
- <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
287
- <p>To manage your search statistics, go to the <a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Settings</a> page.</p>
288
- <?php endif; ?>
289
-
290
- <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. You can also offer suggestions, request new features or report problems.</p>
291
-
292
- <?php if (!$options['sm_disable_donation']) { tguy_sm_show_donation_message(); } ?>
293
-
294
- </div>
295
- <?php
296
- }
297
-
298
- function tguy_sm_summary_table($days, $do_include_successes = true) {
299
- global $wpdb;
300
- // Explanation of the query:
301
- // We group by terms, because we want all rows for a term to be combined.
302
- // For the search count, we simply SUM the count of all searches for the term.
303
- // For the result count, we only want the number of results for the latest search. Each row
304
- // contains the result for the latest search on that row's date. So for each date,
305
- // CONCAT the date with the number of results, and take the MAX. This gives us the
306
- // latest date combined with its hit count. Then strip off the date with SUBSTRING.
307
- // This Rube Goldberg-esque procedure should work in older MySQL versions that
308
- // don't allow subqueries. It's inefficient, but that doesn't matter since it's
309
- // only used in admin pages and the tables involved won't be too big.
310
- $hits_selector = $do_include_successes ? '' : 'HAVING hits = 0';
311
- $results = $wpdb->get_results(
312
- "SELECT `terms`,
313
- SUM( `count` ) AS countsum,
314
- SUBSTRING( MAX( CONCAT( `date` , ' ', `last_hits` ) ) , 12 ) AS hits
315
- FROM `{$wpdb->prefix}searchmeter`
316
- WHERE DATE_SUB( CURDATE( ) , INTERVAL $days DAY ) <= `date`
317
- GROUP BY `terms`
318
- $hits_selector
319
- ORDER BY countsum DESC, `terms` ASC
320
- LIMIT 20");
321
- if (count($results)) {
322
- ?>
323
- <table cellpadding="3" cellspacing="2">
324
- <tbody>
325
- <tr class="alternate"><th class="left">Term</th><th>Searches</th>
326
- <?php
327
- if ($do_include_successes) {
328
- ?><th>Results</th><?php
329
- }
330
- ?></tr><?php
331
- $class= '';
332
- foreach ($results as $result) {
333
- ?>
334
- <tr class="<?php echo $class ?>">
335
- <td><a href="<?php echo get_bloginfo('wpurl').'/wp-admin/edit.php?s='.urlencode($result->terms).'&submit=Search' ?>"><?php echo htmlspecialchars($result->terms) ?></a></td>
336
- <td class="right"><?php echo $result->countsum ?></td>
337
- <?php
338
- if ($do_include_successes) {
339
- ?>
340
- <td class="right"><?php echo $result->hits ?></td></tr>
341
- <?php
342
- }
343
- $class = ($class == '' ? 'alternate' : '');
344
- }
345
- ?>
346
- </tbody>
347
- </table>
348
- <?php
349
- } else {
350
- ?><p><em>No searches recorded for this period.</em></p><?php
351
- }
352
- }
353
-
354
- function tguy_sm_recent_page($max_lines, $do_show_details) {
355
- global $wpdb;
356
-
357
- $options = get_option('tguy_search_meter');
358
- $is_details_available = $options['sm_details_verbose'];
359
- $is_disable_donation = $options['sm_disable_donation'];
360
- $this_url_base = 'index.php?page=' . plugin_basename(__FILE__);
361
- $this_url_recent_arg = '&amp;recent=' . $max_lines;
362
- ?>
363
- <div class="wrap">
364
-
365
- <ul id="search_meter_menu">
366
- <li><a href="<?php echo $this_url_base ?>">Summary</a></li>
367
- <?php if (100 == $max_lines) : ?>
368
- <li class="current"><span>Last 100 Searches</span></li>
369
- <?php else : ?>
370
- <li><a href="<?php echo $this_url_base ?>&amp;recent=100">Last 100 Searches</a></li>
371
- <?php endif ?>
372
- <?php if (500 == $max_lines) : ?>
373
- <li class="current"><span>Last 500 Searches</span></li>
374
- <?php else : ?>
375
- <li><a href="<?php echo $this_url_base ?>&amp;recent=500">Last 500 Searches</a></li>
376
- <?php endif ?>
377
- </ul>
378
-
379
- <h2>Recent searches</h2>
380
-
381
- <p>This table shows the last <?php echo $max_lines; ?> searches on this blog. <strong>Term</strong> is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) <strong>Results</strong> is the number of posts that were returned from the search.
382
- </p>
383
-
384
- <div class="sm-stats-table">
385
- <?php
386
- $query =
387
- "SELECT `datetime`, `terms`, `hits`, `details`
388
- FROM `{$wpdb->prefix}searchmeter_recent`
389
- ORDER BY `datetime` DESC, `terms` ASC
390
- LIMIT $max_lines";
391
- $results = $wpdb->get_results($query);
392
- if (count($results)) {
393
- ?>
394
- <table cellpadding="3" cellspacing="2">
395
- <tbody>
396
- <tr class="alternate"><th class="left">Date &amp; time</th><th class="left">Term</th><th class="right">Results</th>
397
- <?php if ($do_show_details) { ?>
398
- <th class="left">Details</th>
399
- <?php } else if ($is_details_available) { ?>
400
- <th class="left"><a href="<?php echo $this_url_base . $this_url_recent_arg . '&amp;details=1' ?>">Show details</a></th>
401
- <?php } ?>
402
- </tr>
403
- <?php
404
- $class= '';
405
- foreach ($results as $result) {
406
- ?>
407
- <tr valign="top" class="<?php echo $class ?>">
408
- <td><?php echo $result->datetime ?></td>
409
- <td><a href="<?php echo get_bloginfo('wpurl').'/wp-admin/edit.php?s='.urlencode($result->terms).'&submit=Search' ?>"><?php echo htmlspecialchars($result->terms) ?></a></td>
410
- <td class="right"><?php echo $result->hits ?></td>
411
- <?php if ($do_show_details) : ?>
412
- <td><?php echo str_replace("\n", "<br />", htmlspecialchars($result->details)) ?></td>
413
- <?php endif ?>
414
- </tr>
415
- <?php
416
- $class = ($class == '' ? 'alternate' : '');
417
- }
418
- ?>
419
- </tbody>
420
- </table>
421
- <?php
422
- } else {
423
- ?><p>No searches recorded.</p><?php
424
- }
425
- ?>
426
- </div>
427
- <div class="sm-stats-clear"></div>
428
-
429
- <h2>Notes</h2>
430
-
431
- <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
432
- <p>To manage your search statistics, go to the <a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Settings</a> page.</p>
433
- <?php endif; ?>
434
-
435
- <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. You can also offer suggestions, request new features or report problems.</p>
436
-
437
- <?php if (!$options['sm_disable_donation']) { tguy_sm_show_donation_message(); } ?>
438
-
439
- </div>
440
- <?php
441
- }
442
-
443
-
444
- //////// Plugins page
445
-
446
-
447
- // Add settings link on plugin page
448
- add_filter('plugin_action_links_'.plugin_basename(dirname(__FILE__).'/search-meter.php'), 'tguy_sm_settings_link' );
449
-
450
- function tguy_sm_settings_link($links) {
451
- if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) {
452
- $settings_link = '<a href="options-general.php?page='.plugin_basename(__FILE__).'">Settings</a>';
453
- array_unshift($links, $settings_link);
454
- }
455
- return $links;
456
- }
457
-
458
-
459
- //////// Options page
460
-
461
-
462
- function tguy_sm_options_page() {
463
- if (isset($_POST['submitted'])) {
464
- check_admin_referer('search-meter-update-options_all');
465
- $options = get_option('tguy_search_meter');
466
- $options['sm_view_stats_capability'] = ($_POST['sm_view_stats_capability']);
467
- $sm_filter_words = $_POST['sm_filter_words'];
468
- if (get_magic_quotes_gpc()) {
469
- $sm_filter_words = stripslashes($sm_filter_words);
470
- }
471
- $options['sm_filter_words'] = preg_replace('/\\s+/', ' ', trim($sm_filter_words));
472
- $options['sm_details_verbose'] = (bool)($_POST['sm_details_verbose']);
473
- $options['sm_disable_donation'] = (bool)($_POST['sm_disable_donation']);
474
- update_option('tguy_search_meter', $options);
475
- echo '<div id="message" class="updated fade"><p><strong>Plugin settings saved.</strong></p></div>';
476
- } else if (isset($_POST['tguy_sm_reset'])) {
477
- check_admin_referer('search-meter-reset-stats');
478
- tguy_sm_reset_stats();
479
- echo '<div id="message" class="updated fade"><p><strong>Statistics have been reset.</strong></p></div>';
480
- }
481
- $options = get_option('tguy_search_meter');
482
- $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
483
- if ($view_stats_capability == '') {
484
- $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
485
- }
486
- ?>
487
- <div class="wrap">
488
-
489
- <h2>Search Meter Settings</h2>
490
-
491
- <form name="searchmeter" action="" method="post">
492
- <?php
493
- if (function_exists('wp_nonce_field')) {
494
- wp_nonce_field('search-meter-update-options_all');
495
- }
496
- ?>
497
-
498
- <input type="hidden" name="submitted" value="1" />
499
-
500
- <table class="form-table">
501
- <tr>
502
- <th scope="row">Show statistics to</th>
503
- <td>
504
- <fieldset>
505
- <label title='Users with "read" capability'>
506
- <input type="radio" name="sm_view_stats_capability" value="read"
507
- <?php echo ($view_stats_capability=='read'?"checked=\"checked\"":"") ?> />
508
- All logged-in users</label><br>
509
- <label title='Users with "publish_posts" capability'>
510
- <input type="radio" name="sm_view_stats_capability" value="publish_posts"
511
- <?php echo ($view_stats_capability=='publish_posts'?"checked=\"checked\"":"") ?> />
512
- Post authors and administrators</label><br>
513
- <label title='Users with "manage_options" capability'>
514
- <input type="radio" name="sm_view_stats_capability" value="manage_options"
515
- <?php echo ($view_stats_capability=='manage_options'?"checked=\"checked\"":"") ?> />
516
- Administrators only</label>
517
- </fieldset>
518
- </td>
519
- </tr>
520
- <tr valign="top">
521
- <th scope="row">Search filter</th>
522
- <td>
523
- <fieldset>
524
- <label for="sm_filter_words">When a search term contains any of these words, it will be filtered
525
- and will not show up in the Recent Searches or Popular Searches widgets. This will match inside words,
526
- so &#8220;press&#8221; will match &#8220;WordPress&#8221;.</label>
527
- <textarea name="sm_filter_words" rows="3" cols="40" id="sm_filter_words" class="large-text code"><?php echo esc_html(tguy_sm_array_value($options, 'sm_filter_words')); ?></textarea>
528
- </fieldset>
529
- </td>
530
- </tr>
531
- <tr>
532
- <th class="th-full" scope="row" colspan="2">
533
- <label for="sm_details_verbose">
534
- <input type="checkbox" id="sm_details_verbose" name="sm_details_verbose" <?php echo (tguy_sm_array_value($options, 'sm_details_verbose') ? 'checked="checked"' : '') ?> />
535
- Keep detailed information about recent searches (taken from HTTP headers)
536
- </label>
537
- </th>
538
- </tr>
539
- <tr>
540
- <th class="th-full" scope="row" colspan="2">
541
- <label for="sm_disable_donation">
542
- <input type="checkbox" id="sm_disable_donation" name="sm_disable_donation" <?php echo (tguy_sm_array_value($options, 'sm_disable_donation') ? 'checked="checked"' : '') ?> />
543
- Hide the &#8220;Do you find this plugin useful?&#8221; box
544
- </label>
545
- </th>
546
- </tr>
547
- </table>
548
-
549
- <p class="submit">
550
- <input name="Submit" class="button-primary" value="Save Changes" type="submit">
551
- </p>
552
- </form>
553
-
554
- <h3>Reset statistics</h3>
555
-
556
- <p>Click this button to reset all search statistics. This will delete all information about previous searches.</p>
557
-
558
- <form name="tguy_sm_admin" action="" method="post">
559
- <?php
560
- if (function_exists('wp_nonce_field')) {
561
- wp_nonce_field('search-meter-reset-stats');
562
- }
563
- ?>
564
- <p class="submit">
565
- <input name="tguy_sm_reset" class="button-secondary delete" value="Reset Statistics" type="submit" onclick="return confirm('You are about to delete all saved search statistics.\n \'Cancel\' to stop, \'OK\' to delete.');" />
566
- </p>
567
- </form>
568
-
569
- <h3>Notes</h3>
570
-
571
- <p>To see your search statistics, go to the <a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Dashboard</a>.</p>
572
-
573
- <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. At that page, you can also offer suggestions, request new features or report problems.</p>
574
-
575
- <?php if ( ! tguy_sm_array_value($options, 'sm_disable_donation')) { tguy_sm_show_donation_message(); } ?>
576
-
577
- </div>
578
- <?php
579
- }
580
-
581
- function tguy_sm_reset_stats() {
582
- global $wpdb;
583
- // Delete all records
584
- $wpdb->query("DELETE FROM `{$wpdb->prefix}searchmeter`");
585
- $wpdb->query("DELETE FROM `{$wpdb->prefix}searchmeter_recent`");
586
- }
587
-
588
- function tguy_sm_show_donation_message() {
589
- ?>
590
- <p><div style="margin: 0; padding: 0 2ex 0.25ex 0; float: left;">
591
- <?php tguy_sm_show_donation_button() ?>
592
- </div>
593
- <strong>Do you find this plugin useful?</strong><br />
594
- I write WordPress plugins because I enjoy doing it, but it does take up a lot
595
- of my time. If you think this plugin is useful, please consider donating some appropriate
596
- amount by clicking the <strong>Donate</strong> button. You can also send <strong>Bitcoins</strong>
597
- to address <tt>1542gqyprvQd7gwvtZZ4x25cPeGWVKg45x</tt>. Thanks!</p>
598
- <?php
599
- }
600
-
601
- function tguy_sm_show_donation_button() {
602
- // I wish PayPal offered a simple little REST-style URL instead of this monstrosity
603
- ?><form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="margin:0; padding:0;"
604
- ><input name="cmd" value="_s-xclick" type="hidden" style="margin:0; padding:0;"
605
- /><input src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" border="0" type="image" style="margin:0; padding:0;"
606
- /><input name="encrypted" value="-----BEGIN PKCS7-----MIIHXwYJKoZIhvcNAQcEoIIHUDCCB0wCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRY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3DQEHATAcBgkqhkiG9w0BCQUxDxcNMDYwMjA3MTEyOTQ5WjAjBgkqhkiG9w0BCQQxFgQUO31wm3aCiCMdh2XIXxIAeS8LfBIwDQYJKoZIhvcNAQEBBQAEgYB3CtAsDm+ZRBkd/XLEhUx0IbaeyK9ymOT8R5EQfSZnoJ+QP05XWBc8zi21wSOiQ8nH9LtN2MtS4GRBAQFU1vbvGxw6bG2gJfggJ1pDPUOtkFgf1YA8At+m2I6G2E+YWx2/QHdfMo3BpTJWQOUka52wjuTmIX9X6+CFMPokF91f0w==-----END PKCS7-----
607
- " type="hidden" style="margin:0; padding:0;"
608
- /></form><?php
609
- }
1
+ <?php
2
+ /*
3
+ Copyright (C) 2005-12 Bennett McElwee (bennett at thunderguy dotcom)
4
+
5
+ This program is free software; you can redistribute it and/or
6
+ modify it under the terms of version 2 of the GNU General Public
7
+ License as published by the Free Software Foundation.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details, available at
13
+ http://www.gnu.org/copyleft/gpl.html
14
+ */
15
+
16
+ //////// Parameters
17
+
18
+
19
+ define('TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY', 'publish_posts');
20
+ // Default capability users must have in order to see stats.
21
+
22
+ define('TGUY_SM_OPTIONS_CAPABILITY', 'manage_options');
23
+ // Capability users must have in order to set options.
24
+
25
+
26
+ //////// General admin
27
+
28
+
29
+ add_action('admin_head', 'tguy_sm_stats_css');
30
+
31
+ function tguy_sm_stats_css() {
32
+ ?>
33
+ <style type="text/css">
34
+ #search_meter_menu {
35
+ line-height: 1.4em;
36
+ margin: 5px 0 0 0;
37
+ padding: 0;
38
+ border-bottom: 1px solid #aaaaaa;
39
+ }
40
+ #search_meter_menu li {
41
+ border: 1px solid #aaaaaa;
42
+ border-bottom: none;
43
+ line-height: 1.4em;
44
+ display: inline-block;
45
+ margin: 0 5px 0 0;
46
+ padding: 0;
47
+ list-style-type: none;
48
+ list-style-image: none;
49
+ list-style-position: outside;
50
+ }
51
+ #search_meter_menu li.current span {
52
+ background-color: #ffffff;
53
+ font-weight: bold;
54
+ padding: 0 5px 3px 5px;
55
+ }
56
+ #search_meter_menu li a,
57
+ #search_meter_menu li a:visited {
58
+ padding: 0 5px;
59
+ text-decoration: none;
60
+ }
61
+ #search_meter_menu li a:hover {
62
+ background-color: #eaf2fa;
63
+ }
64
+ #search_meter_menu + .wrap {
65
+ margin-top: 0;
66
+ }
67
+ div.sm-stats-table {
68
+ float: left;
69
+ padding-right: 3em;
70
+ padding-bottom: 1.5em;
71
+ }
72
+ div.sm-stats-table th, div.sm-stats-table td {
73
+ padding-right: 0.5em;
74
+ }
75
+ div.sm-stats-table h3 {
76
+ margin-top: 0;
77
+ margin-bottom: 0.5em;
78
+ }
79
+ div.sm-stats-table .left {
80
+ text-align: left;
81
+ }
82
+ div.sm-stats-table .right {
83
+ text-align: right;
84
+ }
85
+ div.sm-stats-clear {
86
+ clear: both;
87
+ }
88
+
89
+ /* Dashboard widget overrides */
90
+ #dashboard_search_meter h4 {
91
+ line-height: 1.7em;
92
+ }
93
+ #dashboard_search_meter div.sm-stats-table {
94
+ float: none;
95
+ padding-bottom: 0;
96
+ padding-right: 0;
97
+ }
98
+ #dashboard_search_meter div.sm-stats-table th {
99
+ color: #8F8F8F;
100
+ }
101
+ #dashboard_search_meter ul.subsubsub {
102
+ float: none;
103
+ }
104
+
105
+ </style>
106
+ <?php
107
+ }
108
+
109
+
110
+ //////// Initialisation
111
+
112
+
113
+ function tguy_sm_init() {
114
+ tguy_sm_create_summary_table();
115
+ tguy_sm_create_recent_table();
116
+ }
117
+
118
+ function tguy_sm_create_summary_table() {
119
+ // Create the table if not already there.
120
+ global $wpdb;
121
+ $table_name = $wpdb->prefix . "searchmeter";
122
+ if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
123
+ require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
124
+ dbDelta("
125
+ CREATE TABLE `{$table_name}` (
126
+ `terms` VARCHAR(50) NOT NULL,
127
+ `date` DATE NOT NULL,
128
+ `count` INT(11) NOT NULL,
129
+ `last_hits` INT(11) NOT NULL,
130
+ PRIMARY KEY (`terms`,`date`)
131
+ )
132
+ CHARACTER SET utf8 COLLATE utf8_general_ci;
133
+ ");
134
+ }
135
+ }
136
+
137
+ function tguy_sm_create_recent_table() {
138
+ // Create the table if not already there.
139
+ global $wpdb;
140
+ $table_name = $wpdb->prefix . "searchmeter_recent";
141
+ if ($wpdb->get_var("show tables like '$table_name'") != $table_name) {
142
+ require_once(ABSPATH . '/wp-admin/includes/upgrade.php');
143
+ dbDelta("
144
+ CREATE TABLE `{$table_name}` (
145
+ `terms` VARCHAR(50) NOT NULL,
146
+ `datetime` DATETIME NOT NULL,
147
+ `hits` INT(11) NOT NULL,
148
+ `details` TEXT NOT NULL,
149
+ KEY `datetimeindex` (`datetime`)
150
+ )
151
+ CHARACTER SET utf8 COLLATE utf8_general_ci;
152
+ ");
153
+ }
154
+ }
155
+
156
+
157
+ //////// Permissions
158
+
159
+
160
+ function smcln_sm_can_view_stats() {
161
+ $options = get_option('tguy_search_meter');
162
+ $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
163
+ if ($view_stats_capability == '') {
164
+ $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
165
+ }
166
+ return current_user_can($view_stats_capability);
167
+ }
168
+
169
+
170
+ //////// Dashboard widget
171
+
172
+ add_action('wp_dashboard_setup', 'smcln_sm_dashboard');
173
+
174
+ // Add the widget to the dashboard
175
+ function smcln_sm_dashboard() {
176
+ if (smcln_sm_can_view_stats()) {
177
+ wp_add_dashboard_widget( 'dashboard_search_meter', 'Search Meter', 'smcln_sm_summary');
178
+ }
179
+ }
180
+
181
+ // Render the summary widget
182
+ function smcln_sm_summary() {
183
+ ?>
184
+ <div class="sm-stats-table">
185
+ <h4>Searches in the Last 7 Days</h4>
186
+ <?php tguy_sm_summary_table(7); ?>
187
+ </div>
188
+ <ul class="subsubsub">
189
+ <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>">Full Dashboard</a> |</li>
190
+ <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
191
+ <li><a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Settings</a> |</li>
192
+ <?php endif; ?>
193
+ <li><a href="http://thunderguy.com/semicolon/donate/">Donate</a></li>
194
+ </ul>
195
+ <?php
196
+ }
197
+
198
+
199
+ //////// Admin pages
200
+
201
+
202
+ add_action('admin_menu', 'tguy_sm_add_admin_pages');
203
+
204
+ function tguy_sm_add_admin_pages() {
205
+ $options = get_option('tguy_search_meter');
206
+ $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
207
+ if ($view_stats_capability == '') {
208
+ $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
209
+ }
210
+ add_submenu_page('index.php', 'Search Meter', 'Search Meter', $view_stats_capability, __FILE__, 'tguy_sm_stats_page');
211
+ add_options_page('Search Meter', 'Search Meter', TGUY_SM_OPTIONS_CAPABILITY, __FILE__, 'tguy_sm_options_page');
212
+ }
213
+
214
+
215
+ //////// Statistics pages
216
+
217
+
218
+ function tguy_sm_stats_page() {
219
+ if (array_key_exists('recent', $_GET)) {
220
+ $recent_count = intval($_GET['recent']);
221
+ $do_show_details = array_key_exists('details', $_GET) && $_GET['details'];
222
+ tguy_sm_recent_page($recent_count, $do_show_details);
223
+ } else {
224
+ tguy_sm_summary_page();
225
+ }
226
+ }
227
+
228
+ function tguy_sm_summary_page() {
229
+ global $wpdb;
230
+
231
+ $options = get_option('tguy_search_meter');
232
+ $is_disable_donation = $options['sm_disable_donation'];
233
+
234
+ // Delete old records
235
+ $result = $wpdb->query(
236
+ "DELETE FROM `{$wpdb->prefix}searchmeter`
237
+ WHERE `date` < DATE_SUB( CURDATE() , INTERVAL 30 DAY)");
238
+ echo "<!-- Search Meter: deleted $result old rows -->\n";
239
+ ?>
240
+ <div class="wrap">
241
+
242
+ <ul id="search_meter_menu">
243
+ <li class="current"><span>Summary</span></li>
244
+ <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>&amp;recent=100">Last 100 Searches</a></li>
245
+ <li><a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>&amp;recent=500">Last 500 Searches</a></li>
246
+ </ul>
247
+
248
+ <h2>Search summary</h2>
249
+
250
+ <p>These tables show the most popular searches on your blog for the given time periods. <strong>Term</strong> is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) <strong>Searches</strong> is the number of times the term was searched for. <strong>Results</strong> is the number of posts that were returned from the <em>last</em> search for that term.</p>
251
+
252
+ <div class="sm-stats-table">
253
+ <h3>Yesterday and today</h3>
254
+ <?php tguy_sm_summary_table(1); ?>
255
+ </div>
256
+ <div class="sm-stats-table">
257
+ <h3>Last 7 days</h3>
258
+ <?php tguy_sm_summary_table(7); ?>
259
+ </div>
260
+ <div class="sm-stats-table">
261
+ <h3>Last 30 days</h3>
262
+ <?php tguy_sm_summary_table(30); ?>
263
+ </div>
264
+ <div class="sm-stats-clear"></div>
265
+
266
+ <h2>Unsuccessful search summary</h2>
267
+
268
+ <p>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.</p>
269
+
270
+ <div class="sm-stats-table">
271
+ <h3>Yesterday and today</h3>
272
+ <?php tguy_sm_summary_table(1, false); ?>
273
+ </div>
274
+ <div class="sm-stats-table">
275
+ <h3>Last 7 days</h3>
276
+ <?php tguy_sm_summary_table(7, false); ?>
277
+ </div>
278
+ <div class="sm-stats-table">
279
+ <h3>Last 30 days</h3>
280
+ <?php tguy_sm_summary_table(30, false); ?>
281
+ </div>
282
+ <div class="sm-stats-clear"></div>
283
+
284
+ <h2>Notes</h2>
285
+
286
+ <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
287
+ <p>To manage your search statistics, go to the <a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Settings</a> page.</p>
288
+ <?php endif; ?>
289
+
290
+ <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. You can also offer suggestions, request new features or report problems.</p>
291
+
292
+ <?php if (!$options['sm_disable_donation']) { tguy_sm_show_donation_message(); } ?>
293
+
294
+ </div>
295
+ <?php
296
+ }
297
+
298
+ function tguy_sm_summary_table($days, $do_include_successes = true) {
299
+ global $wpdb;
300
+ // Explanation of the query:
301
+ // We group by terms, because we want all rows for a term to be combined.
302
+ // For the search count, we simply SUM the count of all searches for the term.
303
+ // For the result count, we only want the number of results for the latest search. Each row
304
+ // contains the result for the latest search on that row's date. So for each date,
305
+ // CONCAT the date with the number of results, and take the MAX. This gives us the
306
+ // latest date combined with its hit count. Then strip off the date with SUBSTRING.
307
+ // This Rube Goldberg-esque procedure should work in older MySQL versions that
308
+ // don't allow subqueries. It's inefficient, but that doesn't matter since it's
309
+ // only used in admin pages and the tables involved won't be too big.
310
+ $hits_selector = $do_include_successes ? '' : 'HAVING hits = 0';
311
+ $results = $wpdb->get_results(
312
+ "SELECT `terms`,
313
+ SUM( `count` ) AS countsum,
314
+ SUBSTRING( MAX( CONCAT( `date` , ' ', `last_hits` ) ) , 12 ) AS hits
315
+ FROM `{$wpdb->prefix}searchmeter`
316
+ WHERE DATE_SUB( CURDATE( ) , INTERVAL $days DAY ) <= `date`
317
+ GROUP BY `terms`
318
+ $hits_selector
319
+ ORDER BY countsum DESC, `terms` ASC
320
+ LIMIT 20");
321
+ if (count($results)) {
322
+ ?>
323
+ <table cellpadding="3" cellspacing="2">
324
+ <tbody>
325
+ <tr class="alternate"><th class="left">Term</th><th>Searches</th>
326
+ <?php
327
+ if ($do_include_successes) {
328
+ ?><th>Results</th><?php
329
+ }
330
+ ?></tr><?php
331
+ $class= '';
332
+ foreach ($results as $result) {
333
+ ?>
334
+ <tr class="<?php echo $class ?>">
335
+ <td><a href="<?php echo get_bloginfo('wpurl').'/wp-admin/edit.php?s='.urlencode($result->terms).'&submit=Search' ?>"><?php echo htmlspecialchars($result->terms) ?></a></td>
336
+ <td class="right"><?php echo $result->countsum ?></td>
337
+ <?php
338
+ if ($do_include_successes) {
339
+ ?>
340
+ <td class="right"><?php echo $result->hits ?></td></tr>
341
+ <?php
342
+ }
343
+ $class = ($class == '' ? 'alternate' : '');
344
+ }
345
+ ?>
346
+ </tbody>
347
+ </table>
348
+ <?php
349
+ } else {
350
+ ?><p><em>No searches recorded for this period.</em></p><?php
351
+ }
352
+ }
353
+
354
+ function tguy_sm_recent_page($max_lines, $do_show_details) {
355
+ global $wpdb;
356
+
357
+ $options = get_option('tguy_search_meter');
358
+ $is_details_available = $options['sm_details_verbose'];
359
+ $is_disable_donation = $options['sm_disable_donation'];
360
+ $this_url_base = 'index.php?page=' . plugin_basename(__FILE__);
361
+ $this_url_recent_arg = '&amp;recent=' . $max_lines;
362
+ ?>
363
+ <div class="wrap">
364
+
365
+ <ul id="search_meter_menu">
366
+ <li><a href="<?php echo $this_url_base ?>">Summary</a></li>
367
+ <?php if (100 == $max_lines) : ?>
368
+ <li class="current"><span>Last 100 Searches</span></li>
369
+ <?php else : ?>
370
+ <li><a href="<?php echo $this_url_base ?>&amp;recent=100">Last 100 Searches</a></li>
371
+ <?php endif ?>
372
+ <?php if (500 == $max_lines) : ?>
373
+ <li class="current"><span>Last 500 Searches</span></li>
374
+ <?php else : ?>
375
+ <li><a href="<?php echo $this_url_base ?>&amp;recent=500">Last 500 Searches</a></li>
376
+ <?php endif ?>
377
+ </ul>
378
+
379
+ <h2>Recent searches</h2>
380
+
381
+ <p>This table shows the last <?php echo $max_lines; ?> searches on this blog. <strong>Term</strong> is the text that was searched for; you can click it to see which posts contain that term. (This won't be counted as another search.) <strong>Results</strong> is the number of posts that were returned from the search.
382
+ </p>
383
+
384
+ <div class="sm-stats-table">
385
+ <?php
386
+ $query =
387
+ "SELECT `datetime`, `terms`, `hits`, `details`
388
+ FROM `{$wpdb->prefix}searchmeter_recent`
389
+ ORDER BY `datetime` DESC, `terms` ASC
390
+ LIMIT $max_lines";
391
+ $results = $wpdb->get_results($query);
392
+ if (count($results)) {
393
+ ?>
394
+ <table cellpadding="3" cellspacing="2">
395
+ <tbody>
396
+ <tr class="alternate"><th class="left">Date &amp; time</th><th class="left">Term</th><th class="right">Results</th>
397
+ <?php if ($do_show_details) { ?>
398
+ <th class="left">Details</th>
399
+ <?php } else if ($is_details_available) { ?>
400
+ <th class="left"><a href="<?php echo $this_url_base . $this_url_recent_arg . '&amp;details=1' ?>">Show details</a></th>
401
+ <?php } ?>
402
+ </tr>
403
+ <?php
404
+ $class= '';
405
+ foreach ($results as $result) {
406
+ ?>
407
+ <tr valign="top" class="<?php echo $class ?>">
408
+ <td><?php echo $result->datetime ?></td>
409
+ <td><a href="<?php echo get_bloginfo('wpurl').'/wp-admin/edit.php?s='.urlencode($result->terms).'&submit=Search' ?>"><?php echo htmlspecialchars($result->terms) ?></a></td>
410
+ <td class="right"><?php echo $result->hits ?></td>
411
+ <?php if ($do_show_details) : ?>
412
+ <td><?php echo str_replace("\n", "<br />", htmlspecialchars($result->details)) ?></td>
413
+ <?php endif ?>
414
+ </tr>
415
+ <?php
416
+ $class = ($class == '' ? 'alternate' : '');
417
+ }
418
+ ?>
419
+ </tbody>
420
+ </table>
421
+ <?php
422
+ } else {
423
+ ?><p>No searches recorded.</p><?php
424
+ }
425
+ ?>
426
+ </div>
427
+ <div class="sm-stats-clear"></div>
428
+
429
+ <h2>Notes</h2>
430
+
431
+ <?php if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) : ?>
432
+ <p>To manage your search statistics, go to the <a href="options-general.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Settings</a> page.</p>
433
+ <?php endif; ?>
434
+
435
+ <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. You can also offer suggestions, request new features or report problems.</p>
436
+
437
+ <?php if (!$options['sm_disable_donation']) { tguy_sm_show_donation_message(); } ?>
438
+
439
+ </div>
440
+ <?php
441
+ }
442
+
443
+
444
+ //////// Plugins page
445
+
446
+
447
+ // Add settings link on plugin page
448
+ add_filter('plugin_action_links_'.plugin_basename(dirname(__FILE__).'/search-meter.php'), 'tguy_sm_settings_link' );
449
+
450
+ function tguy_sm_settings_link($links) {
451
+ if (current_user_can(TGUY_SM_OPTIONS_CAPABILITY)) {
452
+ $settings_link = '<a href="options-general.php?page='.plugin_basename(__FILE__).'">Settings</a>';
453
+ array_unshift($links, $settings_link);
454
+ }
455
+ return $links;
456
+ }
457
+
458
+
459
+ //////// Options page
460
+
461
+
462
+ function tguy_sm_options_page() {
463
+ if (isset($_POST['submitted'])) {
464
+ check_admin_referer('search-meter-update-options_all');
465
+ $options = get_option('tguy_search_meter');
466
+ $options['sm_view_stats_capability'] = ($_POST['sm_view_stats_capability']);
467
+ $sm_filter_words = $_POST['sm_filter_words'];
468
+ if (get_magic_quotes_gpc()) {
469
+ $sm_filter_words = stripslashes($sm_filter_words);
470
+ }
471
+ $options['sm_filter_words'] = preg_replace('/\\s+/', ' ', trim($sm_filter_words));
472
+ $options['sm_details_verbose'] = (bool)($_POST['sm_details_verbose']);
473
+ $options['sm_disable_donation'] = (bool)($_POST['sm_disable_donation']);
474
+ update_option('tguy_search_meter', $options);
475
+ echo '<div id="message" class="updated fade"><p><strong>Plugin settings saved.</strong></p></div>';
476
+ } else if (isset($_POST['tguy_sm_reset'])) {
477
+ check_admin_referer('search-meter-reset-stats');
478
+ tguy_sm_reset_stats();
479
+ echo '<div id="message" class="updated fade"><p><strong>Statistics have been reset.</strong></p></div>';
480
+ }
481
+ $options = get_option('tguy_search_meter');
482
+ $view_stats_capability = tguy_sm_array_value($options, 'sm_view_stats_capability');
483
+ if ($view_stats_capability == '') {
484
+ $view_stats_capability = TGUY_SM_DEFAULT_VIEW_STATS_CAPABILITY;
485
+ }
486
+ ?>
487
+ <div class="wrap">
488
+
489
+ <h2>Search Meter Settings</h2>
490
+
491
+ <form name="searchmeter" action="" method="post">
492
+ <?php
493
+ if (function_exists('wp_nonce_field')) {
494
+ wp_nonce_field('search-meter-update-options_all');
495
+ }
496
+ ?>
497
+
498
+ <input type="hidden" name="submitted" value="1" />
499
+
500
+ <table class="form-table">
501
+ <tr>
502
+ <th scope="row">Show statistics to</th>
503
+ <td>
504
+ <fieldset>
505
+ <label title='Users with "read" capability'>
506
+ <input type="radio" name="sm_view_stats_capability" value="read"
507
+ <?php echo ($view_stats_capability=='read'?"checked=\"checked\"":"") ?> />
508
+ All logged-in users</label><br>
509
+ <label title='Users with "publish_posts" capability'>
510
+ <input type="radio" name="sm_view_stats_capability" value="publish_posts"
511
+ <?php echo ($view_stats_capability=='publish_posts'?"checked=\"checked\"":"") ?> />
512
+ Post authors and administrators</label><br>
513
+ <label title='Users with "manage_options" capability'>
514
+ <input type="radio" name="sm_view_stats_capability" value="manage_options"
515
+ <?php echo ($view_stats_capability=='manage_options'?"checked=\"checked\"":"") ?> />
516
+ Administrators only</label>
517
+ </fieldset>
518
+ </td>
519
+ </tr>
520
+ <tr valign="top">
521
+ <th scope="row">Search filter</th>
522
+ <td>
523
+ <fieldset>
524
+ <label for="sm_filter_words">When a search term contains any of these words, it will be filtered
525
+ and will not show up in the Recent Searches or Popular Searches widgets. This will match inside words,
526
+ so &#8220;press&#8221; will match &#8220;WordPress&#8221;.</label>
527
+ <textarea name="sm_filter_words" rows="3" cols="40" id="sm_filter_words" class="large-text code"><?php echo esc_html(tguy_sm_array_value($options, 'sm_filter_words')); ?></textarea>
528
+ </fieldset>
529
+ </td>
530
+ </tr>
531
+ <tr>
532
+ <th class="th-full" scope="row" colspan="2">
533
+ <label for="sm_details_verbose">
534
+ <input type="checkbox" id="sm_details_verbose" name="sm_details_verbose" <?php echo (tguy_sm_array_value($options, 'sm_details_verbose') ? 'checked="checked"' : '') ?> />
535
+ Keep detailed information about recent searches (taken from HTTP headers)
536
+ </label>
537
+ </th>
538
+ </tr>
539
+ <tr>
540
+ <th class="th-full" scope="row" colspan="2">
541
+ <label for="sm_disable_donation">
542
+ <input type="checkbox" id="sm_disable_donation" name="sm_disable_donation" <?php echo (tguy_sm_array_value($options, 'sm_disable_donation') ? 'checked="checked"' : '') ?> />
543
+ Hide the &#8220;Do you find this plugin useful?&#8221; box
544
+ </label>
545
+ </th>
546
+ </tr>
547
+ </table>
548
+
549
+ <p class="submit">
550
+ <input name="Submit" class="button-primary" value="Save Changes" type="submit">
551
+ </p>
552
+ </form>
553
+
554
+ <h3>Reset statistics</h3>
555
+
556
+ <p>Click this button to reset all search statistics. This will delete all information about previous searches.</p>
557
+
558
+ <form name="tguy_sm_admin" action="" method="post">
559
+ <?php
560
+ if (function_exists('wp_nonce_field')) {
561
+ wp_nonce_field('search-meter-reset-stats');
562
+ }
563
+ ?>
564
+ <p class="submit">
565
+ <input name="tguy_sm_reset" class="button-secondary delete" value="Reset Statistics" type="submit" onclick="return confirm('You are about to delete all saved search statistics.\n \'Cancel\' to stop, \'OK\' to delete.');" />
566
+ </p>
567
+ </form>
568
+
569
+ <h3>Notes</h3>
570
+
571
+ <p>To see your search statistics, go to the <a href="index.php?page=<?php echo plugin_basename(__FILE__); ?>">Search Meter Dashboard</a>.</p>
572
+
573
+ <p>For information and updates, see the <a href="http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/">Search Meter home page</a>. At that page, you can also offer suggestions, request new features or report problems.</p>
574
+
575
+ <?php if ( ! tguy_sm_array_value($options, 'sm_disable_donation')) { tguy_sm_show_donation_message(); } ?>
576
+
577
+ </div>
578
+ <?php
579
+ }
580
+
581
+ function tguy_sm_reset_stats() {
582
+ global $wpdb;
583
+ // Delete all records
584
+ $wpdb->query("DELETE FROM `{$wpdb->prefix}searchmeter`");
585
+ $wpdb->query("DELETE FROM `{$wpdb->prefix}searchmeter_recent`");
586
+ }
587
+
588
+ function tguy_sm_show_donation_message() {
589
+ ?>
590
+ <p><div style="margin: 0; padding: 0 2ex 0.25ex 0; float: left;">
591
+ <?php tguy_sm_show_donation_button() ?>
592
+ </div>
593
+ <strong>Do you find this plugin useful?</strong><br />
594
+ I write WordPress plugins because I enjoy doing it, but it does take up a lot
595
+ of my time. If you think this plugin is useful, please consider donating some appropriate
596
+ amount by clicking the <strong>Donate</strong> button. You can also send <strong>Bitcoins</strong>
597
+ to address <tt>1542gqyprvQd7gwvtZZ4x25cPeGWVKg45x</tt>. Thanks!</p>
598
+ <?php
599
+ }
600
+
601
+ function tguy_sm_show_donation_button() {
602
+ // I wish PayPal offered a simple little REST-style URL instead of this monstrosity
603
+ ?><form action="https://www.paypal.com/cgi-bin/webscr" method="post" style="margin:0; padding:0;"
604
+ ><input name="cmd" value="_s-xclick" type="hidden" style="margin:0; padding:0;"
605
+ /><input src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!" border="0" type="image" style="margin:0; padding:0;"
606
+ /><input name="encrypted" value="-----BEGIN PKCS7-----MIIHXwYJKoZIhvcNAQcEoIIHUDCCB0wCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYA7BglQn0K1FJvdqm+zAop0IFZb02mJnn56wpZYpbqWE6go360iySXAwUS8eMEMSxp2/OUmWh6VQzm07kEP0buqLG0wwi4yOwawTYB2cahVUPadwYA+KyE78xQI4plMGO1LRchjNdVPkjFuD5s0K64SyYOwtCPYOo/Xs1vZPbpH/zELMAkGBSsOAwIaBQAwgdwGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIP5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donate link: http://thunderguy.com/semicolon/donate/
4
  Tags: search, meter, search-meter, statistics, widget, admin, keywords
5
  Requires at least: 2.8
6
- Tested up to: 3.4.1
7
- Stable tag: 2.8
8
 
9
  Search Meter tracks what your readers are searching for on your blog. View full details of recent searches or stats for the last day, week or month.
10
 
@@ -77,6 +77,9 @@ The [Search Meter home page](http://thunderguy.com/semicolon/wordpress/search-me
77
 
78
  == Changelog ==
79
 
 
 
 
80
  = 2.9 =
81
  * Add a Search Meter dashboard widget.
82
  * Add Search Meter settings link on the Plugins page for convenient configuration.
3
  Donate link: http://thunderguy.com/semicolon/donate/
4
  Tags: search, meter, search-meter, statistics, widget, admin, keywords
5
  Requires at least: 2.8
6
+ Tested up to: 4.2.1
7
+ Stable tag: 2.9.1
8
 
9
  Search Meter tracks what your readers are searching for on your blog. View full details of recent searches or stats for the last day, week or month.
10
 
77
 
78
  == Changelog ==
79
 
80
+ = 2.9.1 =
81
+ * Ensure Search Meter can save searches even if other plugins trigger a query before the main WordPress loop.
82
+
83
  = 2.9 =
84
  * Add a Search Meter dashboard widget.
85
  * Add Search Meter settings link on the Plugins page for convenient configuration.
search-meter.php CHANGED
@@ -3,12 +3,12 @@
3
  Plugin Name: Search Meter
4
  Plugin URI: http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/
5
  Description: Keeps track of what your visitors are searching for. After you have activated this plugin, you can check the Search Meter section in the Dashboard to see what your visitors are searching for on your blog.
6
- Version: 2.9
7
  Author: Bennett McElwee
8
  Author URI: http://thunderguy.com/semicolon/
9
  Donate link: http://thunderguy.com/semicolon/donate/
10
 
11
- $Revision: 594693 $
12
 
13
 
14
  INSTRUCTIONS
@@ -30,7 +30,7 @@ INSTRUCTIONS
30
  Thanks to Kaufman (http://www.terrik.com/wordpress/) and the many others who have offered suggestions.
31
 
32
 
33
- Copyright (C) 2005-12 Bennett McElwee (bennett at thunderguy dotcom)
34
 
35
  This program is free software; you can redistribute it and/or
36
  modify it under the terms of version 2 of the GNU General Public
@@ -268,20 +268,19 @@ function sm_constrain_widget_search_count($number) {
268
  return max(1, min((int)$number, 100));
269
  }
270
 
271
- // Keep track of how many times SM has been called for this request.
272
- // Normally we only record the first time.
273
- $tguy_sm_action_count = 0;
274
 
275
  function tguy_sm_save_search($posts) {
276
  // Check if the request is a search, and if so then save details.
277
  // This is a filter but does not change the posts.
278
- global $wpdb, $wp_query, $tguy_sm_action_count;
279
 
280
- ++$tguy_sm_action_count;
281
  if (is_search()
282
  && !is_paged() // not the second or subsequent page of a previously-counted search
283
  && !is_admin() // not using the administration console
284
- && (1 == $tguy_sm_action_count || TGUY_SM_ALLOW_DUPLICATE_SAVES)
285
  && (tguy_sm_array_value($_SERVER, 'HTTP_REFERER') || TGUY_SM_ALLOW_EMPTY_REFERER) // proper referrer (otherwise could be search engine, cache...)
286
  ) {
287
  // Get all details of this search
@@ -300,7 +299,7 @@ function tguy_sm_save_search($posts) {
300
  $options = get_option('tguy_search_meter');
301
  if ($options['sm_details_verbose']) {
302
  if (TGUY_SM_ALLOW_DUPLICATE_SAVES) {
303
- $details .= "Search Meter action count: $tguy_sm_action_count\n";
304
  }
305
  foreach (array('REQUEST_URI','REQUEST_METHOD','QUERY_STRING','REMOTE_ADDR','HTTP_USER_AGENT','HTTP_REFERER')
306
  as $header) {
@@ -346,6 +345,7 @@ function tguy_sm_save_search($posts) {
346
  WHERE `terms` = '$search_terms' AND `date` = CURDATE()";
347
  $success = $wpdb->query($query);
348
  }
 
349
  }
350
  return $posts;
351
  }
3
  Plugin Name: Search Meter
4
  Plugin URI: http://thunderguy.com/semicolon/wordpress/search-meter-wordpress-plugin/
5
  Description: Keeps track of what your visitors are searching for. After you have activated this plugin, you can check the Search Meter section in the Dashboard to see what your visitors are searching for on your blog.
6
+ Version: 2.9.1
7
  Author: Bennett McElwee
8
  Author URI: http://thunderguy.com/semicolon/
9
  Donate link: http://thunderguy.com/semicolon/donate/
10
 
11
+ $Revision: 751330 $
12
 
13
 
14
  INSTRUCTIONS
30
  Thanks to Kaufman (http://www.terrik.com/wordpress/) and the many others who have offered suggestions.
31
 
32
 
33
+ Copyright (C) 2005-13 Bennett McElwee (bennett at thunderguy dotcom)
34
 
35
  This program is free software; you can redistribute it and/or
36
  modify it under the terms of version 2 of the GNU General Public
268
  return max(1, min((int)$number, 100));
269
  }
270
 
271
+ // Keep track of how many times this search has been saved.
272
+ // The save function may be called many times; normally we only save the first time.
273
+ $tguy_sm_save_count = 0;
274
 
275
  function tguy_sm_save_search($posts) {
276
  // Check if the request is a search, and if so then save details.
277
  // This is a filter but does not change the posts.
278
+ global $wpdb, $wp_query, $tguy_sm_save_count;
279
 
 
280
  if (is_search()
281
  && !is_paged() // not the second or subsequent page of a previously-counted search
282
  && !is_admin() // not using the administration console
283
+ && (0 === $tguy_sm_save_count || TGUY_SM_ALLOW_DUPLICATE_SAVES)
284
  && (tguy_sm_array_value($_SERVER, 'HTTP_REFERER') || TGUY_SM_ALLOW_EMPTY_REFERER) // proper referrer (otherwise could be search engine, cache...)
285
  ) {
286
  // Get all details of this search
299
  $options = get_option('tguy_search_meter');
300
  if ($options['sm_details_verbose']) {
301
  if (TGUY_SM_ALLOW_DUPLICATE_SAVES) {
302
+ $details .= "Search Meter save count: $tguy_sm_save_count\n";
303
  }
304
  foreach (array('REQUEST_URI','REQUEST_METHOD','QUERY_STRING','REMOTE_ADDR','HTTP_USER_AGENT','HTTP_REFERER')
305
  as $header) {
345
  WHERE `terms` = '$search_terms' AND `date` = CURDATE()";
346
  $success = $wpdb->query($query);
347
  }
348
+ ++$tguy_sm_save_count;
349
  }
350
  return $posts;
351
  }