Le carnet
WordPress : une classe abstraite pour aider la création de shortcodes
Une petite classe PHP abstraite pour créer des shortcodes WordPress sur une base commune, avec un exemple concret en shortcode attachments.
Une petite classe PHP: Hypertext Preprocessor (PHP) abstraite pour aider la création de shortcodes dans WordPress.
Pour un exemple d’utilisation, se reporter au shortcode attachments.
php
<?php
/**
* Abstract class to create shortcodes in WordPress.
*
* @author Mehdi Kabab <http://pioupioum.fr/>
* @copyright Copyright (C) 2010 Mehdi Kabab
* @license http://www.gnu.org/licenses/gpl.html GNU GPL version 3 or later
* @version 1.0.0
* @link /blog/wordpress-create-shortcode-class.html
*/
abstract class PPM_Shortcode
{
/**
* Default options.
*
* @var array
**/
protected $_default_options = array();
/**
* Options after initialization of shortcode.
*
* @var ArrayObject
*/
private $_options;
/**
* Tag name of shortcode.
*
* @var string
**/
private $_name = null;
/**
* Constructor.
*
* @param string $tag Shortcode tag to be searched in post content.
*/
public function __construct($tag)
{
$this->setTagName($tag);
$this->_options = new ArrayObject(array(),
ArrayObject::ARRAY_AS_PROPS);
}
/**
* Excecute the shortcode.
*
* Called by the shortcode API.
*
* @param array $attributes Attributes used in the call of the shortcode.
* @param string $content The content of the shortcode. Default to null.
* @return string
*/
public function run($attributes, $content = null)
{
$this->setOptions(shortcode_atts($this->_default_options, $attributes));
return $this->_process($attributes, $content);
}
/**
* Check if the current shortcode tag already registered.
*
* @global array $shortcode_tags
* @return boolean
*/
public function isRegistered()
{
global $shortcode_tags;
return array_key_exists($this->getTagName(), $shortcode_tags);
}
/**
* Register the shortcode.
*
* @return PPM_Shortcode
* @throws Exception If the shortcode already exists.
*/
public function register()
{
if ($this->isRegistered()) {
throw new Exception(sprintf('The shortcode `%s` already exists!',
$this->getTagName()));
}
add_shortcode($this->getTagName(), array($this, 'run'));
return $this;
}
/**
* Unregister the shortcode.
*
* @return PPM_Shortcode
*/
public function unregister()
{
remove_shortcode($this->getTagName());
return $this;
}
/**
* Retrieve the name of shortcode.
*
* @return string
*/
public function getTagName()
{
return $this->_name;
}
/**
* Set the name of shortcode.
*
* @param string $name Shortcode tag to be searched in post content.
* @return PPM_Shortcode
*/
public function setTagName($name)
{
$this->_name = $name;
return $this;
}
/**
* Retrieve the requested option.
*
* @param string $name The option to search.
* @return mixed
*/
public function getOption($name)
{
return $this->_options->offsetGet($name);
}
/**
* Test if an option exists for the shortcode.
*
* @param string $name
* @return boolean
*/
public function hasOption($name)
{
return $this->_options->offsetExists($name);
}
/**
* Set an option.
*
* @param string $name
* @param mixed $value
* @return PPM_Shortcode
*/
public function setOption($name, $value)
{
$this->_options->offsetSet($name, $value);
return $this;
}
/**
* Retrieve the options.
*
* @return ArrayObject
*/
public function getOptions()
{
return $this->_options;
}
/**
* Set the options.
*
* @param array $options
* @return PPM_Shortcode
*/
public function setOptions($options)
{
foreach ((array) $options as $name => $value) {
$this->setOption($name, $value);
}
return $this;
}
/**
* Concrete excecution of the shortcode.
*
* @param array $attributes Attributes used in the call of the shortcode.
* @param string $content The content of the shortcode. Default to null.
* @return string
*/
abstract protected function _process($attributes, $content = null);
}
?>
2 commentaires
Archive en lecture seule, importée depuis WordPress.
Une petite explication sur « Comment ça s’utilise ? » …
+1 , auriez-vous un petit exemple a fournir avec cette classe