diff options
| author | danix <danix@danix.xyz> | 2020-10-22 14:24:02 +0200 |
|---|---|---|
| committer | danix <danix@danix.xyz> | 2020-10-22 14:24:02 +0200 |
| commit | 72b5b457ac6d78e76548e8a736d1673c390aca9e (patch) | |
| tree | 5b24ead0a896b9bf7b17ce9150a11990382ab40c /danixland-covid-italy.php | |
| download | danixland-covid-italy-72b5b457ac6d78e76548e8a736d1673c390aca9e.tar.gz danixland-covid-italy-72b5b457ac6d78e76548e8a736d1673c390aca9e.zip | |
Initial commit. The plugin is working as expected.release_22102020-1424
nuovo file: .gitignore
nuovo file: LICENSE.txt
nuovo file: README.txt
nuovo file: danixland-covid-italy.php
nuovo file: index.php
nuovo file: languages/dnxcovita.pot
nuovo file: uninstall.php
Diffstat (limited to 'danixland-covid-italy.php')
| -rw-r--r-- | danixland-covid-italy.php | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/danixland-covid-italy.php b/danixland-covid-italy.php new file mode 100644 index 0000000..c871541 --- /dev/null +++ b/danixland-covid-italy.php @@ -0,0 +1,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: 0.0.1 + * 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, ',', '˙'); + $diff = $value - $yesterday_data[0][$key]; + $diff_plusminus = (is_numeric($diff) && round($diff) > 1) ? '<span class="dnxcovita_goingup">↗</span> +' : '<span class="dnxcovita_goingdown">↘</span> −'; + $diff_content = $diff_plusminus . number_format($diff, 0, ',', '˙'); + } + // 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 + } +} |
