Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.41 KB | None | 0 0
  1. Скопируйте код, представленный ниже, в файл functions.php,
  2. который вы сможете найти в папке с вашей действующей темой,
  3. либо через редактор админ панели WORDPRESS
  4.  
  5. <?
  6.  
  7. class marqueeCurrency extends WP_Widget {
  8.  
  9.    function __construct() {
  10.      parent::__construct('marqueeСurrency','Курс криптовалют', array('description' => 'Отображает актуальные курсы криптовалют бегущей строкой'));
  11.      
  12.      add_action('wp_ajax_getCurrencies', array($this,'my_action_callback'));
  13.      add_action('wp_ajax_nopriv_getCurrencies', array($this,'my_action_callback'));
  14.    }
  15.    
  16.    function widget( $args, $instance ){
  17.    ?>
  18.     <div id="marqueeCurrency" class="marqueeCurrencyDiv" style="text-align:center;"> загрузка...</div>
  19.     <script type="text/javascript" >
  20.     jQuery(document).ready(function($) {
  21.         var data = {
  22.             action: 'getCurrencies',
  23.             currenciesLimit: <? echo $instance['currenciesLimit']?>,
  24.             type: '<? echo $instance['type']?>',
  25.             list: '<? echo $instance['list']?>',
  26.         };
  27.  
  28.        
  29.         jQuery.post( '<?echo admin_url('admin-ajax.php');?>', data, function(response) {
  30.             document.getElementById('marqueeCurrency').innerHTML = response;
  31.         });
  32.     });
  33.     </script>
  34.    <?        
  35.    }
  36.    
  37.    function update( $new_instance, $old_instance ) {
  38.     $instance = $old_instance;
  39.  
  40.     $instance['currenciesLimit'] = strip_tags( $new_instance['currenciesLimit'] );
  41.     $instance['type'] = strip_tags( $new_instance['type'] );
  42.     $instance['list'] = strip_tags( $new_instance['list'] );
  43.    
  44.     return $instance;
  45.    }
  46.    
  47.    function form( $instance ) {
  48.     $defaults = array (
  49.      'currenciesLimit' => 10,
  50.      'type' => 'popular', // <-- новая опция
  51.      'list' => 'bitcoin,Ethereum' // <-- новая опция
  52.     );
  53.     $instance = wp_parse_args((array)$instance, $defaults);
  54.     ?>
  55.    
  56.     <!--новый блок начало-->
  57.      <p>
  58.       Вывод: <br/>
  59.       <label><input onclick="hideInput('popular');" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="popular"  type="radio" <?php checked( $instance['type'], 'popular', true ); ?> /> Популярные криптовалюты </label> <br/>
  60.       <label><input onclick="hideInput('list');" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="list"  type="radio" <?php checked( $instance['type'], 'list', true ); ?>/> Список из криптовалют </label>
  61.      </p>
  62.      <!--новый блок конец-->
  63.      <p id="<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag">
  64.       <label for="<?php echo $this->get_field_id( 'currenciesLimit' ); ?>"> Выводить <input id="<?php echo $this->get_field_id( 'currenciesLimit' ); ?>" name="<?php echo $this->get_field_name( 'currenciesLimit' ); ?>" value="<?php echo $instance['currenciesLimit']; ?>" style="width:50px;" type="number"/> популярных криптовалют</label>
  65.      </p>
  66.      <!--новый блок начало-->
  67.      <p id="<?php echo $this->get_field_id( 'list' ); ?>ptag">
  68.       <label for="<?php echo $this->get_field_id( 'list' ); ?>"> Список криптовалют</label>
  69.       <input id="<?php echo $this->get_field_id( 'list' ); ?>" name="<?php echo $this->get_field_name( 'list' ); ?>" value="<?php echo $instance['list']; ?>" type="text" style="width:100%"/>
  70.      </p>
  71.      <!--новый блок конец-->
  72.      <script type="text/javascript">
  73.  
  74.         function hideInput( val ) {
  75.             if (val == 'popular') {
  76.                 document.getElementById("<?php echo $this->get_field_id( 'list' ); ?>ptag").style.display = 'none';
  77.                 document.getElementById("<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag").style.display = '';
  78.             } else {
  79.                 document.getElementById("<?php echo $this->get_field_id( 'list' ); ?>ptag").style.display = '';
  80.                 document.getElementById("<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag").style.display = 'none';
  81.             }
  82.         }
  83.  
  84.         hideInput('<? echo $instance['type']; ?>');
  85.      </script>
  86.     <?
  87.   }
  88.  
  89.   function my_action_callback () {
  90.     $currencies = array();
  91.  
  92.     switch ($_POST['type']) {
  93.         case 'popular':
  94.             {
  95.                 $res = file_get_contents('https://api.coinmarketcap.com/v1/ticker/?convert=RUB&limit='.$_POST['currenciesLimit']);
  96.                 $currencies = json_decode($res,true);
  97.             }
  98.             break;
  99.  
  100.         case 'list':
  101.             {
  102.                 $list = explode(',',$_POST['list']);
  103.  
  104.                 $currencies = array();
  105.  
  106.                 for ($i=0;$i<count($list);$i++) {
  107.                     $res = file_get_contents('https://api.coinmarketcap.com/v1/ticker/'.str_replace(' ','-',$list[$i]).'/?convert=RUB');
  108.                     $res = json_decode($res,true);
  109.                     $currencies[] = $res[0];
  110.                 }
  111.             }
  112.             break;
  113.  
  114.     }
  115.  
  116.     ?>
  117.      <marquee scrollamount="3" behavior="scroll" loop="infinite" direction="left">
  118.     <?
  119.     foreach ($currencies as $key => $currency) {
  120.     ?>
  121.      <span><strong><? echo $currency['name'].' ('.$currency['symbol'].')'; ?></strong> : <? echo number_format($currency['price_rub'], 2, '.', ' ').' руб.';?> &nbsp;&nbsp;</span>
  122.     <?
  123.     }
  124.     ?>
  125.      </marquee>
  126.     <?
  127.     wp_die();
  128.   }
  129.  
  130. }
  131.  
  132. register_widget('marqueeCurrency');
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement