3 defined( 'ABSPATH' ) ||
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 load_plugin_textdomain( 'dnxcovita', false, dirname( plugin_basename( __FILE__
) ) . '/languages' );
34 * The heart of the plugin, this function retrieves the data from github and
35 * stores it in a transient for up to 24h.
37 * @link https://danix.xyz
41 function dnxcovita_retrieve_data() {
42 // full history since 24/02/2020
43 $all_data_link = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json';
44 if ( false === ( $dnxcovita_data = get_transient( 'dnxcovita_data' ) ) ) {
45 $json_data = file_get_contents( $all_data_link );
46 $raw_data = json_decode( $json_data, true );
48 foreach ( $raw_data as $item => $daily_data ) {
49 // $item is the entry number and $daily_data is the array that we want to filter
50 $processed[ $item ] = dnxcovita_walker( $daily_data );
53 set_transient( 'dnxcovita_data', $processed, DAY_IN_SECONDS
);
58 * Remove all unwanted fields from the data stored in the transient
60 * @link https://danix.xyz
64 function dnxcovita_walker( $value ) {
65 // array that filters all keys we are not interested in
66 $not_interested = array(
68 'casi_da_sospetto_diagnostico',
73 $result = array_diff_key( $value, array_flip( $not_interested ) );
80 * @link https://danix.xyz
84 function dnxcovita_get_all_data() {
85 $data = ( false === get_transient( 'dnxcovita_data' ) ) ?
dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' );
91 * Return the latest data
93 * @link https://danix.xyz
97 function dnxcovita_get_latest_data() {
98 $data = ( false === get_transient( 'dnxcovita_data' ) ) ?
dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' );
99 $rev = array_reverse( $data );
106 * Return the data from yesterday
108 * @link https://danix.xyz
112 function dnxcovita_get_previous_data() {
113 $data = ( false === get_transient( 'dnxcovita_data' ) ) ?
dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' );
114 $last = array_slice( $data, -2, 1, false );
120 * Add function to widgets_init that'll load our widget.
122 * @link https://danix.xyz
126 add_action( 'widgets_init', 'dnxcovita_register' );
129 * Register our widget.
130 * 'dnxcovita_Widget' is the widget class used below.
132 * @link https://danix.xyz
136 function dnxcovita_register() {
137 register_widget( 'dnxcovita_Widget' );
142 * This class handles everything that needs to be handled with the widget:
143 * the settings, form, display, and update. Nice!
145 * @link https://danix.xyz
149 class dnxcovita_Widget
extends WP_Widget
{
155 public function __construct() {
156 $control_ops = array(
161 'dnx-covid-italy', // id_base
162 __( 'Italian Covid19 Situation', 'dnxcovita' ), // Name
163 array( 'description' => __( 'Display the current Covid19 situation in Italy.', 'dnxcovita' ) ),
169 * How to display the widget on the public side of the site.
171 public function widget( $args, $instance ) {
174 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ?
'' : $instance['title'], $instance, $this->id_base
);
176 echo $args['before_widget'];
177 if ( ! empty( $title ) ) {
178 echo $args['before_title'] . $title . $args['after_title'];
180 <div
class="dnxcovita_content">
182 $latest_data = dnxcovita_get_latest_data();
183 $yesterday_data = dnxcovita_get_previous_data();
186 <p
class="dnxcovita_desc">
188 $pull_desc = __( 'data pulled daily from the <a href="%1$s" title="%2$s">official github repository from the Ministry of Health.</a>', 'dnxcovita' );
189 $moh_site = 'https://github.com/pcm-dpc/COVID-19';
190 $moh_site_title = 'official github repository from the Ministry of Health report on COVID19 in Italy.';
193 esc_html( $moh_site ),
194 esc_html( $moh_site_title )
198 <p
class="dnxcovita_latest_push">
200 $expl = __( 'latest data published on ', 'dnxcovita' );
201 $timestring = '<span>%1$s</span><time class="dnxcovita_latest_check" datetime="%2$s">%3$s at %4$s</time>';
202 $w3c_data = $latest_data['data'];
203 $ut = strtotime( $w3c_data );
204 $ld = date( 'd/m/Y', $ut );
205 $lh = date( 'H:i', $ut );
206 $latest_check = sprintf(
209 esc_attr( $w3c_data ),
220 foreach ( $latest_data as $key => $value ) {
221 // we generate the fields first and then populate the output
222 if ( 'data' === $key ) {
223 $latest_date = date( 'd/m/Y', strtotime( $value ) );
224 $previous_date = date( 'd/m/Y', strtotime( $yesterday_data[0][ $key ] ) );
226 // this check is redundant because $value is obtained straight from the json
227 // but still, better check than being sorry
228 if ( is_numeric( $value ) ) {
229 $latest_content = number_format( $value, 0, ',', '˙' );
230 $diff = $value - $yesterday_data[0][ $key ];
231 $diff_plusminus = ( is_numeric( $diff ) && round( $diff ) > 1 ) ?
'<span class="wp-exclude-emoji dnxcovita_goingup">↗</span> +' : '<span class="wp-exclude-emoji dnxcovita_goingdown">↘</span>';
232 $diff_content = $diff_plusminus . number_format( $diff, 0, ',', '˙' );
234 // Now we have all the values formatted so we can output the definition list
235 if ( 'data' !== $key ) {
236 echo '<dt>' . str_replace( '_', ' ', $key ) . '</dt>';
237 echo '<dd><span class="dnxcovita_latest_data">' . $latest_content . '</span> <span class="dnxcovita_previous_data">' . $diff_content . '</span></dd>';
242 </div
> <!-- .dnxcovita_content
-->
244 echo $args['after_widget'];
248 * Handles updating settings for the current widget instance.
250 public function update( $new_instance, $old_instance ) {
251 $instance = $old_instance;
252 $instance['title'] = sanitize_text_field( $new_instance['title'] );
258 * Outputs the widget settings form.
260 public function form( $instance ) {
261 $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
262 $title = sanitize_text_field( $instance['title'] );
265 <label
for="<?php echo $this->get_field_id( 'title' ); ?>">
266 <?php
_e( 'Title:', 'dnxcovita' ); ?
>
268 <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 ); ?>" />