/** * External dependencies */ import { isUndefined, pickBy } from 'lodash'; /** * WordPress dependencies */ import { registerBlockType } from '@wordpress/blocks'; import getEditComponent from './getEditComponent'; import { withSelect } from '@wordpress/data'; import { Path, SVG } from '@wordpress/components'; const name = 'advanced-posts-blocks/children'; const edit = getEditComponent( name ); registerBlockType( name, { title: 'Children Posts (Advanced Posts Blocks)', icon: ( //feathericon#site-map ), category: 'widgets', supports: { align: [ 'wide', 'full' ], html: false, }, edit: withSelect( ( select, props ) => { const { attributes } = props; const { postsToShow, order, orderBy, postType: postTypeName } = attributes; const { postId } = attributes; const { getEntityRecords, getPostType, getPostTypes } = select( 'core' ); const { getCurrentPostId, getCurrentPostType } = select( 'core/editor' ); const postTypes = getPostTypes( { per_page: -1 } ) || []; let selectedPostType; if ( postTypeName ) { selectedPostType = getPostType( postTypeName ) || {}; } else { selectedPostType = getPostType( getCurrentPostType() ) || {}; } const PostsQuery = pickBy( { orderby: orderBy, per_page: -1, }, ( value ) => ! isUndefined( value ) ); const childrenPostsQuery = pickBy( { order, parent: postId ? postId : getCurrentPostId(), orderby: orderBy, per_page: postsToShow, }, ( value ) => ! isUndefined( value ) ); return { postId, posts: getEntityRecords( 'postType', selectedPostType.slug, PostsQuery ) || [], children: getEntityRecords( 'postType', selectedPostType.slug, childrenPostsQuery ) || [], selectedPostType, postTypes: postTypes .filter( postType => postType.hierarchical ) .filter( postType => postType.viewable ) .filter( postType => postType.rest_base !== 'media' ), }; } )( edit ), save() { return null; }, } );