function _panopoly_wysiwyg_process_caption_filter in Panopoly WYSIWYG 7
Filter process callback. Add image style class to caption wrapper div.
1 string reference to '_panopoly_wysiwyg_process_caption_filter'
- panopoly_wysiwyg_filter_info in ./
panopoly_wysiwyg.module - Implements hook_filter_info()
File
- ./
panopoly_wysiwyg.module, line 385
Code
function _panopoly_wysiwyg_process_caption_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
// We are going to use DOMDocument to parse this which causes $text to default to Latin-1 unless there's
// a charset meta tag because of course there is no server header here.
$text = '<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><body>' . $text . '</body>';
// Prevent parse errors from becoming PHP notices.
if (function_exists('libxml_use_internal_errors')) {
$use_errors = libxml_use_internal_errors(TRUE);
}
$doc = new DOMDocument();
$doc
->loadHTML($text);
$xpath = new DOMXpath($doc);
// We need this crazy XPath query because a simple [@class=caption] will not
// match if we have multiple classes and a "contains" query will match even
// for classes like "mycaption". We want to match only if the actual "caption"
// class is present, with or without other classes.
// See http://pivotallabs.com/xpath-css-class-matching/
$captions = $xpath
->query("//div[contains(concat(' ',normalize-space(@class),' '),' caption ')]");
foreach ($captions as $caption) {
$imgs = $caption
->getElementsByTagName("img");
foreach ($imgs as $img) {
$img_classes = $img
->getAttribute("class");
$img_path = $img
->getAttribute("src");
$server_path = str_replace('http://' . $_SERVER['HTTP_HOST'], $_SERVER['DOCUMENT_ROOT'], $img_path);
$doc_path = explode('?', $server_path);
$img_info = image_get_info($doc_path[0]);
// We only do this for panopoly image styles; we don't mess with other
// people's stuff.
preg_match("/panopoly-image-[a-z]+/", $img_classes, $panopoly_image_class);
}
if (empty($panopoly_image_class)) {
return $text;
}
$caption_classes = $caption
->getAttribute('class');
if (!empty($caption_classes)) {
$caption_classes = $caption_classes . ' ' . $panopoly_image_class[0];
}
$caption
->setAttribute('class', $caption_classes);
}
// Generate the HTML and remove everything except the body of the document.
$text = $doc
->saveHTML();
$text = preg_replace('/^.*?<body>/s', '', $text);
$text = preg_replace('/<\\/body>.*$/s', '', $text);
// Clear the error buffer and restore old error handling mode.
if (function_exists('libxml_use_internal_errors')) {
libxml_clear_errors();
libxml_use_internal_errors($use_errors);
}
return $text;
}