Skip to content

Instantly share code, notes, and snippets.

@Rahe
Created November 16, 2014 15:08
Show Gist options
  • Save Rahe/c21149656969b8fa56b8 to your computer and use it in GitHub Desktop.
Save Rahe/c21149656969b8fa56b8 to your computer and use it in GitHub Desktop.
Shortcode album
<?php
/*
Plugin Name: Album shortcode
Version: 0.1
Description: Un shortcode pour afficher un album
*/
// don't load directly
if ( !defined('ABSPATH') )
die('-1');
// Plugin constants
define('SC_ALBUM_VERSION', '0.1');
// Plugin URL and PATH
define('SC_ALBUM_URL', plugin_dir_url ( __FILE__ ));
define('SC_ALBUM_DIR', plugin_dir_path( __FILE__ ));
// Function for easy load files
function _sc_album_load_files($dir, $files, $prefix = '') {
foreach ($files as $file) {
if ( is_file($dir . $prefix . $file . ".php") ) {
require_once($dir . $prefix . $file . ".php");
}
}
}
// Plugin client classes
_sc_album_load_files(SC_ALBUM_DIR . 'classes/', array( 'main', 'shortcode' ) );
add_action('plugins_loaded', 'init_sc_album_plugin');
function init_sc_album_plugin() {
// Client
new SC_Album_Main();
}
class SC_Album_Main{
function __construct() {
add_action( 'init', array( __CLASS__, 'init_shortcode' ) );
}
public static function init_shortcode() {
add_shortcode( 'shortcode-album', array( __CLASS__, 'shortcode' ) );
}
/**
* Shortcode method
*
* @param array $atts
* @param string $content
*
* @return bool|string
* @author Nicolas Juen
*/
public static function shortcode( $atts = array() ) {
// On parse les attributs avec une version par défaut
$atts = shortcode_atts( array(
'id' => 0,
'display_artist' => false,
'add_link' => false
), $atts );
if( absint( $atts['id'] ) <= 0 ) {
return '';
}
// Récupération du template
$tpl = self::get_component_template_path( 'shortcode-album' );
// Vérification de la présence de celui-ci
if ( empty( $tpl ) ) {
return false;
}
}
/**
* Get the template_path for includes
*
* @param string $tpl
*
* @return bool|string
* @author Nicolas Juen
*
*/
public static function get_component_template_path( $tpl = '' ) {
if ( empty( $tpl ) ) {
return false;
}
// Display the form, allow take template from child or parent theme
if ( is_file( STYLESHEETPATH . '/views/shortcode/' . $tpl . '.php' ) ) {// Use custom template from child theme
return ( STYLESHEETPATH . '/views/shortcode/' . $tpl . '.php' );
} elseif ( is_file( TEMPLATEPATH . '/views/shortcode/' . $tpl . '.php' ) ) {// Use custom template from parent theme
return (TEMPLATEPATH . '/views/shortcode/' . $tpl . '.php' );
} elseif ( is_file( SC_ALBUM_DIR . 'views/' . $tpl . '.php' ) ) {// Use builtin template
return ( SC_ALBUM_DIR . 'views/' . $tpl . '.php' );
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment