-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchterms-tagging2.php
1992 lines (1935 loc) · 68.6 KB
/
searchterms-tagging2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Search Terms Grabber
Plugin URI: https://karismaid.com/search-terms-grabber
Description: Search Terms Grabber for your WordPress website.
Author: Amri Karisma
Version: 1.6.1
Author URI: https://karismaid.com
*/
/**
* default values for the plugin settings
**/
define('KRS_STG_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('KRS_STG_MAX_SEARCH_TERMS', '10');
define('KRS_STG_AUTO_ADD', '1');
define('KRS_STG_AUTO_LINK', '0');
define('KRS_STG_SHOW_COUNT', '0');
define('KRS_STG_BEFORE_KEYWORD', '<li>');
define('KRS_STG_AFTER_KEYWORD', '</li>');
define('KRS_STG_BEFORE_LIST', '<ul>');
define('KRS_STG_AFTER_LIST', '</ul>');
define('KRS_STG_LIST_HEADER', '<h4>Recent search terms:</h4>');
define('KRS_STG_AUTO_CLEANUP', '90');
define('KRS_STG_AUTO_TAG', '0');
define('KRS_STG_PROMOTE_OLD_POST', '0');
define('KRS_STG_BADWORDS', 'http:,cache:,site:,utm_source,sex,porn,gamble,xxx,nude,squirt,gay,abortion,attack,bomb,casino,cocaine,die,death,erection,gambling,heroin,marijuana,masturbation,pedophile,penis,poker,pussy,terrorist');
define('KRS_STG_ACTIVATED', 'Thank you for registering the plugin. It has been activated.');
define('KRS_STG_DB_VERSION', '2');
/**
* plugin action & filter
**/
register_activation_hook(__file__, 'KRS_STG_stt2_admin_activation');
register_deactivation_hook(__file__, 'KRS_STG_stt2_admin_deactivation');
add_action('admin_menu', 'KRS_STG_stt2_admin_menu_hook');
add_action('admin_notices', 'KRS_STG_stt2_admin_notices');
add_action('wp_head', 'KRS_STG_stt2_function_wp_head_hook');
add_action('KRS_STG_stt2_admin_event_hook', 'KRS_STG_stt2_admin_scheduled_event');
add_action('KRS_STG_stt2_promote_old_post_event_hook', 'KRS_STG_stt2_promote_old_post_scheduled_event');
add_filter('the_content', 'KRS_STG_stt2_admin_content_filter');
/**
* == ADMIN SECTION ==
* wordpress admin setting
**/
/**
* install the plugin and save the initial values
* */
function KRS_STG_stt2_admin_activation()
{
KRS_STG_stt2_db_create_table();
add_option('KRS_STG_stt2_enabled', '1');
$plugin_settings = array(
'max' => KRS_STG_MAX_SEARCH_TERMS, 'auto_add' => KRS_STG_AUTO_ADD, 'auto_link' => KRS_STG_AUTO_LINK,
'show_count' => KRS_STG_SHOW_COUNT, 'before_keyword' => KRS_STG_BEFORE_KEYWORD, 'after_keyword' => KRS_STG_AFTER_KEYWORD,
'before_list' => KRS_STG_BEFORE_LIST, 'after_list' => KRS_STG_AFTER_LIST, 'list_header' => KRS_STG_LIST_HEADER
);
add_option('KRS_STG_stt2_settings', $plugin_settings);
add_option('onlist_status', 0);
add_option('KRS_STG_stt2_auto_tag', KRS_STG_AUTO_TAG);
add_option('KRS_STG_stt2_promote_old_post', KRS_STG_PROMOTE_OLD_POST);
add_option('KRS_STG_stt2_db_version', KRS_STG_DB_VERSION);
$auto_cleanup = intval(get_option('KRS_STG_stt2_auto_cleanup'));
$auto_cleanup = (0 == $auto_cleanup) ? KRS_STG_AUTO_CLEANUP : $auto_cleanup;
add_option('KRS_STG_stt2_auto_cleanup', $auto_cleanup);
add_option('KRS_STG_stt2_badwords', KRS_STG_BADWORDS);
if (!wp_next_scheduled('KRS_STG_stt2_admin_event_hook')) {
wp_schedule_event(time(), 'daily', 'KRS_STG_stt2_admin_event_hook');
}
if (!wp_next_scheduled('KRS_STG_stt2_promote_old_post_event_hook')) {
wp_schedule_event(time(), 'twicedaily', 'KRS_STG_stt2_promote_old_post_event_hook');
}
KRS_STG_stt2_flush_rewrite_rules();
}
/**
* uninstall the plugin
**/
function KRS_STG_stt2_admin_deactivation()
{
remove_filter('the_content', 'KRS_STG_stt2_admin_content_filter');
wp_clear_scheduled_hook('KRS_STG_stt2_admin_event_hook');
wp_clear_scheduled_hook('KRS_STG_stt2_promote_old_post_event_hook');
remove_action('KRS_STG_stt2_admin_event_hook', 'KRS_STG_stt2_admin_scheduled_event');
remove_action('KRS_STG_stt2_promote_old_post_event_hook', 'KRS_STG_stt2_promote_old_post_scheduled_event');
}
/**
* scheduled event to delete unpopular searchterms
**/
function KRS_STG_stt2_admin_scheduled_event()
{
$days = get_option('KRS_STG_stt2_auto_cleanup');
if (0 < $days) {
$result = KRS_STG_stt2_db_cleanup($days);
update_option('KRS_STG_stt2_last_clean_up', date('F j, Y, g:i A') . '; ' . $result . ' search term(s) deleted.');
}
}
/**
* scheduled event to promote old post
**/
function KRS_STG_stt2_promote_old_post_scheduled_event()
{
$promote = get_option('KRS_STG_stt2_promote_old_post');
if (1 == $promote) {
KRS_STG_stt2_promote_old_post();
}
}
/*
* display admin notice to upgrade the database when previous format of database exist
*/
function KRS_STG_stt2_admin_notices()
{
if (get_option('onlist_status') < 2 || '2' !== get_option('KRS_STG_stt2_db_version')) {
echo "<div class='updated'><p>" . sprintf(__('<a href="%s">Recent search terms</a> plugin need your attention.'), "options-general.php?page=" . basename(plugin_basename(__FILE__))) . "</p></div>";
}
}
/**
* framework to handle the admin form
* */
function KRS_STG_stt2_create_admin_menu()
{
KRS_STG_stt2_admin_print_title();
if (isset($_GET['onlist'])) {
if (trim($_GET['onlist']) == 1) {
echo '<div id="message" class="updated fade"><p><strong>' . KRS_STG_ACTIVATED . '</strong></p></div>';
}
}
if (KRS_STG_stt2_onlist()) {
KRS_STG_stt2_admin_print_header();
if ('2' !== get_option('KRS_STG_stt2_db_version') && !isset($_POST['upgrade_db_structure'])) {
KRS_STG_stt2_admin_upgrade_db();
} else {
/* intercept form submit */
if (isset($_POST['submit'])) {
KRS_STG_stt2_admin_update_options();
} elseif (isset($_POST['upgrade_db_structure'])) {
KRS_STG_stt2_db_upgrade_db_structure();
if ('2' == get_option('KRS_STG_stt2_db_version')) { ?>
<div id="message" class="updated fade">
<p>Recent search terms database was successfully upgraded for better performance.</p>
</div>
<?php } else { ?>
<div id="message" class="updated fade" style='background-color:#f66;'>
<p>Recent search terms database! </p>
</div>
<?php }
} elseif (isset($_POST['delete']) || isset($_POST['delete_all'])) {
KRS_STG_stt2_admin_delete_searchterms();
}
/* print admin page */
if (isset($_GET['stats'])) {
KRS_STG_stt2_admin_print_stats();
} elseif (isset($_GET['stt2_help'])) {
KRS_STG_stt2_admin_help();
} elseif (isset($_GET['donate'])) {
KRS_STG_stt2_admin_donate();
} elseif (isset($_GET['stt2_no_traffics'])) {
KRS_STG_stt2_admin_print_no_traffic($_GET['stt2_no_traffics']);
} else {
KRS_STG_stt2_admin_print_admin_page();
}
}
KRS_STG_stt2_admin_print_footer();
}
}
/**
* print admin page title
**/
function KRS_STG_stt2_admin_print_title()
{
?>
<style type="text/css">
.stt2-table {
margin: 14px;
}
.stt2-table tr {
height: 28px;
}
.inside p {
margin: 14px;
}
.inside .frame {
margin: 10px;
}
.inside .list li {
font-size: 11px;
}
</style>
<div class="wrap">
<div class="icon32" id="icon-options-general"><br></div>
<h2>Recent Search Terms</h2>
<?php
}
/**
* print admin page header
**/
function KRS_STG_stt2_admin_print_header()
{
$baseurl = "options-general.php?page=" . basename(plugin_basename(__FILE__));
?>
<div>
<p> If you think this plugin useful, please consider making a
<a href="<?php echo $baseurl . '&donate'; ?>">donation</a> or write a review about it or at least give it a good rating on
<a href="http://wordpress.org/extend/plugins/recent-search-terms/" target="_blank">WordPress.org</a>.
</p>
</div>
<p><a href="<?php echo $baseurl; ?>">Settings</a> | <a href="<?php echo get_bloginfo('url') . '/wp-admin/widgets.php'; ?>">Widgets</a> | <a href="<?php echo $baseurl . '&stt2_help=1'; ?>">Help</a> | <strong>More Stats:</strong> <a href="<?php echo $baseurl . '&stats=1&count=50'; ?>">50</a>,
<a href="<?php echo $baseurl . '&stats=1&count=100'; ?>">100</a>,
<a href="<?php echo $baseurl . '&stats=1&count=200'; ?>">200</a>,
<a href="<?php echo $baseurl . '&stats=1&count=300'; ?>">300</a>,
<a href="<?php echo $baseurl . '&stats=1&count=400'; ?>">400</a>,
<a href="<?php echo $baseurl . '&stats=1&count=500'; ?>">500</a> | <strong>
<a href="<?php echo $baseurl . '&stt2_no_traffics=0'; ?>">Posts Without Any Traffic from Search Engines</a></strong> ( <a href="<?php echo $baseurl . '&stt2_no_traffics=1'; ?>">URL Only</a> )
</p>
<?php
}
/*
* delete unwanted searchterms from admin menu
*/
function KRS_STG_stt2_admin_delete_searchterms()
{
global $wpdb;
if (isset($_POST['delete_terms']) && !empty($_POST['delete_terms'])) {
$msg = 'Search terms that contain "' . $_POST['delete_terms'] . '"';
$success = KRS_STG_stt2_db_delete_searchterms($_POST['delete_terms']);
} elseif (isset($_POST['delete_all'])) {
$msg = 'All search terms';
$success = KRS_STG_stt2_db_delete_searchterms('delete_all_terms');
} else {
?>
<div id="message" class="updated fade">
<p> Please enter the search terms you want to delete, separate them with a comma.
</p>
</div>
<?php
}
$msg .= ($success) ? ' has been removed from the database.' : ' cannot be removed from the database or cannot be found in the database.';
?>
<div id="message" class="updated fade">
<p><?php echo $msg; ?></p>
</div>
<?php
}
/**
* display list of search terms, used only in the admin page
* */
function KRS_STG_stt2_admin_print_searchterms($type = 'popular')
{
$count = (isset($_GET['stats'])) ? $_GET['count'] : 15;
switch ($type) {
case 'popular':
$searchterms = KRS_STG_stt2_db_get_popular_terms($count);
break;
case 'recent':
$searchterms = KRS_STG_stt2_db_get_recent_terms($count);
break;
case 'home':
$searchterms = KRS_STG_stt2_db_get_home_keywords($count);
break;
}
if (!empty($searchterms)) {
$toReturn .= "<ol>";
foreach ($searchterms as $term) {
$permalink = (0 == $term->post_id) ? 'http://www.google.com/search?q=' . str_replace(' ', '+', $term->meta_value) : get_permalink($term->post_id);
$toReturn .= "<li><a href=\"$permalink\" target=\"_blank\">$term->meta_value</a>";
$toReturn .= " ($term->meta_count)</li>";
}
$toReturn = trim($toReturn, ', ');
$toReturn .= "</ol>";
//$toReturn = htmlspecialchars_decode($toReturn);
return $toReturn;
} else {
return false;
}
}
/**
* print the admin form
* */
function KRS_STG_stt2_admin_print_admin_page()
{
$options = get_option('KRS_STG_stt2_settings');
$auto_cleanup = intval(get_option('KRS_STG_stt2_auto_cleanup'));
$auto_cleanup = (0 == $auto_cleanup) ? KRS_STG_AUTO_CLEANUP : $auto_cleanup;
$promote_old_post = get_option('KRS_STG_stt2_promote_old_post');
$badwords = get_option('KRS_STG_stt2_badwords');
if (empty($badwords)) {
$badwords = KRS_STG_BADWORDS;
update_option('KRS_STG_stt2_badwords', trim($badwords, ' ,.'));
};
?>
<div class="postbox-container" style="width: 74%;">
<div class="metabox-holder">
<div class="meta-box-sortables ui-sortable">
<div id="stt2settings" class="postbox">
<div class="handlediv" title="Click to toggle">
<br />
</div>
<h3 class="hndle">
<span>General Settings ( Widgets has it own settings )</span>
</h3>
<div class="inside">
<form id="stt2-options" method="post" action="">
<table class="stt2-table">
<tr>
<td width="350px">
<label> Enabled:
</label>
</td>
<td>
<input type="checkbox" <?php if (1 == get_option('KRS_STG_stt2_enabled')) {
echo 'checked';
}; ?> value="1" name="enabled" />
</td>
</tr>
<tr>
<td width="350px">
<label> Max number of search terms:
</label>
</td>
<td>
<input type="text" name="max" value="<?php echo $options['max']; ?>" size="10" />
</td>
</tr>
<tr>
<td>
<label> Text and code for list header:
</label>
</td>
<td>
<input type="text" name="list_header" value="<?php echo htmlspecialchars(stripslashes($options['list_header'])); ?>" size="50" />
</td>
</tr>
<tr>
<td>
<label> Text and code before and after the list:
</label>
</td>
<td> Before:
<input type="text" name="before_list" value="<?php echo htmlspecialchars(stripslashes($options['before_list'])); ?>" size="10" /> After:
<input type="text" name="after_list" value="<?php echo htmlspecialchars(stripslashes($options['after_list'])); ?>" size="10" />
</td>
</tr>
<tr>
<td>
<label> Text and code before and after each keyword:
</label>
</td>
<td> Before:
<input type="text" name="before_keyword" value="<?php echo htmlspecialchars(stripslashes($options['before_keyword'])); ?>" size="10" /> After:
<input type="text" name="after_keyword" value="<?php echo htmlspecialchars(stripslashes($options['after_keyword'])); ?>" size="10" />
</td>
</tr>
<tr>
<td style="vertical-align: top;">
<label> Convert search terms into links:
</label>
</td>
<td>
<Input type="radio" name="auto_link" value="1" <?php if (1 == $options['auto_link']) {
echo 'checked';
}; ?> /> Yes, link to post content<br />
<Input type="radio" name="auto_link" value="2" <?php if (2 == $options['auto_link']) {
echo 'checked';
}; ?> /> Yes, link to search page ( Not Recommended )<br />
<Input type="radio" name="auto_link" value="0" <?php if (0 == $options['auto_link']) {
echo 'checked';
}; ?> /> No
</td>
</tr>
<tr>
<td>
<label> Display search counts for each search term:
</label>
</td>
<td>
<Input type="radio" name="show_count" value="1" <?php if (1 == $options['show_count']) {
echo 'checked';
}; ?> /> Yes
<Input type="radio" name="show_count" value="0" <?php if (0 == $options['show_count']) {
echo 'checked';
}; ?> /> No
</td>
</tr>
<tr>
<td>
<label> Add list automatically right after post content:
</label>
</td>
<td>
<Input type="radio" name="auto_add" value="1" <?php if (1 == $options['auto_add']) {
echo 'checked';
}; ?> /> Yes
<Input type="radio" name="auto_add" value="0" <?php if (0 == $options['auto_add']) {
echo 'checked';
}; ?> /> No
</td>
</tr>
<tr>
<td>
<label> Save popular search terms as post tags:
</label>
</td>
<td>
<Input type="radio" name="auto_tag" value="1" <?php if (1 == get_option('KRS_STG_stt2_auto_tag')) {
echo 'checked';
}; ?> /> Yes
<Input type="radio" name="auto_tag" value="0" <?php if (0 == get_option('KRS_STG_stt2_auto_tag')) {
echo 'checked';
}; ?> /> No
</td>
</tr>
<tr>
<td>
<label> Auto clean up unused search terms after:
</label>
</td>
<td>
<input type="text" name="auto_cleanup" value="<?php echo $auto_cleanup; ?>" size="10" /> days ( default is 90 days )
</td>
</tr>
<tr>
<td valign="top" style="padding-top:10px;">
<label>Block the following bad words:</label>
</td>
<td>
<textarea name="badwords" cols="55" rows="3"><?php echo $badwords; ?></textarea>
</td>
</tr>
<tr>
<td>
<label> Promote old post with no search engine traffic:
</label>
</td>
<td>
<Input type="radio" name="promote_old_post" value="1" <?php if (1 == $promote_old_post) {
echo 'checked';
}; ?> /> Yes
<Input type="radio" name="promote_old_post" value="0" <?php if (0 == $promote_old_post) {
echo 'checked';
}; ?> /> No
</td>
</tr>
<tr>
<td colspan=2>
<br />
<span class="submit" style="margin-top:14px;">
<input class="button-primary" type="submit" name="submit" value="Save Changes" />
</span>
</td>
</tr>
</table>
</form>
</div>
</div>
<div id="stt2-cleanup" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Auto Clean Up Unused Search Terms:</span>
</h3>
<div class="inside">
<p>
<ul style="margin: 0pt 0pt 14px 30px; list-style-type: circle;">
<?php $last_cleanup = get_option('KRS_STG_stt2_last_clean_up'); ?>
<li>Last database cleaned up: <?php if (!empty($last_cleanup)) {
echo $last_cleanup;
} else {
echo 'Never';
} ?></li>
<li>Next scheduled database clean up on
<?php echo date('F j, Y, g:i A', wp_next_scheduled('KRS_STG_stt2_admin_event_hook')); ?>.</li>
</ul>
</p>
<p>Once a day, we perform database clean up by removing search terms that are never used again within <?php echo $auto_cleanup; ?> days. It is necessary to prevent an excessive use of server resources, especially if you use shared web hosting.</p>
<p>If your blog has more than a thousand visitors per day, we recommend to reduce the auto clean up setting. You are free to determine when a search term is considered not useful, 7, 15, or 30 days maybe?</p>
</div>
</div>
<div id="stt2-promote-post" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Promote Post With No Search Engine Traffic</span>
</h3>
<div class="inside">
<p>
<ul style="margin: 0pt 0pt 14px 30px; list-style-type: circle;">
<?php
$wo_traffic = KRS_STG_stt2_db_get_number_of_posts_wo_traffic();
$total = KRS_STG_stt2_db_get_number_of_posts();
$percentage = intval(($wo_traffic / $total) * 100);
?>
<li>Number of posts with no search engine traffic: <?php echo "$wo_traffic of $total ( $percentage% )"; ?></li>
<li>Last promoted blog post: <?php KRS_STG_stt2_the_last_promoted_post(); ?></li>
<li>Next scheduled promotion on <?php echo date('F j, Y, g:i A', wp_next_scheduled('KRS_STG_stt2_promote_old_post_event_hook')); ?>.</li>
</ul>
</p>
<p>To ensure that the promoted post get indexed, use free services by<a href="http://www.webspace.technology/free" target="_blank">www.webspace.technology</a> </p>
<p>Thank You!</p>
</div>
</div>
<div id="stt2-del" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Delete Search Terms:</span>
</h3>
<div class="inside">
<form id="stt2-delete" method="post" action="">
<p> Enter the search terms you want to delete, separate them with a comma ( e.g.: keyword1,keyword2 ): </p>
<p><textarea name="delete_terms" cols="75" rows="2"></textarea></p>
<p>
<span class="submit">
<input type="submit" name="delete" value="Delete" /><br /><br /> OR <br /><br />
<input type="submit" name="delete_all" value="Delete All ( Reset )" />
</span>
</p>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="postbox-container" style="width: 25%;">
<div class="metabox-holder">
<div class="meta-box-sortables ui-sortable">
<div id="stt2-donate" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Please Donate to Support Us:
</span>
</h3>
<div class="inside">
<div class="frame list">
<?php echo KRS_STG_stt2_admin_donate(); ?>
</div>
</div>
</div>
<div id="stt2-top" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Popular Search Terms:
</span>
</h3>
<div class="inside">
<div class="frame list">
<?php echo KRS_STG_stt2_admin_print_searchterms('popular'); ?>
</div>
</div>
</div>
<div id="stt2-recent" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Recent Search Terms:
</span>
</h3>
<div class="inside">
<div class="frame list">
<?php echo KRS_STG_stt2_admin_print_searchterms('recent'); ?>
</div>
</div>
</div>
<div id="stt2-mainkeywords" class="postbox">
<div class="handlediv" title="Click to toggle">
</div>
<h3 class="hndle">
<span>Popular Home Keywords:
</span>
</h3>
<div class="inside">
<div class="frame list">
<?php echo KRS_STG_stt2_admin_print_searchterms('home'); ?>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
/**
* filter the content and add the search terms right after the post content ( on single and page only )
* */
function KRS_STG_stt2_admin_content_filter($content)
{
if (!is_home()) {
$options = get_option('KRS_STG_stt2_settings');
if ($options['auto_add'])
$content .= stt_terms_list();
}
return $content;
}
/**
* update options and save it to db
* */
function KRS_STG_stt2_admin_update_options()
{
if (
isset($_POST['max']) && isset($_POST['before_keyword']) && isset($_POST['after_keyword']) &&
isset($_POST['auto_add']) && isset($_POST['auto_link']) && isset($_POST['show_count']) && isset($_POST['before_list'])
&& isset($_POST['after_list']) && isset($_POST['list_header'])
) {
$options['max'] = (intval($_POST['max']) < 2) ? 2 : intval($_POST['max']);
$options['before_keyword'] = $_POST['before_keyword'];
$options['after_keyword'] = $_POST['after_keyword'];
$options['before_list'] = $_POST['before_list'];
$options['after_list'] = $_POST['after_list'];
$options['list_header'] = $_POST['list_header'];
$options['auto_add'] = intval($_POST['auto_add']);
$options['auto_link'] = intval($_POST['auto_link']);
$options['show_count'] = intval($_POST['show_count']);
update_option('KRS_STG_stt2_settings', $options);
update_option('KRS_STG_stt2_enabled', $_POST['enabled']);
update_option('KRS_STG_stt2_auto_tag', intval($_POST['auto_tag']));
update_option('KRS_STG_stt2_promote_old_post', intval($_POST['promote_old_post']));
update_option('KRS_STG_stt2_badwords', trim($_POST['badwords'], ' ,.'));
KRS_STG_stt2_db_delete_searchterms($_POST['badwords']);
$autocleanup = (0 == intval($_POST['auto_cleanup'])) ? 90 : intval($_POST['auto_cleanup']);
update_option('KRS_STG_stt2_auto_cleanup', $autocleanup);
?>
<div id="message" class="updated fade">
<p> Options saved.
</p>
</div>
<?php
} else {
?>
<div id="message" class="updated fade">
<p> Failed to save options.
</p>
</div>
<?php
KRS_STG_stt2_flush_rewrite_rules();
}
}
/**
* add the menu into WordPress admin menu
* */
function KRS_STG_stt2_admin_menu_hook()
{
if (function_exists('add_options_page')) {
add_options_page(
'Recent search terms',
'Recent search terms',
'manage_options',
'searchterms-tagging2.php',
'KRS_STG_stt2_create_admin_menu'
);
}
}
/**
* print footer
**/
function KRS_STG_stt2_admin_print_footer()
{
?>
<div class="postbox-container" style="width: 98%;">
<div class="metabox-holder">
<div id="stt2-copyright" class="postbox">
<div class="inside">
<div class="frame" style="text-align:center">
<p><strong>Recommended SEO Resources: <a href="http://www.webspace.technology" target="_blank">Webspace Technology</a></strong></p>
<p>Copyright © 2010-2011 by webspace.technology Original writer : Purwedi Kurniawan.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php }
require_once(KRS_STG_PLUGIN_DIR . '/plugin-update-checker/plugin-update-checker.php');
$updateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://github.com/amrikarisma/recent-search-terms',
__FILE__,
'recent-search-terms'
);
//Optional: If you're using a private repository, specify the access token like this:
$updateChecker->setAuthentication('803c80fad921d633c3c0e9e9b754b6dbd3398877');
//Optional: Set the branch that contains the stable release.
$updateChecker->setBranch('master');
/**
* == DATABASE SECTION ==
* all main database related functions of recent search terms plugin
**/
/**
* list blog posts with no search engine traffic
* @return: OBJECT results with ID, post_title properties
**/
function KRS_STG_stt2_db_get_posts_wo_traffic($count = 100)
{
$results = wp_cache_get('stt2_posts_wo_traffic_' . $count);
if (false == $results) {
global $wpdb;
$sql = "SELECT ID, post_title FROM $wpdb->posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND ID NOT IN ( SELECT post_id FROM " . $wpdb->prefix . "stt2_meta )
ORDER BY post_date DESC LIMIT " . $count . ";";
$results = $wpdb->get_results($sql);
wp_cache_set('stt2_posts_wo_traffic_' . $count, $results, 3600);
}
if ($results) return $results;
}
/**
* promote single blog post with no search engine traffic
* @params: $old_post_ID, $new_time, $gmt_time
* @return: 1 if succes
**/
function KRS_STG_stt2_db_promote_single_post_wo_traffic($old_post_ID)
{
global $wpdb;
$new_time = date('Y-m-d H:i:s');
$gmt_time = get_gmt_from_date($new_time);
$sql = "UPDATE $wpdb->posts
SET post_date = '$new_time', post_date_gmt = '$gmt_time', post_modified = '$new_time', post_modified_gmt = '$gmt_time'
WHERE ID = '$old_post_ID';";
$return = $wpdb->query($sql);
return $return;
}
/**
* get the number of posts with no traffic yet
**/
function KRS_STG_stt2_db_get_number_of_posts_wo_traffic()
{
$post_count = wp_cache_get('stt2_number_of_posts_wo_traffic');
// wp_die($post_count);
if (!isset($post_count)) {
global $wpdb;
$sql = "SELECT count(`ID`) FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post' AND ID NOT IN (
SELECT post_id FROM " . $wpdb->prefix . "stt2_meta );";
$post_count = $wpdb->get_var($sql);
wp_cache_set('stt2_number_of_posts_wo_traffic', $post_count, 86400);
}
return $post_count;
}
/**
* get single post ID of posts with no traffic yet
* doesn't caching, only run twice a day, and need the most current status
**/
function KRS_STG_stt2_db_get_id_post_wo_traffic()
{
global $wpdb;
$sql = "SELECT `ID` FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post' AND ID NOT IN (
SELECT post_id FROM " . $wpdb->prefix . "stt2_meta WHERE `post_id` != 0 ) LIMIT 1;";
$post_ID = $wpdb->get_var($wpdb->prepare($sql));
return $post_ID;
}
/**
* get last promoted post title
**/
function KRS_STG_stt2_db_get_last_promoted_post_title($id)
{
$post_title = wp_cache_get('stt2_last_promoted_post_title_' . $id);
if (false == $post_title) {
global $wpdb;
$sql = "SELECT `post_title` FROM $wpdb->posts WHERE `ID` = $id;";
$post_title = $wpdb->get_var($wpdb->prepare($sql));
wp_cache_set('stt2_last_promoted_post_title_' . $id, $post_title);
}
return $post_title;
}
/**
* get the number of posts
**/
function KRS_STG_stt2_db_get_number_of_posts()
{
// wp_die($post_count);
global $id;
if (!isset($post_count)) {
global $wpdb;
$sql = "SELECT count(`ID`) FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post';";
$post_count = $wpdb->get_var($sql);
wp_cache_set('stt2_get_number_of_posts' . $id, $post_count, 3600);
} else
$post_count = wp_cache_get('stt2_get_number_of_posts' . $id);
return $post_count;
}
/**
* get 10 popular search terms for the posts, will be used as tags
* @param $id: post id
* @return: comma separated text of popular search terms
**/
function KRS_STG_stt2_db_get_popular_tags($id)
{
$a_results = wp_cache_get('stt2_get_popular_tags_' . $id);
if (false == $a_results) {
global $wpdb;
$sql = "SELECT meta_value FROM " . $wpdb->prefix . "stt2_meta WHERE ( post_id = " . $id . " AND meta_count > 25 AND meta_value NOT LIKE '%http%' ) ORDER BY meta_count DESC LIMIT 5;";
$a_results = $wpdb->get_results($sql);
wp_cache_set('stt2_get_popular_tags_' . $id, $a_results, 86400);
}
if ($a_results) {
foreach ($a_results as $value) {
$result .= $value->meta_value . ',';
}
$result = trim($result, ',');
}
return $result;
}
/**
* get popular search terms
* @param $count: max number of search terms
**/
function KRS_STG_stt2_db_get_popular_terms($count)
{
$result = wp_cache_get('stt2_popular_terms');
if (false == $result) {
global $wpdb;
$result = $wpdb->get_results("SELECT `meta_value`,`meta_count`,`post_id` FROM `" . $wpdb->prefix . "stt2_meta` WHERE `post_id` != 0 ORDER BY `meta_count` DESC LIMIT " . $count . ";");
wp_cache_set('stt2_popular_terms', $result, 86400);
}
return $result;
}
/**
* get home keywords
* @param $count: max number of search terms
**/
function KRS_STG_stt2_db_get_home_keywords($count)
{
$result = wp_cache_get('stt2_home_keywords');
if (false == $result) {
global $wpdb;
$result = $wpdb->get_results("SELECT `meta_value`,`meta_count`,`post_id` FROM `" . $wpdb->prefix . "stt2_meta` WHERE `post_id` = 0 ORDER BY `meta_count` DESC LIMIT " . $count . ";");
wp_cache_set('stt2_home_keywords', $result, 86400);
}
return $result;
}
/**
* get list of search terms
* @param $max: max number of search terms
**/
function KRS_STG_stt2_db_get_search_terms($max)
{
$result = wp_cache_get('stt2_search_terms_' . $max);
if (false == $result) {
global $wpdb, $post;
$result = $wpdb->get_results("SELECT `meta_value`,`meta_count` FROM `" . $wpdb->prefix . "stt2_meta` WHERE `post_id` = $post->ID ORDER BY `meta_count` DESC LIMIT " . $max . ";");
wp_cache_set('stt2_search_terms_' . $max, $result, 900);
}
return $result;
}
/**
* get recent search terms
* @param $count: max number of search terms
**/
function KRS_STG_stt2_db_get_recent_terms($count)
{
$result = wp_cache_get('stt2_recent_terms');
if (false == $result) {
global $wpdb;
$result = $wpdb->get_results("SELECT `meta_value`,`meta_count`,`post_id` FROM `" . $wpdb->prefix . "stt2_meta` WHERE `post_id` != 0 ORDER BY `last_modified` DESC LIMIT " . $count . ";");
wp_cache_set('stt2_recent_terms', $result, 900);
}
return $result;
}
/**
* get random search terms
* @param $count: max number of search terms
**/
function KRS_STG_stt2_db_get_random_terms($count)
{
$result = wp_cache_get('stt2_random_terms');
if (false == $result) {
global $wpdb;
$result = $wpdb->get_results("SELECT `meta_value`,`post_id` FROM `" . $wpdb->prefix . "stt2_meta` WHERE `post_id` != 0 ORDER BY RAND() LIMIT " . $count . ";");
wp_cache_set('stt2_random_terms', $result, 3600);
}
return $result;
}
/**
* get the number of searchterms
**/
function KRS_STG_stt2_db_get_number_of_searchterms()
{
$result = wp_cache_get('stt2_number_of_searchterms');
if (false == $result) {
global $wpdb;
$result = $wpdb->get_var("SELECT COUNT(`meta_value`) FROM " . $wpdb->prefix . "stt2_meta;");
wp_cache_set('stt2_number_of_searchterms', $result, 3600);
}
return $result;
}
/**
* delete searchterms from the database
* @param $searchterms: comma separated search terms or 'delete_all_terms'
* @output: number of rows effected by the delete query
**/
function KRS_STG_stt2_db_delete_searchterms($searchterms)
{
global $wpdb;
if ('delete_all_terms' !== $searchterms) {
$arr_searchterms = explode(',', $searchterms);
foreach ($arr_searchterms as $value) {
if (!empty($value)) {
$success += $wpdb->query("DELETE FROM " . $wpdb->prefix . "stt2_meta WHERE LOWER(meta_value) LIKE '%" . strtolower(trim($value)) . "%'; ");
}
}
} else {
$success = $wpdb->query("DELETE FROM " . $wpdb->prefix . "stt2_meta");
}
$opt = $wpdb->query('OPTIMIZE TABLE ' . $wpdb->prefix . 'stt2_meta;');
return $success;
}
/**
* clean the database in daily basis
**/
function KRS_STG_stt2_db_cleanup($days)
{
global $wpdb;
$result = $wpdb->query('DELETE FROM ' . $wpdb->prefix . 'stt2_meta WHERE (meta_count < 10) AND (date(last_modified) < date(now()-interval ' . $days . ' day));');
$opt = $wpdb->query('OPTIMIZE TABLE ' . $wpdb->prefix . 'stt2_meta;');
return $result;
}
/**
* create stt2 database; UTF-8 version
* */
function KRS_STG_stt2_db_create_table()
{
global $wpdb;
$table_name = $wpdb->prefix . 'stt2_meta';
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name';") != $table_name) {
$sql = "CREATE TABLE `" . $wpdb->prefix . "stt2_meta` (
`post_id` INT( 20 ) NOT NULL ,
`meta_value` VARCHAR ( 255 ) NOT NULL,
`meta_count` INT( 20 ) NOT NULL DEFAULT '1',
`last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE (`meta_value`),
PRIMARY KEY ( `post_id` , `meta_value` )
) CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
update_option('KRS_STG_stt2_db_version', '2');
}
}
/**
* update stt2 database into version 2; UTF-8 with additional UNIQUE column
**/
function KRS_STG_stt2_db_upgrade_db_structure()
{
global $wpdb;
$sql = "CREATE TABLE `" . $wpdb->prefix . "stt2_meta_tmp` (
`post_id` INT( 20 ) NOT NULL ,
`meta_value` VARCHAR ( 255 ) NOT NULL,
`meta_count` INT( 20 ) NOT NULL DEFAULT '1',
`last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE (`meta_value`),
PRIMARY KEY ( `post_id` , `meta_value` )
) CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);