3 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
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
10 Author: Danilo 'danix' Macrì
11 Author URI: http://danixland.net
13 License URI: https://www.gnu.org/licenses/gpl-2.0.html
19 * Add plugin i18n domain: dnxlico
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' );
27 if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL
. '/plugins' );
29 load_plugin_textdomain('dnxlico', WP_PLUGIN_URL
. '/danixland-lico/i18n/', 'danixland-lico/i18n/');
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.. ;)
38 function dnxlico_retrieve_data( $machine_id = '', $apikey = '' ) {
39 if ( empty($machine_id) ||
empty($apikey) )
41 $m_id = (integer)$machine_id;
42 $address = 'http://api.linuxcounter.net/v1/machines/' . $m_id;
44 $header[] = 'x-lico-apikey: ' . $apikey;
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);
53 $response = json_decode($result, true);
59 * Add function to widgets_init that'll load our widget.
62 add_action( 'widgets_init', 'dnxlico_register' );
65 * Register our widget.
66 * 'User_Panel' is the widget class used below.
70 function dnxlico_register() {
71 register_widget( 'Dnxlico_Widget' );
76 * This class handles everything that needs to be handled with the widget:
77 * the settings, form, display, and update. Nice!
81 class Dnxlico_Widget
extends WP_Widget
{
86 public function __construct() {
87 $control_ops = array('width' => 400, 'height' => 350);
89 'dnx-lico', // id_base
90 __('Linux Counter', 'dnxlico' ), // Name
91 array( 'description' => __('Display Linux Counter informations', 'dnxlico') ),
97 * How to display the widget on the public side of the site.
99 public function widget( $args, $instance ) {
102 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ?
'' : $instance['title'], $instance, $this->id_base
);
104 echo $args['before_widget'];
105 if ( ! empty( $title ) ) {
106 echo $args['before_title'] . $title . $args['after_title'];
111 $test = dnxlico_retrieve_data( $instance['machine_id'], $instance['apikey'] );
117 echo $args['after_widget'];
121 * Handles updating settings for the current Text widget instance.
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'];
133 * Outputs the Text widget settings form.
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'] );
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
>