summaryrefslogtreecommitdiffstats
path: root/danixland-lico.php
blob: a4503f1f60bfb575a3ab0b32df13640e40fd191a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/* 
Plugin Name:    danixland lico
Description:    Display linux counter data inside WordPress. You will need to register on <a href="https://www.linuxcounter.net/">linuxcounter.net</a> in order to get an API key and use this plugin.
Plugin URI:     http://danixland.net
Version:        0.1
Author:         Danilo 'danix' Macr&igrave;
Author URI:     http://danixland.net
License:        GPL2
License URI:    https://www.gnu.org/licenses/gpl-2.0.html
Domain Path:    /i18n
Text Domain:    dnxlico
*/

/**
 * Add plugin i18n domain: dnxlico
 * @since 0.1
 */
// Pre 2.6 compatibility
if ( ! defined( 'WP_CONTENT_URL' ) ) {
    if ( defined( 'WP_SITEURL' ) ) define( 'WP_CONTENT_URL', WP_SITEURL . '/wp-content' );
    else define( 'WP_CONTENT_URL', get_option( 'url' ) . '/wp-content' );
}
if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );

load_plugin_textdomain('dnxlico', WP_PLUGIN_URL . '/danixland-lico/i18n/', 'danixland-lico/i18n/');

/**
 * The heart of the plugin, this function retrieves the data from the Rest API.
 * as of 21-01-16, it works just fine and retrieves data for one single machine
 * from the account. As soon as more API data is unlocked, this should be modified
 * accordingly to allow for different kind of data to be retrieved.. ;)
 * @since 0.1
 */
function dnxlico_retrieve_data( $machine_id = '', $apikey = '' ) {
    if ( empty($machine_id) || empty($apikey) )
        return;
    $m_id = (integer)$machine_id;
    $address = 'http://api.linuxcounter.net/v1/machines/' . $m_id;
    $header = array();
    $header[] = 'x-lico-apikey: ' . $apikey;

    $curl = curl_init($address);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent: danixland-lico/1.0");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);

    $response = json_decode($result, true);

    return $response;
}

/**
 * Add function to widgets_init that'll load our widget.
 * @since 0.1
 */
add_action( 'widgets_init', 'dnxlico_register' );

/**
 * Register our widget.
 * 'User_Panel' is the widget class used below.
 *
 * @since 0.1
 */
function dnxlico_register() {
    register_widget( 'Dnxlico_Widget' );
}

/**
 * User_Panel class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Dnxlico_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    public function __construct() {
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct(
            'dnx-lico', // id_base
            __('Linux Counter', 'dnxlico' ), // Name
            array( 'description' => __('Display Linux Counter informations', 'dnxlico') ),
            $control_ops
        );
    }

    /**
     * How to display the widget on the public side of the site.
     */
    public function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
<div>
    <pre>
        <?php
        $test = dnxlico_retrieve_data( $instance['machine_id'], $instance['apikey'] );
        var_dump($test);
        ?>
    </pre>
</div>
        <?php
        echo $args['after_widget'];
    }

    /**
     * Handles updating settings for the current Text widget instance.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
        $instance['apikey'] = sanitize_text_field( $new_instance['apikey'] );
        $instance['machine_id'] = (integer)$new_instance['machine_id'];

        return $instance;
    }

    /**
     * Outputs the Text widget settings form.
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'apikey' => '', 'machine_id' => '' ) );
        $title = sanitize_text_field( $instance['title'] );
        $apikey = sanitize_text_field( $instance['apikey'] );
        $machine_id = sanitize_text_field( $instance['machine_id'] );
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
        <p><label for="<?php echo $this->get_field_id('apikey'); ?>"><?php _e('API Key:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('apikey'); ?>" name="<?php echo $this->get_field_name('apikey'); ?>" type="text" value="<?php echo esc_attr($apikey); ?>" /></p>
        <p><label for="<?php echo $this->get_field_id('machine_id'); ?>"><?php _e('Machine ID:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('machine_id'); ?>" name="<?php echo $this->get_field_name('machine_id'); ?>" type="text" value="<?php echo esc_attr($machine_id); ?>" /></p>
        <?php
    }

}