[Linuxtrent] Re: R: Re: Integrare Meteo Trentino in Wordpress

  • From: luca capra <luca.capra@xxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxx
  • Date: Wed, 03 Aug 2011 22:12:31 +0200

On 03/08/2011 15:44, Maurizio Napolitano wrote:
Dunque,
come già detto da altri NON ho preso il feed RSS perché oltre ad avere minori 
informazioni è anche davvero poco piacevole da vedere...
Detto questo ringrazio moltissimo tutti voi e in particolare Luca per il 
plugin, ma purtroppo sembra NON funzionare.
Ho creato all'interno della directory "plugins" una sottodirectory "meteotn" salvando lo 
script come "meteotn.php".
Ho quindi
  creato una nuova pagina in cui ho scritto [meteo-tn] però sembra che il 
plugin NON venga nemmeno richiamato.

Qualche idea?
Come gia' detto:
- scarica il file .xslt che ti ho segnalato
- usa il secondo esempio che trovi qui
http://www.php.net/manual/en/xsl.examples.php

Un esempietto al volo con la bash
wget http://www.meteotrentino.it/bollettini/today/generale_it.xml
wget 
https://raw.github.com/Morail/taolin/master/webroot/xslt/meteotrentino-it.xslt
xsltproc meteotrentino-it.xslt generale_it.xml>  meteo.html

... e in meteo.html trovi l'xml convertito in html :)
Un po' di css e lo hai sistemato
Ciao,
riposto il codice, l'ho provato con l'ultimo Wordpress.
Ho aggiunto anche il riferimento di Maurizio perchè è ottimo, lo richiami con [meteo-tn-full].
(Devi salvare il file xslt nella cartella del plugin, prima)

Mandami una copia se lo migliori, grazie.
Luca

<?php
/*
Plugin Name: meteo tn
Plugin URI:
Description: add [meteo-tn] substitution token to page content
Author:
Version: 0.2
Author URI:
*/

/*  This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/

define('METEOTN_XML_BOLLETTINO', 
'http://www.meteotrentino.it/bollettini/today/generale_it');
define('METEOTN_XSLT', 
'https://raw.github.com/Morail/taolin/master/webroot/xslt/meteotrentino-it.xslt');

if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die(); }

function get_meteotn($url = null)
{

  if(is_null($url))
    $url = constant('METEOTN_XML_BOLLETTINO');

  $output='';

  try{

    $xmlstr = file_get_contents($url.'.xml');
    $xmlstr = _convert_to_utf8($xmlstr, 'UTF-16*');
    $xmlstr = trim($xmlstr, "\r\n");
    $xml = simplexml_load_string( $xmlstr );
    $img = "<img class='meteo-foto-oggi' src='" . (string)$xml->Oggi->imgtrentino . "' 
alt='previsioni per oggi' />";
    $output .= "{$img}<div 
class='evoluzioneTempo'>{$xml->EvoluzioneTempo}</div>";
  }
  catch(Exception $e)
  {
    $output.= 'Errore reperendo i data da Meteo TN: '.$e->getMessage();
  }
  return $output;
}

function get_meteotn_full($url = null, $xslt = null)
{

  $output = '';

  if(is_null($url))
    $url = constant('METEOTN_XML_BOLLETTINO');
  if(is_null($xslt))
    $xslt = './wp-content/plugins/'.str_replace(basename( 
__FILE__),"",plugin_basename(__FILE__)).'meteotrentino-it.xslt';

  if($missing_xslt=(!file_exists($xslt)))
  {
    $output .= "<div class='error'>Per vedere il bollettino completo salva nella cartella del plugin il file<a 
href='".constant('METEOTN_XSLT'). "'>".constant('METEOTN_XSLT')."</a></div>";
  }

  try{
    if(!class_exists("XSLTProcessor") || $missing_xslt)
    {
      return $output . get_meteotn($url);
    }

    $xslDoc = new DOMDocument();
    $xslDoc->load($xslt);

    $xmlDoc = new DOMDocument();
    $xmlDoc->load($url.'.xml');

    $proc = new XSLTProcessor();
    $proc->importStylesheet($xslDoc);
    $output .= $proc->transformToXML($xmlDoc);
  }catch(Exception $e){
    $output.= "Errore: ". $e->getMessage();
  }

  return $output;
}

/**

  * @see 
http://api.drupal.org/api/drupal/includes--unicode.inc/function/drupal_convert_to_utf8/6

  */

function _convert_to_utf8($data, $encoding) {

  if (function_exists('iconv')) {
    $out = @iconv($encoding, 'utf-8', $data);
  }
  else if (function_exists('mb_convert_encoding')) {
    $out = @mb_convert_encoding($data, 'utf-8', $encoding);
  }
  else if (function_exists('recode_string')) {
    $out = @recode_string($encoding . '..utf-8', $data);
  }
  else {
    throw new Exception(sprintf('Unsupported encoding %s. Please install iconv, 
GNU recode or mbstring for PHP.', $encoding));
    return FALSE;
  }
  return $out;
}

function f_content($content){

  $codes = array(
    "[meteo-tn]" =>  get_meteotn(),
    "[meteo-tn-full]" =>  get_meteotn_full(),
  );

  foreach($codes as $find =>  $output ){
    if(!(strstr($content, $find) === FALSE)){
      $content = str_ireplace($find, $output, $content);
    }
  }// foreach

  return $content;
}

// hooks
add_filter('the_content', 'f_content',16);

--
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con OGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxx


Other related posts: