Advertisement
Razorspined

Untitled

Apr 2nd, 2023
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.13 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.             echo "<tr>
  55.                    <td>admin</td>
  56.                    <td>Как се казва звездата най-близка до нашата планета?</td>
  57.                    <td>
  58.                    <form method='POST' action='checkAnswer'>
  59.                        <input id='1234' type='text' name='answer'/>
  60.                        <input id='1234-submit' type='button'>Провери отговор</input>
  61.                    </form>
  62.                    </td>
  63.                </tr>";
  64.         }
  65.         echo "</tbody></table>";
  66.         ?>
  67.         <script>
  68.             const text = document.getElementById("1234");
  69.             const input = document.getElementById("1234-submit");
  70.  
  71.             input.addActionListener("click", () => {
  72.                 if(text.answer == text.value) alert("veren otgovor");
  73.                 else alert ("greshen otgovor");
  74.             });
  75.         </script>
  76.         <?php
  77.         echo $args["after_widget"];
  78.     }
  79. }
  80.  
  81. function checkAnswer($question_id, $answer) {
  82.     global $wpdb;
  83.  
  84.     $answer = $wpdb->get_var("SELECT COUNT(*) from plugin_questions WHERE question_id={$question_id} AND answer={$answer}");
  85.  
  86.     return $answer;
  87. }
  88.  
  89. function register_ranking_widget() {
  90.     register_widget("top_rankings_widget");
  91. }
  92.  
  93. add_action("widgets_init", "register_ranking_widget");
  94.  
  95. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement