Add WP style shortcodes support to emails
Also pass shortcode arguments to custom shortcode functions [MAILPOET-3465]
This commit is contained in:
@ -85,6 +85,7 @@ class Link implements CategoryInterface {
|
|||||||
$newsletterModel,
|
$newsletterModel,
|
||||||
$subscriberModel,
|
$subscriberModel,
|
||||||
$queueModel,
|
$queueModel,
|
||||||
|
$shortcodeDetails['arguments'],
|
||||||
$wpUserPreview
|
$wpUserPreview
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -80,19 +80,60 @@ class Shortcodes {
|
|||||||
false;
|
false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a MailPoet-style shortcode.
|
||||||
|
* The syntax is [category:action | argument:argument_value], it can have a single argument.
|
||||||
|
*/
|
||||||
public function match($shortcode) {
|
public function match($shortcode) {
|
||||||
preg_match(
|
preg_match(
|
||||||
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?(?P<argument>\w+):(?P<argument_value>.*?))?\]/',
|
'/\[(?P<category>\w+)?:(?P<action>\w+)(?:.*?\|.*?(?P<argument>\w+):(?P<argument_value>.*?))?\]/',
|
||||||
$shortcode,
|
$shortcode,
|
||||||
$match
|
$match
|
||||||
);
|
);
|
||||||
|
// If argument exists, copy it to the arguments array
|
||||||
|
if (!empty($match['argument'])) {
|
||||||
|
$match['arguments'] = [$match['argument'] => isset($match['argument_value']) ? $match['argument_value'] : ''];
|
||||||
|
}
|
||||||
return $match;
|
return $match;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a WordPress-style shortcode.
|
||||||
|
* The syntax is [category:action arg1="value1" arg2="value2"], it can have multiple arguments.
|
||||||
|
*/
|
||||||
|
public function matchWPShortcode($shortcode) {
|
||||||
|
$atts = WPFunctions::get()->shortcodeParseAtts(trim($shortcode, '[]/'));
|
||||||
|
if (empty($atts[0])) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
$shortcodeName = $atts[0];
|
||||||
|
list($category, $action) = explode(':', $shortcodeName);
|
||||||
|
$shortcodeDetails = [];
|
||||||
|
$shortcodeDetails['category'] = $category;
|
||||||
|
$shortcodeDetails['action'] = $action;
|
||||||
|
$shortcodeDetails['arguments'] = [];
|
||||||
|
foreach ($atts as $attrName => $attrValue) {
|
||||||
|
if (is_numeric($attrName)) {
|
||||||
|
continue; // Skip unnamed attributes
|
||||||
|
}
|
||||||
|
$shortcodeDetails['arguments'][$attrName] = $attrValue;
|
||||||
|
// Make a shortcut to the first argument
|
||||||
|
if (!isset($shortcodeDetails['argument'])) {
|
||||||
|
$shortcodeDetails['argument'] = $attrName;
|
||||||
|
$shortcodeDetails['argument_value'] = $attrValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $shortcodeDetails;
|
||||||
|
}
|
||||||
|
|
||||||
public function process($shortcodes, $content = '') {
|
public function process($shortcodes, $content = '') {
|
||||||
$processedShortcodes = [];
|
$processedShortcodes = [];
|
||||||
foreach ($shortcodes as $shortcode) {
|
foreach ($shortcodes as $shortcode) {
|
||||||
$shortcodeDetails = $this->match($shortcode);
|
$shortcodeDetails = $this->match($shortcode);
|
||||||
|
if (empty($shortcodeDetails)) {
|
||||||
|
// Wrong MailPoet shortcode syntax, try to parse as a native WP shortcode
|
||||||
|
$shortcodeDetails = $this->matchWPShortcode($shortcode);
|
||||||
|
}
|
||||||
$shortcodeDetails['shortcode'] = $shortcode;
|
$shortcodeDetails['shortcode'] = $shortcode;
|
||||||
$shortcodeDetails['category'] = !empty($shortcodeDetails['category']) ?
|
$shortcodeDetails['category'] = !empty($shortcodeDetails['category']) ?
|
||||||
$shortcodeDetails['category'] :
|
$shortcodeDetails['category'] :
|
||||||
@ -106,6 +147,8 @@ class Shortcodes {
|
|||||||
$shortcodeDetails['action_argument_value'] = !empty($shortcodeDetails['argument_value']) ?
|
$shortcodeDetails['action_argument_value'] = !empty($shortcodeDetails['argument_value']) ?
|
||||||
$shortcodeDetails['argument_value'] :
|
$shortcodeDetails['argument_value'] :
|
||||||
false;
|
false;
|
||||||
|
$shortcodeDetails['arguments'] = !empty($shortcodeDetails['arguments']) ?
|
||||||
|
$shortcodeDetails['arguments'] : [];
|
||||||
|
|
||||||
$category = strtolower($shortcodeDetails['category']);
|
$category = strtolower($shortcodeDetails['category']);
|
||||||
$categoryClass = $this->getCategoryObject($category);
|
$categoryClass = $this->getCategoryObject($category);
|
||||||
@ -126,6 +169,7 @@ class Shortcodes {
|
|||||||
$this->subscriber,
|
$this->subscriber,
|
||||||
$this->queue,
|
$this->queue,
|
||||||
$content,
|
$content,
|
||||||
|
$shortcodeDetails['arguments'],
|
||||||
$this->wpUserPreview
|
$this->wpUserPreview
|
||||||
);
|
);
|
||||||
$processedShortcodes[] = ($customShortcode === $shortcode) ?
|
$processedShortcodes[] = ($customShortcode === $shortcode) ?
|
||||||
|
@ -435,6 +435,10 @@ class Functions {
|
|||||||
return delete_transient($transient);
|
return delete_transient($transient);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function shortcodeParseAtts($text) {
|
||||||
|
return shortcode_parse_atts($text);
|
||||||
|
}
|
||||||
|
|
||||||
public function singlePostTitle($prefix = '', $display = true) {
|
public function singlePostTitle($prefix = '', $display = true) {
|
||||||
return single_post_title($prefix, $display);
|
return single_post_title($prefix, $display);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user