PageRenderTime 15ms CodeModel.GetById 7ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

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