Widget El tiempo

<?php
/*
  Plugin Name: El Tiempo
  Plugin URI: http://intelisen.com/plugins
  Description: Un plugin para ver el tiempo
  Author: Intelisen
  Version: 1
  Author URI: http://intelisen.com
 */

class EltiempoWidget extends WP_Widget {

    function EltiempoWidget() {
        $widget_ops = array('classname' => 'EltiempoWidget', 'description' => 'Muestra el tiempo');
        $this->WP_Widget('EltiempoWidget', 'Muestra el tiempo', $widget_ops);
    }

    function form($instance) {
        $instance = wp_parse_args((array) $instance, array('title' => ''));
        $title = $instance['title'];
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?= _e('Title') ?>: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
        <?php
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
    }

    function widget($args, $instance) {
        extract($args, EXTR_SKIP);

        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

        if (!empty($title))
            echo $before_title . $title . $after_title;

        // CóDIGO DEL WIDGET
        // Llamo a la api 'ip', le paso la ip que quiero buscar y me devuelve un json
        // con los datos de la localización (http://ip-api.com/docs/api:json#usage)
        //$a = json_decode(file_get_contents('http://ip-api.com/json/'.$_SERVER['REMOTE_ADDR']));
        $a = json_decode(file_get_contents('http://ip-api.com/json/'));
        if (!empty($a)) {
            $ciudad = $a->city;
            //Obtengo el tiempo a partir de una ciudad. Necesito una clave
            //Le paso la ciudad y la clave (https://openweathermap.org/current)
            $b = json_decode(file_get_contents('https://api.openweathermap.org/data/2.5/weather?q=' . $a->city . '&appid=db13c3a29d4018cca5e6ef261442cc64'));
            if (!empty($b)) {
                echo "<p>Máxima:" . ($b->main->temp_max - 273) . "</p>";
                echo "<p>Mínima:" . ($b->main->temp_min - 273) . "</p>";
            }
        }
        //FIN DEL CÓDIGO WIDGET
        echo $after_widget;
    }

}

add_action('widgets_init', create_function('', 'return register_widget("EltiempoWidget");'));
?>

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos