72b5b457 |
1 | <?php |
2 | |
82cad5d9 |
3 | defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); |
72b5b457 |
4 | |
5 | /** |
6 | * |
82cad5d9 |
7 | * @link https://danix.xyz |
8 | * @since 0.0.1 |
9 | * @package Dnxcovita |
72b5b457 |
10 | * |
11 | * @wordpress-plugin |
12 | * Plugin Name: danixland-covid-italy |
13 | * Plugin URI: https://danix.xyz |
14 | * Description: Show the latest covid19 stats for italy in a widget |
0f19eaec |
15 | * Version: 1.0 |
72b5b457 |
16 | * Author: Danilo 'danix' M. |
17 | * Author URI: https://danix.xyz |
18 | * License: GPL-2.0+ |
19 | * License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
20 | * Text Domain: dnxcovita |
21 | * Domain Path: /languages |
22 | */ |
23 | |
24 | /** |
25 | * Add plugin i18n domain: dnxcovita |
26 | * |
82cad5d9 |
27 | * @link https://danix.xyz |
28 | * @since 0.0.1 |
29 | * @package Dnxcovita |
72b5b457 |
30 | */ |
82cad5d9 |
31 | load_plugin_textdomain( 'dnxcovita', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
72b5b457 |
32 | |
33 | /** |
34 | * The heart of the plugin, this function retrieves the data from github and |
35 | * stores it in a transient for up to 24h. |
36 | * |
82cad5d9 |
37 | * @link https://danix.xyz |
38 | * @since 0.0.1 |
39 | * @package Dnxcovita |
72b5b457 |
40 | */ |
41 | function dnxcovita_retrieve_data() { |
42 | // full history since 24/02/2020 |
82cad5d9 |
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 ); |
47 | $processed = array(); |
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 ); |
72b5b457 |
51 | } |
52 | |
82cad5d9 |
53 | set_transient( 'dnxcovita_data', $processed, DAY_IN_SECONDS ); |
54 | } |
72b5b457 |
55 | } |
56 | |
57 | /** |
58 | * Remove all unwanted fields from the data stored in the transient |
59 | * |
82cad5d9 |
60 | * @link https://danix.xyz |
61 | * @since 0.0.1 |
62 | * @package Dnxcovita |
72b5b457 |
63 | */ |
82cad5d9 |
64 | function dnxcovita_walker( $value ) { |
72b5b457 |
65 | // array that filters all keys we are not interested in |
66 | $not_interested = array( |
67 | 'stato', |
68 | 'casi_da_sospetto_diagnostico', |
69 | 'casi_da_screening', |
70 | 'casi_testati', |
82cad5d9 |
71 | 'note', |
72b5b457 |
72 | ); |
82cad5d9 |
73 | $result = array_diff_key( $value, array_flip( $not_interested ) ); |
72b5b457 |
74 | return $result; |
75 | } |
76 | |
77 | /** |
78 | * Return all data |
79 | * |
82cad5d9 |
80 | * @link https://danix.xyz |
81 | * @since 0.0.1 |
82 | * @package Dnxcovita |
72b5b457 |
83 | */ |
84 | function dnxcovita_get_all_data() { |
82cad5d9 |
85 | $data = ( false === get_transient( 'dnxcovita_data' ) ) ? dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' ); |
72b5b457 |
86 | |
87 | return $data; |
88 | } |
89 | |
90 | /** |
91 | * Return the latest data |
92 | * |
82cad5d9 |
93 | * @link https://danix.xyz |
94 | * @since 0.0.1 |
95 | * @package Dnxcovita |
72b5b457 |
96 | */ |
97 | function dnxcovita_get_latest_data() { |
82cad5d9 |
98 | $data = ( false === get_transient( 'dnxcovita_data' ) ) ? dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' ); |
99 | $rev = array_reverse( $data ); |
72b5b457 |
100 | $output = $rev[0]; |
101 | |
102 | return $output; |
103 | } |
104 | |
105 | /** |
106 | * Return the data from yesterday |
107 | * |
82cad5d9 |
108 | * @link https://danix.xyz |
109 | * @since 0.0.1 |
110 | * @package Dnxcovita |
72b5b457 |
111 | */ |
112 | function dnxcovita_get_previous_data() { |
82cad5d9 |
113 | $data = ( false === get_transient( 'dnxcovita_data' ) ) ? dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' ); |
114 | $last = array_slice( $data, -2, 1, false ); |
72b5b457 |
115 | |
116 | return $last; |
117 | } |
118 | |
119 | /** |
120 | * Add function to widgets_init that'll load our widget. |
121 | * |
82cad5d9 |
122 | * @link https://danix.xyz |
123 | * @since 0.0.1 |
124 | * @package Dnxcovita |
72b5b457 |
125 | */ |
126 | add_action( 'widgets_init', 'dnxcovita_register' ); |
127 | |
128 | /** |
129 | * Register our widget. |
130 | * 'dnxcovita_Widget' is the widget class used below. |
131 | * |
82cad5d9 |
132 | * @link https://danix.xyz |
133 | * @since 0.0.1 |
134 | * @package Dnxcovita |
72b5b457 |
135 | */ |
136 | function dnxcovita_register() { |
82cad5d9 |
137 | register_widget( 'dnxcovita_Widget' ); |
72b5b457 |
138 | } |
139 | |
140 | /** |
141 | * User_Panel class. |
142 | * This class handles everything that needs to be handled with the widget: |
143 | * the settings, form, display, and update. Nice! |
144 | * |
82cad5d9 |
145 | * @link https://danix.xyz |
146 | * @since 0.0.1 |
147 | * @package Dnxcovita |
72b5b457 |
148 | */ |
149 | class dnxcovita_Widget extends WP_Widget { |
150 | |
72b5b457 |
151 | |
82cad5d9 |
152 | /** |
153 | * Widget setup. |
154 | */ |
155 | public function __construct() { |
156 | $control_ops = array( |
157 | 'width' => 400, |
158 | 'height' => 350, |
159 | ); |
160 | parent::__construct( |
161 | 'dnx-covid-italy', // id_base |
162 | __( 'Italian Covid19 Situation', 'dnxcovita' ), // Name |
163 | array( 'description' => __( 'Display the current Covid19 situation in Italy.', 'dnxcovita' ) ), |
164 | $control_ops |
165 | ); |
166 | } |
72b5b457 |
167 | |
82cad5d9 |
168 | /** |
169 | * How to display the widget on the public side of the site. |
170 | */ |
171 | public function widget( $args, $instance ) { |
172 | extract( $args ); |
72b5b457 |
173 | |
82cad5d9 |
174 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
175 | |
176 | echo $args['before_widget']; |
177 | if ( ! empty( $title ) ) { |
178 | echo $args['before_title'] . $title . $args['after_title']; |
179 | } ?> |
72b5b457 |
180 | <div class="dnxcovita_content"> |
82cad5d9 |
181 | <?php |
182 | $latest_data = dnxcovita_get_latest_data(); |
183 | $yesterday_data = dnxcovita_get_previous_data(); |
184 | ?> |
72b5b457 |
185 | |
82cad5d9 |
186 | <p class="dnxcovita_desc"> |
187 | <?php |
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' ); |
72b5b457 |
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.'; |
82cad5d9 |
191 | echo sprintf( |
192 | $pull_desc, |
193 | esc_html( $moh_site ), |
194 | esc_html( $moh_site_title ) |
72b5b457 |
195 | ); |
82cad5d9 |
196 | ?> |
197 | </p> |
198 | <p class="dnxcovita_latest_push"> |
199 | <?php |
200 | $expl = __( 'latest data published on ', 'dnxcovita' ); |
72b5b457 |
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']; |
82cad5d9 |
203 | $ut = strtotime( $w3c_data ); |
204 | $ld = date( 'd/m/Y', $ut ); |
205 | $lh = date( 'H:i', $ut ); |
206 | $latest_check = sprintf( |
207 | $timestring, |
208 | esc_html( $expl ), |
209 | esc_attr( $w3c_data ), |
210 | esc_html( $ld ), |
211 | esc_html( $lh ) |
72b5b457 |
212 | ); |
82cad5d9 |
213 | echo $latest_check; |
214 | ?> |
215 | </p> |
72b5b457 |
216 | |
217 | <dl> |
218 | |
82cad5d9 |
219 | <?php |
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 ] ) ); |
225 | } |
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, ',', '˙' ); |
233 | } |
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>'; |
238 | } |
72b5b457 |
239 | } |
82cad5d9 |
240 | ?> |
72b5b457 |
241 | </dl> |
242 | </div> <!-- .dnxcovita_content --> |
82cad5d9 |
243 | <?php |
244 | echo $args['after_widget']; |
245 | } |
72b5b457 |
246 | |
82cad5d9 |
247 | /** |
248 | * Handles updating settings for the current widget instance. |
249 | */ |
250 | public function update( $new_instance, $old_instance ) { |
251 | $instance = $old_instance; |
252 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
72b5b457 |
253 | |
82cad5d9 |
254 | return $instance; |
255 | } |
72b5b457 |
256 | |
82cad5d9 |
257 | /** |
258 | * Outputs the widget settings form. |
259 | */ |
260 | public function form( $instance ) { |
261 | $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
262 | $title = sanitize_text_field( $instance['title'] ); |
263 | ?> |
264 | <p> |
265 | <label for="<?php echo $this->get_field_id( 'title' ); ?>"> |
266 | <?php _e( 'Title:', 'dnxcovita' ); ?> |
267 | </label> |
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 ); ?>" /> |
269 | </p> |
270 | <?php |
271 | } |
72b5b457 |
272 | } |