Advertisement
miriamdepaula

WordPress: Cotação do Dolar/Euro - Widget

Feb 29th, 2012
1,554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. <?php
  2. /*
  3. Para ativar este widget:
  4. 1) Crie um arquivo chamado widget_cotacao_dolar.php (ou outro nome à sua escolha) e insira este codigo dentro.. =P
  5. 2) Abra o arquivo functions.php e faça o include desse arquivo. Assim:  require('widget_cotacao_dolar.php');
  6. 3) Salve e teste! Vá em APARÊNCIA > WIDGETS e veja se o novo widget aparece lá... =)
  7. */
  8.  
  9. class cotacao_dolar_Widget extends WP_Widget
  10. {
  11.    
  12.         function cotacao_dolar_Widget(){
  13.             $widget_ops = array( 'description' => 'Exibe a cotação do dolar e euro direto do site do Banco Central' );
  14.             $control_ops = array( 'width' => 250, 'height' => 200 );
  15.             parent::WP_Widget( false, $name = 'Cotação do Dolar', $widget_ops, $control_ops );
  16.         }
  17.        
  18.         /* Displays the Widget in the front-end */
  19.         function widget( $args, $instance ){
  20.             extract($args);
  21.             $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? 'Cotação de Moedas' : esc_html( $instance['title'] ) );
  22.                
  23.             echo $before_widget;
  24.            
  25.             if ( $title )
  26.                echo $before_title . $title . $after_title;
  27.                
  28.             /*  - NÃO ALTERAR - inicio codigo que pega os valores do Banco Central*/
  29.             //$url = file_get_contents('http://www.bcb.gov.br/'); <--- função desativada no servidor!
  30.            
  31.             //get the url via php cURL
  32.             $url = 'http://www.bcb.gov.br/';
  33.             $ch = curl_init();
  34.             curl_setopt($ch, CURLOPT_URL, $url);
  35.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  36.             $response = curl_exec($ch);
  37.             curl_close($ch);
  38.            
  39.             preg_match_all('/<div class="tabela">(.+)<\/div>/s', $response, $conteudo);
  40.            
  41.             $n = explode('<div class="tabela">', $conteudo[0][0]);
  42.             //print_r($n);
  43.            
  44.             preg_match_all('/<table border="0" cellpadding="0" cellspacing="0">(.+)<\/table>/s', $n[4], $conteudo);
  45.             $newhtml = preg_replace( "/\s+/"," ", $conteudo[0][0]);
  46.             $output = str_replace('<a href="/?TXCOTACAO"> mais moedas</a>', '<a href="http://www.bcb.gov.br/?TXCOTACAO" rel="external">'. __('fonte: bacen') .'</a>', $newhtml);
  47.             $output = str_replace('bgcolor="#f8f5f6"', '', $output);
  48.                        
  49.             echo $output;          
  50.             /*  - NÃO ALTERAR - fim do codigo que pega os valores do Banco Central*/
  51.            
  52.  
  53.             echo $after_widget;
  54.         }
  55.        
  56.         /*Saves the settings. */
  57.         function update( $new_instance, $old_instance ){
  58.             $instance = $old_instance;
  59.             $instance['title'] = strip_tags( $new_instance['title'] );
  60.  
  61.             return $instance;
  62.         }
  63.        
  64.         /*Creates the form for the widget in the back-end. */
  65.         function form( $instance ){
  66.             //Defaults
  67.             $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  68.    
  69.             $title = esc_attr( $instance['title'] );
  70.    
  71.             # Title
  72.             echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Título da cotação:' . '</label><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
  73.             # Page id
  74.            
  75.            
  76.         }
  77. } // end pagina_destaque_Widget class
  78.  
  79. function cotacao_dolar_Widget_Init() {
  80.     register_widget('cotacao_dolar_Widget');
  81. }
  82.  
  83. add_action('widgets_init', 'cotacao_dolar_Widget_Init');
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement