/** * Extract selected items array from html multiselect tag * * @param array Html multiselect tag * @return array Selected items array */ private function getSelectedItems($html) { $dom = new domDocument(); $dom->loadHTML($html); $dom->preserveWhiteSpace = false; $options = $dom->getElementsByTagname('option'); $items = array(); foreach ($options as $option) { if ($option->hasAttribute('selected')) { $items[] = $option->nodeValue; } } return $items; }
/** * * from http://www.phpro.org/examples/Get-Text-Between-Tags.html * * @get text between tags * @param string $tag The tag name * @param string $html The XML or XHTML string * @param int $strict Whether to use strict mode * @return array * */ function getTextBetweenTags($tag, $html, $strict = 0) { $dom = new domDocument(); if ($strict == 1) { $dom->loadXML($html); } else { $dom->loadHTML($html); } $dom->preserveWhiteSpace = false; $content = $dom->getElementsByTagname($tag); $out = array(); foreach ($content as $item) { $out[] = $item->nodeValue; } return $out; }
protected function formatCode($article) { echo $cont = $article['content']; $dom = new \domDocument(); $dom->loadHTML($cont); $dom->preserveWhiteSpace = false; $codeelement = $dom->getElementsByTagname('code'); $i = $codeelement->length - 1; while ($i > -1) { $element = $codeelement->item($i); echo $element->nodeValue; echo "<br />"; echo $newelement = htmlentities($element->nodeValue); exit; $element->parentNode->replaceChild($newelement, $element); $i--; } exit; return $article; }
function getTextBetweenTags($tag, $html, $strict = 0) { /*** a new dom object ***/ //$dom = new domDocument; $dom = new domDocument('1.0', 'UTF-8'); /*** load the html into the object ***/ $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"); @$dom->loadHTML($html); /*** discard white space ***/ $dom->preserveWhiteSpace = false; /*** the tag by its tag name ***/ $content = $dom->getElementsByTagname($tag); /*** the array to return ***/ ////$out = array(); $out = ""; foreach ($content as $item) { /*** add node value to the out array ***/ $out .= "\"" . $item->nodeValue . "\", "; } /*** return the results ***/ return $out; }
/** * * @get text between tags * * @param string $tag The tag name * * @param string $html The XML or XHTML string * * @param int $strict Whether to use strict mode * * @return array * */ function getTextBetweenTags($tag, $html, $strict = 0) { /*** a new dom object ***/ $dom = new domDocument(); /*** load the html into the object ***/ if ($strict == 1) { $dom->loadXML($html); } else { $dom->loadHTML($html); } /*** discard white space ***/ $dom->preserveWhiteSpace = false; /*** the tag by its tag name ***/ $content = $dom->getElementsByTagname($tag); /*** the array to return ***/ $out = array(); foreach ($content as $item) { /*** add node value to the out array ***/ $out[] = $item->nodeValue; } /*** return the results ***/ return $out; }
echo "--------- Remove Attribute Node\n"; $attr = $rootnode->removeAttribute("src"); print "Removed " . $attr . " attributes.\n"; echo "--------- attributes of rootnode\n"; $attrs = $rootnode->attributes; print_node_list($attrs); echo "--------- children of an attribute\n"; $children = $attrs->item(0)->childNodes; print_node_list($children); echo "--------- Add child to root\n"; $myelement = new domElement("Silly", "Symphony"); $newchild = $rootnode->appendChild($myelement); print_node($newchild); print $dom->saveXML(); print "\n"; echo "--------- Find element by tagname\n"; echo " Using dom\n"; $children = $dom->getElementsByTagname("Silly"); print_node_list($children); echo " Using elem\n"; $children = $rootnode->getElementsByTagName("Silly"); print_node_list($children); echo "--------- Unlink Node\n"; print_node($children->item(0)); $rootnode->removeChild($children->item(0)); print_node_list($rootnode->childNodes); print $dom->savexml(); echo "--------- Find element by id\n"; print "Not implemented\n"; echo "--------- Check various node_name return values\n"; print "Not needed\n";
/** * Build background HTML */ function uncode_get_back_html($background = array(), $overlay_color = '', $overlay_color_alpha = '', $overlay_pattern = '', $type) { global $front_background_colors; $back_color = $back_url = $back_repeat = $back_position = $back_attachment = $back_size = $background_mime = $background_url = $header_background_video = $header_background_selfvideo = $header_overlay_html = $header_overlay_style = $header_overlay_pattern_style = $back_mime_css = $back_html = $content_html = $carousel_html = $overlay_html = ''; $poster_id = $is_carousel = $content_only_text = false; if (!empty($background['background-image'])) { $media_ids = explode(',', $background['background-image']); if (count($media_ids) === 1) { $back_attributes = uncode_get_media_info($background['background-image']); if (isset($back_attributes->post_mime_type)) { $background_mime = $back_attributes->post_mime_type; } $back_repeat = isset($background['background-repeat']) && $background['background-repeat'] !== '' ? 'background-repeat: ' . $background['background-repeat'] . ';' : ''; $back_position = isset($background['background-position']) && $background['background-position'] !== '' ? 'background-position: ' . $background['background-position'] . ';' : ''; $back_attachment = isset($background['background-attachment']) && $background['background-attachment'] !== '' ? 'background-attachment: ' . $background['background-attachment'] . ';' : ''; $back_size = isset($background['background-size']) && $background['background-size'] !== '' ? 'background-size: ' . $background['background-size'] . ';' : ''; if (strpos($background_mime, 'video/') !== false || $background_mime === 'oembed/vimeo' || $background_mime === 'oembed/youtube') { if (wp_is_mobile()) { $background_mime = 'mobile_no_video'; } } if (strpos($background_mime, 'image/') !== false) { $back_metavalues = unserialize($back_attributes->metadata); $image_orig_w = $back_metavalues['width']; $image_orig_h = $back_metavalues['height']; $resized_back = uncode_resize_image($back_attributes->guid, $back_attributes->path, $image_orig_w, $image_orig_h, 12, '', false); $background_url = $resized_back['url']; $back_url = $background_url != '' ? 'background-image: url(' . $background_url . ');' : ''; } else { if (strpos($background_mime, 'video/') !== false) { $videos = array(); $exloded_url = explode(".", strtolower($back_attributes->guid)); $ext = end($exloded_url); $videos[(string) $ext] = $back_attributes->guid; $alt_videos = get_post_meta($background['background-image'], "_uncode_video_alternative", true); if (!empty($alt_videos)) { foreach ($alt_videos as $key => $value) { $exloded_url = explode(".", strtolower($value)); $ext = end($exloded_url); $videos[(string) $ext] = $value; } } else { $videos = array('src' => '"' . $back_attributes->guid . '"'); } $video_src = ''; foreach ($videos as $key => $value) { $video_src .= ' ' . $key . '=' . $value; } $back_mime_css = ' self-video uncode-video-container'; add_filter("wp_video_shortcode_class", "uncode_back_video_class", 10, 2); $header_background_selfvideo = apply_filters('the_content', '[video' . $video_src . ' loop="y"]'); $header_background_selfvideo = str_replace('controls="controls"', '', $header_background_selfvideo); remove_filter("wp_video_shortcode_class", "uncode_back_video_class"); } else { switch ($background_mime) { case 'oembed/flickr': case 'oembed/Imgur': case 'oembed/photobucket': $back_oembed = wp_oembed_get($back_attributes->guid); preg_match_all('/src="([^"]*)"/i', $back_oembed, $img_src); $back_url = isset($img_src[1][0]) ? 'background-image: url(' . str_replace('"', '', $img_src[1][0]) . ');' : ''; if ($background_mime === 'oembed/flickr') { $back_url = str_replace('_n.', '_b.', $back_url); } $background_url = $back_url; $background_mime = 'image'; break; case 'oembed/instagram': $url = 'http://api.instagram.com/oembed?url=' . $back_attributes->guid; $json = wp_remote_fopen($url); $json_data = json_decode($json, true); $background_url = $back_url = $json_data['thumbnail_url']; $back_url = isset($json_data['thumbnail_url']) ? 'background-image: url(' . esc_url($json_data['thumbnail_url']) . ');' : ''; $background_mime = 'image'; break; case 'oembed/vimeo': case 'oembed/youtube': $back_metavalues = unserialize($back_attributes->metadata); $video_orig_w = $back_metavalues['width']; $video_orig_h = $back_metavalues['height']; $video_ratio = $video_orig_h === 0 ? 1.777 : $video_orig_w / $video_orig_h; $header_background_video = ' data-ratio="' . $video_ratio . '" data-provider="' . ($background_mime === 'oembed/vimeo' ? 'vimeo' : 'youtube') . '" data-video="' . $back_attributes->guid . '" data-id="' . rand(10000, 99999) . '"'; $back_mime_css = ' video uncode-video-container'; break; case 'oembed/soundcloud': $url = $back_attributes->guid; $accent_color = $front_background_colors['accent']; $accent_color = str_replace('#', '', $accent_color); $getValues = wp_remote_fopen('http://soundcloud.com/oembed?format=js&url=' . $url . '&iframe=true'); $decodeiFrame = substr($getValues, 1, -2); $decodeiFrame = json_decode($decodeiFrame); preg_match('/src="([^"]+)"/', $decodeiFrame->html, $iframe_src); $iframe_url = str_replace('visual=true', 'visual=false', $iframe_src[1]); $content_html = '<iframe width="100%" scrolling="no" frameborder="no" src="' . $iframe_url . '&color=' . $accent_color . '&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>'; break; case 'oembed/twitter': $url = 'https://api.twitter.com/1/statuses/oembed.json?id=' . basename($back_attributes->guid); $json = wp_remote_fopen($url); $json_data = json_decode($json, true); $id = basename($json_data['url']); $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $json_data['html']); $html = str_replace("— ", '', $html); $dom = new domDocument(); $dom->loadHTML($html); $dom->preserveWhiteSpace = false; $twitter_content = $dom->getElementsByTagname('blockquote'); $twitter_blockquote = ''; $twitter_footer = ''; foreach ($twitter_content as $item) { $twitter_content_inner = $item->getElementsByTagname('p'); foreach ($twitter_content_inner as $item_inner) { foreach ($item_inner->childNodes as $child) { $twitter_blockquote .= $child->ownerDocument->saveXML($child); } $item_inner->parentNode->removeChild($item_inner); } foreach ($item->childNodes as $child) { $twitter_footer .= $child->ownerDocument->saveXML($child); } $item->parentNode->removeChild($item); } $content_html = '<div class="twitter-item"> <div class="twitter-item-data"> <blockquote class="tweet-text pullquote"> <p>' . $twitter_blockquote . '</p>'; $content_html .= '<p class="twitter-footer"><small>' . $twitter_footer . '</small></p>'; $content_html .= '</blockquote> </div> </div>'; $poster = get_post_meta($background['background-image'], "_uncode_poster_image", true); if ($poster !== '') { $poster_attributes = uncode_get_media_info($poster); $media_metavalues = unserialize($poster_attributes->metadata); $image_orig_w = $media_metavalues['width']; $image_orig_h = $media_metavalues['height']; $resized_image = uncode_resize_image($poster_attributes->guid, $poster_attributes->path, $image_orig_w, $image_orig_h, 12, '', false); $poster_url = $resized_image['url']; if (isset($poster_attributes->post_mime_type)) { $background_mime = $poster_attributes->post_mime_type; } if (strpos($background_mime, 'image/') !== false) { $background_url = $poster_url; $back_url = $background_url !== '' ? 'background-image: url(' . $background_url . ');' : ''; } else { $back_oembed = wp_oembed_get($poster_attributes->guid); preg_match_all('/src="([^"]*)"/i', $back_oembed, $img_src); $back_url = isset($img_src[1][0]) ? 'background-image: url(' . str_replace('"', '', $img_src[1][0]) . ');' : ''; } $poster_id = $background['background-image']; } $content_only_text = true; break; case 'oembed/html': if (isset($back_attributes->post_excerpt) && $back_attributes->post_excerpt !== '') { $author = '<p><small>' . $back_attributes->post_excerpt . '</small></p>'; } else { $author = ''; } $content_html = '<blockquote class="pullquote"><p>' . $back_attributes->post_content . '</p>' . $author . '</blockquote>'; $poster = get_post_meta($background['background-image'], "_uncode_poster_image", true); if ($poster !== '') { $poster_attributes = uncode_get_media_info($poster); $media_metavalues = unserialize($poster_attributes->metadata); $image_orig_w = $media_metavalues['width']; $image_orig_h = $media_metavalues['height']; $resized_image = uncode_resize_image($poster_attributes->guid, $poster_attributes->path, $image_orig_w, $image_orig_h, 12, '', false); $poster_url = $resized_image['url']; if (isset($poster_attributes->post_mime_type)) { $background_mime = $poster_attributes->post_mime_type; } if (strpos($background_mime, 'image/') !== false) { $background_url = $poster_url; $back_url = $background_url !== '' ? 'background-image: url(' . $background_url . ');' : ''; } else { $back_oembed = wp_oembed_get($poster_attributes->guid); preg_match_all('/src="([^"]*)"/i', $back_oembed, $img_src); $back_url = isset($img_src[1][0]) ? 'background-image: url(' . str_replace('"', '', $img_src[1][0]) . ');' : ''; } $poster_id = $background['background-image']; } $content_only_text = true; break; default: if (strpos($background_mime, 'audio/') !== false) { $content_html = apply_filters('the_content', '[audio src="' . $back_attributes->guid . '"]'); } else { if ($background_mime === 'oembed/spotify') { $content_html = wp_oembed_get($back_attributes->guid); } } $poster = get_post_meta($background['background-image'], "_uncode_poster_image", true); if ($poster !== '') { $poster_attributes = uncode_get_media_info($poster); $media_metavalues = unserialize($poster_attributes->metadata); $image_orig_w = $media_metavalues['width']; $image_orig_h = $media_metavalues['height']; $resized_image = uncode_resize_image($poster_attributes->guid, $poster_attributes->path, $image_orig_w, $image_orig_h, 12, '', false); $poster_url = $resized_image['url']; if (isset($poster_attributes->post_mime_type)) { $background_mime = $poster_attributes->post_mime_type; } if (strpos($background_mime, 'image/') !== false) { $background_url = $poster_url; $back_url = $background_url !== '' ? 'background-image: url(' . $background_url . ');' : ''; } else { $back_oembed = wp_oembed_get($poster_attributes->guid); preg_match_all('/src="([^"]*)"/i', $back_oembed, $img_src); $back_url = isset($img_src[1][0]) ? 'background-image: url(' . str_replace('"', '', $img_src[1][0]) . ');' : ''; } $poster_id = $background['background-image']; } break; } } } } else { $carousel_html = do_shortcode('[vc_gallery el_id="gallery-' . rand() . '" medias="' . $background['background-image'] . '" type="carousel" style_preset="metro" single_padding="0" carousel_fluid="yes" carousel_lg="1" carousel_md="1" carousel_sm="1" gutter_size="0" media_items="media" carousel_interval="0" carousel_dots="yes" carousel_autoh="no" carousel_type="fade" carousel_nav="no" carousel_dots_inside="yes" single_text="overlay" single_border="yes" single_width="12" single_height="12" single_text_visible="no" single_text_anim="no" single_overlay_visible="no" single_overlay_anim="no" single_image_anim="no"]'); $is_carousel = true; } } if (isset($background['background-color']) && $background['background-color'] !== '') { $back_color = ' style-' . $background['background-color'] . '-bg'; } if ($overlay_color !== '') { $overlay_color = ' style-' . $overlay_color . '-bg'; } if ($overlay_color_alpha !== '' && $overlay_color !== '') { $overlay_color_alpha = ' style="opacity: ' . $overlay_color_alpha / 100 . ';"'; } else { $overlay_color_alpha = ''; } if (!empty($overlay_pattern)) { $overlay_pattern = ' uncode-' . $overlay_pattern; } if (!empty($overlay_pattern) && $overlay_pattern !== '') { $header_overlay_pattern_style = '<div class="header-bg-overlay-inner' . $overlay_pattern . '"' . $overlay_color_alpha . '></div>'; } if (!empty($overlay_color) && $overlay_color !== '') { $header_overlay_style = '<div class="header-bg-overlay-inner' . $overlay_color . '"' . $overlay_color_alpha . '></div>'; } if ($header_overlay_style !== '' || $header_overlay_pattern_style !== '') { $header_overlay_html = '<div class="header-bg-overlay">' . $header_overlay_style . $header_overlay_pattern_style . ' </div>'; } $back_image = $back_url != '' || $back_repeat != '' || $back_position != '' || $back_attachment != '' || $back_size != '' ? ' style="' . $back_url . $back_repeat . $back_position . $back_attachment . $back_size . '"' : ''; if ($overlay_color !== '') { $overlay_html = '<div class="block-bg-overlay' . $overlay_color . '"' . $overlay_color_alpha . '></div>'; } if ($type === 'row') { $back_html = '<div class="row-background background-element"' . ($back_mime_css === '' && $header_background_video === '' && $back_image === '' && $header_background_selfvideo === '' && $back_html === '' ? ' style="opacity: 1;"' : '') . '> <div class="background-wrapper"> <div class="background-inner' . $back_mime_css . '"' . $header_background_video . $back_image . '>' . $header_background_selfvideo . $back_html . '</div> ' . $overlay_html . ' </div> </div>'; } else { if ($type === 'column') { $back_html = '<div class="column-background background-element"' . ($back_mime_css === '' && $header_background_video === '' && $back_image === '' && $header_background_selfvideo === '' && $back_html === '' ? ' style="opacity: 1;"' : '') . '> <div class="background-wrapper"> <div class="background-inner' . $back_mime_css . '"' . $header_background_video . $back_image . '>' . $header_background_selfvideo . $back_html . '</div> ' . $overlay_html . ' </div> </div>'; } else { if ($type === 'div') { if ($header_background_video !== '' || $back_image !== '' || $header_background_selfvideo !== '' || $back_html !== '') { $back_html = '<div class="main-background background-element"> <div class="' . $back_mime_css . '"' . $header_background_video . $back_image . '>' . $header_background_selfvideo . $back_html . '</div> </div>'; } } else { if ($overlay_html !== '' || $header_background_video !== '' || $back_image !== '' || $header_background_selfvideo !== '' || $carousel_html !== '') { $back_html = '<div class="header-bg-wrapper"> <div class="header-bg' . $back_mime_css . ($carousel_html !== '' ? ' header-carousel-wrapper' : '') . '"' . $header_background_video . $back_image . '>' . $header_background_selfvideo . $carousel_html . '</div> ' . $overlay_html . ' </div>'; } } } } return array('back_color' => $back_color, 'back_html' => $back_html, 'content_html' => $content_html, 'content_only_text' => $content_only_text, 'back_url' => $background_url, 'poster_id' => $poster_id, 'is_carousel' => $is_carousel, 'mime' => $background_mime); }
/** * Returns the text between 2 tags * * @return array * @param string $tag The tag. * @param string $html The HTML to search in. * @param bool[optional] $strict Use strictmode? */ private function getTextBetweenTags($tag, $html, $strict = false) { // new dom document $dom = new domDocument(); // load HTML $strict == true ? $dom->loadXML($html) : $dom->loadHTML($html); // discard whitespace $dom->preserveWhiteSpace = false; // the array with results $results = array(); // fetch the tag by name $content = $dom->getElementsByTagname($tag); // loop the content foreach ($content as $item) { // add node value to results $results[] = $item->nodeValue; } // return the results return $results; }
/** * oEmbed helper * @param [int] $id * @param [string] $url * @return [array] */ function uncode_get_oembed($id, $url, $mime, $with_poster = false, $excerpt = null, $html = null, $lighbox_code = false) { global $front_background_colors; $object_class = $poster = $poster_id = ''; $oembed_size = uncode_get_dummy_size($id); $media_type = 'other'; if ($with_poster) { $poster = get_post_meta($id, "_uncode_poster_image", true); $poster_id = $poster; } switch ($mime) { case 'oembed/flickr': case 'oembed/Imgur': case 'oembed/photobucket': $media_type = 'image'; $media_oembed = wp_oembed_get($url); preg_match_all('/src="([^"]*)"/i', $media_oembed, $img_src); $media_oembed = isset($img_src[1][0]) ? str_replace('"', '', $img_src[1][0]) : ''; if ($mime === 'oembed/flickr') { $media_oembed = str_replace('_n.', '_b.', $media_oembed); } break; case 'oembed/instagram': $media_type = 'image'; $url = 'http://api.instagram.com/oembed?url=' . $url; $json = wp_remote_fopen($url); $json_data = json_decode($json, true); $media_oembed = $json_data['thumbnail_url']; $oembed_size['width'] = $oembed_size['height'] = 640; break; case 'oembed/youtube': if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { $get_url = parse_url($url); if (isset($get_url['query'])) { $get_id = explode('=', $get_url['query']); $src_id = $get_id[1]; } else { $src_id = basename($url); } $media_oembed = 'https://www.youtube.com/embed/' . $src_id; } else { $media_oembed = wp_oembed_get($url); } break; case 'oembed/vimeo': if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { $media_oembed = 'https://player.vimeo.com/video/' . basename($url); } else { $media_oembed = wp_oembed_get($url); $media_oembed = str_replace(basename($url), basename($url) . '?title=0&byline=0&portrait=0', $media_oembed); } break; case 'oembed/soundcloud': if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { //Get the JSON data of song details with embed code from SoundCloud oEmbed $getValues = wp_remote_fopen('http://soundcloud.com/oembed?format=js&url=' . $url . '&iframe=true'); //Clean the Json to decode $decodeiFrame = substr($getValues, 1, -2); //json decode to convert it as an array $decodeiFrame = json_decode($decodeiFrame); preg_match('/src="([^"]+)"/', $decodeiFrame->html, $iframe_src); $media_oembed = $iframe_src[1]; } else { $accent_color = $front_background_colors['accent']; $accent_color = str_replace('#', '', $accent_color); $getValues = wp_remote_fopen('http://soundcloud.com/oembed?format=js&url=' . $url . '&iframe=true'); $decodeiFrame = substr($getValues, 1, -2); $decodeiFrame = json_decode($decodeiFrame); if (isset($decodeiFrame->html)) { preg_match('/src="([^"]+)"/', $decodeiFrame->html, $iframe_src); $iframe_url = str_replace('visual=true', 'visual=false', $iframe_src[1]); $media_oembed = '<iframe width="100%" scrolling="no" frameborder="no" src="' . $iframe_url . '&color=' . $accent_color . '&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>'; if (strpos($iframe_url, 'playlist') !== false) { $object_class = 'soundcloud-playlist'; } else { $object_class = 'soundcloud-single'; } } else { $media_oembed = '<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=media+not+available&w=500&h=500" />'; } } break; case 'oembed/spotify': if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { $get_url = parse_url($url); $break_spotify = explode('/', $get_url['path']); $media_oembed = 'https://embed.spotify.com/?uri=spotify' . implode(':', $break_spotify); } else { $media_oembed = wp_oembed_get($url); $media_oembed = preg_replace('#\\s(width)="([^"]+)"#', '', $media_oembed); $media_oembed = preg_replace('#\\s(height)="([^"]+)"#', '', $media_oembed); $object_class = 'object-size spotify'; } break; case 'oembed/twitter': $url = 'https://api.twitter.com/1/statuses/oembed.json?id=' . basename($url); $json = wp_remote_fopen($url); $json_data = json_decode($json, true); $id = basename($json_data['url']); $html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $json_data['html']); $html = str_replace("— ", '', $html); $dom = new domDocument(); $dom->loadHTML($html); $dom->preserveWhiteSpace = false; $twitter_content = $dom->getElementsByTagname('blockquote'); $twitter_blockquote = ''; $twitter_footer = ''; foreach ($twitter_content as $item) { $twitter_content_inner = $item->getElementsByTagname('p'); foreach ($twitter_content_inner as $item_inner) { foreach ($item_inner->childNodes as $child) { $twitter_blockquote .= $child->ownerDocument->saveXML($child); } $item_inner->parentNode->removeChild($item_inner); } foreach ($item->childNodes as $child) { $twitter_footer .= $child->ownerDocument->saveXML($child); } $item->parentNode->removeChild($item); } $media_oembed = '<div class="twitter-item"> <div class="twitter-item-data"> <blockquote class="tweet-text pullquote"> <p>' . $twitter_blockquote . '</p>'; $media_oembed .= '<p class="twitter-footer"><i class="fa fa-twitter"></i><small>' . $twitter_footer . '</small></p>'; $media_oembed .= '</blockquote> </div> </div>'; $width = 1; $height = 0; $object_class = 'tweet object-size regular-text'; break; case 'oembed/html': $author = $author_img = ''; $width = 1; $height = 0; $poster = get_post_meta($id, "_uncode_poster_image", true); $poster_id = $poster; if ($poster !== '' && $with_poster || $lighbox_code) { $attr = array('class' => "avatar"); $author_img = wp_get_attachment_image($poster, 'thumbnail', false, $attr); $author_img = '<figure class="gravatar">' . $author_img . '</figure>'; } if ($excerpt) { $author = '<p><small>' . $excerpt . '</small></p>'; } $media_oembed = '<blockquote class="pullquote">' . $author_img . '<p>' . esc_html($html) . '</p>' . $author . '</blockquote>'; $object_class = 'regular-text object-size'; $poster = ''; break; case 'shortcode': $media_oembed = do_shortcode($url); $object_class = 'object-size shortcode'; break; default: if (strpos($mime, 'audio/') !== false) { if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { $media_oembed = $url; } else { $object_class = 'object-size self-audio'; $media_oembed = apply_filters('the_content', '[audio src="' . $url . '"]'); $poster = get_post_meta($id, "_uncode_poster_image", true); $poster_id = $poster; } } else { if (strpos($mime, 'video/') !== false) { if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) { $media_oembed = $url; } else { $videos = array(); $exloded_url = explode(".", strtolower($url)); $ext = end($exloded_url); $videos[(string) $ext] = $url; $alt_videos = get_post_meta($id, "_uncode_video_alternative", true); if (!empty($alt_videos)) { foreach ($alt_videos as $key => $value) { $exloded_url = explode(".", strtolower($value)); $ext = end($exloded_url); $videos[(string) $ext] = $value; } } else { $videos = array('src' => '"' . $url . '"'); } $video_src = ''; foreach ($videos as $key => $value) { $video_src .= ' ' . $key . '=' . $value; } $object_class = 'object-size self-video'; $poster = get_post_meta($id, "_uncode_poster_image", true); $poster_url = ''; if (isset($poster) && $poster !== '') { $poster_attributes = uncode_get_media_info($poster); if (isset($poster_attributes->metadata)) { $media_metavalues = unserialize($poster_attributes->metadata); $image_orig_w = $media_metavalues['width']; $image_orig_h = $media_metavalues['height']; $resized_image = uncode_resize_image($poster_attributes->guid, $poster_attributes->path, $image_orig_w, $image_orig_h, 12, '', false); $poster_url = $resized_image['url']; } } if ($poster_url !== '') { $poster_url = ' poster="' . $poster_url . '"'; } $media_oembed = apply_filters('the_content', '[video' . $video_src . $poster_url . ']'); } } else { $media_oembed = wp_oembed_get($url); } } break; } if ($oembed_size['dummy'] == 0) { preg_match_all('/width="([^"]*)"/i', $media_oembed, $iWidth); $width = isset($iWidth[1][0]) ? $iWidth[1][0] : 1; preg_match_all('/height="([^"]*)"/i', $media_oembed, $iHeight); $height = isset($iHeight[1][0]) ? $iHeight[1][0] : 1; $oembed_size['dummy'] = round($height / $width * 100, 2); } return array('code' => $media_oembed, 'width' => $oembed_size['width'], 'height' => $oembed_size['height'], 'dummy' => $oembed_size['dummy'], 'type' => $media_type, 'class' => $object_class, 'poster' => $poster, 'poster_id' => $poster_id); }
/** * * @get text between tags * @param string $tag The tag name * @param string $html The XML or XHTML string * @param int $strict Whether to use strict mode * @param string $encoding * @return array */ private function getTextBetweenTags($tag, $html, $strict = 0, $encoding = "UTF-8") { global $PAGE, $CFG; if (!isset($CFG->filter_jsxgraph_divid)) { set_config('filter_jsxgraph_divid', 'box'); } if (!isset($CFG->filter_jsxgraph_boardvar)) { set_config('filter_jsxgraph_boardvar', 'board'); } if (!isset($CFG->filter_jsxgraph_width)) { set_config('filter_jsxgraph_width', '500'); } if (!isset($CFG->filter_jsxgraph_height)) { set_config('filter_jsxgraph_height', '400'); } // a new dom object $dom = new domDocument(); $dom->formatOutput = true; // load the html into the object if ($strict == 1) { $dom->loadXML($html); } else { libxml_use_internal_errors(true); $htmlutf8 = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding); $dom->loadHTML($htmlutf8); libxml_use_internal_errors(false); } // discard white space $dom->preserveWhiteSpace = false; $dom->strictErrorChecking = false; $dom->recover = true; // the tag by its tag name $content = $dom->getElementsByTagname($tag); if (count($content) > 0) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/jsxgraphcore.js')); } // Iterate backwards through the jsxgraph tags $i = $content->length - 1; while ($i > -1) { $item = $content->item($i); // Read attributes $w = $item->getAttribute('width'); if ($w == "") { $w = $CFG->filter_jsxgraph_width; } $h = $item->getAttribute('height'); if ($h == "") { $h = $CFG->filter_jsxgraph_height; } $b = $item->getAttribute('box'); if ($b == "") { $b = $CFG->filter_jsxgraph_divid . $i; } $brd = $item->getAttribute('board'); if ($brd == "") { $brd = $CFG->filter_jsxgraph_boardvar . $i; } /* Create new div element containing JSXGraph */ $out = $dom->createElement('div'); $a = $dom->createAttribute('id'); $a->value = $b; $out->appendChild($a); $a = $dom->createAttribute('class'); $a->value = "jxgbox"; $out->appendChild($a); $a = $dom->createAttribute('style'); $a->value = "width:" . $w . "px; height:" . $h . "px; "; $out->appendChild($a); $t = $dom->createTextNode(""); $out->appendChild($t); $out = $dom->appendChild($out); // Replace <jsxgraph> by <div> $item->parentNode->replaceChild($out, $item); $code = ""; $needGXT = false; $url = $item->getAttribute('file'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromFile('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { $url = $item->getAttribute('filestring'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromString('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { // Plain JavaScript code $code = $item->nodeValue; } } // Place JavaScript code at the end of the page. $PAGE->requires->js_init_call($code); if ($needGXT) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/GeonextReader.js')); } --$i; } // remove DOCTYPE $dom->removeChild($dom->firstChild); // remove <html><body></body></html> //$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); //return $dom->saveXML(); $str = $dom->saveHTML(); $str = str_replace("<body>", "", $str); $str = str_replace("</body>", "", $str); $str = str_replace("<html>", "", $str); $str = str_replace("</html>", "", $str); return $str; }
function tagwrap($html, $width = 75, $break = "nr") { $html = '<div>' . $html . '</div>'; //using dom object $dom = new domDocument(); //load the html into the object $dom->loadXML($html); // preserve white space $dom->preserveWhiteSpace = true; //getting all tags $content = $dom->getElementsByTagname("*"); $html = ""; foreach ($content as $item) { //wrapping contents of tags, function described above is used, //but you can use your own function or simple wordwrap() php function $item->nodeValue = mb_wordwrap($item->nodeValue, $width, $break); $html .= $dom->saveXML($item); } //return the results return $html; //html_entity_decode($html); }