diff --git a/lib/Newsletter/Shortcodes/Categories/Link.php b/lib/Newsletter/Shortcodes/Categories/Link.php index f6f08c18bb..89f3a4c75f 100644 --- a/lib/Newsletter/Shortcodes/Categories/Link.php +++ b/lib/Newsletter/Shortcodes/Categories/Link.php @@ -85,6 +85,7 @@ class Link implements CategoryInterface { $newsletterModel, $subscriberModel, $queueModel, + $shortcodeDetails['arguments'], $wpUserPreview ); diff --git a/lib/Newsletter/Shortcodes/Shortcodes.php b/lib/Newsletter/Shortcodes/Shortcodes.php index c6dd46d6e6..f764583edd 100644 --- a/lib/Newsletter/Shortcodes/Shortcodes.php +++ b/lib/Newsletter/Shortcodes/Shortcodes.php @@ -80,19 +80,60 @@ class Shortcodes { false; } + /** + * Parse a MailPoet-style shortcode. + * The syntax is [category:action | argument:argument_value], it can have a single argument. + */ public function match($shortcode) { preg_match( '/\[(?P\w+)?:(?P\w+)(?:.*?\|.*?(?P\w+):(?P.*?))?\]/', $shortcode, $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; } + /** + * 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 = '') { $processedShortcodes = []; foreach ($shortcodes as $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['category'] = !empty($shortcodeDetails['category']) ? $shortcodeDetails['category'] : @@ -106,6 +147,8 @@ class Shortcodes { $shortcodeDetails['action_argument_value'] = !empty($shortcodeDetails['argument_value']) ? $shortcodeDetails['argument_value'] : false; + $shortcodeDetails['arguments'] = !empty($shortcodeDetails['arguments']) ? + $shortcodeDetails['arguments'] : []; $category = strtolower($shortcodeDetails['category']); $categoryClass = $this->getCategoryObject($category); @@ -126,6 +169,7 @@ class Shortcodes { $this->subscriber, $this->queue, $content, + $shortcodeDetails['arguments'], $this->wpUserPreview ); $processedShortcodes[] = ($customShortcode === $shortcode) ? diff --git a/lib/WP/Functions.php b/lib/WP/Functions.php index 7571eea963..a88dbf4595 100644 --- a/lib/WP/Functions.php +++ b/lib/WP/Functions.php @@ -435,6 +435,10 @@ class Functions { return delete_transient($transient); } + public function shortcodeParseAtts($text) { + return shortcode_parse_atts($text); + } + public function singlePostTitle($prefix = '', $display = true) { return single_post_title($prefix, $display); }