Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created January 29, 2012 12:53
Show Gist options
  • Save GaryJones/1698692 to your computer and use it in GitHub Desktop.
Save GaryJones/1698692 to your computer and use it in GitHub Desktop.
Amendments to oEmbed Gist WP plugin (http://wordpress.org/extend/plugins/oembed-gist/)
<?php
/*
Plugin Name: oEmbed gist
Plugin URI: http://firegoby.theta.ne.jp/wp/oembed-gist
Description: Embed source from gist.github.
Author: Takayuki Miyauchi (THETA NETWORKS Co,.Ltd)
Version: 1.2.0-gj
Author URI: http://firegoby.theta.ne.jp/
*/
/*
Copyright (c) 2010 Takayuki Miyauchi (THETA NETWORKS Co,.Ltd).
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Modified August 18, 2011 by Alex King (alexking.org) to add NOSCRIPT and i18n support
Modified January 29, 2012 by Gary Jones (@GaryJ) to simplify and tidy code, and add documentation.
*/
new Gist;
class Gist {
/**
* Holds the noscript replacement content.
*
* @since 1.1.0
*
* @var string
*/
private $noscript;
/**
* Plugin constructor.
*
* Handles i18n, sets noscript content, registers oEmbed handler and hooks
* in shortcode callback.
*
* @since 0.1.0
*/
public function __construct() {
load_plugin_textdomain( 'oembed-gist', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
$this->noscript = '<p>' . sprintf( __( '<a href="%s">View the code on Gist</a>.', 'oembed-gist' ), 'https://gist.github.com/%s' ) . '</p>';
wp_embed_register_handler(
'gist',
'#https://gist.github.com/([a-zA-Z0-9]+)(\#file_(.+))?$#i',
array( &$this, 'handler' )
);
add_shortcode( 'gist', array( &$this, 'shortcode' ) );
}
/**
* oEmbed callback for gist.github.com.
*
* @since 0.1.0
*
* @param array $matches Regex matches from URL.
*
* @return string WordPress shortcode string.
*/
public function handler( $matches ) {
// Check if #file_* in URL is added, and ensure the match is empty if not
if ( ! isset( $matches[2] ) || ! isset( $matches[3] ) || ! $matches[3] )
$matches[3] = null;
return '[gist id="' . $matches[1] . '" file="' . $matches[3] . '"]';
}
/**
* Callback handler for "gist" shortcode.
*
* Supports id and file attributes.
*
* @since 0.1.0
*
* @param array $atts Shortcode attributes.
*
* @return string Replacement content.
*/
public function shortcode( $atts ) {
extract(
shortcode_atts(
array(
'id' => '',
'file' => '',
),
$atts
)
);
if ( preg_match( "/^[a-zA-Z0-9]+$/", $id ) ) {
$noscript = sprintf( $this->noscript, $id );
$file = $file ? '?file=' . $file : '';
return sprintf( '<script src="https://gist.github.com/%s.js%s"></script><noscript>%s</noscript>', $id, $file, $noscript );
}
}
}
@jaredatch
Copy link

I used a plugin similar to that, until I found http://wordpress.org/extend/plugins/embed-github-gist/ - specifically because

If json_decode is available, the HTML markup for the source code will be injected into the post inline.
This will ensure that the code is available to spiders and feed readers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment