(function ( wpI18n, wpBlocks, wpElement, wpEditor, wpComponents, wpData, lodash, wpHtmlEntities, wpDate ) { const { __ } = wpI18n; const { Component, Fragment } = wpElement; const { registerBlockType } = wpBlocks; const { InspectorControls, BlockControls } = wpEditor; const { PanelBody, RangeControl, ToggleControl, TextControl, QueryControls, Spinner, Toolbar, Placeholder, IconButton } = wpComponents; const { withSelect } = wpData; const { pickBy, isUndefined } = lodash; const { decodeEntities } = wpHtmlEntities; const { dateI18n, __experimentalGetSettings } = wpDate; const advRecentPostsBlockIcon = ( ); let initSlider = null; class RecentPostsEdit extends Component { constructor() { super( ...arguments ); this.state = { categoriesList: [], updating: false, } } componentWillMount() { const { attributes, setAttributes } = this.props; const currentBlockConfig = advgbDefaultConfig['advgb-recent-posts']; const categoriesListQuery = { per_page: -1, hide_empty: true, }; // No override attributes of blocks inserted before if (attributes.changed !== true) { if (typeof currentBlockConfig === 'object' && currentBlockConfig !== null) { Object.keys(currentBlockConfig).map((attribute) => { if (typeof attributes[attribute] === 'boolean') { attributes[attribute] = !!currentBlockConfig[attribute]; } else { attributes[attribute] = currentBlockConfig[attribute]; } }); } // Finally set changed attribute to true, so we don't modify anything again setAttributes( { changed: true } ); } wp.apiFetch( { path: wp.url.addQueryArgs( 'wp/v2/categories', categoriesListQuery ), } ).then( ( categoriesList ) => this.setState( { categoriesList: categoriesList } ) ) } componentWillUpdate( nextProps ) { const { recentPosts: nextPosts } = nextProps; const { postView: nextView } = nextProps.attributes; const { attributes, clientId, recentPosts } = this.props; const $ = jQuery; if (nextView !== 'slider' || (nextPosts && recentPosts && nextPosts.length !== recentPosts.length) ) { $(`#block-${clientId} .advgb-recent-posts.slick-initialized`).slick('unslick'); $(`#block-${clientId} .advgb-recent-post`) .removeAttr('tabindex') .removeAttr('role') .removeAttr('aria-describedby'); if (nextView === 'slider' && (nextPosts && recentPosts && nextPosts.length !== recentPosts.length)) { if (!this.state.updating) { this.setState( { updating: true } ); } } if (initSlider) { clearTimeout(initSlider); } } } componentDidUpdate( prevProps ) { const that = this; const { attributes, clientId } = this.props; const { postView } = attributes; const $ = jQuery; if (postView === 'slider') { initSlider = setTimeout(function () { $(`#block-${clientId} .advgb-recent-posts-block.slider-view .advgb-recent-posts:not(.slick-initialized)`).slick( { dots: true, adaptiveHeight: true, } ); if (that.state.updating) { that.setState( { updating: false } ); } }, 100 ); } else { $(`#block-${clientId} .advgb-recent-posts.slick-initialized`).slick('unslick'); } } static extractContent(html, length) { const span= document.createElement('span'); span.innerHTML= html; // Remove script tag const scripts = span.getElementsByTagName('script'); let j = scripts.length; while (j--) { scripts[j].parentNode.removeChild(scripts[j]); } // Remove style tag const styles = span.getElementsByTagName('style'); let k = styles.length; while (k--) { styles[k].parentNode.removeChild(styles[k]); } const children= span.querySelectorAll('*'); for(let i = 0 ; i < children.length ; i++) { if(children[i].textContent) children[i].textContent += ' '; else children[i].innerText += ' '; } let text = [span.textContent || span.innerText].toString().replace(/\s\s+/g,' '); text = text.slice(0, length).trim(); if (text.length) text += '…' ; return text; }; render() { const { categoriesList } = this.state; const { attributes, setAttributes, recentPosts } = this.props; const { postView, order, orderBy, category, numberOfPosts, columns, displayFeaturedImage, displayAuthor, displayDate, displayExcerpt, postTextAsExcerpt, postTextExcerptLength, displayReadMore, readMoreLbl, } = attributes; const inspectorControls = ( setAttributes( { order: value } ) } onOrderByChange={ ( value ) => setAttributes( { orderBy: value } ) } onCategoryChange={ ( value ) => setAttributes( { category: value !== '' ? value : undefined } ) } onNumberOfItemsChange={ (value) => setAttributes( { numberOfPosts: value } ) } /> {postView === 'grid' && setAttributes( { columns: value } ) } /> } setAttributes( { displayFeaturedImage: !displayFeaturedImage } ) } /> setAttributes( { displayAuthor: !displayAuthor } ) } /> setAttributes( { displayDate: !displayDate } ) } /> setAttributes( { displayReadMore: !displayReadMore } ) } /> {displayReadMore && setAttributes( { readMoreLbl: value } ) } /> } setAttributes( { displayExcerpt: !displayExcerpt } ) } /> {displayExcerpt && setAttributes( { postTextAsExcerpt: !postTextAsExcerpt } ) } /> } {displayExcerpt && postTextAsExcerpt && setAttributes( { postTextExcerptLength: value } ) } /> } ); const hasPosts = Array.isArray( recentPosts ) && recentPosts.length; // If no posts found we show this notice if (!hasPosts) { return ( { inspectorControls } { ! Array.isArray( recentPosts ) ? : __( 'No posts found! Try to change category or publish posts.' ) } ) } const postViewControls = [ { icon: 'grid-view', title: __( 'Grid View' ), onClick: () => setAttributes( { postView: 'grid' } ), isActive: postView === 'grid', }, { icon: 'list-view', title: __( 'List View' ), onClick: () => setAttributes( { postView: 'list' } ), isActive: postView === 'list', }, { icon: 'slides', title: __( 'Slider View' ), onClick: () => setAttributes( { postView: 'slider' } ), isActive: postView === 'slider', }, ]; const blockClassName = [ 'advgb-recent-posts-block', this.state.updating && 'loading', postView === 'grid' && 'columns-' + columns, postView === 'grid' && 'grid-view', postView === 'list' && 'list-view', postView === 'slider' && 'slider-view', ].filter( Boolean ).join( ' ' ); const dateFormat = __experimentalGetSettings().formats.date; return ( { inspectorControls } setAttributes( { myToken: Math.floor(Math.random() * Math.floor(999)) } ) } />
{this.state.updating &&
}
{recentPosts.map( ( post, index ) => ( ) ) }
) } } registerBlockType( 'advgb/recent-posts', { title: __( 'Recent Posts' ), description: __( 'Display your recent posts in slider or grid view with beautiful styles.' ), icon: { src: advRecentPostsBlockIcon, foreground: typeof advgbBlocks !== 'undefined' ? advgbBlocks.color : undefined, }, category: 'advgb-category', keywords: [ __( 'latest posts' ), __( 'posts slide' ), __( 'posts grid' ) ], supports: { html: false, }, edit: withSelect( ( select, props ) => { const { getEntityRecords } = select( 'core' ); const { category, order, orderBy, numberOfPosts, myToken } = props.attributes; const recentPostsQuery = pickBy( { categories: category, order, orderby: orderBy, per_page: numberOfPosts, token: myToken, }, ( value ) => !isUndefined( value ) ); return { recentPosts: getEntityRecords( 'postType', 'post', recentPostsQuery ), } } )( RecentPostsEdit ), save: function () { // Render in PHP return null; }, } ) })( wp.i18n, wp.blocks, wp.element, wp.editor, wp.components, wp.data, lodash, wp.htmlEntities, wp.date );