Skip to content

Instantly share code, notes, and snippets.

@nickstewart95
Created December 15, 2020 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickstewart95/4ce52c73605ad58fb6538bbe55a4d929 to your computer and use it in GitHub Desktop.
Save nickstewart95/4ce52c73605ad58fb6538bbe55a4d929 to your computer and use it in GitHub Desktop.
foreach ($shortcodes as $key => $shortcode) {
if (
preg_match_all('/'. $pattern .'/s', $content, $matches) &&
array_key_exists(2, $matches) &&
in_array($shortcode, $matches[2])
) {
switch ($shortcode) {
case 'row':
$content = preg_replace('/\[' . $shortcode . '(.*?)\]/s', '<row>', $content);
$content = str_replace('[/row]', '</row>', $content);
break;
case 'intro':
$content = preg_replace('/\[' . $shortcode . '(.*?)\]/s', '<p>', $content);
$content = str_replace('[/' . $shortcode . ']', '</p>', $content);
break;
case ($shortcode == 'btn_primary' || $shortcode == 'arrow_callout'):
preg_match_all('/\[' . $shortcode . '(\s.*?)?\](?:([^\[]+)?\[\/' . $shortcode . '\])?/', $content, $matches);
foreach ($matches[0] as $match) {
preg_match_all('/(\w+)\s*=\s*"(.*?)"/', $match, $shortcode_attrs);
$attributes = [];
foreach ($shortcode_attrs[0] as $shortcode) {
preg_match_all('/([^\r\n\t\f\v= \'"]+)(?:=(["\'])?((?:.(?!\2?\s+(?:\S+)=|\2))+.)\2?)?/', $shortcode, $attr);
$attributes[$attr[1][0]] = $attr[3][0];
}
preg_match('/\](.*?)\[\/btn_primary]/', $match, $link_text);
$new = '<ctabutton href="' . $attributes['href'] . '">' . $link_text[1] . '</ctabutton>';
$content = str_replace($match, $new, $content);
}
break;
case 'gallery':
preg_match_all('/\[gallery(\s.*?)?\](?:([^\[]+)?\[\/gallery\])?/', $content, $matches);
foreach ($matches[0] as $match) {
preg_match_all('/(\w+)\s*=\s*"(.*?)"/', $match, $shortcode_attrs);
$attributes = [];
$image_string = '';
foreach ($shortcode_attrs[0] as $shortcode) {
preg_match_all('/([^\r\n\t\f\v= \'"]+)(?:=(["\'])?((?:.(?!\2?\s+(?:\S+)=|\2))+.)\2?)?/', $shortcode, $attr);
$attributes[$attr[1][0]] = $attr[3][0];
}
if (!empty($attributes['ids'])) {
$images = explode(',', $attributes['ids']);
}
foreach ($images as $image) {
$image_string .= '<galleryimage src="' . wp_get_attachment_url($image) . '"></galleryimage>';
}
$new = '<row>' . $image_string . '</row>';
$content = str_replace($match, $new, $content);
}
break;
default:
$content = preg_replace('/\[' . $shortcode . '(.*?)\]/s', '<div>', $content);
$content = str_replace('[/' . $shortcode . ']', '</div>', $content);
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment