Skip to content

Instantly share code, notes, and snippets.

@rehmatworks
Created November 18, 2016 13:47
Show Gist options
  • Save rehmatworks/f1f8e5dc2a0558b2ba4a02fca8b1fea4 to your computer and use it in GitHub Desktop.
Save rehmatworks/f1f8e5dc2a0558b2ba4a02fca8b1fea4 to your computer and use it in GitHub Desktop.
<?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