ID; $optout = get_post_meta( $postId, 'apprl__auto_transform_optout', true ); if( empty($optout) ) { $linksJson = get_post_meta( $postId, 'apprl__links', true ); // Replace old links with new links if( !empty($linksJson) ) { $content = apprl_do_auto_replace( $content, (array)json_decode( $linksJson ), $showAdLinkSpan ); } } } return $content; } function apprl_do_auto_replace( $content = '', $links = array(), $showAdLinkSpan) { if( is_array( $links ) && !empty( $links ) ) { $dom = new DOMDocument('1.0', 'UTF-8'); $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8')); $anchors = $dom->getElementsByTagName('a'); foreach( $links as $oldLink => $newLink ) { if( !empty( $newLink ) ) { foreach ($anchors as $anchor) { if($anchor->getAttribute('href') == $oldLink) { $anchor->setAttribute('href', $newLink); if ($showAdLinkSpan == 'Yes') { $span = $dom->createElement('span', '(adlink)'); $span->setAttribute('class', 'apprl-link'); $span->setAttribute('style', 'margin-left:0.5ex'); $anchor->parentNode->insertBefore($span, $anchor->nextSibling); } } } } } $html = ''; $nodes = $dom->documentElement->firstChild->childNodes; if (is_array($nodes) || is_object($nodes)) { foreach ($nodes as $node) { $html .= $dom->saveHTML($node); } } return $html; } else { // if no links return original content return $content; } }