PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.8.2
GenerateBlocks v1.8.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / extend / dynamic-content / hooks / usePostRecord.js
generateblocks / src / extend / dynamic-content / hooks Last commit date
useDynamicContent.js 4 years ago usePostRecord.js 3 years ago usePostTypeRecords.js 3 years ago usePostTypes.js 4 years ago
usePostRecord.js
98 lines
1 import { useSelect } from '@wordpress/data';
2 import { store as coreStore } from '@wordpress/core-data';
3 import { applyFilters } from '@wordpress/hooks';
4
5 export default function usePostRecord( postType, postId, load = [], options = {} ) {
6 return useSelect( ( select ) => {
7 const {
8 getUser,
9 isResolving,
10 getEntityRecord,
11 getEntityRecords,
12 hasFinishedResolution,
13 } = select( coreStore );
14
15 // Post data fetching.
16 const params = applyFilters(
17 'generateblocks.editor.dynamicContent.post-request-params',
18 [ 'postType', postType, postId ]
19 );
20
21 let postRecord = getEntityRecord( ...params );
22
23 const postRecordIsLoading = (
24 ! hasFinishedResolution( 'getEntityRecord', params ) ||
25 isResolving( 'getEntityRecord', params )
26 );
27
28 // Author data fetching.
29 let authorIsLoading = false;
30
31 if ( load.includes( 'author' ) && ! postRecordIsLoading && !! postRecord ) {
32 const authorParams = applyFilters(
33 'generateblocks.editor.dynamicContent.author-request-params',
34 [ postRecord.author ]
35 );
36
37 const author = getUser( ...authorParams );
38
39 authorIsLoading = (
40 ! hasFinishedResolution( 'getUser', authorParams ) ||
41 isResolving( 'getUser', authorParams )
42 );
43
44 if ( ! authorIsLoading && !! author ) {
45 postRecord = Object.assign( {}, postRecord, { author } );
46 }
47 }
48
49 // Comments data fetching.
50 let commentsIsLoading = false;
51
52 if ( load.includes( 'comments' ) && ! postRecordIsLoading && !! postRecord ) {
53 const commentsParams = applyFilters(
54 'generateblocks.editor.dynamicContent.comments-request-params',
55 [ 'root', 'comment', { post: postId } ]
56 );
57 const comments = getEntityRecords( ...commentsParams );
58
59 commentsIsLoading = (
60 ! hasFinishedResolution( 'getEntityRecords', commentsParams ) ||
61 isResolving( 'getEntityRecords', commentsParams )
62 );
63
64 if ( ! commentsIsLoading && !! comments ) {
65 postRecord = Object.assign( {}, postRecord, { comments } );
66 }
67 }
68
69 // Terms data fetching.
70 let termsIsLoading = false;
71
72 if ( load.includes( 'terms' ) && ! postRecordIsLoading && !! postRecord ) {
73 const termParams = applyFilters(
74 'generateblocks.editor.dynamicContent.terms-request-params',
75 [ 'taxonomy', options.taxonomy, { post: postId } ]
76 );
77 const terms = getEntityRecords( ...termParams );
78
79 termsIsLoading = (
80 ! hasFinishedResolution( 'getEntityRecords', termParams ) ||
81 isResolving( 'getEntityRecords', termParams )
82 );
83
84 if ( ! termsIsLoading && !! terms ) {
85 postRecord = Object.assign( {}, postRecord, { terms } );
86 }
87 }
88
89 return {
90 record: applyFilters(
91 'generateblocks.editor.dynamicContent.postRecord',
92 postRecord
93 ),
94 isLoading: ( postRecordIsLoading || authorIsLoading || commentsIsLoading || termsIsLoading ),
95 };
96 }, [ postType, postId, load.join(), JSON.stringify( options ) ] );
97 }
98