3 defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
7 * @link https://danix.xyz
12 * Plugin Name: danixland-covid-italy
13 * Plugin URI: https://danix.xyz
14 * Description: Show the latest covid19 stats for italy in a widget
16 * Author: Danilo 'danix' M.
17 * Author URI: https://danix.xyz
19 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
20 * Text Domain: dnxcovita
21 * Domain Path: /languages
25 * Add plugin i18n domain: dnxcovita
27 * @link https://danix.xyz
31 // Pre 2.6 compatibility
32 if ( ! defined( 'WP_CONTENT_URL' ) ) {
33 if ( defined( 'WP_SITEURL' ) ) define( 'WP_CONTENT_URL', WP_SITEURL
. '/wp-content' );
34 else define( 'WP_CONTENT_URL', get_option( 'url' ) . '/wp-content' );
36 if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL
. '/plugins' );
38 load_plugin_textdomain('dnxcovita', WP_PLUGIN_URL
. '/danixland-covid-italy/languages/', 'danixland-covid-italy/languages/');
41 * The heart of the plugin, this function retrieves the data from github and
42 * stores it in a transient for up to 24h.
44 * @link https://danix.xyz
48 function dnxcovita_retrieve_data() {
49 // full history since 24/02/2020
50 $all_data_link = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json';
51 if ( false === ($dnxcovita_data = get_transient('dnxcovita_data') ) ) {
52 $json_data = file_get_contents($all_data_link);
53 $raw_data = json_decode($json_data, true);
55 foreach ($raw_data as $item => $daily_data) {
56 # $item is the entry number and $daily_data is the array that we want to filter
57 $processed[$item] = dnxcovita_walker($daily_data);
60 set_transient('dnxcovita_data', $processed, DAY_IN_SECONDS
);
65 * Remove all unwanted fields from the data stored in the transient
67 * @link https://danix.xyz
71 function dnxcovita_walker($value) {
72 // array that filters all keys we are not interested in
73 $not_interested = array(
75 'casi_da_sospetto_diagnostico',
80 $result = array_diff_key($value, array_flip($not_interested));
87 * @link https://danix.xyz
91 function dnxcovita_get_all_data() {
92 $data = ( false === get_transient('dnxcovita_data') ) ?
dnxcovita_retrieve_data() : get_transient('dnxcovita_data');
98 * Return the latest data
100 * @link https://danix.xyz
104 function dnxcovita_get_latest_data() {
105 $data = ( false === get_transient('dnxcovita_data') ) ?
dnxcovita_retrieve_data() : get_transient('dnxcovita_data');
106 $rev = array_reverse($data);
113 * Return the data from yesterday
115 * @link https://danix.xyz
119 function dnxcovita_get_previous_data() {
120 $data = ( false === get_transient('dnxcovita_data') ) ?
dnxcovita_retrieve_data() : get_transient('dnxcovita_data');
121 $last = array_slice($data, -2, 1, false);
127 * Add function to widgets_init that'll load our widget.
129 * @link https://danix.xyz
133 add_action( 'widgets_init', 'dnxcovita_register' );
136 * Register our widget.
137 * 'dnxcovita_Widget' is the widget class used below.
139 * @link https://danix.xyz
143 function dnxcovita_register() {
144 register_widget( 'dnxcovita_Widget' );
149 * This class handles everything that needs to be handled with the widget:
150 * the settings, form, display, and update. Nice!
152 * @link https://danix.xyz
156 class dnxcovita_Widget
extends WP_Widget
{
161 public function __construct() {
162 $control_ops = array('width' => 400, 'height' => 350);
164 'dnx-covid-italy', // id_base
165 __('Italian Covid19 Situation', 'dnxcovita' ), // Name
166 array( 'description' => __('Display the current Covid19 situation in Italy.', 'dnxcovita') ),
172 * How to display the widget on the public side of the site.
174 public function widget( $args, $instance ) {
177 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ?
'' : $instance['title'], $instance, $this->id_base
);
179 echo $args['before_widget'];
180 if ( ! empty( $title ) ) {
181 echo $args['before_title'] . $title . $args['after_title'];
183 <div
class="dnxcovita_content">
185 $latest_data = dnxcovita_get_latest_data();
186 $yesterday_data = dnxcovita_get_previous_data(); ?
>
188 <p
class="dnxcovita_desc"><?php
189 $pull_desc = __('data pulled daily from the <a href="%1$s" title="%2$s">official github repository from the Ministry of Health.</a>', 'dnxcovita');
190 $moh_site = 'https://github.com/pcm-dpc/COVID-19';
191 $moh_site_title = 'official github repository from the Ministry of Health report on COVID19 in Italy.';
192 echo sprintf( $pull_desc,
194 esc_html($moh_site_title)
197 <p
class="dnxcovita_latest_push"><?php
198 $expl = __('latest data published on ', 'dnxcovita');
199 $timestring = '<span>%1$s</span><time class="dnxcovita_latest_check" datetime="%2$s">%3$s at %4$s</time>';
200 $w3c_data = $latest_data['data'];
201 $ut = strtotime($w3c_data);
202 $ld = date('d/m/Y', $ut);
203 $lh = date('H:i', $ut);
204 $latest_check = sprintf( $timestring,
215 <?php
foreach ($latest_data as $key => $value) {
216 // we generate the fields first and then populate the output
217 if ( 'data' === $key ) {
218 $latest_date = date('d/m/Y', strtotime($value));
219 $previous_date = date('d/m/Y', strtotime($yesterday_data[0][$key]));
221 // this check is redundant because $value is obtained straight from the json
222 // but still, better check than being sorry
223 if ( is_numeric($value) ) {
224 $latest_content = number_format($value, 0, ',', '˙');
225 $diff = $value - $yesterday_data[0][$key];
226 $diff_plusminus = (is_numeric($diff) && round($diff) > 1) ?
'<span class="wp-exclude-emoji dnxcovita_goingup">↗</span> +' : '<span class="wp-exclude-emoji dnxcovita_goingdown">↘</span> −';
227 $diff_content = $diff_plusminus . number_format($diff, 0, ',', '˙');
229 // Now we have all the values formatted so we can output the definition list
230 if ('data' !== $key) {
231 echo '<dt>' . str_replace('_', ' ', $key) . '</dt>';
232 echo '<dd><span class="dnxcovita_latest_data">' . $latest_content . '</span> <span class="dnxcovita_previous_data">' . $diff_content . '</span></dd>';
236 </div
> <!-- .dnxcovita_content
-->
238 echo $args['after_widget'];
242 * Handles updating settings for the current widget instance.
244 public function update( $new_instance, $old_instance ) {
245 $instance = $old_instance;
246 $instance['title'] = sanitize_text_field( $new_instance['title'] );
252 * Outputs the widget settings form.
254 public function form( $instance ) {
255 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
256 $title = sanitize_text_field( $instance['title'] );
259 <label
for="<?php echo $this->get_field_id('title'); ?>">
260 <?php
_e('Title:', 'dnxcovita'); ?
>
262 <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); ?>" />