Skip to content

Instantly share code, notes, and snippets.

@nfreear
Last active July 4, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfreear/6e53e8458ea5a582288f734c5277eb5d to your computer and use it in GitHub Desktop.
Save nfreear/6e53e8458ea5a582288f734c5277eb5d to your computer and use it in GitHub Desktop.
simple-speak shortcode plugin for WordPress | MIT License — https://github.com/nfreear/simple-speak
<?php namespace Nfreear\WordPress\Plugins\Simple_Speak;
/**
* Plugin Name: simple-speak
* Plugin URI: https://github.com/nfreear/simple-speak
* Description: Shortcodes for text-to-speech and spelling ~ [speak] Hello [/speak] & [spell] Spell me! [/spell].
* Author: Nick Freear
* Author URI: https://twitter.com/nfreear
* Version: 1.1-alpha
*
* @license https://nfreear.mit-license.org MIT License
* @copyright © 2017 Nick Freear
* @author Nick Freear, 15-June-2017.
*/
class Simple_Speak_Plugin {
const SHORTCODE = 'speak';
const SHORTCODE_SPELL = 'spell';
const TEMPLATE = "<div id='id-simple-speak' data-simple-speak='%s'>%s</div>" . PHP_EOL;
const VERSION = '1.3.0-beta';
const SCRIPT_URL= 'https://unpkg.co/simple-speak@%s#._.js';
public function __construct() {
add_shortcode( self::SHORTCODE, [ &$this, 'shortcode' ] );
add_shortcode( self::SHORTCODE_SPELL, [ &$this, 'shortcode_spell' ] );
add_action( 'wp_enqueue_scripts', [ &$this, 'enqueue_scripts' ] );
}
public function shortcode( $attrs = [], $content = null, $ss_mode = null ) {
$lang = get_locale(); // https://codex.wordpress.org/Function_Reference/get_locale
$inp = (object) shortcode_atts( [
'mode' => $ss_mode,
'voiceFamily' => null,
'rate' => 1,
'lang' => str_replace( '_', '-', $lang ),
'client' => 'WordPress/' . get_bloginfo( 'version' ),
], $attrs );
$json_attr = json_encode( $inp );
return sprintf( self::TEMPLATE, $json_attr, $content );
}
public function shortcode_spell( $attrs = [], $content = null ) {
return $this->shortcode( $attrs, $content, 'spell' );
}
public function enqueue_scripts() {
$version = get_option( 'simple_speak_version', self::VERSION );
$script_url = sprintf( self::SCRIPT_URL, $version );
self::debug([ 'fn' => __FUNCTION__, 'simple_speak_version' => $version ]);
return wp_enqueue_script( 'simple-speak-plugin', $script_url, [ 'jquery' ], $ver = null, $in_footer = true );
}
public static function debug( $obj ) {
static $count = 0;
$str = sprintf( 'X-simple-speak-plugin-%02d: %s', $count, json_encode( $obj ) );
if (headers_sent()) {
echo "<!-- $str -->";
} else {
header($str);
}
$count++;
}
}
$wp_plugin = new Simple_Speak_Plugin();
// End.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment