/widget.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 80 lines · 50 code · 20 blank · 10 comment · 1 complexity · 8a6cfd1d385dfba1e76ed9f21735593e MD5 · raw file

  1. <?php
  2. //widget
  3. class online_widget extends WP_Widget {
  4. // Constructor //
  5. function online_widget() {
  6. $widget_ops = array( 'classname' => 'online_widget', 'description' => 'Displays online employees' ); // Widget Settings
  7. $control_ops = array( 'id_base' => 'online_widget' ); // Widget Control Settings
  8. $this->WP_Widget( 'online_widget', 'Online Employees', $widget_ops, $control_ops ); // Create the widget
  9. }
  10. // Extract Args //
  11. function widget($args, $instance) {
  12. global $wpdb;
  13. extract( $args );
  14. $title = apply_filters('widget_title', $instance['title']); // the widget title
  15. $users_to_show = $instance['num_users']; // grab how many users to show
  16. // Before widget //
  17. echo $before_widget;
  18. // Title of widget //
  19. if ( $title ) { echo $before_title . $title . $after_title; }
  20. // Widget output //
  21. $table_name = $wpdb->prefix . "employee_time";
  22. $results = $wpdb->get_results($wpdb->prepare("SELECT user FROM $table_name WHERE online = 1 LIMIT $users_to_show "));
  23. echo "<div class=info>";
  24. echo "<ul>";
  25. foreach($results as $result){
  26. $id = $result->user;
  27. //poll wp_users for $user
  28. $user_info = get_userdata($id);
  29. echo "<li>".$user_info->display_name."</li>";
  30. }
  31. echo "</ul>";
  32. echo "</div>";
  33. // After widget //
  34. echo $after_widget;
  35. }
  36. // Update Settings //
  37. function update($new_instance, $old_instance) {
  38. $instance['title'] = strip_tags($new_instance['title']);
  39. $instance['num_users'] = strip_tags($new_instance['num_users']);
  40. return $instance;
  41. }
  42. // Widget Control Panel //
  43. function form($instance) {
  44. $defaults = array( 'title' => 'Online Employees', 'num_users' => '8');
  45. $instance = wp_parse_args( (array) $instance, $defaults ); ?>
  46. <p>
  47. <label for="<?php echo $this->get_field_id('title'); ?>">Title:</label>
  48. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>'" type="text" value="<?php echo $instance['title']; ?>" />
  49. </p>
  50. <p>
  51. <label for="<?php echo $this->get_field_id('link_titles'); ?>">Number of Users to Show</label>
  52. <input class="widefat" id="<?php echo $this->get_field_id('num_users'); ?>" name="<?php echo $this->get_field_name('num_users'); ?>" value ="<?php echo $instance['num_users']; ?>" >
  53. </p>
  54. <?php }
  55. }
  56. add_action('widgets_init', create_function('', 'return register_widget("online_widget");'));
  57. ?>