/** * External dependencies */ import isUndefined from 'lodash/isUndefined'; import pickBy from 'lodash/pickBy'; import moment from 'moment'; import classnames from 'classnames'; import Inspector from './inspector'; import PostGridImage from './image'; const { compose } = wp.compose; const { Component, Fragment } = wp.element; const { __ } = wp.i18n; const { decodeEntities } = wp.htmlEntities; const { withSelect, } = wp.data; const { Placeholder, Spinner, Toolbar, } = wp.components; const { BlockAlignmentToolbar, BlockControls, } = wp.editor; class LatestPostsBlock extends Component { render() { const { attributes, setAttributes, latestPosts, } = this.props; // Check if there are posts const hasPosts = Array.isArray( latestPosts ) && latestPosts.length; // Check the post type const isPost = attributes.postType === 'post'; if ( ! hasPosts ) { return ( { ! Array.isArray( latestPosts ) ? : __( 'No posts found.', 'atomic-blocks' ) } ); } // Removing posts from display should be instant. const displayPosts = latestPosts.length > attributes.postsToShow ? latestPosts.slice( 0, attributes.postsToShow ) : latestPosts; // Add toolbar controls to change layout const layoutControls = [ { icon: 'grid-view', title: __( 'Grid View', 'atomic-blocks' ), onClick: () => setAttributes( { postLayout: 'grid' } ), isActive: attributes.postLayout === 'grid', }, { icon: 'list-view', title: __( 'List View', 'atomic-blocks' ), onClick: () => setAttributes( { postLayout: 'list' } ), isActive: attributes.postLayout === 'list', }, ]; // Get the section tag const SectionTag = attributes.sectionTag ? attributes.sectionTag : "section"; // Get the section title tag const SectionTitleTag = attributes.sectionTitleTag ? attributes.sectionTitleTag : "h2"; // Get the post title tag const PostTag = attributes.postTitleTag ? attributes.postTitleTag : "h3"; return ( { setAttributes( { align: value } ); } } controls={ [ 'center', 'wide', 'full' ] } /> { attributes.displaySectionTitle && attributes.sectionTitle && { attributes.sectionTitle } }
{ displayPosts.map( ( post, i ) =>
{ attributes.displayPostImage && post.featured_media ? ( ) : ( null ) }
{ attributes.displayPostTitle && { decodeEntities( post.title.rendered.trim() ) || __( '(Untitled)', 'atomic-blocks' ) } } { isPost &&
{ attributes.displayPostAuthor && post.author_info.display_name && } { attributes.displayPostDate && post.date_gmt && }
}
{ attributes.displayPostExcerpt && post.excerpt &&
} { attributes.displayPostLink &&

{ attributes.readMoreText }

}
) }
); } } export default compose( [ withSelect( ( select, props ) => { const { order, categories, } = props.attributes; const { getEntityRecords } = select( 'core', 'atomic-blocks' ); const latestPostsQuery = pickBy( { categories, order, orderby: props.attributes.orderBy, per_page: props.attributes.postsToShow, offset: props.attributes.offset, }, ( value ) => ! isUndefined( value ) ); return { latestPosts: getEntityRecords( 'postType', props.attributes.postType, latestPostsQuery ), }; } ), ] )( LatestPostsBlock ); // Truncate excerpt function truncate(str, no_words) { return str.split(" ").splice(0,no_words).join(" "); }