Instantly share code, notes, and snippets.

@Rahe /shortcode-album.php Secret
Last active Aug 29, 2015

Embed
What would you like to do?
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();
}
<div>
<h5><?php echo get_the_title( $album ); ?></h5>
<?php if( true === (bool)$atts['display_details'] ) : ?>
<dl>
<dt>
Artiste
</dt>
<dd>
<?php echo esc_html( $album->arstist ); ?>
</dd>
<dt>
Année
</dt>
<dd>
<?php echo esc_html( $album->year ); ?>
</dd>
</dl>
<?php endif; ?>
<?php if( true === (bool)$atts['add_link'] ) : ?>
<a href="<?php echo get_permalink( $album ); ?>" >Aller sur la fiche de l'album</a>
<?php endif; ?>
</div>
<?php
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(), $content = '' ) {
// On parse les attributs avec une version par défaut
$atts = shortcode_atts( array(
'id' => 0,
'display_details' => true,
'add_link' => true
), $atts, 'shortcode-album' );
if( absint( $atts['id'] ) <= 0 ) {
return $content;
}
// 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 $content;
}
// Une variable pour que le contenu soit facilement accessible.
$album = get_post( $atts['id'] );
//include template and display it
ob_start();
include ( $tpl );
return ob_get_clean().$content;
}
/**
* 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