diff options
| -rw-r--r-- | danixland-covid-italy.php | 298 | ||||
| -rw-r--r-- | index.php | 2 | ||||
| -rw-r--r-- | phpcs.xml | 31 | ||||
| -rw-r--r-- | uninstall.php | 6 |
4 files changed, 187 insertions, 150 deletions
diff --git a/danixland-covid-italy.php b/danixland-covid-italy.php index a4e2bac..f335ac9 100644 --- a/danixland-covid-italy.php +++ b/danixland-covid-italy.php @@ -1,12 +1,12 @@ <?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 @@ -24,72 +24,65 @@ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); /** * 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; } @@ -97,13 +90,13 @@ function dnxcovita_get_all_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; @@ -112,13 +105,13 @@ function dnxcovita_get_latest_data() { /** * 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; } @@ -126,9 +119,9 @@ function dnxcovita_get_previous_data() { /** * 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' ); @@ -136,12 +129,12 @@ 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' ); } /** @@ -149,118 +142,131 @@ function dnxcovita_register() { * 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 + } } @@ -1 +1 @@ -<?php // Silence is golden
\ No newline at end of file +<?php // Silence is golden diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 0000000..9e19d6e --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,31 @@ +<?xml version="1.0"?> +<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="dagreynix" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd"> + <description>danix Wordpress Coding Standards</description> + + <!-- Scan all files in directory --> + <file>.</file> + + <!-- Scan only PHP files --> + <arg name="extensions" value="php"/> + + <!-- Show colors in console --> + <arg value="-colors"/> + + <!-- Show sniff codes in all reports --> + <arg value="ns"/> + + <!-- Include the WordPress-Extra standard. --> + <rule ref="WordPress-Extra"> + <!-- Exclude any rules here --> + <exclude name="WordPress.PHP.DisallowShortTernary"/> + </rule> + + <!-- Let's also check that everything is properly documented. --> + <!-- <rule ref="WordPress-Docs"/> --> + + <!-- Add in some extra rules from other standards. --> + <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/> + <rule ref="Generic.Commenting.Todo"/> + + <config name="minimum_supported_wp_version" value="4.9"/> +</ruleset>
\ No newline at end of file diff --git a/uninstall.php b/uninstall.php index db3ef81..77db867 100644 --- a/uninstall.php +++ b/uninstall.php @@ -19,10 +19,10 @@ * For more information, see the following discussion: * https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate/pull/123#issuecomment-28541913 * - * @link https://danix.xyz - * @since 0.0.1 + * @link https://danix.xyz + * @since 0.0.1 * - * @package Dnxcovita + * @package Dnxcovita */ // If uninstall not called from WordPress, then exit. |
