<?php
-defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
+defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
/**
*
- * @link https://danix.xyz
- * @since 0.0.1
- * @package Dnxcovita
+ * @link https://danix.xyz
+ * @since 0.0.1
+ * @package Dnxcovita
*
* @wordpress-plugin
* Plugin Name: danixland-covid-italy
/**
* Add plugin i18n domain: dnxcovita
*
- * @link https://danix.xyz
- * @since 0.0.1
- * @package 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/');
+load_plugin_textdomain( 'dnxcovita', false, dirname( plugin_basename( __FILE__ ) ) . '/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
+ * @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);
+ $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 = array();
+ 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);
- }
+ 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
+ * @link https://danix.xyz
+ * @since 0.0.1
+ * @package Dnxcovita
*/
-function dnxcovita_walker($value) {
+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'
+ 'note',
);
- $result = array_diff_key($value, array_flip($not_interested));
+ $result = array_diff_key( $value, array_flip( $not_interested ) );
return $result;
}
/**
* Return all data
*
- * @link https://danix.xyz
- * @since 0.0.1
- * @package Dnxcovita
+ * @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');
+ $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
+ * @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);
+ $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
+ * @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);
+ $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
+ * @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
+ * @link https://danix.xyz
+ * @since 0.0.1
+ * @package Dnxcovita
*/
function dnxcovita_register() {
- register_widget( 'dnxcovita_Widget' );
+ register_widget( 'dnxcovita_Widget' );
}
/**
* 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
+ * @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 );
+ /**
+ * 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
+ );
+ }
- $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
+ /**
+ * How to display the widget on the public side of the site.
+ */
+ public function widget( $args, $instance ) {
+ extract( $args );
- echo $args['before_widget'];
- if ( ! empty( $title ) ) {
- echo $args['before_title'] . $title . $args['after_title'];
- } ?>
+ $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(); ?>
+ <?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');
+ <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)
+ 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');
+ ?>
+ </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)
+ $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>
+ 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="wp-exclude-emoji dnxcovita_goingup">↗</span> +' : '<span class="wp-exclude-emoji 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>';
+ <?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="wp-exclude-emoji dnxcovita_goingup">↗</span> +' : '<span class="wp-exclude-emoji 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'];
- }
+ <?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'] );
+ /**
+ * 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;
- }
+ 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
- }
+ /**
+ * 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
+ }
}