initial commit
[danixland-lico.git] / danixland-lico.php
1 <?php
2
3 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
4
5 /*
6 Plugin Name: danixland lico
7 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.
8 Plugin URI: http://danixland.net
9 Version: 0.1
10 Author: Danilo 'danix' Macr&igrave;
11 Author URI: http://danixland.net
12 License: GPL2
13 License URI: https://www.gnu.org/licenses/gpl-2.0.html
14 Domain Path: /i18n
15 Text Domain: dnxlico
16 */
17
18 /**
19 * Add plugin i18n domain: dnxlico
20 * @since 0.1
21 */
22 // Pre 2.6 compatibility
23 if ( ! defined( 'WP_CONTENT_URL' ) ) {
24 if ( defined( 'WP_SITEURL' ) ) define( 'WP_CONTENT_URL', WP_SITEURL . '/wp-content' );
25 else define( 'WP_CONTENT_URL', get_option( 'url' ) . '/wp-content' );
26 }
27 if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' );
28
29 load_plugin_textdomain('dnxlico', WP_PLUGIN_URL . '/danixland-lico/i18n/', 'danixland-lico/i18n/');
30
31 /**
32 * The heart of the plugin, this function retrieves the data from the Rest API.
33 * as of 21-01-16, it works just fine and retrieves data for one single machine
34 * from the account. As soon as more API data is unlocked, this should be modified
35 * accordingly to allow for different kind of data to be retrieved.. ;)
36 * @since 0.1
37 */
38 function dnxlico_retrieve_data( $machine_id = '', $apikey = '' ) {
39 if ( empty($machine_id) || empty($apikey) )
40 return;
41 $m_id = (integer)$machine_id;
42 $address = 'http://api.linuxcounter.net/v1/machines/' . $m_id;
43 $header = array();
44 $header[] = 'x-lico-apikey: ' . $apikey;
45
46 $curl = curl_init($address);
47 curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
48 curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent: danixland-lico/1.0");
49 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
50 $result = curl_exec($curl);
51 curl_close($curl);
52
53 $response = json_decode($result, true);
54
55 return $response;
56 }
57
58 /**
59 * Add function to widgets_init that'll load our widget.
60 * @since 0.1
61 */
62 add_action( 'widgets_init', 'dnxlico_register' );
63
64 /**
65 * Register our widget.
66 * 'User_Panel' is the widget class used below.
67 *
68 * @since 0.1
69 */
70 function dnxlico_register() {
71 register_widget( 'Dnxlico_Widget' );
72 }
73
74 /**
75 * User_Panel class.
76 * This class handles everything that needs to be handled with the widget:
77 * the settings, form, display, and update. Nice!
78 *
79 * @since 0.1
80 */
81 class Dnxlico_Widget extends WP_Widget {
82
83 /**
84 * Widget setup.
85 */
86 public function __construct() {
87 $control_ops = array('width' => 400, 'height' => 350);
88 parent::__construct(
89 'dnx-lico', // id_base
90 __('Linux Counter', 'dnxlico' ), // Name
91 array( 'description' => __('Display Linux Counter informations', 'dnxlico') ),
92 $control_ops
93 );
94 }
95
96 /**
97 * How to display the widget on the public side of the site.
98 */
99 public function widget( $args, $instance ) {
100 extract( $args );
101
102 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
103
104 echo $args['before_widget'];
105 if ( ! empty( $title ) ) {
106 echo $args['before_title'] . $title . $args['after_title'];
107 } ?>
108 <div>
109 <pre>
110 <?php
111 $test = dnxlico_retrieve_data( $instance['machine_id'], $instance['apikey'] );
112 var_dump($test);
113 ?>
114 </pre>
115 </div>
116 <?php
117 echo $args['after_widget'];
118 }
119
120 /**
121 * Handles updating settings for the current Text widget instance.
122 */
123 public function update( $new_instance, $old_instance ) {
124 $instance = $old_instance;
125 $instance['title'] = sanitize_text_field( $new_instance['title'] );
126 $instance['apikey'] = sanitize_text_field( $new_instance['apikey'] );
127 $instance['machine_id'] = (integer)$new_instance['machine_id'];
128
129 return $instance;
130 }
131
132 /**
133 * Outputs the Text widget settings form.
134 */
135 public function form( $instance ) {
136 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'apikey' => '', 'machine_id' => '' ) );
137 $title = sanitize_text_field( $instance['title'] );
138 $apikey = sanitize_text_field( $instance['apikey'] );
139 $machine_id = sanitize_text_field( $instance['machine_id'] );
140 ?>
141 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
142 <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>
143 <p><label for="<?php echo $this->get_field_id('apikey'); ?>"><?php _e('API Key:'); ?></label>
144 <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>
145 <p><label for="<?php echo $this->get_field_id('machine_id'); ?>"><?php _e('Machine ID:'); ?></label>
146 <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>
147 <?php
148 }
149
150 }