PluginProbe
WebberZone Top 10 — Popular Posts / 1.4.1
WebberZone Top 10 — Popular Posts v1.4.1
4.5.2 4.5.1 4.5.0 4.4.3 4.4.2 4.4.1 4.4.0 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 trunk 1.0 1.0.1 1.1 1.2 1.3 1.4 1.4.1 1.5 1.5.1 1.5.2 1.5.3 1.6 All 118 releases
top-10 / admin.inc.php

admin.inc.php in WebberZone Top 10 — Popular Posts 1.4.1, at admin.inc.php

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