Permalink
Browse files

Initial commit.

  • Loading branch information...
bjohnson@bluemetal.com
bjohnson@bluemetal.com committed Oct 25, 2015
1 parent ad8c77b commit cd293e7ba45fcb00d7f7c9776eceffb4d5cfa50f
Showing with 156 additions and 0 deletions.
  1. +46 −0 index.php
  2. +39 −0 readme.txt
  3. +71 −0 widget.php
View
@@ -0,0 +1,46 @@
<?php
/*
Plugin Name: WP Twitch
Plugin URI: https://github.com/bertjohnson/WPTwitch
Description: Display your Twitch stream when online.
Author: Bert Johnson
Version: 1.0
Author URI: https://bertjohnson.com
*/
// Register shortcodes.
function WPTwitchShortcodes() {
add_shortcode( 'twitch', 'WPTwitch');
}
add_action('init', 'WPTwitchShortcodes');
// Handle embedding.
function WPTwitch($atts) {
extract(shortcode_atts(array('channel' => '', 'width' => null, 'height' => null, 'https' => false), $atts));
// Only display the stream if it's online.
$json = @file_get_contents("https://api.twitch.tv/kraken/streams/{$channel}", 0, null, null);
if (strpos($json, '{"stream":{') !== false)
{
$returnValue = "<div class='wptwitch'><iframe type='text/html' src='";
if ($https)
$returnValue .= "https://www-cdn.jtvnw.net/swflibs/TwitchPlayer.rfc07d37fc4eed1d17243b452dd3441665496e1e0.swf?channel={$channel}";
else
$returnValue .= "http://www.twitch.tv/{$channel}/embed'";
$returnValue .= "' frameborder='0'";
if (isset($width))
$returnValue .= " width='{$width}'";
if (isset($height))
$returnValue .= " height='{$height}'";
$returnValue .= "></iframe></div>";
return $returnValue;
}
}
include('widget.php');
?>
View
@@ -0,0 +1,39 @@
=== Plugin Name ===
Contributors: bertjohnson)
Donate link: https://bertjohnson.com
Tags: twitch, video, embed
Requires at least: 2.0
Tested up to: 4.4
Stable tag: trunk
License: MIT
License URI: http://mit-license.org/
Simple embedding of your Twitch stream, which will only show when you're online.
== Description ==
Simple embedding of your Twitch stream, which will only show when you're online.
== Installation ==
1. Download and unzip the file.
2. Place the entire contents of the directory into your `/wp-content/plugins/` directory.
3. Activate the plugin through the 'Plugins' menu in WordPress.
== Screenshots ==
== Shortcode Usage ==
To add your Twitch stream, use the following code:
[twitch channel="twitchchannelname" height="640" height="480"]
Options are:
1. channel: Twitch channel name
2. width: Width of the video player
3. height: Height of the video player
4. https: "true" if the channel needs to be served over SSL
== Changelog ==
= 1.0 =
Initial release.
View
@@ -0,0 +1,71 @@
<?php
// Class to allow Twitch embedding as a widget.
class WPTwitchWidget extends WP_Widget {
// Constructor.
function WPTwitchWidget() {
$widget_ops = array('classname' => 'WPTwitchWidget','description' => 'Display your Twitch stream when online');
$this->WP_Widget('WPTwitchWidget','WP Twitch',$widget_ops);
}
// Form for editing widget settings.
function form($instance)
{
$instance = wp_parse_args( (array) $instance, array( 'channel' => '' ) );
$channel = $instance['channel'];
$https = $instance['https'];
?>
<p>
<label for="<?php echo $this->get_field_id('channel'); ?>">Channel Name:
<input class="widefat" id="<?php echo $this->get_field_id('channel'); ?>" name="<?php echo $this->get_field_name('channel'); ?>" type="text" value="<?php echo esc_attr($channel); ?>" />
</label>
</p>
<p><label for="<?php echo $this->get_field_id('https'); ?>">Use HTTPS: <input id="<?php echo $this->get_field_id('https'); ?>" name="<?php echo $this->get_field_name('https'); ?>" type="checkbox" value="1" <?php checked(isset($instance['https']) ? $instance['https'] : 0); ?> /></label></p>
<?php
}
// Handle saving of widget settings.
function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['channel'] = $new_instance['channel'];
$instance['https'] = $new_instance['https'];
return $instance;
}
// Display the widget.
function widget($args, $instance)
{
$channel = $instance['channel'];
$https = $instance['https'];
// Only display the stream if it's online.
$json = @file_get_contents("https://api.twitch.tv/kraken/streams/{$channel}", 0, null, null);
if (strpos($json, '{"stream":{') !== false)
{
$returnValue = "<div class='wptwitch'><iframe type='text/html' src='";
if ($https)
$returnValue .= "https://www-cdn.jtvnw.net/swflibs/TwitchPlayer.rfc07d37fc4eed1d17243b452dd3441665496e1e0.swf?channel={$channel}";
else
$returnValue .= "http://www.twitch.tv/{$channel}/embed'";
$returnValue .= "' frameborder='0'";
if (isset($width))
$returnValue .= " width='{$width}'";
if (isset($height))
$returnValue .= " height='{$height}'";
$returnValue .= "></iframe></div>";
echo $returnValue;
}
}
}
// Register the widget.
add_action('widgets_init',create_function('','return register_widget("WPTwitchWidget");'));
?>

0 comments on commit cd293e7

Please sign in to comment.