! empty( $attributes['query']['numberOfItems'] ) ? $attributes['query']['numberOfItems'] : 10, 'post_status' => 'publish', 'order' => ! empty( $attributes['query']['order'] ) ? $attributes['query']['order'] : 'desc', 'orderby' => ! empty( $attributes['query']['orderBy'] ) ? $attributes['query']['orderBy'] : 'date', 'suppress_filters' => false, 'post__not_in' => array( $post->ID ), ); if ( isset( $attributes['query']['selectedCategories'] ) ) { $args['category__in'] = array_column( $attributes['query']['selectedCategories'], 'id' ); } $recent_posts = get_posts( $args ); $formatted_posts = gutenify_get_post_info( $recent_posts ); $block_style = null; if ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-horizontal' ) !== false ) { $block_style = 'horizontal'; } elseif ( isset( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ) { $block_style = 'stacked'; } return gutenify_post_list( $formatted_posts, $attributes ); } /** * Renders the list and grid styles. * * @param array $posts Current posts. * @param array $attributes The block attributes. * * @return string Returns the block content for the list and grid styles. */ function gutenify_post_list( $posts, $attributes ) { $client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : ''; $class_name = array( 'wp-block-gutenify-post-list', 'gutenify-section-' . $attributes['blockClientId'], 'gutenify-post-list-' . $attributes['layout'] ); $class = array( 'gutenify-post-list' ); $styles = array(); $block_style = ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'is-style-stacked' ) !== false ? 'stacked' : 'horizontal'; $columns = ! empty( $attributes['columns'] ) ? $attributes['columns'] : 1; if ( isset( $attributes['className'] ) ) { array_push( $class_name, $attributes['className'] ); } if ( isset( $attributes['align'] ) ) { array_push( $class_name, 'align' . $attributes['align'] ); } if ( isset( $attributes['columns'] ) ) { array_push( $class, 'has-columns has-' . $attributes['columns'] . '-columns has-responsive-columns' ); } if ( isset( $attributes['listPosition'] ) && 'right' === $attributes['listPosition'] && 'horizontal' === $block_style ) { array_push( $class, 'has-image-right' ); } if ( isset( $attributes['imageSize'] ) && 'horizontal' === $block_style ) { array_push( $class, 'has-' . $attributes['imageSize'] . '-image' ); } if ( isset( $attributes['imageStyle'] ) ) { array_push( $class, 'has-' . $attributes['imageStyle'] . '-image' ); } if ( isset( $columns ) ) { array_push( $class_name, 'gutenify-post-list-col-' . $columns ); } $class_name = apply_filters( 'gutenify_block_wrapper_class', esc_attr( implode( ' ', $class_name ) ), $attributes ); $block_content = sprintf( '
', $class_name, ); $list_items_markup = ''; foreach ( $posts as $post ) { $list_class = ''; $align_self_class = ''; $list_items_markup .= '
'; $list_items_markup .= '
'; if ( ! empty( $attributes['displayFeaturedImage'] ) && null !== $post['thumbnailURL'] && $post['thumbnailURL'] ) { $list_items_markup .= sprintf( '
', esc_url( $post['postLink'] ), esc_url( $post['thumbnailURL'] ) ); if ( 'horizontal' === $block_style && ( isset( $attributes['displayPostContent'] ) && ! $attributes['displayPostContent'] ) && ( isset( $attributes['columns'] ) && 2 >= $attributes['columns'] ) ) { $align_self_class = 'self-center'; } } else { $align_self_class = ' flex-start'; } $list_items_markup .= sprintf( '
', esc_attr( $align_self_class ) ); if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'stacked' === $block_style ) { $list_items_markup .= sprintf( '', esc_url( $post['date'] ), esc_html( $post['dateReadable'] ) ); } $title = $post['title']; if ( ! $title ) { $title = _x( '(no title)', 'placeholder when a post has no title', 'gutenify' ); } $list_items_markup .= sprintf( '

%2$s

', $post['postLink'], $title ); $meta_data = ''; if ( isset( $attributes['displayPostDate'] ) && $attributes['displayPostDate'] && 'horizontal' === $block_style ) { $meta_data .= sprintf( '', esc_html( $post['date'] ), esc_html( $post['dateReadable'] ) ); } if ( ! empty( $attributes['displayPostAuthor'] ) ) { if ( ! get_the_author_meta( 'description' ) && post_type_supports( get_post_type(), 'author' ) ) { $meta_data .= ''; } } if ( ! empty( $attributes['displayPostCategories'] ) ) { /* translators: used between list items, there is a space after the comma. */ $categories_list = get_the_category_list( __( ', ' ), '', $post['ID'] ); if ( $categories_list ) { $meta_data .= sprintf( '%s ', $categories_list // phpcs:ignore WordPress.Security.EscapeOutput ); } } if ( ! empty( $meta_data ) ) { $list_items_markup .= sprintf( '
%1$s
', $meta_data ); } if ( isset( $attributes['displayPostContent'] ) && $attributes['displayPostContent'] ) { $post_excerpt = $post['postExcerpt']; $trimmed_excerpt = esc_html( wp_trim_words( $post_excerpt, $attributes['excerptLength'], ' … ' ) ); $list_items_markup .= '
'; $list_items_markup .= sprintf( '

%1$s

', esc_html( $trimmed_excerpt ) ); if ( isset( $attributes['displayPostLink'] ) && $attributes['displayPostLink'] ) { $list_items_markup .= sprintf( '%2$s%3$s', esc_url( $post['postLink'] ), esc_html( $attributes['postLink'] ), esc_html( $post['title'] ) ); } $list_items_markup .= '
'; } $list_items_markup .= '
'; } $block_content .= $list_items_markup; $block_content .= '
'; $block_content .= '
'; ob_start(); echo $block_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $output = ob_get_contents(); ob_end_clean(); return $output; } /** * Registers the `posts` block on server. */ function gutenify_register_post_list_block() { // Return early if this function does not exist. if ( ! function_exists( 'register_block_type' ) ) { return; } // Load attributes from block.json. ob_start(); include GUTENIFY_PLUGIN_DIR . 'src/blocks/post-list/block.json'; $metadata = json_decode( ob_get_clean(), true ); register_block_type( 'gutenify/post-list', array( 'attributes' => $metadata['attributes'], 'render_callback' => 'gutenify_render_post_list_block', ) ); } add_action( 'init', 'gutenify_register_post_list_block' );