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 (isset($attributes['displayFeaturedImage']) && $attributes['displayFeaturedImage']) { $postThumb = ''; 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( '
%2$s
', get_permalink($post->ID), $postThumb ); } $postHtml .= '
'; $postHtml .= sprintf( '

%2$s

', get_permalink($post->ID), get_the_title($post->ID) ); $postHtml .= ''; // end advgb-post-info $postHtml .= '
'; if (isset($attributes['displayExcerpt']) && $attributes['displayExcerpt']) { $introText = $post->post_excerpt; if (isset($attributes['displayExcerpt']) && $attributes['postTextAsExcerpt']) { if (!is_admin()) { $postContent = get_post_field('post_content', $post->ID); $postContent = strip_shortcodes($postContent); $postContent = preg_replace('/)/is', '', $postContent); $introText = advgbExtractHtml($postContent, $attributes['postTextExcerptLength']); } } $postHtml .= sprintf( '
%1$s
', $introText ); } if (isset($attributes['displayReadMore']) && $attributes['displayReadMore']) { $readMoreText = __('Read More', 'advanced-gutenberg'); if (isset($attributes['readMoreLbl']) && $attributes['readMoreLbl']) { $readMoreText = $attributes['readMoreLbl']; } $postHtml .= sprintf( '
%2$s
', get_permalink($post->ID), $readMoreText ); } $postHtml .= '
'; // end advgb-post-content $postHtml .= '
'; // end advgb-post-wrapper $postHtml .= '
'; } $blockClass = ''; if ($attributes['postView'] === 'grid') { $blockClass = 'grid-view columns-' . $attributes['columns']; } elseif ($attributes['postView'] === 'list') { $blockClass = 'list-view'; } elseif ($attributes['postView'] === 'slider') { $blockClass = 'slider-view'; } if (isset($attributes['className'])) { $blockClass .= ' ' . $attributes['className']; } $blockHtml = sprintf( '
%1$s
', $postHtml, esc_attr($blockClass) ); return $blockHtml; } /** * Register block Recent Posts * * @return void */ function advgbRegisterBlockRecentPosts() { if (!function_exists('register_block_type')) { return; } register_block_type('advgb/recent-posts', array( 'attributes' => array( 'postView' => array( 'type' => 'string', 'default' => 'grid', ), 'order' => array( 'type' => 'string', 'default' => 'desc', ), 'orderBy' => array( 'type' => 'string', 'default' => 'date', ), 'category' => array( 'type' => 'string', ), 'numberOfPosts' => array( 'type' => 'number', 'default' => 8, ), 'columns' => array( 'type' => 'number', 'default' => 2, ), 'displayFeaturedImage' => array( 'type' => 'boolean', 'default' => true, ), 'displayAuthor' => array( 'type' => 'boolean', 'default' => false, ), 'displayDate' => array( 'type' => 'boolean', 'default' => false, ), 'displayExcerpt' => array( 'type' => 'boolean', 'default' => true, ), 'postTextAsExcerpt' => array( 'type' => 'boolean', 'default' => false, ), 'postTextExcerptLength' => array( 'type' => 'number', 'default' => 150, ), 'displayReadMore' => array( 'type' => 'boolean', 'default' => true, ), 'myToken' => array( 'type' => 'number', ), 'readMoreLbl' => array( 'type' => 'string', ) ), 'render_callback' => 'advgbRenderBlockRecentPosts', )); } add_action('init', 'advgbRegisterBlockRecentPosts');