Created
November 18, 2016 13:47
-
-
Save rehmatworks/f1f8e5dc2a0558b2ba4a02fca8b1fea4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SimpleShortcodes { | |
private $_shortcodes = array(); | |
public function __construct() { | |
/* | |
You can use a predefined array that contains shortcodes in bulk | |
global $my_shortcode_array; | |
$this->_shortcodes = $my_shortcode_array; | |
*/ | |
} | |
public function add($shortcode = array()) { | |
$this->_shortcodes = array_merge($shortcode, $this->_shortcodes); | |
} | |
public function read($message) { | |
$shortcodes = array(); | |
$values = array(); | |
foreach($this->_shortcodes AS $shortcode => $variable) { | |
$cleaned = str_replace('{$', '', $variable); | |
$cleaned = str_replace('}', '', $cleaned); | |
global $$cleaned; | |
$shortcodes[] = $shortcode; | |
$values[] = $variable; | |
} | |
$message = str_replace($shortcodes, $values, $message); | |
foreach($values AS $value) { | |
$cleaned = str_replace('{$', '', $value); | |
$cleaned = str_replace('}', '', $cleaned); | |
$message = strtr($message, array($value => $$cleaned)); | |
} | |
return $message; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment