| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) or 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 | // 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' ); |
| 35 | } |
| 36 | if ( ! defined( 'WP_PLUGIN_URL' ) ) define( 'WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins' ); |
| 37 | |
| 38 | load_plugin_textdomain('dnxcovita', WP_PLUGIN_URL . '/danixland-covid-italy/languages/', 'danixland-covid-italy/languages/'); |
| 39 | |
| 40 | /** |
| 41 | * The heart of the plugin, this function retrieves the data from github and |
| 42 | * stores it in a transient for up to 24h. |
| 43 | * |
| 44 | * @link https://danix.xyz |
| 45 | * @since 0.0.1 |
| 46 | * @package Dnxcovita |
| 47 | */ |
| 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); |
| 54 | $processed = []; |
| 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); |
| 58 | } |
| 59 | |
| 60 | set_transient('dnxcovita_data', $processed, DAY_IN_SECONDS); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Remove all unwanted fields from the data stored in the transient |
| 66 | * |
| 67 | * @link https://danix.xyz |
| 68 | * @since 0.0.1 |
| 69 | * @package Dnxcovita |
| 70 | */ |
| 71 | function dnxcovita_walker($value) { |
| 72 | // array that filters all keys we are not interested in |
| 73 | $not_interested = array( |
| 74 | 'stato', |
| 75 | 'casi_da_sospetto_diagnostico', |
| 76 | 'casi_da_screening', |
| 77 | 'casi_testati', |
| 78 | 'note' |
| 79 | ); |
| 80 | $result = array_diff_key($value, array_flip($not_interested)); |
| 81 | return $result; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Return all data |
| 86 | * |
| 87 | * @link https://danix.xyz |
| 88 | * @since 0.0.1 |
| 89 | * @package Dnxcovita |
| 90 | */ |
| 91 | function dnxcovita_get_all_data() { |
| 92 | $data = ( false === get_transient('dnxcovita_data') ) ? dnxcovita_retrieve_data() : get_transient('dnxcovita_data'); |
| 93 | |
| 94 | return $data; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Return the latest data |
| 99 | * |
| 100 | * @link https://danix.xyz |
| 101 | * @since 0.0.1 |
| 102 | * @package Dnxcovita |
| 103 | */ |
| 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); |
| 107 | $output = $rev[0]; |
| 108 | |
| 109 | return $output; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Return the data from yesterday |
| 114 | * |
| 115 | * @link https://danix.xyz |
| 116 | * @since 0.0.1 |
| 117 | * @package Dnxcovita |
| 118 | */ |
| 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); |
| 122 | |
| 123 | return $last; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Add function to widgets_init that'll load our widget. |
| 128 | * |
| 129 | * @link https://danix.xyz |
| 130 | * @since 0.0.1 |
| 131 | * @package Dnxcovita |
| 132 | */ |
| 133 | add_action( 'widgets_init', 'dnxcovita_register' ); |
| 134 | |
| 135 | /** |
| 136 | * Register our widget. |
| 137 | * 'dnxcovita_Widget' is the widget class used below. |
| 138 | * |
| 139 | * @link https://danix.xyz |
| 140 | * @since 0.0.1 |
| 141 | * @package Dnxcovita |
| 142 | */ |
| 143 | function dnxcovita_register() { |
| 144 | register_widget( 'dnxcovita_Widget' ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * User_Panel class. |
| 149 | * This class handles everything that needs to be handled with the widget: |
| 150 | * the settings, form, display, and update. Nice! |
| 151 | * |
| 152 | * @link https://danix.xyz |
| 153 | * @since 0.0.1 |
| 154 | * @package Dnxcovita |
| 155 | */ |
| 156 | class dnxcovita_Widget extends WP_Widget { |
| 157 | |
| 158 | /** |
| 159 | * Widget setup. |
| 160 | */ |
| 161 | public function __construct() { |
| 162 | $control_ops = array('width' => 400, 'height' => 350); |
| 163 | parent::__construct( |
| 164 | 'dnx-covid-italy', // id_base |
| 165 | __('Italian Covid19 Situation', 'dnxcovita' ), // Name |
| 166 | array( 'description' => __('Display the current Covid19 situation in Italy.', 'dnxcovita') ), |
| 167 | $control_ops |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * How to display the widget on the public side of the site. |
| 173 | */ |
| 174 | public function widget( $args, $instance ) { |
| 175 | extract( $args ); |
| 176 | |
| 177 | $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
| 178 | |
| 179 | echo $args['before_widget']; |
| 180 | if ( ! empty( $title ) ) { |
| 181 | echo $args['before_title'] . $title . $args['after_title']; |
| 182 | } ?> |
| 183 | <div class="dnxcovita_content"> |
| 184 | <?php |
| 185 | $latest_data = dnxcovita_get_latest_data(); |
| 186 | $yesterday_data = dnxcovita_get_previous_data(); ?> |
| 187 | |
| 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, |
| 193 | esc_html($moh_site), |
| 194 | esc_html($moh_site_title) |
| 195 | ); |
| 196 | ?></p> |
| 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, |
| 205 | esc_html($expl), |
| 206 | esc_attr($w3c_data), |
| 207 | esc_html($ld), |
| 208 | esc_html($lh) |
| 209 | ); |
| 210 | echo $latest_check; |
| 211 | ?></p> |
| 212 | |
| 213 | <dl> |
| 214 | |
| 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])); |
| 220 | } |
| 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="dnxcovita_goingup">↗</span> +' : '<span class="dnxcovita_goingdown">↘</span> −'; |
| 227 | $diff_content = $diff_plusminus . number_format($diff, 0, ',', '˙'); |
| 228 | } |
| 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>'; |
| 233 | } |
| 234 | } ?> |
| 235 | </dl> |
| 236 | </div> <!-- .dnxcovita_content --> |
| 237 | <?php |
| 238 | echo $args['after_widget']; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Handles updating settings for the current widget instance. |
| 243 | */ |
| 244 | public function update( $new_instance, $old_instance ) { |
| 245 | $instance = $old_instance; |
| 246 | $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 247 | |
| 248 | return $instance; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Outputs the widget settings form. |
| 253 | */ |
| 254 | public function form( $instance ) { |
| 255 | $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
| 256 | $title = sanitize_text_field( $instance['title'] ); |
| 257 | ?> |
| 258 | <p> |
| 259 | <label for="<?php echo $this->get_field_id('title'); ?>"> |
| 260 | <?php _e('Title:', 'dnxcovita'); ?> |
| 261 | </label> |
| 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); ?>" /> |
| 263 | </p> |
| 264 | <?php |
| 265 | } |
| 266 | } |