Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Скопируйте код, представленный ниже, в файл functions.php,
- который вы сможете найти в папке с вашей действующей темой,
- либо через редактор админ панели WORDPRESS
- <?
- class marqueeCurrency extends WP_Widget {
- function __construct() {
- parent::__construct('marqueeСurrency','Курс криптовалют', array('description' => 'Отображает актуальные курсы криптовалют бегущей строкой'));
- add_action('wp_ajax_getCurrencies', array($this,'my_action_callback'));
- add_action('wp_ajax_nopriv_getCurrencies', array($this,'my_action_callback'));
- }
- function widget( $args, $instance ){
- ?>
- <div id="marqueeCurrency" class="marqueeCurrencyDiv" style="text-align:center;"> загрузка...</div>
- <script type="text/javascript" >
- jQuery(document).ready(function($) {
- var data = {
- action: 'getCurrencies',
- currenciesLimit: <? echo $instance['currenciesLimit']?>,
- type: '<? echo $instance['type']?>',
- list: '<? echo $instance['list']?>',
- };
- jQuery.post( '<?echo admin_url('admin-ajax.php');?>', data, function(response) {
- document.getElementById('marqueeCurrency').innerHTML = response;
- });
- });
- </script>
- <?
- }
- function update( $new_instance, $old_instance ) {
- $instance = $old_instance;
- $instance['currenciesLimit'] = strip_tags( $new_instance['currenciesLimit'] );
- $instance['type'] = strip_tags( $new_instance['type'] );
- $instance['list'] = strip_tags( $new_instance['list'] );
- return $instance;
- }
- function form( $instance ) {
- $defaults = array (
- 'currenciesLimit' => 10,
- 'type' => 'popular', // <-- новая опция
- 'list' => 'bitcoin,Ethereum' // <-- новая опция
- );
- $instance = wp_parse_args((array)$instance, $defaults);
- ?>
- <!--новый блок начало-->
- <p>
- Вывод: <br/>
- <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/>
- <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>
- </p>
- <!--новый блок конец-->
- <p id="<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag">
- <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>
- </p>
- <!--новый блок начало-->
- <p id="<?php echo $this->get_field_id( 'list' ); ?>ptag">
- <label for="<?php echo $this->get_field_id( 'list' ); ?>"> Список криптовалют</label>
- <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%"/>
- </p>
- <!--новый блок конец-->
- <script type="text/javascript">
- function hideInput( val ) {
- if (val == 'popular') {
- document.getElementById("<?php echo $this->get_field_id( 'list' ); ?>ptag").style.display = 'none';
- document.getElementById("<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag").style.display = '';
- } else {
- document.getElementById("<?php echo $this->get_field_id( 'list' ); ?>ptag").style.display = '';
- document.getElementById("<?php echo $this->get_field_id( 'currenciesLimit' ); ?>ptag").style.display = 'none';
- }
- }
- hideInput('<? echo $instance['type']; ?>');
- </script>
- <?
- }
- function my_action_callback () {
- $currencies = array();
- switch ($_POST['type']) {
- case 'popular':
- {
- $res = file_get_contents('https://api.coinmarketcap.com/v1/ticker/?convert=RUB&limit='.$_POST['currenciesLimit']);
- $currencies = json_decode($res,true);
- }
- break;
- case 'list':
- {
- $list = explode(',',$_POST['list']);
- $currencies = array();
- for ($i=0;$i<count($list);$i++) {
- $res = file_get_contents('https://api.coinmarketcap.com/v1/ticker/'.str_replace(' ','-',$list[$i]).'/?convert=RUB');
- $res = json_decode($res,true);
- $currencies[] = $res[0];
- }
- }
- break;
- }
- ?>
- <marquee scrollamount="3" behavior="scroll" loop="infinite" direction="left">
- <?
- foreach ($currencies as $key => $currency) {
- ?>
- <span><strong><? echo $currency['name'].' ('.$currency['symbol'].')'; ?></strong> : <? echo number_format($currency['price_rub'], 2, '.', ' ').' руб.';?> </span>
- <?
- }
- ?>
- </marquee>
- <?
- wp_die();
- }
- }
- register_widget('marqueeCurrency');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement