· 9 years ago · Nov 23, 2016, 08:30 AM
1<?php
2/*
3Plugin Name: SEO SearchTerms Tagging 2
4Plugin URI: http://exclusivewordpress.com/searchterms-tagging-2-plugin/
5Description: Multiply blog traffic by strengthening on page SEO, increasing the number of indexed pages, auto convert search terms into post tags, and more.
6Version: 1.53
7Author: Purwedi Kurniawan
8Author URI: http://exclusivewordpress.com
9*/
10/**
11* update: july 30, 2011
12* :: security bug fix
13* last update: december 29, 2010
14* :: auto correct bad words list not in the right format
15* :: upgrade db structure for better performance
16* :: using wordpress cache to cache db query
17* update: december 23, 2010
18* :: option to upgrade database to support international characters (default for new installation)
19* :: change search permalink (using - instead of +)
20* :: promote post with no traffic after 30 days as most recent post (max 2 post daily)
21* :: auto clean up database now is mandatory
22* :: record all search terms, if ID = 0 -> homepage
23* :: display home keywords in admin page
24**/
25/**
26* default values for the plugin settings
27**/
28define('PK_MAX_SEARCH_TERMS','10');
29define('PK_AUTO_ADD','1');
30define('PK_AUTO_LINK','0');
31define('PK_SHOW_COUNT','0');
32define('PK_BEFORE_KEYWORD','<li>');
33define('PK_AFTER_KEYWORD','</li>');
34define('PK_BEFORE_LIST','<ul>');
35define('PK_AFTER_LIST','</ul>');
36define('PK_LIST_HEADER','<h4>Incoming search terms:</h4>');
37define('PK_AUTO_CLEANUP','90');
38define('PK_AUTO_TAG','0');
39define('PK_PROMOTE_OLD_POST','0');
40define('PK_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');
41define('PK_ACTIVATED','Thank you for registering the plugin. It has been activated.');
42define('PK_DB_VERSION','2');
43/**
44* plugin action & filter
45**/
46register_activation_hook(__file__,'pk_stt2_admin_activation');
47register_deactivation_hook(__file__,'pk_stt2_admin_deactivation');
48add_action('admin_menu','pk_stt2_admin_menu_hook');
49add_action('admin_notices', 'pk_stt2_admin_notices');
50add_action('wp_head', 'pk_stt2_function_wp_head_hook');
51add_action('pk_stt2_admin_event_hook', 'pk_stt2_admin_scheduled_event');
52add_action('pk_stt2_promote_old_post_event_hook', 'pk_stt2_promote_old_post_scheduled_event');
53add_filter('the_content','pk_stt2_admin_content_filter');
54/**
55* == ADMIN SECTION ==
56* wordpress admin setting
57**/
58/**
59 * install the plugin and save the initial values
60 * */
61function pk_stt2_admin_activation(){
62 pk_stt2_db_create_table();
63 add_option('pk_stt2_enabled', '1');
64 $plugin_settings = array( 'max' => PK_MAX_SEARCH_TERMS, 'auto_add' => PK_AUTO_ADD, 'auto_link' => PK_AUTO_LINK,
65 'show_count' => PK_SHOW_COUNT, 'before_keyword' => PK_BEFORE_KEYWORD, 'after_keyword' => PK_AFTER_KEYWORD,
66 'before_list' => PK_BEFORE_LIST, 'after_list' => PK_AFTER_LIST,'list_header' => PK_LIST_HEADER );
67
68 add_option ( 'pk_stt2_settings', $plugin_settings );
69 add_option ( 'onlist_status', 0 );
70 add_option ( 'pk_stt2_auto_tag', PK_AUTO_TAG );
71 add_option ( 'pk_stt2_promote_old_post', PK_PROMOTE_OLD_POST );
72 add_option ( 'pk_stt2_db_version', PK_DB_VERSION );
73 $auto_cleanup = intval( get_option ( 'pk_stt2_auto_cleanup' ) );
74 $auto_cleanup = ( 0 == $auto_cleanup ) ? PK_AUTO_CLEANUP : $auto_cleanup;
75 add_option ( 'pk_stt2_auto_cleanup', $auto_cleanup );
76 add_option ( 'pk_stt2_badwords', PK_BADWORDS );
77
78 if (!wp_next_scheduled('pk_stt2_admin_event_hook')) {
79 wp_schedule_event( time(), 'daily', 'pk_stt2_admin_event_hook' );
80 }
81
82 if (!wp_next_scheduled('pk_stt2_promote_old_post_event_hook')) {
83 wp_schedule_event( time(), 'twicedaily', 'pk_stt2_promote_old_post_event_hook' );
84 }
85
86 pk_stt2_flush_rewrite_rules();
87}
88/**
89* uninstall the plugin
90**/
91function pk_stt2_admin_deactivation(){
92 remove_filter('the_content','pk_stt2_admin_content_filter');
93 wp_clear_scheduled_hook('pk_stt2_admin_event_hook');
94 wp_clear_scheduled_hook('pk_stt2_promote_old_post_event_hook');
95 remove_action('pk_stt2_admin_event_hook', 'pk_stt2_admin_scheduled_event');
96 remove_action('pk_stt2_promote_old_post_event_hook', 'pk_stt2_promote_old_post_scheduled_event');
97}
98/**
99* scheduled event to delete unpopular searchterms
100**/
101function pk_stt2_admin_scheduled_event(){
102 $days = get_option('pk_stt2_auto_cleanup');
103 if ( 0 < $days ) {
104 $result = pk_stt2_db_cleanup( $days );
105 update_option('pk_stt2_last_clean_up',date('F j, Y, g:i A').'; '.$result.' search term(s) deleted.');
106 }
107}
108/**
109* scheduled event to promote old post
110**/
111function pk_stt2_promote_old_post_scheduled_event(){
112 $promote = get_option('pk_stt2_promote_old_post');
113 if ( 1 == $promote ) {
114 pk_stt2_promote_old_post();
115 }
116}
117/*
118* display admin notice to upgrade the database when previous format of database exist
119*/
120function pk_stt2_admin_notices() {
121 if ( get_option('onlist_status') < 2 || '2' !== get_option('pk_stt2_db_version') ){
122 echo "<div class='updated'><p>" . sprintf(__('<a href="%s">SEO SearchTerms Tagging 2</a> plugin need your attention.'), "options-general.php?page=".basename(plugin_basename(__FILE__))). "</p></div>";
123 }
124}
125/**
126 * framework to handle the admin form
127 * */
128function pk_stt2_create_admin_menu(){
129 pk_stt2_admin_print_title();
130 if ( trim($_GET['onlist']) == 1 )
131 echo '<div id="message" class="updated fade"><p><strong>'.PK_ACTIVATED.'</strong></p></div>';
132
133 if ( pk_stt2_onlist() ) {
134 pk_stt2_admin_print_header();
135
136 if ('2' !== get_option('pk_stt2_db_version') && !isset($_POST['upgrade_db_structure'])){
137 pk_stt2_admin_upgrade_db();
138 } else {
139
140 /* intercept form submit */
141 if (isset($_POST['submit'])){
142 pk_stt2_admin_update_options();
143 } elseif ( isset($_POST['upgrade_db_structure']) ){
144 pk_stt2_db_upgrade_db_structure();
145 if ( '2' == get_option('pk_stt2_db_version') ){ ?>
146 <div id="message" class="updated fade">
147 <p>SEO SearchTerms Tagging 2 database was successfully upgraded for better performance.</p>
148 </div>
149 <?php } else { ?>
150 <div id="message" class="updated fade" style='background-color:#f66;'>
151 <p>Failed to upgrade SEO SearchTerms Tagging 2 database! </p>
152 </div>
153 <?php }
154 } elseif ( isset($_POST['delete']) || isset($_POST['delete_all']) ){
155 pk_stt2_admin_delete_searchterms();
156 }
157 /* print admin page */
158 if (isset($_GET['stats'])){
159 pk_stt2_admin_print_stats();
160 } elseif (isset($_GET['stt2_help'])){
161 pk_stt2_admin_help();
162 } elseif (isset($_GET['donate'])){
163 pk_stt2_admin_donate();
164 } elseif (isset($_GET['stt2_no_traffics'])){
165 pk_stt2_admin_print_no_traffic($_GET['stt2_no_traffics']);
166 } else {
167 pk_stt2_admin_print_admin_page();
168 }
169
170 }
171
172 pk_stt2_admin_print_footer();
173 }
174}
175/**
176* print admin page title
177**/
178function pk_stt2_admin_print_title(){
179 ?>
180 <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; }
181 </style>
182 <div class = "wrap">
183 <div class="icon32" id="icon-options-general"><br></div>
184 <h2>SEO SearchTerms Tagging 2</h2>
185 <?php
186}
187/**
188* print admin page header
189**/
190function pk_stt2_admin_print_header(){
191 $baseurl = "options-general.php?page=".basename(plugin_basename(__FILE__));
192 ?>
193 <div><p> If you think this plugin useful, please consider making a
194 <a href="<?php echo $baseurl.'&donate'; ?>">donation</a> or write a review about it or at least give it a good rating on
195 <a href="http://wordpress.org/extend/plugins/searchterms-tagging-2/" target="_blank">WordPress.org</a>.</p></div>
196 <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>,
197 <a href="<?php echo $baseurl.'&stats=1&count=100'; ?>">100</a>,
198 <a href="<?php echo $baseurl.'&stats=1&count=200'; ?>">200</a>,
199 <a href="<?php echo $baseurl.'&stats=1&count=300'; ?>">300</a>,
200 <a href="<?php echo $baseurl.'&stats=1&count=400'; ?>">400</a>,
201 <a href="<?php echo $baseurl.'&stats=1&count=500'; ?>">500</a> | <strong>
202 <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>
203 <?php
204}
205/*
206* delete unwanted searchterms from admin menu
207*/
208function pk_stt2_admin_delete_searchterms(){
209 global $wpdb;
210 if ( isset($_POST['delete_terms']) && !empty($_POST['delete_terms']) ){
211 $msg = 'Search terms that contain "'.$_POST['delete_terms'].'"';
212 $success = pk_stt2_db_delete_searchterms( $_POST['delete_terms'] );
213 } elseif ( isset($_POST['delete_all']) ){
214 $msg = 'All search terms';
215 $success = pk_stt2_db_delete_searchterms( 'delete_all_terms' );
216 } else {
217 ?>
218 <div id="message" class="updated fade">
219 <p> Please enter the search terms you want to delete, separate them with a comma.
220 </p>
221 </div>
222 <?php
223 }
224 $msg .= ( $success ) ? ' has been removed from the database.' : ' cannot be removed from the database or cannot be found in the database.';
225 ?>
226 <div id="message" class="updated fade">
227 <p><?php echo $msg; ?></p>
228 </div>
229 <?php
230}
231
232/**
233 * display list of search terms, used only in the admin page
234 * */
235function pk_stt2_admin_print_searchterms( $type = 'popular' ){
236 $count = ( isset($_GET['stats']) ) ? $_GET['count'] : 15;
237 switch ( $type ) {
238 case 'popular':
239 $searchterms = pk_stt2_db_get_popular_terms( $count );
240 break;
241 case 'recent':
242 $searchterms = pk_stt2_db_get_recent_terms( $count );
243 break;
244 case 'home':
245 $searchterms = pk_stt2_db_get_home_keywords( $count );
246 break;
247 }
248 if(!empty($searchterms)) {
249 $toReturn .= "<ol>";
250 foreach($searchterms as $term){
251 $permalink = ( 0 == $term->post_id ) ? 'http://www.google.com/search?q='.str_replace(' ','+',$term->meta_value) : get_permalink($term->post_id);
252 $toReturn .= "<li><a href=\"$permalink\" target=\"_blank\">$term->meta_value</a>";
253 $toReturn .= " ($term->meta_count)</li>";
254 }
255 $toReturn = trim($toReturn,', ');
256 $toReturn .= "</ol>";
257 //$toReturn = htmlspecialchars_decode($toReturn);
258 return $toReturn;
259 } else {
260 return false;
261 }
262}
263/**
264 * print the admin form
265 * */
266function pk_stt2_admin_print_admin_page(){
267 $options = get_option('pk_stt2_settings');
268 $auto_cleanup = intval( get_option ( 'pk_stt2_auto_cleanup' ) );
269 $auto_cleanup = ( 0 == $auto_cleanup ) ? PK_AUTO_CLEANUP : $auto_cleanup;
270 $promote_old_post = get_option('pk_stt2_promote_old_post');
271 $badwords = get_option('pk_stt2_badwords');
272 if ( empty($badwords) ){
273 $badwords = PK_BADWORDS;
274 update_option ( 'pk_stt2_badwords', trim( $badwords,' ,.' ) );
275 };
276 ?>
277 <div class="postbox-container" style="width: 74%;">
278 <div class="metabox-holder">
279 <div class="meta-box-sortables ui-sortable">
280 <div id="stt2settings" class="postbox">
281 <div class="handlediv" title="Click to toggle">
282 <br/>
283 </div>
284 <h3 class="hndle">
285 <span>General Settings ( Widgets has it own settings )</span></h3>
286 <div class="inside">
287 <form id="stt2-options" method="post" action="">
288 <table class="stt2-table">
289 <tr>
290 <td width="350px">
291 <label> Enabled:
292 </label>
293 </td>
294 <td>
295 <input type="checkbox" <?php if( 1 == get_option('pk_stt2_enabled') ){ echo 'checked'; }; ?> value="1" name="enabled"/>
296 </td>
297 </tr>
298 <tr>
299 <td width="350px">
300 <label> Max number of search terms:
301 </label>
302 </td>
303 <td>
304 <input type = "text" name = "max" value = "<?php echo $options['max']; ?>" size="10"/>
305 </td>
306 </tr>
307 <tr>
308 <td>
309 <label> Text and code for list header:
310 </label>
311 </td>
312 <td>
313 <input type = "text" name = "list_header" value = "<?php echo htmlspecialchars(stripslashes($options['list_header'])); ?>" size="50"/>
314 </td>
315 </tr>
316 <tr>
317 <td>
318 <label> Text and code before and after the list:
319 </label>
320 </td>
321 <td> Before:
322 <input type = "text" name = "before_list" value = "<?php echo htmlspecialchars(stripslashes($options['before_list'])); ?>" size="10"/> After:
323 <input type = "text" name = "after_list" value = "<?php echo htmlspecialchars(stripslashes($options['after_list'])); ?>" size="10"/>
324 </td>
325 </tr>
326 <tr>
327 <td>
328 <label> Text and code before and after each keyword:
329 </label>
330 </td>
331 <td> Before:
332 <input type = "text" name = "before_keyword" value = "<?php echo htmlspecialchars(stripslashes($options['before_keyword'])); ?>" size="10"/> After:
333 <input type = "text" name = "after_keyword" value = "<?php echo htmlspecialchars(stripslashes($options['after_keyword'])); ?>" size="10"/>
334 </td>
335 </tr>
336 <tr>
337 <td style="vertical-align: top;">
338 <label> Convert search terms into links:
339 </label>
340 </td>
341 <td>
342 <Input type="radio" name="auto_link" value="1" <?php if ( 1 == $options['auto_link'] ){ echo 'checked'; } ;?> /> Yes, link to post content<br />
343 <Input type="radio" name="auto_link" value="2" <?php if ( 2 == $options['auto_link'] ){ echo 'checked'; } ;?> /> Yes, link to search page ( Not Recommended )<br />
344 <Input type="radio" name="auto_link" value="0" <?php if ( 0 == $options['auto_link'] ){ echo 'checked'; } ;?> /> No
345 </td>
346 </tr>
347 <tr>
348 <td>
349 <label> Display search counts for each search term:
350 </label>
351 </td>
352 <td>
353 <Input type="radio" name="show_count" value="1" <?php if ( 1 == $options['show_count'] ){ echo 'checked'; } ;?> /> Yes
354 <Input type="radio" name="show_count" value="0" <?php if ( 0 == $options['show_count'] ){ echo 'checked'; } ;?> /> No
355 </td>
356 </tr>
357 <tr>
358 <td>
359 <label> Add list automatically right after post content:
360 </label>
361 </td>
362 <td>
363 <Input type="radio" name="auto_add" value="1" <?php if ( 1 == $options['auto_add'] ){ echo 'checked'; } ;?> /> Yes
364 <Input type="radio" name="auto_add" value="0" <?php if ( 0 == $options['auto_add'] ){ echo 'checked'; } ;?> /> No
365 </td>
366 </tr>
367 <tr>
368 <td>
369 <label> Save popular search terms as post tags:
370 </label>
371 </td>
372 <td>
373 <Input type="radio" name="auto_tag" value="1" <?php if ( 1 == get_option('pk_stt2_auto_tag') ){ echo 'checked'; } ;?> /> Yes
374 <Input type="radio" name="auto_tag" value="0" <?php if ( 0 == get_option('pk_stt2_auto_tag') ){ echo 'checked'; } ;?> /> No
375 </td>
376 </tr>
377 <tr>
378 <td>
379 <label> Auto clean up unused search terms after:
380 </label>
381 </td>
382 <td>
383 <input type = "text" name = "auto_cleanup" value = "<?php echo $auto_cleanup; ?>" size="10"/> days ( default is 90 days )
384 </td>
385 </tr>
386 <tr>
387 <td valign="top" style="padding-top:10px;">
388 <label>Block the following bad words:</label>
389 </td>
390 <td>
391 <textarea name = "badwords" cols="55" rows="3" ><?php echo $badwords; ?></textarea>
392 </td>
393 </tr>
394 <tr>
395 <td>
396 <label> Promote old post with no search engine traffic:
397 </label>
398 </td>
399 <td>
400 <Input type="radio" name="promote_old_post" value="1" <?php if ( 1 == $promote_old_post ){ echo 'checked'; } ;?> /> Yes
401 <Input type="radio" name="promote_old_post" value="0" <?php if ( 0 == $promote_old_post ){ echo 'checked'; } ;?> /> No
402 </td>
403 </tr>
404 <tr>
405 <td colspan=2>
406 <br />
407 <span class="submit" style="margin-top:14px;">
408 <input class="button-primary" type = "submit" name="submit" value="Save Changes" />
409 </span>
410 </td>
411 </tr>
412 </table>
413 </form>
414 </div>
415 </div>
416 <div id="stt2-cleanup" class="postbox">
417 <div class="handlediv" title="Click to toggle">
418 </div>
419 <h3 class="hndle">
420 <span>Auto Clean Up Unused Search Terms:</span></h3>
421 <div class="inside">
422 <p><ul style="margin: 0pt 0pt 14px 30px; list-style-type: circle;">
423 <?php $last_cleanup = get_option('pk_stt2_last_clean_up'); ?>
424 <li>Last database cleaned up: <?php if (!empty($last_cleanup)) { echo $last_cleanup; } else { echo 'Never'; } ?></li>
425 <li>Next scheduled database clean up on
426 <?php echo date('F j, Y, g:i A',wp_next_scheduled('pk_stt2_admin_event_hook')); ?>.</li>
427 </ul></p>
428 <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>
429 <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>
430 </div>
431 </div>
432 <div id="stt2-promote-post" class="postbox">
433 <div class="handlediv" title="Click to toggle">
434 </div>
435 <h3 class="hndle">
436 <span>Promote Post With No Search Engine Traffic</span></h3>
437 <div class="inside">
438 <p><ul style="margin: 0pt 0pt 14px 30px; list-style-type: circle;">
439 <?php
440 $wo_traffic = pk_stt2_db_get_number_of_posts_wo_traffic();
441 $total = pk_stt2_db_get_number_of_posts();
442 $percentage = intval(( $wo_traffic / $total ) * 100); ?>
443 <li>Number of posts with no search engine traffic: <?php echo "$wo_traffic of $total ( $percentage% )"; ?></li>
444 <li>Last promoted blog post: <?php pk_stt2_the_last_promoted_post(); ?></li>
445 <li>Next scheduled promotion on <?php echo date('F j, Y, g:i A',wp_next_scheduled('pk_stt2_promote_old_post_event_hook')); ?>.</li>
446 </ul></p>
447 <p>To ensure that the promoted post get indexed, use free services such as <a href="http://twitterfeed.com/" target="_blank">TwitterFeed</a> to publish your blog feed into social network sites such as Twitter, Facebook, Ping.fm etc.</p>
448 <p>As an alternative, you can also use a premium plugin like <a href="http://9cab9omg4qgd8r98wbjl1jucfe.hop.clickbank.net/?tid=stt2" target="_blank">Indexing Tool</a> to make sure that your articles are indexed.</p>
449 </div>
450 </div>
451 <div id="stt2-del" class="postbox">
452 <div class="handlediv" title="Click to toggle">
453 </div>
454 <h3 class="hndle">
455 <span>Delete Search Terms:</span></h3>
456 <div class="inside">
457 <form id="stt2-delete" method="post" action="">
458 <p> Enter the search terms you want to delete, separate them with a comma ( e.g.: keyword1,keyword2 ): </p>
459 <p><textarea name = "delete_terms" cols="75" rows="2" ></textarea></p>
460 <p>
461 <span class="submit">
462 <input type = "submit" name="delete" value="Delete" /><br /><br /> OR <br /><br />
463 <input type = "submit" name="delete_all" value="Delete All ( Reset )" />
464 </span>
465 </p>
466 </form>
467 </div>
468 </div>
469 </div>
470 </div>
471 </div>
472 <div class="postbox-container" style="width: 25%;">
473 <div class="metabox-holder">
474 <div class="meta-box-sortables ui-sortable">
475 <div id="stt2-donate" class="postbox">
476 <div class="handlediv" title="Click to toggle">
477 </div>
478 <h3 class="hndle">
479 <span>Please Donate to Support Us:
480 </span></h3>
481 <div class="inside">
482 <div class="frame list">
483 <?php echo pk_stt2_admin_donate(); ?>
484 </div>
485 </div>
486 </div>
487 <div id="stt2-top" class="postbox">
488 <div class="handlediv" title="Click to toggle">
489 </div>
490 <h3 class="hndle">
491 <span>Popular Search Terms:
492 </span></h3>
493 <div class="inside">
494 <div class="frame list">
495 <?php echo pk_stt2_admin_print_searchterms( 'popular' ); ?>
496 </div>
497 </div>
498 </div>
499 <div id="stt2-recent" class="postbox">
500 <div class="handlediv" title="Click to toggle">
501 </div>
502 <h3 class="hndle">
503 <span>Recent Search Terms:
504 </span></h3>
505 <div class="inside">
506 <div class="frame list">
507 <?php echo pk_stt2_admin_print_searchterms( 'recent' ); ?>
508 </div>
509 </div>
510 </div>
511 <div id="stt2-mainkeywords" class="postbox">
512 <div class="handlediv" title="Click to toggle">
513 </div>
514 <h3 class="hndle">
515 <span>Popular Home Keywords:
516 </span></h3>
517 <div class="inside">
518 <div class="frame list">
519 <?php echo pk_stt2_admin_print_searchterms( 'home' ); ?>
520 </div>
521 </div>
522 </div>
523 </div>
524 </div>
525 </div>
526 <?php
527}
528/**
529 * filter the content and add the search terms right after the post content ( on single and page only )
530 * */
531function pk_stt2_admin_content_filter($content){
532 if ( !is_home() ){
533 $options = get_option('pk_stt2_settings');
534 if($options['auto_add'])
535 $content .= stt_terms_list();
536 }
537 return $content;
538}
539/**
540 * update options and save it to db
541 * */
542function pk_stt2_admin_update_options(){
543 if ( isset($_POST['max']) && isset($_POST['before_keyword']) && isset($_POST['after_keyword']) &&
544 isset($_POST['auto_add']) && isset($_POST['auto_link']) && isset($_POST['show_count']) && isset($_POST['before_list'])
545 && isset($_POST['after_list']) && isset($_POST['list_header']) ){
546
547 $options['max'] = ( intval($_POST['max']) < 2 ) ? 2 : intval($_POST['max']);
548 $options['before_keyword'] = $_POST['before_keyword'];
549 $options['after_keyword'] = $_POST['after_keyword'];
550 $options['before_list'] = $_POST['before_list'];
551 $options['after_list'] = $_POST['after_list'];
552 $options['list_header'] = $_POST['list_header'];
553 $options['auto_add'] = intval($_POST['auto_add']);
554 $options['auto_link'] = intval($_POST['auto_link']);
555 $options['show_count'] = intval($_POST['show_count']);
556 update_option( 'pk_stt2_settings', $options );
557 update_option( 'pk_stt2_enabled', $_POST['enabled'] );
558 update_option( 'pk_stt2_auto_tag', intval($_POST['auto_tag']) );
559 update_option( 'pk_stt2_promote_old_post', intval($_POST['promote_old_post']) );
560 update_option ( 'pk_stt2_badwords', trim( $_POST['badwords'], ' ,.' ) );
561 pk_stt2_db_delete_searchterms ( $_POST['badwords'] );
562 $autocleanup = ( 0 == intval($_POST['auto_cleanup']) ) ? 90 : intval($_POST['auto_cleanup']);
563 update_option( 'pk_stt2_auto_cleanup', $autocleanup );
564 ?>
565 <div id="message" class="updated fade">
566 <p> Options saved.
567 </p>
568 </div>
569 <?php
570 } else {
571 ?>
572 <div id="message" class="updated fade">
573 <p> Failed to save options.
574 </p>
575 </div>
576 <?php
577 pk_stt2_flush_rewrite_rules();
578 }
579}
580/**
581 * add the menu into WordPress admin menu
582 * */
583function pk_stt2_admin_menu_hook(){
584 if (function_exists('add_options_page')) {
585 add_options_page(
586 'SEO SearchTerms 2',
587 'SEO SearchTerms 2',
588 'manage_options',
589 'searchterms-tagging2.php',
590 'pk_stt2_create_admin_menu'
591 );
592 }
593}
594/**
595* print footer
596**/
597function pk_stt2_admin_print_footer(){
598?>
599<div class="postbox-container" style="width: 98%;">
600 <div class="metabox-holder">
601 <div id="stt2-copyright" class="postbox">
602 <div class="inside">
603 <div class="frame" style="text-align:center">
604 <p><strong>Recommended SEO Resources: <a href="http://www.warriorplus.com/linkwso/6pwllx/11123" target="_blank">Easy WP SEO Plugin</a> | <a href="http://www.warriorplus.com/linkwso/j2jz3p/11123" target="_blank">Stealth Keyword Competition Analyzer</a> | <a href="http://www.warriorplus.com/linkwso/2ns6d1/11123" target="_blank">FastAttacks SEO eBook</a></strong></p>
605 <p>Copyright © 2010-2011 by Purwedi Kurniawan. Feel free to <a href="http://exclusivewordpress.com/contact/" target="_blank">contact me</a> if you need help with the plugin.</p> </div>
606 </div>
607 </div>
608 </div>
609</div>
610</div>
611<?php }
612/**
613* == DATABASE SECTION ==
614* all main database related functions of searchterms tagging 2 plugin
615**/
616/**
617* list blog posts with no search engine traffic
618* @return: OBJECT results with ID, post_title properties
619**/
620function pk_stt2_db_get_posts_wo_traffic( $count=100 ){
621 $results = wp_cache_get( 'stt2_posts_wo_traffic_'.$count );
622 if ( false == $results ) {
623 global $wpdb;
624 $sql = "SELECT ID, post_title FROM $wpdb->posts
625 WHERE post_type = 'post'
626 AND post_status = 'publish'
627 AND ID NOT IN ( SELECT post_id FROM ".$wpdb->prefix."stt2_meta )
628 ORDER BY post_date DESC LIMIT ".$count.";";
629 $results = $wpdb->get_results( $sql );
630 wp_cache_set( 'stt2_posts_wo_traffic_'.$count, $results, 3600 );
631 }
632 if ( $results ) return $results;
633}
634/**
635* promote single blog post with no search engine traffic
636* @params: $old_post_ID, $new_time, $gmt_time
637* @return: 1 if succes
638**/
639function pk_stt2_db_promote_single_post_wo_traffic( $old_post_ID ){
640 global $wpdb;
641 $new_time = date('Y-m-d H:i:s');
642 $gmt_time = get_gmt_from_date($new_time);
643 $sql = "UPDATE $wpdb->posts
644 SET post_date = '$new_time', post_date_gmt = '$gmt_time', post_modified = '$new_time', post_modified_gmt = '$gmt_time'
645 WHERE ID = '$old_post_ID';";
646 $return = $wpdb->query($sql);
647 return $return;
648}
649/**
650 * get the number of posts with no traffic yet
651 **/
652function pk_stt2_db_get_number_of_posts_wo_traffic(){
653 $post_count = wp_cache_get( 'stt2_number_of_posts_wo_traffic' );
654 if ( false == $post_count ) {
655 global $wpdb;
656 $sql = "SELECT count(`ID`) FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post' AND ID NOT IN (
657 SELECT post_id FROM ".$wpdb->prefix."stt2_meta );";
658 $post_count = $wpdb->get_var($wpdb->prepare( $sql, "" ));
659 wp_cache_set( 'stt2_number_of_posts_wo_traffic', $post_count, 86400 );
660 }
661 return $post_count;
662}
663/**
664 * get single post ID of posts with no traffic yet
665 * doesn't caching, only run twice a day, and need the most current status
666 **/
667function pk_stt2_db_get_id_post_wo_traffic(){
668 global $wpdb;
669 $sql = "SELECT `ID` FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post' AND ID NOT IN (
670 SELECT post_id FROM ".$wpdb->prefix."stt2_meta WHERE `post_id` != 0 ) LIMIT 1;";
671 $post_ID = $wpdb->get_var($wpdb->prepare( $sql ));
672 return $post_ID;
673}
674/**
675* get last promoted post title
676**/
677function pk_stt2_db_get_last_promoted_post_title( $id ){
678 $post_title = wp_cache_get( 'stt2_last_promoted_post_title_'.$id );
679 if ( false == $post_title ) {
680 global $wpdb;
681 $sql = "SELECT `post_title` FROM $wpdb->posts WHERE `ID` = $id;";
682 $post_title = $wpdb->get_var($wpdb->prepare( $sql ));
683 wp_cache_set( 'stt2_last_promoted_post_title_'.$id, $post_title );
684 }
685 return $post_title;
686}
687/**
688 * get the number of posts
689 **/
690function pk_stt2_db_get_number_of_posts(){
691 $post_count = wp_cache_get( 'stt2_get_number_of_posts' );
692 if ( false == $post_count ) {
693 global $wpdb;
694 $sql = "SELECT count(`ID`) FROM $wpdb->posts WHERE `post_status` = 'publish' AND `post_type` = 'post';";
695 $post_count = $wpdb->get_var($wpdb->prepare( $sql, "" ));
696 wp_cache_set( 'stt2_get_number_of_posts'.$id, $post_count, 3600 );
697 }
698 return $post_count;
699}
700/**
701 * get 10 popular search terms for the posts, will be used as tags
702 * @param $id: post id
703 * @return: comma separated text of popular search terms
704 **/
705function pk_stt2_db_get_popular_tags( $id ){
706 $a_results = wp_cache_get( 'stt2_get_popular_tags_'.$id );
707 if ( false == $a_results ) {
708 global $wpdb;
709 $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;";
710 $a_results = $wpdb->get_results( $sql );
711 wp_cache_set( 'stt2_get_popular_tags_'.$id, $a_results, 86400 );
712 }
713 if ( $a_results ){
714 foreach ( $a_results as $value ){
715 $result .= $value->meta_value.',';
716 }
717 $result = trim($result,',');
718 }
719 return $result;
720}
721/**
722 * get popular search terms
723 * @param $count: max number of search terms
724 **/
725function pk_stt2_db_get_popular_terms( $count ){
726 $result = wp_cache_get( 'stt2_popular_terms' );
727 if ( false == $result ) {
728 global $wpdb;
729 $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.";" );
730 wp_cache_set( 'stt2_popular_terms', $result, 86400 );
731 }
732 return $result;
733}
734/**
735 * get home keywords
736 * @param $count: max number of search terms
737 **/
738function pk_stt2_db_get_home_keywords( $count ){
739 $result = wp_cache_get( 'stt2_home_keywords' );
740 if ( false == $result ) {
741 global $wpdb;
742 $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.";" );
743 wp_cache_set( 'stt2_home_keywords', $result, 86400 );
744 }
745 return $result;
746}
747/**
748 * get list of search terms
749 * @param $max: max number of search terms
750 **/
751function pk_stt2_db_get_search_terms( $max ){
752 $result = wp_cache_get( 'stt2_search_terms_'.$max );
753 if ( false == $result ) {
754 global $wpdb, $post;
755 $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.";" );
756 wp_cache_set( 'stt2_search_terms_'.$max, $result, 900 );
757 }
758 return $result;
759}
760/**
761 * get recent search terms
762 * @param $count: max number of search terms
763 **/
764function pk_stt2_db_get_recent_terms( $count ){
765 $result = wp_cache_get( 'stt2_recent_terms' );
766 if ( false == $result ) {
767 global $wpdb;
768 $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.";" );
769 wp_cache_set( 'stt2_recent_terms', $result, 900 );
770 }
771 return $result;
772}
773/**
774 * get random search terms
775 * @param $count: max number of search terms
776 **/
777function pk_stt2_db_get_random_terms( $count ){
778 $result = wp_cache_get( 'stt2_random_terms' );
779 if ( false == $result ) {
780 global $wpdb;
781 $result = $wpdb->get_results( "SELECT `meta_value`,`post_id` FROM `".$wpdb->prefix."stt2_meta` WHERE `post_id` != 0 ORDER BY RAND() LIMIT ".$count.";" );
782 wp_cache_set( 'stt2_random_terms', $result, 3600 );
783 }
784 return $result;
785}
786/**
787 * get the number of searchterms
788 **/
789function pk_stt2_db_get_number_of_searchterms(){
790 $result = wp_cache_get( 'stt2_number_of_searchterms' );
791 if ( false == $result ) {
792 global $wpdb;
793 $result = $wpdb->get_var("SELECT COUNT(`meta_value`) FROM ".$wpdb->prefix."stt2_meta;");
794 wp_cache_set( 'stt2_number_of_searchterms', $result, 3600 );
795 }
796 return $result;
797}
798/**
799 * delete searchterms from the database
800 * @param $searchterms: comma separated search terms or 'delete_all_terms'
801 * @output: number of rows effected by the delete query
802 **/
803function pk_stt2_db_delete_searchterms( $searchterms ){
804 global $wpdb;
805 if ( 'delete_all_terms' !== $searchterms ) {
806 $arr_searchterms = explode(',',$searchterms);
807 foreach ($arr_searchterms as $value) {
808 if (!empty($value)) {
809 $success += $wpdb->query( "DELETE FROM ".$wpdb->prefix."stt2_meta WHERE LOWER(meta_value) LIKE '%". strtolower(trim($value)) ."%'; ");
810 }
811 }
812 } else {
813 $success = $wpdb->query( "DELETE FROM ".$wpdb->prefix."stt2_meta" );
814 }
815 $opt = $wpdb->query('OPTIMIZE TABLE '.$wpdb->prefix.'stt2_meta;');
816 return $success;
817}
818/**
819* clean the database in daily basis
820**/
821function pk_stt2_db_cleanup( $days ){
822 global $wpdb;
823 $result = $wpdb->query('DELETE FROM '.$wpdb->prefix.'stt2_meta WHERE (meta_count < 10) AND (date(last_modified) < date(now()-interval '.$days.' day));');
824 $opt = $wpdb->query('OPTIMIZE TABLE '.$wpdb->prefix.'stt2_meta;');
825 return $result;
826}
827/**
828 * create stt2 database; UTF-8 version
829 * */
830function pk_stt2_db_create_table() {
831 global $wpdb;
832 $table_name = $wpdb->prefix.'stt2_meta';
833 if($wpdb->get_var("SHOW TABLES LIKE '$table_name';") != $table_name) {
834 $sql = "CREATE TABLE `".$wpdb->prefix."stt2_meta` (
835 `post_id` INT( 20 ) NOT NULL ,
836 `meta_value` VARCHAR ( 255 ) NOT NULL,
837 `meta_count` INT( 20 ) NOT NULL DEFAULT '1',
838 `last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
839 UNIQUE (`meta_value`),
840 PRIMARY KEY ( `post_id` , `meta_value` )
841 ) CHARACTER SET utf8
842 DEFAULT CHARACTER SET utf8
843 COLLATE utf8_general_ci
844 DEFAULT COLLATE utf8_general_ci;";
845 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
846 dbDelta($sql);
847 update_option('pk_stt2_db_version','2');
848 }
849}
850/**
851* update stt2 database into version 2; UTF-8 with additional UNIQUE column
852**/
853function pk_stt2_db_upgrade_db_structure(){
854 global $wpdb;
855 $sql = "CREATE TABLE `".$wpdb->prefix."stt2_meta_tmp` (
856 `post_id` INT( 20 ) NOT NULL ,
857 `meta_value` VARCHAR ( 255 ) NOT NULL,
858 `meta_count` INT( 20 ) NOT NULL DEFAULT '1',
859 `last_modified` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
860 UNIQUE (`meta_value`),
861 PRIMARY KEY ( `post_id` , `meta_value` )
862 ) CHARACTER SET utf8
863 DEFAULT CHARACTER SET utf8
864 COLLATE utf8_general_ci
865 DEFAULT COLLATE utf8_general_ci;";
866 require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
867 dbDelta($sql);
868
869 $sql = "INSERT IGNORE INTO `".$wpdb->prefix."stt2_meta_tmp` ( `post_id`,`meta_value`,`meta_count`,`last_modified` )
870 SELECT `post_id`,`meta_value`,`meta_count`,`last_modified` FROM `".$wpdb->prefix."stt2_meta`;";
871 $return = $wpdb->query( $sql );
872
873 $sql = "RENAME TABLE `".$wpdb->prefix."stt2_meta` TO `".$wpdb->prefix."stt2_meta_backup`,
874 `".$wpdb->prefix."stt2_meta_tmp` TO `".$wpdb->prefix."stt2_meta`;";
875 $return = $wpdb->query( $sql );
876
877 update_option('pk_stt2_db_version','2');
878}
879/**
880 * save search terms into database
881 **/
882function pk_stt2_db_save_searchterms( $meta_value) {
883 if ( strlen($meta_value) > 3 ){
884 if ( is_home() ){
885 $ID = 0;
886 } else {
887 global $post;
888 $ID = $post->ID;
889 }
890 global $wpdb;
891 $success = $wpdb->query( $wpdb->prepare( "INSERT INTO ".$wpdb->prefix."stt2_meta ( `post_id`,`meta_value`,`meta_count` ) VALUES ( %s, %s, 1 )
892 ON DUPLICATE KEY UPDATE `meta_count` = `meta_count` + 1", $ID, $meta_value ) );
893 }
894 return $success;
895}
896/**
897* get popular search terms in category
898* @author: pile ( http://pile.web.id )
899**/
900function pk_stt2_db_get_popular_searchterms_in_category( $count ){
901 $results = wp_cache_get( 'stt2_popular_searchterms_in_category_'.$count );
902 if ( false == $results ) {
903 global $wpdb;
904 $cat_ID = get_query_var('cat');
905 $sql = " SELECT wps.post_id, wps.meta_value, wps.meta_count
906 FROM ".$wpdb->prefix."terms
907 INNER JOIN ".$wpdb->prefix."term_taxonomy ON ".$wpdb->prefix."terms.term_id = ".$wpdb->prefix."term_taxonomy.term_id
908 INNER JOIN ".$wpdb->prefix."term_relationships wpr ON wpr.term_taxonomy_id = ".$wpdb->prefix."term_taxonomy.term_taxonomy_id
909 INNER JOIN ".$wpdb->prefix."posts p ON p.ID = wpr.object_id
910 INNER JOIN ".$wpdb->prefix."stt2_meta wps ON wps.post_id = p.ID
911 WHERE taxonomy = 'category'
912 AND p.post_type = 'post'
913 AND p.post_status = 'publish'
914 AND ( ".$wpdb->prefix."terms.term_id = '".$cat_ID."' )
915 ORDER BY `wps`.`meta_count` DESC
916 LIMIT ".$count.";";
917 $results = $wpdb->get_results($sql);
918 wp_cache_set( 'stt2_popular_searchterms_in_category_'.$count, $results, 86400 );
919 }
920
921 return $results;
922}
923/**
924* == MAIN SECTION ==
925* all main functions of searchterms tagging 2 plugin
926**/
927/**
928* get popular search terms on coresponding category
929* @param $count: number of search terms to be displayed
930**/
931function stt_popular_terms_in_category( $count=10 ){
932 if (is_category()) {
933 $options = get_option('pk_stt2_settings');
934 $searchterms = pk_stt2_db_get_popular_searchterms_in_category( $count );
935 if(!empty($searchterms)) {
936 $result = pk_stt2_function_prepare_searchterms($searchterms,$options,true);
937 return $result;
938 } else {
939 return false;
940 }
941 }
942}
943/**
944 * save popular search terms as tags in monthly basis
945 **/
946function pk_stt2_function_save_tags(){
947 if ( '1' == get_option('pk_stt2_auto_tag') && !empty($post->ID) ) {
948 $day_diff = ceil(( strtotime(date('F j, Y')) - strtotime( get_post_meta( $id,'stt2_update_tags',true ) ) + 1) / (60*60*24) ) ;
949 if ( 30 < $day_diff ){
950 global $post;
951 $tags = pk_stt2_db_get_popular_tags( $post->ID );
952 wp_set_post_tags( $post->ID, $tags, true );
953 update_post_meta( $post->ID,'stt2_update_tags', date('F j, Y') );
954 }
955 }
956}
957/**
958 * hooked to wp-head()
959 * */
960function pk_stt2_function_wp_head_hook() {
961
962 if( 1 == intval(get_option('pk_stt2_enabled')) ){
963 $referer = pk_stt2_function_get_referer();
964 if (!$referer) return false;
965 $delimiter = pk_stt2_function_get_delimiter($referer);
966 if( $delimiter ){
967 $term = pk_stt2_function_get_terms($delimiter);
968 if (!pk_stt2_is_contain_bad_words( $term )) {
969 pk_stt2_db_save_searchterms( $term );
970 }
971 }
972 pk_stt2_function_save_tags();
973 }
974}
975/**
976 * display the search terms below post content
977 * */
978function stt_terms_list(){
979 $options = get_option('pk_stt2_settings');
980 $searchterms = pk_stt2_db_get_search_terms( $options['max'] );
981 if(!empty( $searchterms )) {
982 $result = pk_stt2_function_prepare_searchterms( $searchterms, $options );
983 return $result;
984 } else {
985 return false;
986 }
987}
988/**
989 * strip the slash from each search terms
990 * */
991function pk_stt2_function_stripslashes_options($options){
992 foreach($options as $i=>$row){
993 $row = stripslashes($row);
994 $options[$i]=$row;
995 }
996 return $options;
997}
998/**
999 * common function to print the search terms
1000 * */
1001function pk_stt2_function_prepare_searchterms( $searchterms, $options, $popular=false ){
1002 global $post;
1003 $options = pk_stt2_function_stripslashes_options($options);
1004 $toReturn .= ( $popular == false ) ? $options['list_header'].$options['before_list'] : $options['before_list'];
1005 foreach($searchterms as $term){
1006 if ( 0 == $options['auto_link'] ){
1007 $toReturn .= $options['before_keyword'].$term->meta_value;
1008 } else {
1009 if( !$popular ){
1010 if ( 1 == $options['auto_link'] ){
1011 $permalink = get_permalink( $post->ID );
1012 } elseif ( 2 == $options['auto_link']){
1013 $permalink = get_bloginfo( 'url' ).'/search/'.user_trailingslashit(pk_stt2_function_sanitize_search_link($term->meta_value));
1014 }
1015 } else {
1016 $permalink = ( 0 == $term->post_id ) ? get_bloginfo('url') : get_permalink($term->post_id);
1017 }
1018 $toReturn .= $options['before_keyword']."<a href=\"$permalink\" title=\"$term->meta_value\">$term->meta_value</a>";
1019 }
1020 $toReturn .= ( $options['show_count'] == true ) ? " ($term->meta_count)".$options['after_keyword'] : $options['after_keyword'];
1021 }
1022 $toReturn = trim($toReturn, ', ');
1023 $toReturn .= $options['after_list'];
1024 //$toReturn = htmlspecialchars_decode($toReturn);
1025 return $toReturn;
1026}
1027/**
1028 * display popular search terms manually
1029 * @param $count: number of search terms to be displayed
1030 * */
1031function stt_popular_terms( $count=10 ){
1032 $options = get_option('pk_stt2_settings');
1033 $searchterms = pk_stt2_db_get_popular_terms($count);
1034 if(!empty($searchterms)) {
1035 $result = pk_stt2_function_prepare_searchterms($searchterms,$options,true);
1036 return $result;
1037 } else {
1038 return false;
1039 }
1040}
1041/**
1042 * display recent search terms manually
1043 * @param $count: number of search terms to be displayed
1044 * */
1045function stt_recent_terms( $count=10 ){
1046 $options = get_option('pk_stt2_settings');
1047 $searchterms = pk_stt2_db_get_recent_terms( $count );
1048 if(!empty($searchterms)) {
1049 $result = pk_stt2_function_prepare_searchterms($searchterms,$options,true);
1050 return $result;
1051 } else {
1052 return false;
1053 }
1054}
1055/**
1056 * display random search terms manually
1057 * @param $count: number of search terms to be displayed
1058 * */
1059function stt_random_terms( $count=10 ){
1060 $options = get_option('pk_stt2_settings');
1061 $searchterms = pk_stt2_db_get_random_terms( $count );
1062 if(!empty($searchterms)) {
1063 $result = pk_stt2_function_prepare_searchterms($searchterms,$options,true);
1064 return $result;
1065 } else {
1066 return false;
1067 }
1068}
1069/**
1070 * get search delimiter for each search engine
1071 * base on the original searchterms tagging plugin
1072 * */
1073function pk_stt2_function_get_delimiter($ref) {
1074 $search_engines = array('google.com' => 'q',
1075 'go.google.com' => 'q',
1076 'images.google.com' => 'q',
1077 'video.google.com' => 'q',
1078 'news.google.com' => 'q',
1079 'blogsearch.google.com' => 'q',
1080 'maps.google.com' => 'q',
1081 'local.google.com' => 'q',
1082 'search.yahoo.com' => 'p',
1083 'search.msn.com' => 'q',
1084 'bing.com' => 'q',
1085 'msxml.excite.com' => 'qkw',
1086 'search.lycos.com' => 'query',
1087 'alltheweb.com' => 'q',
1088 'search.aol.com' => 'query',
1089 'search.iwon.com' => 'searchfor',
1090 'ask.com' => 'q',
1091 'ask.co.uk' => 'ask',
1092 'search.cometsystems.com' => 'qry',
1093 'hotbot.com' => 'query',
1094 'overture.com' => 'Keywords',
1095 'metacrawler.com' => 'qkw',
1096 'search.netscape.com' => 'query',
1097 'looksmart.com' => 'key',
1098 'dpxml.webcrawler.com' => 'qkw',
1099 'search.earthlink.net' => 'q',
1100 'search.viewpoint.com' => 'k',
1101 'yandex.kz' => 'text',
1102 'yandex.ru' => 'text',
1103 'baidu.com' => 'wd',
1104 'mamma.com' => 'query');
1105 $delim = false;
1106 if (isset($search_engines[$ref])) {
1107 $delim = $search_engines[$ref];
1108 } else {
1109 if (strpos('ref:'.$ref,'google'))
1110 $delim = "q";
1111 elseif (strpos('ref:'.$ref,'search.atomz.'))
1112 $delim = "sp-q";
1113 elseif (strpos('ref:'.$ref,'search.msn.'))
1114 $delim = "q";
1115 elseif (strpos('ref:'.$ref,'search.yahoo.'))
1116 $delim = "p";
1117 elseif (strpos('ref:'.$ref,'yandex'))
1118 $delim = "text";
1119 elseif (strpos('ref:'.$ref,'baidu'))
1120 $delim = "wd";
1121 elseif (preg_match('/home\.bellsouth\.net\/s\/s\.dll/i', $ref))
1122 $delim = "bellsouth";
1123 }
1124 return $delim;
1125}
1126/**
1127 * retrieve the search terms from search engine query
1128 * */
1129function pk_stt2_function_get_terms($d) {
1130 $terms = null;
1131 $query_array = array();
1132 $query_terms = null;
1133 $query = explode($d.'=', $_SERVER['HTTP_REFERER']);
1134 $query = explode('&', $query[1]);
1135 $query = urldecode($query[0]);
1136 $query = str_replace("'", '', $query);
1137 $query = str_replace('"', '', $query);
1138 $query_array = preg_split('/[\s,\+\.]+/',$query);
1139 $query_terms = implode(' ', $query_array);
1140 $terms = htmlspecialchars(urldecode(trim($query_terms)));
1141 return $terms;
1142}
1143/**
1144 * get the referer
1145 * */
1146function pk_stt2_function_get_referer() {
1147 if (!isset($_SERVER['HTTP_REFERER']) || ($_SERVER['HTTP_REFERER'] == '')) return false;
1148 $referer_info = parse_url($_SERVER['HTTP_REFERER']);
1149 $referer = $referer_info['host'];
1150 if(substr($referer, 0, 4) == 'www.')
1151 $referer = substr($referer, 4);
1152 return $referer;
1153}
1154/**
1155* sanitize link to search page
1156* @param $title: search engine terms
1157* @output: search terms in form of web safe url slug
1158**/
1159function pk_stt2_function_sanitize_search_link($title) {
1160 $title = strip_tags($title);
1161 // Preserve escaped octets.
1162 $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
1163 // Remove percent signs that are not part of an octet.
1164 $title = str_replace('%', '', $title);
1165 // Restore octets.
1166 $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
1167 $title = remove_accents($title);
1168 if (seems_utf8($title)) {
1169 if (function_exists('mb_strtolower')) {
1170 $title = mb_strtolower($title, 'UTF-8');
1171 }
1172 $title = utf8_uri_encode($title);
1173 }
1174 $title = strtolower($title);
1175 $title = preg_replace('/&.+?;/', '', $title); // kill entities
1176 $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
1177 $title = preg_replace('/\s+/', '-', $title);
1178 $title = preg_replace('|-+|', '-', $title);
1179 $title = trim($title, '-');
1180 return $title;
1181 }
1182/**
1183* check whether the search term contain forbidden word
1184**/
1185function pk_stt2_is_contain_bad_words( $term ){
1186 $option = get_option('pk_stt2_badwords');
1187 $option = ( empty($option) ) ? PK_BADWORDS : $option;
1188 $badwords = explode( ',',$option );
1189 $term = str_ireplace( $badwords,'***',$term );
1190 if( false === strpos( $term, '***' ) )
1191 return false;
1192 else
1193 return true;
1194}
1195/**
1196* == PLUGIN REGISTRATION SECTION ==
1197**/
1198/**
1199 * check onlist status
1200 */
1201function pk_stt2_onlist(){
1202 $form_1 = 'stt2_reg_form_1';
1203 $form_2 = 'stt2_reg_form_2';
1204 $pk_stt2_list = get_option('onlist_status');
1205 if ( trim($_GET['onlist']) == 1 || $_GET['no'] == 1 ) {
1206 $pk_stt2_list = 2; update_option('onlist_status', $pk_stt2_list);
1207 }
1208 if ( ((trim($_GET['activate']) != '' && trim($_GET['from']) != '') || trim($_GET['activate_again']) != '') && $pk_stt2_list != 2 ) {
1209 update_option('pk_stt2_name', $_GET['name']);
1210 update_option('pk_stt2_email', $_GET['from']);
1211 $pk_stt2_list = 1; update_option('onlist_status', $pk_stt2_list);
1212 }
1213 if ( $pk_stt2_list == 0 ) {
1214 pk_stt2_register_1($form_1);
1215 } else if ( $pk_stt2_list == 1 ) {
1216 $name = get_option('pk_stt2_name');
1217 $email = get_option('pk_stt2_email');
1218 pk_stt2_register_2($form_2,$name,$email);
1219 } else if ( $pk_stt2_list == 2 ) {
1220 return true;
1221 }
1222}
1223/**
1224 * Plugin registration form
1225 */
1226function pk_stt2_registration_form($form_name, $submit_btn_txt='Register', $name, $email, $hide=0, $activate_again='') {
1227 $wp_url = get_bloginfo('wpurl');
1228 $wp_url = (strpos($wp_url,'http://') === false) ? get_bloginfo('siteurl') : $wp_url;
1229 $thankyou_url = $wp_url.'/wp-admin/options-general.php?page='.$_GET['page'];
1230 $onlist_url = $wp_url.'/wp-admin/options-general.php?page='.$_GET['page'].'&onlist=1';
1231 $nothankyou_url = $wp_url.'/wp-admin/options-general.php?page='.$_GET['page'].'&no=1';
1232
1233 if ( $hide == 1 ) $align_tbl = 'left';
1234 else $align_tbl = 'center';
1235 ?>
1236
1237 <?php if ( $activate_again != 1 ) { ?>
1238 <script><!--
1239 function trim(str){
1240 var n = str;
1241 while ( n.length>0 && n.charAt(0)==' ' )
1242 n = n.substring(1,n.length);
1243 while( n.length>0 && n.charAt(n.length-1)==' ' )
1244 n = n.substring(0,n.length-1);
1245 return n;
1246 }
1247 function pk_stt2_validate_form_0() {
1248 var name = document.<?php echo $form_name;?>.name;
1249 var email = document.<?php echo $form_name;?>.from;
1250 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
1251 var err = ''
1252 if ( trim(name.value) == '' )
1253 err += '- Name Required\n';
1254 if ( reg.test(email.value) == false )
1255 err += '- Valid Email Required\n';
1256 if ( err != '' ) {
1257 alert(err);
1258 return false;
1259 }
1260 return true;
1261 }
1262 //-->
1263 </script>
1264 <?php } ?>
1265 <table align="<?php echo $align_tbl;?>">
1266 <form name="<?php echo $form_name;?>" method="post" action="http://www.aweber.com/scripts/addlead.pl" <?php if($activate_again!=1){;?>onsubmit="return pk_stt2_validate_form_0()"<?php }?>>
1267 <input type="hidden" name="meta_web_form_id" value="943418582" />
1268 <input type="hidden" name="listname" value="wp-stt2" />
1269 <input type="hidden" name="redirect" value="<?php echo $thankyou_url;?>">
1270 <input type="hidden" name="meta_redirect_onlist" value="<?php echo $onlist_url;?>">
1271 <input type="hidden" name="meta_adtracking" value="stt2_activate" />
1272 <input type="hidden" name="meta_message" value="1">
1273 <input type="hidden" name="meta_required" value="from,name">
1274 <input type="hidden" name="meta_forward_vars" value="1">
1275 <?php if ( $activate_again == 1 ) { ?>
1276 <input type="hidden" name="activate_again" value="1">
1277 <?php } ?>
1278 <?php if ( $hide == 1 ) { ?>
1279 <input type="hidden" name="name" value="<?php echo $name;?>">
1280 <input type="hidden" name="from" value="<?php echo $email;?>">
1281 <?php } else { ?>
1282 <tr><td>Name: </td><td><input type="text" name="name" value="<?php echo $name;?>" size="25" maxlength="150" /></td></tr>
1283 <tr><td>Email: </td><td><input type="text" name="from" value="<?php echo $email;?>" size="25" maxlength="150" /></td></tr>
1284 <?php } ?>
1285 <tr><td span=2> </td></tr>
1286 <tr><td> </td><td><input class="button-primary" type="submit" name="activate" value="<?php echo $submit_btn_txt;?>" /> </td></tr>
1287 </form>
1288 <form name="nothankyou" method="post" action="<?php echo $nothankyou_url;?>">
1289 <tr><td> </td><td><input class="button" type="submit" name="nothankyou" value="No Thank You!" /></td></tr>
1290 </form>
1291 </table>
1292 <?php
1293}
1294/**
1295 * Register Plugin - Step 2
1296 */
1297function pk_stt2_register_2($form_name='frm2',$name,$email) {
1298 $msg = 'You have not clicked on the confirmation link yet. A confirmation email has been sent to you again. Please check your email and click on the confirmation link to register the plugin.';
1299 if ( trim($_GET['activate_again']) != '' && $msg != '' ) {
1300 echo '<div id="message" class="updated fade"><p><strong>'.$msg.'</strong></p></div>';
1301 }
1302 ?>
1303 <center>
1304 <table width="640" cellspacing="1" style="border:1px solid #e9e9e9; padding: 0 14px 14px;">
1305 <tr><td align="center"><h3>Almost Done....</h3></td></tr>
1306 <tr><td><h3>Step 1:</h3></td></tr>
1307 <tr><td>A confirmation email has been sent to your email "<?php echo $email;?>". You must click on the confirmation link inside the email to register the plugin.</td></tr>
1308 <tr><td> </td></tr>
1309 <tr><td>The confirmation email will look like this:<br /><img src="http://exclusivewordpress.com/wp-content/uploads/2010/08/email-confirmation.gif" style="margin-top:10px;border:0;" /></td></tr>
1310 <tr><td> </td></tr>
1311 <tr><td><h3>Step 2:</h3></td></tr>
1312 <tr><td>Click on the button below to Verify and Activate the plugin.</td></tr>
1313 <tr><td> </td></tr>
1314 <tr><td><?php pk_stt2_registration_form($form_name.'_0','Verify and Activate',$name,$email,$hide=1,$activate_again=1);?></td></tr>
1315 <tr><td> </td></tr>
1316 </table>
1317 <p> </p>
1318 <table width="640" cellspacing="1" style="border:1px solid #e9e9e9; padding: 0 14px 14px;">
1319 <tr><td><h3>Troubleshooting</h3></td></tr>
1320 <tr><td>1. I can't found the confirmation email in my inbox.<br/>Please check your spam or bulk folder.</td></tr>
1321 <tr><td> </td></tr>
1322 <tr><td>2. It's not there in the junk folder either.<br/>In a rare case, it took times to arrive. Please wait for 6 hours at most. If after 6 hours and still no confirmation email, please register again using the form below:</td></tr>
1323 <tr><td> </td></tr>
1324 <tr><td><?php pk_stt2_registration_form($form_name,'Register Again',$name,$email,$hide=0,$activate_again=2);?></td></tr>
1325 <tr><td> </td></tr>
1326 <tr><td>3. Still no confirmation email and I have already registered twice.<br/>Using the form above, try to register again using DIFFERENT EMAIL ADDRESS.</td></tr>
1327 <tr><td> </td></tr>
1328 <tr><td>4. I receive an error like this:<br />
1329 <img src="http://exclusivewordpress.com/wp-content/uploads/2010/08/email-confirmation-error.gif" style="margin-top:10px;border:0;" /><br />
1330 <br/>This error means that you have already subscribed but have not yet clicked on the link inside confirmation email. In order to avoid any spam complain we don't send repeated confirmation emails.</td>
1331 </tr>
1332 <tr><td> </td></tr>
1333 <tr><td>5. If you still got problems, please <a href="http://exclusivewordpress.com/contact/" target="_blank">contact us</a></strong> about it and we will get to you ASAP.</td></tr>
1334 </table>
1335 </center>
1336 <p> </p>
1337 </div>
1338 <?php
1339}
1340/**
1341 * Register Plugin - Step 1
1342 */
1343function pk_stt2_register_1($form_name='frm1') {
1344 global $current_user;
1345 get_currentuserinfo();
1346 $name = $current_user->user_firstname;
1347 $email = $current_user->user_email;
1348 ?>
1349 <center>
1350 <table width="620" cellpadding="10" cellspacing="1" bgcolor="#ffffff" style="border:1px solid #e9e9e9; padding: 0 14px 14px;">
1351 <tr><td align="center"><h3>Please register the plugin...</h3></td></tr>
1352 <tr><td align="left">Registration is <strong>Free</strong> and only has to be done once. If you've register before or don't want to register, please click "No Thank You!" button.</td></tr>
1353 <tr><td> </td></tr>
1354 <tr><td align="left">In addition, you'll receive complimentary subscription to our Email Newsletter which will give you many news and tips about SEO. Of course, you can unsubscribe any time you want.</td></tr>
1355 <tr><td> </td></tr>
1356 <tr><td align="center"><?php pk_stt2_registration_form($form_name,'Register',$name,$email);?></td></tr>
1357 <tr><td> </td></tr>
1358 <tr><td align="left">Disclaimer: Your contact information will be handled with the strictest confidence and will never be sold or shared with third parties.</td></td></tr>
1359 </table>
1360 </center>
1361 <?php
1362}
1363/**
1364* == END OF PLUGIN REGISTRATION SECTION ==
1365**/
1366/**
1367* == ADMIN TAB SECTION ==
1368**/
1369/**
1370* print posts with no traffics
1371**/
1372function pk_stt2_admin_print_no_traffic($url=0){
1373?>
1374 <div class="postbox-container" style="width: 98%;">
1375 <div class="metabox-holder">
1376 <div class="meta-box-sortables ui-sortable">
1377 <div id="popular-search-terms" class="postbox">
1378 <div class="handlediv" title="Click to toggle">
1379 </div>
1380 <h3 class="hndle">
1381 <span>100 Posts Without Any Traffic from Search Engines: </span></h3>
1382 <div class="inside">
1383 <div class="frame list">
1384 <?php echo pk_stt2_admin_print_list_of_no_traffic($url); ?>
1385 </div>
1386 </div>
1387 </div>
1388 </div>
1389 </div>
1390 </div>
1391 <?php
1392}
1393/**
1394* print popular and recent search terms full stats
1395**/
1396function pk_stt2_admin_print_stats(){
1397?>
1398 <div class="postbox-container" style="width: 48%;">
1399 <div class="metabox-holder">
1400 <div class="meta-box-sortables ui-sortable">
1401 <div id="popular-search-terms" class="postbox">
1402 <div class="handlediv" title="Click to toggle">
1403 </div>
1404 <h3 class="hndle">
1405 <span><?php echo $_GET['count']; ?> Popular Search Terms:</span></h3>
1406 <div class="inside">
1407 <div class="frame list">
1408 <?php echo pk_stt2_admin_print_searchterms( 'popular' ); ?>
1409 </div>
1410 </div>
1411 </div>
1412 </div>
1413 </div>
1414 </div>
1415 <div class="postbox-container" style="width: 48%;">
1416 <div class="metabox-holder">
1417 <div class="meta-box-sortables ui-sortable">
1418 <div id="recent-search-terms" class="postbox">
1419 <div class="handlediv" title="Click to toggle">
1420 </div>
1421 <h3 class="hndle">
1422 <span><?php echo $_GET['count']; ?> Recent Search Terms:</span></h3>
1423 <div class="inside">
1424 <div class="frame list">
1425 <?php echo pk_stt2_admin_print_searchterms( 'recent' ); ?>
1426 </div>
1427 </div>
1428 </div>
1429 </div>
1430 </div>
1431 </div> <?php
1432}
1433/**
1434* help page
1435**/
1436function pk_stt2_admin_help(){
1437?>
1438 <div class="postbox-container" style="width: 98%;">
1439 <div class="metabox-holder">
1440 <div class="meta-box-sortables ui-sortable">
1441 <div id="stt2-manual" class="postbox">
1442 <div class="handlediv" title="Click to toggle">
1443 </div>
1444 <h3 class="hndle">
1445 <span>Manual Usage Instructions:
1446 </span></h3>
1447 <div class="inside">
1448 <div class="frame">
1449 <ol>
1450 <li> To display incoming search terms to the article:
1451 <p>
1452 <span style="color:blue"> <?php if(function_exists('stt_terms_list')) echo stt_terms_list() ;?>
1453 </span>
1454 </p></li>
1455 <li> To display most popular search terms for all articles:
1456 <p>
1457 <span style="color:blue"> <?php if(function_exists('stt_popular_terms')) echo stt_popular_terms(10) ;?>
1458 </span>
1459 </p>
1460 <p> If your theme supports widgets, it is better to use the "Popular Search Terms" widget.
1461 </p></li>
1462 <li> To display most recent search terms:
1463 <p>
1464 <span style="color:blue"> <?php if(function_exists('stt_recent_terms')) echo stt_recent_terms(10) ;?>
1465 </span>
1466 </p>
1467 <p> If your theme supports widgets, it is better to use the "Recent Search Terms" widget.
1468 </p></li>
1469 <li> To display most popular search terms in category archive:
1470 <p>
1471 <span style="color:blue"> <?php if(function_exists('stt_popular_terms_in_category')) echo stt_popular_terms_in_category(10) ;?>
1472 </span>
1473 </p>
1474 <p> If your theme supports widgets, it is better to use the "Popular Terms in Category" widget.
1475 </p></li>
1476<li> To display random search terms (not recommended dt extensive resources):
1477 <p>
1478 <span style="color:blue"> <?php if(function_exists('stt_random_terms')) echo stt_random_terms(10) ;?>
1479 </span>
1480 </p>
1481 <p> If your theme supports widgets, it is better to use the "Random Search Terms" widget.
1482 </p></li>
1483 </ol>
1484 <small>* Replace '10' with the number of search terms to display.</small>
1485 </div>
1486 </div>
1487 </div>
1488 <div id="stt2-faq" class="postbox">
1489 <div class="handlediv" title="Click to toggle">
1490 </div>
1491 <h3 class="hndle">
1492 <span>Frequently Asked Questions:
1493 </span></h3>
1494 <div class="inside">
1495 <div class="frame">
1496 <ol>
1497 <li> Do I still need to edit the template and add the plugin code manually if I choose to add the search terms list automatically after post content?
1498 <p> No, the list of search terms will automatically be added right after the post content.
1499 </p></li>
1500 <li> Where do I need to add the plugin code manually?
1501 <p> You can put the plugin code into the current template that you use in file index.php or single.php and page.php. The most common place is under the post content or below the comment form.
1502 </p></li>
1503 <li> I prefer to display the search terms in the form of paragraphs, separated by commas, not in the form of a list like the plugin default format. How can I get results like that?
1504 <p>* Go to 'WordPress Admin > Settings > SEO searchTerms 2' menu,
1505 <br />* Empty columns 'Text and code After and Before the list',
1506 <br />* Empty columns 'Text and code before each keyword',
1507 <br />* And type `','` (a comma followed by a space) in the 'Text and code after each keyword',
1508 <br />* Save.
1509 </p></li>
1510 <li>I can't see any changes on the single post page. How do I know that this plugin works well?
1511 <p>This plugin will not display the list of search terms until there were visitors who come from search engines into the blog posts. Until then, no search terms will be displayed.
1512 </p>
1513 <p>So please wait until the plugin logs any visitors coming from search engines. Alternatively, you can search your own blog post from search engines to do a test.
1514 </p></li>
1515 <li>It seems the plugin won't work with my theme, what is wrong?
1516 <p> Like many others SEO plugins, this plugin requires your theme to call wp_head() hook on the HTML header. Put <?php wp_head(); ?> code between your <head>...</head> tag, usually on header.php file. Open the header file of WordPress Default Theme if you need any references.
1517 </p></li>
1518 </ol>
1519 </div>
1520 </div>
1521 </div>
1522 </div>
1523 </div>
1524 </div>
1525<?php
1526}
1527/**
1528* upgrade database to support international character or v2
1529**/
1530function pk_stt2_admin_upgrade_db(){
1531?>
1532 <div class="postbox-container" style="width: 98%;">
1533 <div class="metabox-holder">
1534 <div class="meta-box-sortables ui-sortable">
1535 <div id="upgrade_db" class="postbox">
1536 <div class="handlediv" title="Click to toggle">
1537 </div>
1538 <h3 class="hndle"><span>Database Upgrade for Faster Performance and International Characters Support</span></h3>
1539 <div class="inside">
1540 <form method = "post">
1541 <p>This upgrade will update your current database structure to gain better performance and lower server resources. Backup of your current database will be available on <strong><?php global $wpdb; echo $wpdb->prefix.'stt2_meta_backup'; ?></strong> table.</p>
1542
1543 <p>The possibility for the occurrence of error is very small, <span style="color:red"><i>but it is highly recommended to create a database backup before upgrading</i></span>. Detailed information on <a href="http://codex.wordpress.org/Backing_Up_Your_Database" target="_blank">how to backing up your WordPress database can be found here</a>.</p>
1544 <p class="submit">
1545 <input class="button-primary" type = "submit" name="upgrade_db_structure" value="Upgrade Now" />
1546 </p>
1547 </form>
1548 </div>
1549 </div>
1550 </div>
1551 </div>
1552 </div>
1553 <?php
1554}
1555/**
1556* donation tab
1557**/
1558function pk_stt2_admin_donate(){
1559?>
1560 <div style="text-align:center;">
1561 <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_blank">
1562 <input name="cmd" value="_donations" type="hidden" />
1563 <input name="business" value="poer@exclusivewordpress.com" type="hidden" />
1564 <input name="item_name" value="Donation for SEO SearchTerms Tagging 2 Plugin" type="hidden" />
1565 <input name="item_number" value="wp-admin" type="hidden" />
1566 <select name="currency_code" value="USD">
1567 <option value="AUD" />AUD
1568 <option value="CAD" />CAD
1569 <option value="EUR" />EUR
1570 <option value="GBP" />GBP
1571 <option value="JPY" />JPY
1572 <option value="USD" selected />USD
1573 <option value="NZD" />NZD
1574 <option value="CHF" />CHF
1575 <option value="HKD" />HKD
1576 <option value="SGD" />SGD
1577 <option value="SEK" />SEK
1578 <option value="DKK" />DKK
1579 <option value="PLN" />PLN
1580 <option value="NOK" />NOK
1581 <option value="HUF" />HUF
1582 <option value="CZK" />CZK
1583 <option value="ILS" />ILS
1584 <option value="MXN" />MXN
1585 <option value="BRL" />BRL
1586 </select>
1587 <select name="amount" value="25">
1588 <option value="5" />5
1589 <option value="10" />10
1590 <option value="15" />15
1591 <option value="20" />20
1592 <option value="25" selected />25
1593 <option value="50" />50
1594 </select>
1595 <input name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHostedGuest" type="hidden" />
1596 <p>
1597 <input src="https://www.paypal.com/en_US/GB/i/btn/btn_donateCC_LG.gif" name="submit" alt="Donate with PayPal" style="border: medium none; background: none repeat scroll 0% 0% transparent;" border="0" type="image" />
1598 <img alt="" src="https://www.paypal.com/en_US/i/scr/pixel.gif" border="0" height="1" width="1" />
1599 </p>
1600 </form>
1601 </div>
1602 <?php
1603}
1604/**
1605* == END OF ADMIN TAB SECTION ==
1606**/
1607/**
1608* == PROMOTE OLD POST SECTION ==
1609**/
1610/**
1611* get 1 random post publish over 30 days ago but still doesn't have traffic yet, then update the publish date time into now, make it the most recent post.
1612**/
1613function pk_stt2_promote_old_post () {
1614 $old_post_ID = pk_stt2_db_get_id_post_wo_traffic();
1615 if ( !empty($old_post_ID) ) {
1616 pk_stt2_db_promote_single_post_wo_traffic( $old_post_ID );
1617 update_option('pk_stt2_last_promoted_id',$old_post_ID);
1618 }
1619}
1620/**
1621* diplay last promoted post in admin area
1622**/
1623function pk_stt2_the_last_promoted_post() {
1624 $last_promoted_ID = get_option('pk_stt2_last_promoted_id');
1625 if ( $last_promoted_ID ) {
1626 $post_title = pk_stt2_db_get_last_promoted_post_title($last_promoted_ID);
1627 echo '<a href="'.get_permalink($last_promoted_ID).'" target="_blank">'.$post_title.'</a>';
1628 } else {
1629 echo 'None';
1630 }
1631}
1632/**
1633 * display list of posts with no search engines traffic
1634 * */
1635function pk_stt2_admin_print_list_of_no_traffic($url=0){
1636 $searchterms = pk_stt2_db_get_posts_wo_traffic();
1637
1638 if(!empty($searchterms)) {
1639 if ($url==0){
1640 $toReturn .= "<ol>";
1641 foreach($searchterms as $term){
1642 $permalink = get_permalink($term->ID);
1643 $toReturn .= "<li><a href=\"$permalink\" target=\"_blank\">$term->post_title</a>
1644 (<a href=\"post.php?post=$term->ID&action=edit\" target=\"_blank\">edit</a>)</li>";
1645 }
1646 $toReturn = trim($toReturn,', ');
1647 $toReturn .= "</ol>";
1648 } else {
1649 $toReturn .= "<ul>";
1650 foreach($searchterms as $term){
1651 $permalink = get_permalink($term->ID);
1652 $toReturn .= "<li>$permalink</li>";
1653 }
1654 $toReturn .= "</ul>";
1655 }
1656 return $toReturn;
1657 } else {
1658 return false;
1659 }
1660}
1661/**
1662* == END OF PROMOTE POST SECTION ==
1663**/
1664/**
1665* == PHP 4 SECTION ==
1666**/
1667/**
1668* PHP 4 equivalent of PHP 5 str_ireplace function
1669**/
1670if( !function_exists('str_ireplace') ){
1671 function str_ireplace($search,$replace,$subject){
1672 $token = chr(1);
1673 $haystack = strtolower($subject);
1674 $needle = strtolower($search);
1675 while (($pos=strpos($haystack,$needle))!==FALSE){
1676 $subject = substr_replace($subject,$token,$pos,strlen($search));
1677 $haystack = substr_replace($haystack,$token,$pos,strlen($search));
1678 }
1679 $subject = str_replace($token,$replace,$subject);
1680 return $subject;
1681 }
1682}
1683/**
1684* PHP 4 equivalent of PHP 5 htmlspecialchars_decode function
1685**/
1686if ( !function_exists('htmlspecialchars_decode') ){
1687 function htmlspecialchars_decode($text){
1688 return strtr($text,array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
1689 }
1690}
1691/**
1692* == END OF PHP 4 SECTION ==
1693**/
1694/**
1695* == SEARCH QUERY SECTION ==
1696**/
1697add_action('generate_rewrite_rules', 'pk_stt2_add_rewrite_rules');
1698function pk_stt2_add_rewrite_rules( $wp_rewrite ){
1699 $new_rules = array('^search/(.+)\$' => 'index.php?s=' .$wp_rewrite->preg_index(1));
1700 $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
1701}
1702// add_action('init', 'pk_stt2_flush_rewrite_rules');
1703function pk_stt2_flush_rewrite_rules(){
1704 global $wp_rewrite;
1705 $wp_rewrite->flush_rules();
1706}
1707add_action('parse_request', 'pk_stt2_filter_search_query');
1708function pk_stt2_filter_search_query(){
1709 global $wp;
1710 if (!empty($wp->query_vars['s'])){
1711 $wp->set_query_var('s', str_replace('-',' ',$wp->query_vars['s']));
1712 }
1713}
1714/**
1715* == END OF SEARCH QUERY SECTION ==
1716**/
1717/**
1718* add widgets
1719**/
1720include_once ( 'widget.php' );
1721?>