PHP: Tetris WordPress Shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Tetris | |
* Plugin URI: http://wpsmith.net/ | |
* Description: Adds tetris game shortcode. | |
* Version: 0.0.1 | |
* Author: Travis Smith, WP Smith | |
* Author URI: http://wpsmith.net | |
* Text Domain: tetris | |
* | |
* @copyright 2015 | |
* @author Travis Smith | |
* @link http://wpsmith.net/ | |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
* | |
*/ | |
/** | |
* Core Plugin File | |
* | |
* @package Tetris | |
* @author Travis Smith <t@wpsmith.net> | |
* @copyright Copyright (c) 2015, Travis Smith | |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License | |
* @since 1.0.0 | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
define( 'TETRIS_VERSION', '0.0.1' ); | |
define( 'TETRIS_SLUG', 'tetris' ); | |
define( 'TETRIS_FILE', __FILE__ ); | |
spl_autoload_register( 'tetris_autoload' ); | |
/** | |
* SPL Class Autoloader for classes. | |
* | |
* @param string $class_name Class name being autoloaded. | |
* @link http://us1.php.net/spl_autoload_register | |
* @author Travis Smith | |
* @since 0.1.0 | |
*/ | |
function tetris_autoload( $class_name ) { | |
// Do nothing if class already exists, not prefixed WPS_ | |
if ( class_exists( $class_name, false ) || | |
( !class_exists( $class_name, false ) && false === strpos( $class_name, 'Tetris_' ) ) ) { | |
return; | |
} | |
// Set file | |
$file = plugin_dir_path( __FILE__ ) . "includes/classes/$class_name.php"; | |
// Load file | |
if ( file_exists( $file ) ) { | |
include_once( $file ); | |
} | |
} | |
new Tetris_Shortcode(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Tetris_Shortcode | |
*/ | |
class Tetris_Shortcode { | |
/** | |
* Whether in debug mode or not. | |
* @var bool | |
*/ | |
public $debug = false; | |
/** | |
* Whether scripts have been registered. | |
* @var bool | |
*/ | |
public $scripts_registered = false; | |
/** | |
* Whether scripts have been enqueued. | |
* @var bool | |
*/ | |
public $scripts_enqueued = false; | |
/** | |
* Constructor | |
*/ | |
public function __construct() { | |
// Add the shortcode | |
add_shortcode( 'tetris', array( $this, 'shortcode' ) ); | |
// Set debug class var. | |
$this->debug = ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ); | |
// Register scripts!! | |
add_action( 'init', array( $this, 'register_scripts' ), 11 ); | |
} | |
/** | |
* Register the scripts | |
* | |
* @uses wp_register_script | |
* @uses wp_localize_script | |
*/ | |
public function register_scripts() { | |
if ( $this->scripts_registered ) { | |
return; | |
} | |
/* SCRIPTS */ | |
$suffix = $this->debug ? '.js' : '.min.js'; | |
wp_register_script( | |
'jgestures', | |
plugins_url( "js/jgestures$suffix", TETRIS_FILE ), | |
array( 'jquery', ), | |
filemtime( plugin_dir_path( TETRIS_FILE ) . "js/jgestures$suffix" ), | |
false | |
); | |
wp_register_script( | |
'blockrain', | |
plugins_url( "js/jquery.blockrain$suffix", TETRIS_FILE ), | |
array( 'jquery', 'jquery-ui-widget', 'jgestures', ), | |
filemtime( plugin_dir_path( TETRIS_FILE ) . "js/jquery.blockrain$suffix" ), | |
false | |
); | |
wp_register_script( | |
'blockrain-init', | |
plugins_url( "js/blockrain.init$suffix", TETRIS_FILE ), | |
array( 'blockrain', ), | |
filemtime( plugin_dir_path( TETRIS_FILE ) . "js/blockrain.init$suffix" ), | |
false | |
); | |
$args = array( | |
'playText' => __( 'Let\'s play some Tetris', TETRIS_SLUG ), | |
'playButtonText' => __( 'Play', TETRIS_SLUG ), | |
'gameOverText' => __( 'Game Over', TETRIS_SLUG ), | |
'restartButtonText' => __( 'Play Again', TETRIS_SLUG ), | |
'scoreText' => __( 'Score', TETRIS_SLUG ), | |
); | |
wp_localize_script( 'blockrain-init', 'blockrainI10n', $args ); | |
/* STYLES */ | |
$suffix = $this->debug ? '.css' : '.min.css'; | |
wp_register_style( | |
'blockrain', | |
plugins_url( "css/blockrain$suffix", TETRIS_FILE ), | |
null, | |
filemtime( plugin_dir_path( TETRIS_FILE ) . "css/blockrain$suffix" ) | |
); | |
} | |
/** | |
* Output the scripts for WordPress | |
* | |
* @uses wp_enqueue_script | |
*/ | |
public function enqueue_scripts() { | |
if ( $this->scripts_enqueued ) { | |
return; | |
} | |
// Ensure scripts are registered | |
$this->register_scripts(); | |
/* SCRIPTS */ | |
wp_enqueue_script( 'blockrain-init' ); | |
/* STYLES */ | |
wp_enqueue_style( 'blockrain' ); | |
// Prevent redundant calls | |
$this->scripts_enqueued = true; | |
} | |
/** | |
* Returns the HTML markup for the shortcode. | |
* | |
* @param array $atts Array of attributes (height, width). | |
* @param string $content Content string. N/A. | |
* | |
* @return string HTML markup. | |
*/ | |
public function shortcode( $atts, $content = '' ) { | |
// Parse the attributes with defaults | |
$atts = shortcode_atts( array( | |
'height' => '100%', | |
'width' => '100%' | |
), $atts, 'tetris' ); | |
// Output the script | |
$this->enqueue_scripts(); | |
// Return the simple markup | |
return sprintf( '<div class="tetris-game" style="max-width:%s; max-height:%s;"></div>', $atts['width'], $atts['height'] ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment