replacement_str) pairs for str_replace. */ public function process( string $content, string $link, string $new_link, array $tags ) { if ( empty( $content ) ) { return array(); } $dom = new \DOMDocument(); $replacements = array(); libxml_use_internal_errors( true ); $dom->loadHTML( $content ); foreach ( $tags as $tag_name => $tag_atts ) { foreach ( $tag_atts as $tag_att ) { foreach ( $dom->getElementsByTagName( $tag_name ) as $dom_link ) { $search_markup = ''; $replacement_str = ''; $old_link = $dom_link->getAttribute( $tag_att ); $link = untrailingslashit( trim( $link, '\'"' ) ); $old_link = untrailingslashit( trim( $old_link, '\'"' ) ); if ( $this->link_processor->links_match( $link, $old_link ) ) { $search_markup = $dom->saveHTML( $dom_link ); $replacement_str = $dom_link->nodeValue; //phpcs:ignore -- Using camelCase to match the DOMDocument property name. // If already processed this exact markup, skip to avoid redundant callbacks and replacements. if ( ! empty( $replacements[ $search_markup ] ) ) { continue; } $special_actions = $this->link_processor->content_special_actions( $content ); if ( ! empty( $special_actions ) ) { $replacement_str = $search_markup; // Loop through any special actions and apply their callbacks to the replacement string. foreach ( $special_actions as $special_case_key => $callback ) { if ( is_callable( $callback ) ) { $replacement_str = call_user_func( $callback, $replacement_str, ); } } $replacements[ $search_markup ] = $replacement_str; continue; } // If the 'unlink_wrap' filter is enabled, wrap the inner text in a // with a class and data attribute for the original URL. // This allows styling of unlinked items and preserves the original URL in a data attribute. if ( apply_filters( 'wpmudev_blc_link_action_unlink_wrap', false, $link, $new_link ) ) { $replacement_el = $dom->createElement( 'span', $replacement_str ); $replacement_el->setAttribute( 'class', apply_filters( 'wpmudev_blc_link_action_unlink_wrap_class', 'blc_unlinked', $link, $new_link ) ); $replacement_el->setAttribute( 'data-blc-orig-url', $link ); $replacement_str = $dom->saveHTML( $replacement_el ); } $replacements[ $search_markup ] = $replacement_str; } } } } return $replacements; } }