1 回答
TA贡献1827条经验 获得超9个赞
经过一系列的反复试验,我自己设法让这个工作起来。并不像我想象的那么困难,DOMDocument 实际上负责一些删除逻辑本身。
/**
* Wrap handorgel groups in appropriate containers
*
* @param string $content
*
* @return string
*/
function gpl_wrap_handorgel_shortcodes(string $content): string {
if (! is_admin() && $content) {
$DOM = new DOMDocument();
// disable errors to get around HTML5 warnings...
libxml_use_internal_errors(true);
// load in content
$DOM->loadHTML(mb_convert_encoding("<html><body>{$content}</body></html>", "HTML-ENTITIES", "UTF-8"), LIBXML_HTML_NODEFDTD);
// reset errors to get around HTML5 warnings...
libxml_clear_errors();
$body = $DOM->getElementsByTagName("body");
$handorgels = [];
$prev_class = "";
foreach ($body[0]->childNodes as $element) {
/**
* Ensure that only HTML nodes get checked/modified
*/
if ($element->nodeType == 1) {
$current_class = $element->getAttribute("class");
/**
* Find any handorgel elements
*/
if (preg_match("/handorgel__/", $current_class)) {
$group = array_key_last($handorgels);
/**
* If the previous class didn't include `handorgel__`, create a new handorgel object
*/
if (! preg_match("/handorgel__/", $prev_class)) {
$handorgels[] = [
"container" => $DOM->createElement("div"),
"elements" => [],
];
/**
* Update `$group` to match the new container
*/
$group = array_key_last($handorgels);
}
/**
* Append the current element to the group to be moved after all sequential handorgel
* elements are located for its group
*/
$handorgels[$group]["elements"][] = $element;
}
/**
* Update `$prev_class` to track where handorgel groups should begin and end
*/
$prev_class = $current_class;
}
}
/**
* Construct the grouped handorgels
*/
if ($handorgels) {
foreach ($handorgels as $group => $handorgel) {
$handorgel["container"]->setAttribute("class", "handorgel");
foreach ($handorgel["elements"] as $key => $element) {
/**
* Insert the container in the starting position for the group
*/
if ($key === 0) {
$element->parentNode->insertBefore($handorgels[$group]["container"], $element);
}
$handorgel["container"]->appendChild($element);
}
}
}
/**
* Remove unneeded tags (inserted for parsing reasons)
*/
$content = remove_extra_tags($DOM); // custom function that removes html/body and outputs the DOMDocument as a string
}
return $content;
}
add_filter("the_content", "gpl_wrap_handorgel_shortcodes", 30, 1);
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报