Created
May 9, 2017 18:07
-
-
Save smcjones/47e24fcc51bd40a2ef0a2cef8f7c18f1 to your computer and use it in GitHub Desktop.
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
class MyCitations_Plugin { | |
/** | |
* @var int | |
*/ | |
private $citeID; | |
/** | |
* @var array | |
*/ | |
private $citationList; | |
public function __construct() { | |
$this->citeID = 0; | |
} | |
/** | |
* Method to call to load actions and shortcodes. | |
*/ | |
public function load() { | |
add_shortcode( 'cite', array($this,'cite') ); | |
add_shortcode( 'print_citations', array($this,'printCitations') ); | |
add_filter( 'the_content', array($this,'theContent') ); | |
} | |
/** | |
* Adds a citation when this shortcode is used. | |
* | |
* @param $atts array Allows "description" and "source" - a URL | |
* @param $content The text for which a citation is made. | |
*/ | |
public function cite($atts, $content='') { | |
$citeID = ++$this->citeID; | |
if (isset($atts['description']) || isset($atts['source'])) { | |
$this->addCitation($citeID, $atts); | |
return $content . sprintf('<sup><a name="text-%1$s"></a> <a href="#citation-%1$s">[%1$s]</a></sup>, $citeID); | |
} else { | |
//do nothing | |
return $content; | |
} | |
/** | |
* Adds a citation to to a footer string. | |
* | |
* @param int $citeID The index for this citation. | |
* @param array $atts An array of attributes that matches the [cite] shortcode. | |
*/ | |
protected function addCitation($citeID, $atts) | |
{ | |
$text = ''; | |
if (isset($atts['description'])) { | |
$text .= $atts['description'] . ' '; | |
} | |
if (isset($atts['source'])) { | |
$text .= sprintf('<a href="%1$s">[%1$s]</a>', $atts['source']); | |
} | |
$this->citationList .= sprintf('<div><a name="citation-%1$d"></a>' . | |
'<a href="#text-%1$d" alt="Citation Origin">%1$d</a><cite>%2$s</cite></div>', $citeID, $text); | |
} | |
/** | |
* Returns a list of citations, if there are any. | |
* | |
* @param | |
public function printCitations() | |
{ | |
if ($this->citationList) { | |
return "<div class='citations'>" . $this->citationList . "</div>"; | |
} | |
return ''; | |
} | |
public function theContent($content) | |
{ | |
return $content . '[print-citations]'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment