| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) || die( 'No script kiddies please!' ); |
| 4 | |
| 5 | /** |
| 6 | * |
| 7 | * @link https://danix.xyz |
| 8 | * @since 0.0.1 |
| 9 | * @package Dnxcovita |
| 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 |
| 15 | * Version: 1.0 |
| 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 | * |
| 27 | * @link https://danix.xyz |
| 28 | * @since 0.0.1 |
| 29 | * @package Dnxcovita |
| 30 | */ |
| 31 | load_plugin_textdomain( 'dnxcovita', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
| 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 | * |
| 37 | * @link https://danix.xyz |
| 38 | * @since 0.0.1 |
| 39 | * @package Dnxcovita |
| 40 | */ |
| 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 ); |
| 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 ); |
| 51 | } |
| 52 | |
| 53 | set_transient( 'dnxcovita_data', $processed, DAY_IN_SECONDS ); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Remove all unwanted fields from the data stored in the transient |
| 59 | * |
| 60 | * @link https://danix.xyz |
| 61 | * @since 0.0.1 |
| 62 | * @package Dnxcovita |
| 63 | */ |
| 64 | function dnxcovita_walker( $value ) { |
| 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', |
| 71 | 'note', |
| 72 | ); |
| 73 | $result = array_diff_key( $value, array_flip( $not_interested ) ); |
| 74 | return $result; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Return all data |
| 79 | * |
| 80 | * @link https://danix.xyz |
| 81 | * @since 0.0.1 |
| 82 | * @package Dnxcovita |
| 83 | */ |
| 84 | function dnxcovita_get_all_data() { |
| 85 | $data = ( false === get_transient( 'dnxcovita_data' ) ) ? dnxcovita_retrieve_data() : get_transient( 'dnxcovita_data' ); |
| 86 | |
| 87 | return $data; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Return the latest data |
| 92 | * |
| 93 | * @link https://danix.xyz |
| 94 | * @since 0.0.1 |
| 95 | * @package Dnxcovita |
| 96 | */ |
| 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 ); |
| 100 | $output = $rev[0]; |
| 101 | |
| 102 | return $output; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Return the data from yesterday |
| 107 | * |
| 108 | * @link https://danix.xyz |
| 109 | * @since 0.0.1 |
| 110 | * @package Dnxcovita |
| 111 | */ |
| 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 ); |
| 115 | |
| 116 | return $last; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add function to widgets_init that'll load our widget. |
| 121 | * |
| 122 | * @link https://danix.xyz |
| 123 | * @since 0.0.1 |
| 124 | * @package Dnxcovita |
| 125 | */ |
| 126 | add_action( 'widgets_init', 'dnxcovita_register' ); |
| 127 | |
| 128 | /** |
| 129 | * Register our widget. |
| 130 | * 'dnxcovita_Widget' is the widget class used below. |
| 131 | * |
| 132 | * @link https://danix.xyz |
| 133 | * @since 0.0.1 |
| 134 | * @package Dnxcovita |
| 135 | */ |
| 136 | function dnxcovita_register() { |
| 137 | register_widget( 'dnxcovita_Widget' ); |
| 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 | * |
| 145 | * @link https://danix.xyz |
| 146 | * @since 0.0.1 |
| 147 | * @package Dnxcovita |
| 148 | */ |
| 149 | class dnxcovita_Widget extends WP_Widget { |
| 150 | |
| 151 | |
| 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 | } |
| 167 | |
| 168 | /** |
| 169 | * How to display the widget on the public side of the site. |
| 170 | */ |
| 171 | public function widget( $args, $instance ) { |
| 172 | extract( $args ); |
| 173 | |
| 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 | } ?> |
| 180 | <div class="dnxcovita_content"> |
| 181 | <?php |
| 182 | $latest_data = dnxcovita_get_latest_data(); |
| 183 | $yesterday_data = dnxcovita_get_previous_data(); |
| 184 | ?> |
| 185 | |
| 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' ); |
| 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.'; |
| 191 | echo sprintf( |
| 192 | $pull_desc, |
| 193 | esc_html( $moh_site ), |
| 194 | esc_html( $moh_site_title ) |
| 195 | ); |
| 196 | ?> |
| 197 | </p> |
| 198 | <p class="dnxcovita_latest_push"> |
| 199 | <?php |
| 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( |
| 207 | $timestring, |
| 208 | esc_html( $expl ), |
| 209 | esc_attr( $w3c_data ), |
| 210 | esc_html( $ld ), |
| 211 | esc_html( $lh ) |
| 212 | ); |
| 213 | echo $latest_check; |
| 214 | ?> |
| 215 | </p> |
| 216 | |
| 217 | <dl> |
| 218 | |
| 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 | } |
| 239 | } |
| 240 | ?> |
| 241 | </dl> |
| 242 | </div> <!-- .dnxcovita_content --> |
| 243 | <?php |
| 244 | echo $args['after_widget']; |
| 245 | } |
| 246 | |
| 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'] ); |
| 253 | |
| 254 | return $instance; |
| 255 | } |
| 256 | |
| 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 | } |
| 272 | } |