From 6f2a3fed97bcc463b3b2961bb79c39d21f38bd38 Mon Sep 17 00:00:00 2001 From: danix Date: Tue, 10 Jul 2018 16:37:12 +0200 Subject: [PATCH 1/1] initial commit --- danixland-lico.php | 150 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 danixland-lico.php diff --git a/danixland-lico.php b/danixland-lico.php new file mode 100644 index 0000000..a4503f1 --- /dev/null +++ b/danixland-lico.php @@ -0,0 +1,150 @@ +linuxcounter.net in order to get an API key and use this plugin. +Plugin URI: http://danixland.net +Version: 0.1 +Author: Danilo 'danix' Macrì +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']; + } ?> +
+
+        
+    
+
+ '', 'apikey' => '', 'machine_id' => '' ) ); + $title = sanitize_text_field( $instance['title'] ); + $apikey = sanitize_text_field( $instance['apikey'] ); + $machine_id = sanitize_text_field( $instance['machine_id'] ); + ?> +

+

+

+

+

+

+