loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$scripts = $dom->getElementsByTagName('script');
$styles = $dom->getElementsByTagName('style');
$remove = array();
foreach ($scripts as $item) {
$remove[] = $item;
}
foreach ($styles as $item) {
$remove[] = $item;
}
foreach ($remove as $item) {
$item->parentNode->removeChild($item);
}
$html = $dom->saveHTML();
$text = strip_tags($html);
$text = trim(preg_replace('/\s\s+/', ' ', $text));
if (!$text) {
return '';
}
$text = substr($text, 0, $length);
return $text;
}
/**
* Render content for Recent Posts block
*
* @param array $attributes Attributes of the block
*
* @return null
*/
function advgbRenderBlockRecentPosts($attributes)
{
$recent_posts = wp_get_recent_posts(
array(
'numberposts' => empty($attributes['numberOfPosts'])?8:$attributes['numberOfPosts'],
'post_status' => 'publish',
'order' => empty($attributes['order'])?'desc':$attributes['order'],
'orderby' => empty($attributes['orderBy'])?'date':$attributes['orderBy'],
'category' => empty($attributes['category'])?0:$attributes['category'],
),
OBJECT
);
$saved_settings = get_option('advgb_settings');
$default_thumb = plugins_url('assets/blocks/recent-posts/recent-post-default.png', ADVANCED_GUTENBERG_PLUGIN);
$rp_default_thumb = isset($saved_settings['rp_default_thumb']) ? $saved_settings['rp_default_thumb'] : array('url' => $default_thumb, 'id' => 0);
$postHtml = '';
foreach ($recent_posts as $post) {
$postThumbID = get_post_thumbnail_id($post->ID);
$postHtml .= '';
if ($postThumbID) {
$postThumb = wp_get_attachment_image($postThumbID, 'large');
} else {
if ($rp_default_thumb['id']) {
$postThumb = wp_get_attachment_image($rp_default_thumb['id'], 'large');
}
}
$postHtml .= sprintf(
'',
get_permalink($post->ID),
$postThumb
);
}
$postHtml .= '
%2$s
',
get_permalink($post->ID),
get_the_title($post->ID)
);
$postHtml .= '