Advertisement
Razorspined

Untitled

Apr 2nd, 2023
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.03 KB | None | 0 0
  1. <?php
  2. /*
  3. * Plugin Name: Ranking Widget
  4. *
  5. *
  6. */
  7.  
  8. class top_rankings_widget extends WP_Widget {
  9.     public function __construct() {
  10.         $widget_options = array(
  11.             'classname' => 'ranking_widget',
  12.             'description' => 'A widget that displays the top ranking users in order of posts and comments'
  13.         );
  14.  
  15.         parent::__construct("ranking_widget", "Ranking Widget", $widget_options);
  16.     }
  17.  
  18.     public function widget($args, $i) {
  19.         $title = isset($i["title"]) ? apply_filter('widget_title', $i['title']) : "";
  20.  
  21.         echo $args["before_widget"] . $args["before_title"] . $title . $args["after_title"];
  22.         global $wpdb;
  23.  
  24.         $rows = $wpdb->get_results("SELECT ID, display_name FROM wp_users");
  25.  
  26.         $map_fn = function($obj) {
  27.             return (object)array(
  28.                 "ID" => $obj->ID,
  29.                 "display_name" => $obj->display_name
  30.             );
  31.         };
  32.  
  33.         $results = array_map($map_fn, $rows);
  34.  
  35.         foreach($results as $user) {
  36.             $comment_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_comments WHERE user_id = {$user->ID}");
  37.             $user->comments = $comment_count;
  38.  
  39.             $post_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_posts WHERE post_author = '{$user->ID}' AND post_status='publish' AND post_type='post'");
  40.             $user->posts = $post_count;
  41.         }
  42.  
  43.         echo "<table>
  44.                <thead>
  45.                    <tr>
  46.                        <td>Name</td>
  47.                        <td>Comments</td>
  48.                        <td>Posts</td>
  49.                    </tr>
  50.                </thead>
  51.                <tbody>";
  52.         foreach($results as $user) {
  53.             echo "<tr><td>{$user->display_name}</td><td>{$user->comments}</td><td>{$user->posts}</td></tr>";
  54.         }
  55.         echo "</tbody></table>";    
  56.         echo $args["after_widget"];
  57.     }
  58. }
  59.  
  60. function register_ranking_widget() {
  61.     register_widget("top_rankings_widget");
  62. }
  63.  
  64. add_action("widgets_init", "register_ranking_widget");
  65.  
  66. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement