summaryrefslogtreecommitdiffstats
path: root/danixland-covid-italy.php
blob: a4e2bac57fd4bae650872efa43a68fe210eec724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/**
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 *
 * @wordpress-plugin
 * Plugin Name:       danixland-covid-italy
 * Plugin URI:        https://danix.xyz
 * Description:       Show the latest covid19 stats for italy in a widget
 * Version:           1.0
 * Author:            Danilo 'danix' M.
 * Author URI:        https://danix.xyz
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       dnxcovita
 * Domain Path:       /languages
 */

/**
 * Add plugin i18n domain: dnxcovita
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
// 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('dnxcovita', WP_PLUGIN_URL . '/danixland-covid-italy/languages/', 'danixland-covid-italy/languages/');

/**
 * The heart of the plugin, this function retrieves the data from github and
 * stores it in a transient for up to 24h.
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_retrieve_data() {
	// full history since 24/02/2020
    $all_data_link = 'https://raw.githubusercontent.com/pcm-dpc/COVID-19/master/dati-json/dpc-covid19-ita-andamento-nazionale.json';
    if ( false === ($dnxcovita_data = get_transient('dnxcovita_data') ) ) {
		$json_data = file_get_contents($all_data_link);
		$raw_data = json_decode($json_data, true);
		$processed = [];
		foreach ($raw_data as $item => $daily_data) {
			# $item is the entry number and $daily_data is the array that we want to filter
			$processed[$item] = dnxcovita_walker($daily_data);
		}

		set_transient('dnxcovita_data', $processed, DAY_IN_SECONDS);
    }
}

/**
 * Remove all unwanted fields from the data stored in the transient
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_walker($value) {
	// array that filters all keys we are not interested in
	$not_interested = array(
		'stato',
		'casi_da_sospetto_diagnostico',
		'casi_da_screening',
		'casi_testati',
		'note'
	);
	$result = array_diff_key($value, array_flip($not_interested));
	return $result;
}

/**
 * Return all data
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_get_all_data() {
	$data = ( false === get_transient('dnxcovita_data') ) ? dnxcovita_retrieve_data() : get_transient('dnxcovita_data');

	return $data;
}

/**
 * Return the latest data
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_get_latest_data() {
	$data = ( false === get_transient('dnxcovita_data') ) ? dnxcovita_retrieve_data() : get_transient('dnxcovita_data');
	$rev = array_reverse($data);
	$output = $rev[0];

	return $output;
}

/**
 * Return the data from yesterday
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_get_previous_data() {
	$data = ( false === get_transient('dnxcovita_data') ) ? dnxcovita_retrieve_data() : get_transient('dnxcovita_data');
	$last = array_slice($data, -2, 1, false);

	return $last;
}

/**
 * Add function to widgets_init that'll load our widget.
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
add_action( 'widgets_init', 'dnxcovita_register' );

/**
 * Register our widget.
 * 'dnxcovita_Widget' is the widget class used below.
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
function dnxcovita_register() {
    register_widget( 'dnxcovita_Widget' );
}

/**
 * User_Panel class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @link              https://danix.xyz
 * @since             0.0.1
 * @package           Dnxcovita
 */
class dnxcovita_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    public function __construct() {
        $control_ops = array('width' => 400, 'height' => 350);
        parent::__construct(
            'dnx-covid-italy', // id_base
            __('Italian Covid19 Situation', 'dnxcovita' ), // Name
            array( 'description' => __('Display the current Covid19 situation in Italy.', 'dnxcovita') ),
            $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'];
        } ?>
<div class="dnxcovita_content">
	<?php
	$latest_data = dnxcovita_get_latest_data();
	$yesterday_data = dnxcovita_get_previous_data(); ?>

	<p class="dnxcovita_desc"><?php
		$pull_desc = __('data pulled daily from the <a href="%1$s" title="%2$s">official github repository from the Ministry of Health.</a>', 'dnxcovita');
		$moh_site = 'https://github.com/pcm-dpc/COVID-19';
		$moh_site_title = 'official github repository from the Ministry of Health report on COVID19 in Italy.';
		echo sprintf( $pull_desc,
			esc_html($moh_site),
			esc_html($moh_site_title)
		);
	?></p>
	<p class="dnxcovita_latest_push"><?php
		$expl = __('latest data published on ', 'dnxcovita');
		$timestring = '<span>%1$s</span><time class="dnxcovita_latest_check" datetime="%2$s">%3$s at %4$s</time>';
		$w3c_data = $latest_data['data'];
		$ut = strtotime($w3c_data);
		$ld = date('d/m/Y', $ut);
		$lh = date('H:i', $ut);
		$latest_check = sprintf( $timestring,
			esc_html($expl),
			esc_attr($w3c_data),
			esc_html($ld),
			esc_html($lh)
		);
		echo $latest_check;
	?></p>

	<dl>

	<?php foreach ($latest_data as $key => $value) {
		// we generate the fields first and then populate the output
		if ( 'data' === $key ) {
			$latest_date = date('d/m/Y', strtotime($value));
			$previous_date = date('d/m/Y', strtotime($yesterday_data[0][$key]));
		}
		// this check is redundant because $value is obtained straight from the json
		// but still, better check than being sorry
		if ( is_numeric($value) ) {
			$latest_content = number_format($value, 0, ',', '&dot;');
			$diff = $value - $yesterday_data[0][$key];
			$diff_plusminus = (is_numeric($diff) && round($diff) > 1) ? '<span class="wp-exclude-emoji dnxcovita_goingup">&nearr;</span> &plus;' : '<span class="wp-exclude-emoji dnxcovita_goingdown">&searr;</span> &minus;';
			$diff_content = $diff_plusminus . number_format($diff, 0, ',', '&dot;');
		}
		// Now we have all the values formatted so we can output the definition list
		if ('data' !== $key) {
			echo '<dt>' . str_replace('_', ' ', $key) . '</dt>';
			echo '<dd><span class="dnxcovita_latest_data">' . $latest_content . '</span> <span class="dnxcovita_previous_data">' . $diff_content . '</span></dd>';
		}
	} ?>
	</dl>
</div> <!-- .dnxcovita_content -->
        <?php
        echo $args['after_widget'];
    }

    /**
     * Handles updating settings for the current widget instance.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field( $new_instance['title'] );

        return $instance;
    }

    /**
     * Outputs the widget settings form.
     */
    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
        $title = sanitize_text_field( $instance['title'] );
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
            	<?php _e('Title:', 'dnxcovita'); ?>
        	</label>
        	<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>
        <?php
    }
}