newsletter = $newsletter; $this->subscriber = $subscriber; } function extract($text) { preg_match_all('/\[(?:\w+):.*?\]/', $text, $shortcodes); return array_unique($shortcodes[0]); } function match($shortcode) { preg_match( '/\[(?P\w+):(?P\w+)(?:.*?default:(?P.*?))?\]/', $shortcode, $match ); return $match; } function process($shortcodes, $text) { $processed_shortcodes = array_map( function($shortcode) use($text) { $shortcode_details = $this->match($shortcode); $shortcode_type = ucfirst($shortcode_details['type']); $shortcode_action = $shortcode_details['action']; $shortcode_class = __NAMESPACE__ . '\\Categories\\' . $shortcode_type; $shortcode_default_value = isset($shortcode_details['default']) ? $shortcode_details['default'] : false; if(!class_exists($shortcode_class)) return false; return $shortcode_class::process( $shortcode_action, $shortcode_default_value, $this->newsletter, $this->subscriber, $text ); }, $shortcodes); return $processed_shortcodes; } function replace($text) { $shortcodes = $this->extract($text); $processed_shortcodes = $this->process($shortcodes, $text); $shortcodes = array_intersect_key($shortcodes, $processed_shortcodes); return str_replace($shortcodes, $processed_shortcodes, $text); } }