Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
function bonfire_sst_header() {
    ?>

        <!-- BEGIN MAIN WRAPPER (show only if Twitch channel name entered) -->
        <?php if( get_theme_mod('sst_channel_name') != '') { 
        
        $url="https://api.twitch.tv/helix/streams?user_login=".get_theme_mod('sst_channel_name');
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        /*curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);*/
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Client-ID: ls2awgx5gfg9m1q6iopdqb1b7d0y6a'
        ));
        
        $result=curl_exec($ch);
        $json=json_decode($result, true);
        $array1=$json['data']['pagination'];
        ?>
        <div class="sst-main-wrapper">
            <?php include( plugin_dir_path( __FILE__ ) . 'include.php'); ?>
        </div>
        <?php } ?>
        <!-- END MAIN WRAPPER (show only if Twitch channel name entered) -->
        
        <script>
        jQuery('.sst-twitch').each(function () {
            var nickname = jQuery(this).data('nickname');
            
        <?php
        if ($array1[0]['type']== 0) {
            if( get_theme_mod('sst_offline_hide') != '') { ?><?php } else { ?>jQuery('.sst-main-wrapper, .sst-main-widget-wrapper').addClass('sst-main-wrapper-active');<?php } ?>
            // animations
            jQuery('.sst-status-text-offline').addClass('sst-current-status');
            setTimeout(function() {
                jQuery('.sst-status-text-offline').addClass('sst-current-status-active');
                jQuery('.sst-status-wrapper').addClass('sst-status-wrapper-active');
            }, 25);
        <?php
        } else {
        ?>
            // show if online
            jQuery('.sst-main-wrapper, .sst-main-widget-wrapper').addClass('sst-main-wrapper-active');
            // animations
            jQuery('.sst-status-text-live').addClass('sst-current-status');
            setTimeout(function() {
                jQuery('.sst-status-text-live').addClass('sst-current-status-active');
                jQuery('.sst-status-wrapper').addClass('sst-status-wrapper-active');
            }, 25);
        <?php
        }
        ?>
        });
        </script>

    <?php
    }


What I have tried:

the plugin isn't working because of the outdated API. I need to use it but my knowledge of json-PHP isn't the best. I want to put a URL from a twitch account and the plugin checks the URL and if it streams live then the tag shows the message "LIVE NOW" else shows "offline". I can't make it work. I changed parts of the code in order to migrate from apiv3 to v5 but the
if ($array1[0]['type']== 0)
doesn't seem to work well. it shows the message "LIVE NOW" everytime I change the twitch account even if the account isn't streaming.
this is the original code of the plugin BEFORE my changes:
function bonfire_sst_header() {
	?>

        <!-- BEGIN MAIN WRAPPER (show only if Twitch channel name entered) -->
        <?php if( get_theme_mod('sst_channel_name') != '') { ?>
        <div class="sst-main-wrapper">
            <?php include( plugin_dir_path( __FILE__ ) . 'include.php'); ?>
        </div>
        <?php } ?>
        <!-- END MAIN WRAPPER (show only if Twitch channel name entered) -->
        
        <script>
        jQuery('.sst-twitch').each(function () {
            var nickname = jQuery(this).data('nickname');
            jQuery.getJSON("https://api.twitch.tv/kraken/streams/"+nickname+"?client_id=ls2awgx5gfg9m1q6iopdqb1b7d0y6a", function(c) {
                if (c.stream == null) {
                    // show if offline (unless 'Hide when not streaming' option enabled)
                    <?php if( get_theme_mod('sst_offline_hide') != '') { ?><?php } else { ?>jQuery('.sst-main-wrapper, .sst-main-widget-wrapper').addClass('sst-main-wrapper-active');<?php } ?>
                    // animations
                    jQuery('.sst-status-text-offline').addClass('sst-current-status');
                    setTimeout(function() {
                        jQuery('.sst-status-text-offline').addClass('sst-current-status-active');
                        jQuery('.sst-status-wrapper').addClass('sst-status-wrapper-active');
                    }, 25);
                } else {
                    // show if online
                    jQuery('.sst-main-wrapper, .sst-main-widget-wrapper').addClass('sst-main-wrapper-active');
                    // animations
                    jQuery('.sst-status-text-live').addClass('sst-current-status');
                    setTimeout(function() {
                        jQuery('.sst-status-text-live').addClass('sst-current-status-active');
                        jQuery('.sst-status-wrapper').addClass('sst-status-wrapper-active');
                    }, 25);
                }
            });
        });
        </script>

	<?php
	}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900