| 1 |
<?php |
| 2 |
/********************************************************************** |
| 3 |
* Admin Page * |
| 4 |
*********************************************************************/ |
| 5 |
function tptn_options() { |
| 6 |
|
| 7 |
global $wpdb; |
| 8 |
$poststable = $wpdb->posts; |
| 9 |
|
| 10 |
$tptn_settings = tptn_read_options(); |
| 11 |
|
| 12 |
if($_POST['tptn_save']){ |
| 13 |
$tptn_settings[title] = ($_POST['title']); |
| 14 |
$tptn_settings[title_daily] = ($_POST['title_daily']); |
| 15 |
$tptn_settings[daily_range] = ($_POST['daily_range']); |
| 16 |
$tptn_settings[limit] = ($_POST['limit']); |
| 17 |
$tptn_settings[count_disp_form] = ($_POST['count_disp_form']); |
| 18 |
$tptn_settings[add_to_content] = (($_POST['add_to_content']) ? true : false); |
| 19 |
$tptn_settings[exclude_pages] = (($_POST['exclude_pages']) ? true : false); |
| 20 |
$tptn_settings[track_authors] = (($_POST['track_authors']) ? true : false); |
| 21 |
$tptn_settings[pv_in_admin] = (($_POST['pv_in_admin']) ? true : false); |
| 22 |
$tptn_settings[disp_list_count] = (($_POST['disp_list_count']) ? true : false); |
| 23 |
$tptn_settings[d_use_js] = (($_POST['d_use_js']) ? true : false); |
| 24 |
$tptn_settings[show_credit] = (($_POST['show_credit']) ? true : false); |
| 25 |
|
| 26 |
update_option('ald_tptn_settings', $tptn_settings); |
| 27 |
|
| 28 |
$str = '<div id="message" class="updated fade"><p>'. __('Options saved successfully.','ald_tptn_plugin') .'</p></div>'; |
| 29 |
echo $str; |
| 30 |
} |
| 31 |
|
| 32 |
if ($_POST['tptn_default']){ |
| 33 |
delete_option('ald_tptn_settings'); |
| 34 |
$tptn_settings = tptn_default_options(); |
| 35 |
update_option('ald_tptn_settings', $tptn_settings); |
| 36 |
|
| 37 |
$str = '<div id="message" class="updated fade"><p>'. __('Options set to Default.','ald_tptn_plugin') .'</p></div>'; |
| 38 |
echo $str; |
| 39 |
} |
| 40 |
?> |
| 41 |
|
| 42 |
<div class="wrap"> |
| 43 |
<h2>Top 10 </h2> |
| 44 |
<div style="border: #ccc 1px solid; padding: 10px"> |
| 45 |
<fieldset class="options"> |
| 46 |
<legend> |
| 47 |
<h3> |
| 48 |
<?php _e('Support the Development','ald_tptn_plugin'); ?> |
| 49 |
</h3> |
| 50 |
</legend> |
| 51 |
<p> |
| 52 |
<?php _e('If you find ','ald_tptn_plugin'); ?> |
| 53 |
<a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10</a> |
| 54 |
<?php _e('useful, please do','ald_tptn_plugin'); ?> |
| 55 |
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&[email protected]&item_name=Top%10%20(From%20WP-Admin)&no_shipping=1&return=http://ajaydsouza.com/wordpress/plugins/top-10/&cancel_return=http://ajaydsouza.com/wordpress/plugins/top-10/&cn=Note%20to%20Author&tax=0&currency_code=USD&bn=PP-DonationsBF&charset=UTF-8" title="Donate via PayPal"><?php _e('drop in your contribution','ald_tptn_plugin'); ?></a>. |
| 56 |
(<a href="http://ajaydsouza.com/donate/"><?php _e('Some reasons why you should.','ald_tptn_plugin'); ?></a>)</p> |
| 57 |
</fieldset> |
| 58 |
</div> |
| 59 |
<form method="post" id="tptn_options" name="tptn_options" style="border: #ccc 1px solid; padding: 10px"> |
| 60 |
<fieldset class="options"> |
| 61 |
<legend> |
| 62 |
<h3> |
| 63 |
<?php _e('Options:','ald_tptn_plugin'); ?> |
| 64 |
</h3> |
| 65 |
</legend> |
| 66 |
<p> |
| 67 |
<label> |
| 68 |
<?php _e('Format to display the count in: ','ald_tptn_plugin'); ?><br /> |
| 69 |
<textarea name="count_disp_form" id="count_disp_form" cols="50" rows="5"><?php echo htmlspecialchars(stripslashes($tptn_settings[count_disp_form])); ?></textarea> |
| 70 |
</label> |
| 71 |
</p> |
| 72 |
<p><?php _e('Use <code>%totalcount%</code> to display the total count and <code>%dailycount%</code> to display the daily count. e.g. the default options displays <code>(Visited 123 times, 23 visits today)</code>','ald_tptn_plugin'); ?></p> |
| 73 |
<p> |
| 74 |
<label> |
| 75 |
<?php _e('Number of popular posts to display: ','ald_tptn_plugin'); ?> |
| 76 |
<input type="textbox" name="limit" id="limit" value="<?php echo stripslashes($tptn_settings[limit]); ?>"> |
| 77 |
</label> |
| 78 |
</p> |
| 79 |
<p> |
| 80 |
<label> |
| 81 |
<?php _e('Title of popular posts: ','ald_tptn_plugin'); ?> |
| 82 |
<input type="textbox" name="title" id="title" value="<?php echo stripslashes($tptn_settings[title]); ?>"> |
| 83 |
</label> |
| 84 |
</p> |
| 85 |
<p> |
| 86 |
<label> |
| 87 |
<?php _e('Title of daily popular posts: ','ald_tptn_plugin'); ?> |
| 88 |
<input type="textbox" name="title_daily" id="title_daily" value="<?php echo stripslashes($tptn_settings[title_daily]); ?>"> |
| 89 |
</label> |
| 90 |
</p> |
| 91 |
<p> |
| 92 |
<label> |
| 93 |
<?php _e('Daily Popular should contain views of how many days? ','ald_tptn_plugin'); ?> |
| 94 |
<input type="textbox" name="daily_range" id="daily_range" size="3" value="<?php echo stripslashes($tptn_settings[daily_range]); ?>"> |
| 95 |
</label> |
| 96 |
</p> |
| 97 |
<p> |
| 98 |
<label> |
| 99 |
<input type="checkbox" name="exclude_pages" id="exclude_pages" <?php if ($tptn_settings[exclude_pages]) echo 'checked="checked"' ?> /> |
| 100 |
<?php _e('Exclude Pages in display of Popular Posts? Number of views on Pages will continue to be counted.','ald_tptn_plugin'); ?> |
| 101 |
</label> |
| 102 |
</p> |
| 103 |
<p> |
| 104 |
<label> |
| 105 |
<input type="checkbox" name="track_authors" id="track_authors" <?php if ($tptn_settings[track_authors]) echo 'checked="checked"' ?> /> |
| 106 |
<?php _e('Track visits of authors on their own posts?','ald_tptn_plugin'); ?> |
| 107 |
</label> |
| 108 |
</p> |
| 109 |
<p> |
| 110 |
<label> |
| 111 |
<input type="checkbox" name="disp_list_count" id="disp_list_count" <?php if ($tptn_settings[disp_list_count]) echo 'checked="checked"' ?> /> |
| 112 |
<?php _e('Display number of page views in popular lists?','ald_tptn_plugin'); ?> |
| 113 |
</label> |
| 114 |
</p> |
| 115 |
<p> |
| 116 |
<label> |
| 117 |
<input type="checkbox" name="d_use_js" id="d_use_js" <?php if ($tptn_settings[d_use_js]) echo 'checked="checked"' ?> /> |
| 118 |
<?php _e('Force daily posts\' list to be dynamic? This options uses JavaScript to load the post and can increase your page load time','ald_tptn_plugin'); ?> |
| 119 |
</label> |
| 120 |
</p> |
| 121 |
<p> |
| 122 |
<label> |
| 123 |
<input type="checkbox" name="pv_in_admin" id="pv_in_admin" <?php if ($tptn_settings[pv_in_admin]) echo 'checked="checked"' ?> /> |
| 124 |
<?php _e('Display page views on Edit posts/pages in WP-Admin? An extra column is added with the count','ald_tptn_plugin'); ?> |
| 125 |
</label> |
| 126 |
</p> |
| 127 |
<p> |
| 128 |
<label> |
| 129 |
<input type="checkbox" name="add_to_content" id="add_to_content" <?php if ($tptn_settings[add_to_content]) echo 'checked="checked"' ?> /> |
| 130 |
<?php _e('Add post count to content?','ald_tptn_plugin'); ?> |
| 131 |
</label> |
| 132 |
</p> |
| 133 |
<p> |
| 134 |
<label> |
| 135 |
<input type="checkbox" name="show_credit" id="show_credit" <?php if ($tptn_settings[show_credit]) echo 'checked="checked"' ?> /> |
| 136 |
<?php _e('A link to the plugin is added as an extra list item to the list of popular posts. Not mandatory, but thanks if you do it!','ald_tptn_plugin'); ?> |
| 137 |
</label> |
| 138 |
</p> |
| 139 |
<p> |
| 140 |
<input type="submit" name="tptn_save" id="tptn_save" value="Save Options" style="border:#00CC00 1px solid" /> |
| 141 |
<input name="tptn_default" type="submit" id="tptn_default" value="Default Options" style="border:#FF0000 1px solid" onclick="if (!confirm('<?php _e('Do you want to set options to Default?','ald_tptn_plugin'); ?>')) return false;" /> |
| 142 |
</p> |
| 143 |
</fieldset> |
| 144 |
</form> |
| 145 |
</div> |
| 146 |
<?php |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
function tptn_manage() { |
| 151 |
$paged = intval($_GET['paged']); |
| 152 |
$limit = intval($_GET['limit']); |
| 153 |
$daily = $_GET['daily']; |
| 154 |
|
| 155 |
echo '<div class="wrap">'; |
| 156 |
echo '<h2>'; |
| 157 |
if (!$daily) echo 'Popular Posts'; else echo 'Daily Popular Posts'; |
| 158 |
echo '</h2>'; |
| 159 |
echo '<div style="border: #ccc 1px solid; padding: 10px">'; |
| 160 |
echo tptn_pop_display($daily,$paged,$limit); |
| 161 |
echo '</div></div>'; |
| 162 |
} |
| 163 |
|
| 164 |
/* Add menu item in WP-Admin */ |
| 165 |
function tptn_adminmenu() { |
| 166 |
if (function_exists('current_user_can')) { |
| 167 |
// In WordPress 2.x |
| 168 |
if (current_user_can('manage_options')) { |
| 169 |
$tptn_is_admin = true; |
| 170 |
} |
| 171 |
} else { |
| 172 |
// In WordPress 1.x |
| 173 |
global $user_ID; |
| 174 |
if (user_can_edit_user($user_ID, 0)) { |
| 175 |
$tptn_is_admin = true; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ((function_exists('add_options_page'))&&($tptn_is_admin)) { |
| 180 |
add_options_page(__("Top 10", 'myald_tptn_plugin'), __("Top 10", 'myald_tptn_plugin'), 9, 'tptn_options', 'tptn_options'); |
| 181 |
add_posts_page(__("Popular Posts", 'myald_tptn_plugin'), __("Top 10", 'myald_tptn_plugin'), 9, 'tptn_manage', 'tptn_manage'); |
| 182 |
} |
| 183 |
} |
| 184 |
add_action('admin_menu', 'tptn_adminmenu'); |
| 185 |
|
| 186 |
|
| 187 |
/* Create a Dashboard Widget */ |
| 188 |
function tptn_pop_display($daily = false, $page = 0, $limit = 10) { |
| 189 |
global $wpdb, $siteurl, $tableposts, $id; |
| 190 |
|
| 191 |
$table_name = $wpdb->prefix . "top_ten"; |
| 192 |
if ($daily) $table_name .= "_daily"; // If we're viewing daily posts, set this to true |
| 193 |
|
| 194 |
$tptn_settings = tptn_read_options(); |
| 195 |
if (!($limit)) $limit = $tptn_settings['limit']; |
| 196 |
if (!($page)) $page = 0; // Default page value. |
| 197 |
|
| 198 |
if(!$daily) { |
| 199 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 200 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 201 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 202 |
$sql .= "AND post_status = 'publish' "; |
| 203 |
$sql .= "ORDER BY cntaccess DESC"; |
| 204 |
} else { |
| 205 |
$daily_range = $tptn_settings[daily_range]. ' DAY'; |
| 206 |
$current_date = $wpdb->get_var("SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $daily_range), INTERVAL 1 DAY) "); |
| 207 |
|
| 208 |
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status "; |
| 209 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 210 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 211 |
$sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' "; |
| 212 |
$sql .= "GROUP BY postnumber "; |
| 213 |
$sql .= "ORDER BY sumCount DESC"; |
| 214 |
} |
| 215 |
|
| 216 |
$results = $wpdb->get_results($sql); |
| 217 |
$numrows = 0; |
| 218 |
if ($results) { |
| 219 |
foreach ($results as $result) { |
| 220 |
$numrows++; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
$pages = intval($numrows/$limit); // Number of results pages. |
| 225 |
|
| 226 |
// $pages now contains int of pages, unless there is a remainder from division. |
| 227 |
|
| 228 |
if ($numrows % $limit) {$pages++;} // has remainder so add one page |
| 229 |
|
| 230 |
$current = ($page/$limit) + 1; // Current page number. |
| 231 |
|
| 232 |
if (($pages < 1) || ($pages == 0)) {$total = 1;} // If $pages is less than one or equal to 0, total pages is 1. |
| 233 |
else { $total = $pages;} // Else total pages is $pages value. |
| 234 |
|
| 235 |
$first = $page + 1; // The first result. |
| 236 |
|
| 237 |
if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {$last = $page + $limit;} //If not last results page, last result equals $page plus $limit. |
| 238 |
else{$last = $numrows;} // If last results page, last result equals total number of results. |
| 239 |
|
| 240 |
if(!$daily) { |
| 241 |
$sql = "SELECT postnumber, cntaccess , ID, post_type "; |
| 242 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 243 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 244 |
$sql .= "AND post_status = 'publish' "; |
| 245 |
$sql .= "ORDER BY cntaccess DESC LIMIT $page, $limit"; |
| 246 |
} else { |
| 247 |
$sql = "SELECT postnumber, SUM(cntaccess) as sumCount, dp_date, ID, post_type, post_status "; |
| 248 |
$sql .= "FROM $table_name INNER JOIN ". $wpdb->posts ." ON postnumber=ID " ; |
| 249 |
if ($tptn_settings['exclude_pages']) $sql .= "AND post_type = 'post' "; |
| 250 |
$sql .= "AND post_status = 'publish' AND dp_date >= '$current_date' "; |
| 251 |
$sql .= "GROUP BY postnumber "; |
| 252 |
$sql .= "ORDER BY sumCount DESC LIMIT $page, $limit"; |
| 253 |
} |
| 254 |
|
| 255 |
$results = $wpdb->get_results($sql); |
| 256 |
|
| 257 |
$output = '<div id="tptn_popular_posts">'; |
| 258 |
$output .= '<table width="100%" border="0"> |
| 259 |
<tr> |
| 260 |
<td width="50%" align="left"> |
| 261 |
Results <strong>'.$first.'</strong> - <strong>'.$last.'</strong> of <strong>'.$numrows.'</strong> |
| 262 |
</td> |
| 263 |
<td width="50%" align="right"> |
| 264 |
Page <strong>'.$current.'</strong> of <strong>'.$total.'</strong> |
| 265 |
</td> |
| 266 |
</tr> |
| 267 |
<tr> |
| 268 |
<td colspan="2" align="right"> </td> |
| 269 |
</tr> |
| 270 |
<tr> |
| 271 |
<td align="left">'; |
| 272 |
|
| 273 |
if(!$daily) { |
| 274 |
$output .= '<a href="./edit.php?page=tptn_manage&daily=1">View Daily Popular Posts</a></td>'; |
| 275 |
} else { |
| 276 |
$output .= '<a href="./edit.php?page=tptn_manage&daily=0">View Overall Popular Posts</a></td>'; |
| 277 |
} |
| 278 |
$output .= '<td align="right"> |
| 279 |
Results per-page: <a href="./edit.php?page=tptn_manage&daily='.$daily.'&limit=10">10</a> | <a href="./edit.php?page=tptn_manage&daily='.$daily.'&limit=20">20</a> | <a href="./edit.php?page=tptn_manage&daily='.$daily.'&limit=50">50</a> | <a href="./edit.php?page=tptn_manage&daily='.$daily.'&limit=100">100</a> |
| 280 |
</td> |
| 281 |
</tr> |
| 282 |
<tr> |
| 283 |
<td colspan="2" align="right"><hr /></td> |
| 284 |
</tr> |
| 285 |
</table>'; |
| 286 |
|
| 287 |
|
| 288 |
$output .= '<ul>'; |
| 289 |
if ($results) { |
| 290 |
foreach ($results as $result) { |
| 291 |
$output .= '<li><a href="'.get_permalink($result->postnumber).'">'.get_the_title($result->postnumber).'</a>'; |
| 292 |
if ($daily) $output .= ' ('.$result->sumCount.')'; else $output .= ' ('.$result->cntaccess.')'; |
| 293 |
$output .= '</li>'; |
| 294 |
} |
| 295 |
} |
| 296 |
$output .= '</ul>'; |
| 297 |
|
| 298 |
$output .= '<p align="center">'; |
| 299 |
if ($page != 0) { // Don't show back link if current page is first page. |
| 300 |
$back_page = $page - $limit; |
| 301 |
$output .= "<a href=\"./edit.php?page=tptn_manage&paged=$back_page&daily=$daily&limit=$limit\">« Previous</a> \n"; |
| 302 |
} |
| 303 |
|
| 304 |
for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. |
| 305 |
{ |
| 306 |
$ppage = $limit*($i - 1); |
| 307 |
if ($ppage == $page){ |
| 308 |
$output .= ("<b>$i</b>\n");} // If current page don't give link, just text. |
| 309 |
else{ |
| 310 |
$output .= ("<a href=\"./edit.php?page=tptn_manage&paged=$ppage&daily=$daily&limit=$limit\">$i</a> \n"); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. |
| 315 |
$next_page = $page + $limit; |
| 316 |
$output .= " <a href=\"./edit.php?page=tptn_manage&paged=$next_page&daily=$daily&limit=$limit\">Next »</a>"; |
| 317 |
} |
| 318 |
$output .= '</p>'; |
| 319 |
$output .= '<p style="text-align:center;border-top: #000 1px solid">Popular posts by <a href="http://ajaydsouza.com/wordpress/plugins/top-10/">Top 10 plugin</a></p>'; |
| 320 |
$output .= '</div>'; |
| 321 |
|
| 322 |
return $output; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
// Dashboard for Popular Posts |
| 327 |
function tptn_pop_dashboard() { |
| 328 |
echo tptn_pop_display(false,0,10); |
| 329 |
} |
| 330 |
// Dashboard for Daily Popular Posts |
| 331 |
function tptn_pop_daily_dashboard() { |
| 332 |
echo tptn_pop_display(true,0,10); |
| 333 |
} |
| 334 |
|
| 335 |
function tptn_pop_dashboard_setup() { |
| 336 |
if (function_exists('wp_add_dashboard_widget')) { |
| 337 |
wp_add_dashboard_widget( 'tptn_pop_dashboard', __( 'Popular Posts' ), 'tptn_pop_dashboard' ); |
| 338 |
wp_add_dashboard_widget( 'tptn_pop_daily_dashboard', __( 'Daily Popular' ), 'tptn_pop_daily_dashboard' ); |
| 339 |
} |
| 340 |
} |
| 341 |
add_action('wp_dashboard_setup', 'tptn_pop_dashboard_setup'); |
| 342 |
|
| 343 |
|
| 344 |
/* Display page views on the Edit Posts / Pages screen */ |
| 345 |
// Add an extra column |
| 346 |
function tptn_column($cols) { |
| 347 |
$tptn_settings = tptn_read_options(); |
| 348 |
|
| 349 |
if ($tptn_settings[pv_in_admin]) $cols['tptn'] = __('Total / Today\'s Views','ald_tptn_plugin'); |
| 350 |
return $cols; |
| 351 |
} |
| 352 |
|
| 353 |
// Display page views for each column |
| 354 |
function tptn_value($column_name, $id) { |
| 355 |
$tptn_settings = tptn_read_options(); |
| 356 |
if (($column_name == 'tptn')&&($tptn_settings[pv_in_admin])) { |
| 357 |
global $wpdb; |
| 358 |
|
| 359 |
$table_name = $wpdb->prefix . "top_ten"; |
| 360 |
|
| 361 |
$resultscount = $wpdb->get_row("select postnumber, cntaccess from $table_name WHERE postnumber = $id"); |
| 362 |
$cntaccess = number_format((($resultscount) ? $resultscount->cntaccess : 0)); |
| 363 |
|
| 364 |
$cntaccess .= ' / '; |
| 365 |
|
| 366 |
// Now process daily count |
| 367 |
$table_name = $wpdb->prefix . "top_ten_daily"; |
| 368 |
|
| 369 |
$daily_range = $tptn_settings[daily_range]. ' DAY'; |
| 370 |
$current_date = $wpdb->get_var("SELECT DATE_ADD(DATE_SUB(CURDATE(), INTERVAL $daily_range), INTERVAL 1 DAY) "); |
| 371 |
$resultscount = $wpdb->get_row("SELECT postnumber, SUM(cntaccess) as sumCount FROM $table_name WHERE postnumber = $id AND dp_date >= '$current_date' GROUP BY postnumber "); |
| 372 |
$cntaccess .= number_format((($resultscount) ? $resultscount->sumCount : 0)); |
| 373 |
|
| 374 |
echo $cntaccess; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
// Output CSS for width of new column |
| 379 |
function tptn_css() { |
| 380 |
?> |
| 381 |
<style type="text/css"> |
| 382 |
#tptn { width: 50px; } |
| 383 |
</style> |
| 384 |
<?php |
| 385 |
} |
| 386 |
|
| 387 |
// Actions/Filters for various tables and the css output |
| 388 |
add_filter('manage_posts_columns', 'tptn_column'); |
| 389 |
add_action('manage_posts_custom_column', 'tptn_value', 10, 2); |
| 390 |
add_filter('manage_pages_columns', 'tptn_column'); |
| 391 |
add_action('manage_pages_custom_column', 'tptn_value', 10, 2); |
| 392 |
add_filter('manage_media_columns', 'tptn_column'); |
| 393 |
add_action('manage_media_custom_column', 'tptn_value', 10, 2); |
| 394 |
add_filter('manage_link-manager_columns', 'tptn_column'); |
| 395 |
add_action('manage_link_custom_column', 'tptn_value', 10, 2); |
| 396 |
add_action('admin_head', 'tptn_css'); |
| 397 |
|
| 398 |
?> |