| Server IP : 185.252.147.100 / Your IP : 216.73.217.33 Web Server : nginx/1.27.3 System : Linux mitrofanov.ru 6.1.0-37-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22) x86_64 User : mitr ( 1000) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /www/html/apt.mitrofanov.ru/wordpress/wp-content/plugins/generateblocks/src/hooks/ |
Upload File : |
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import useRecordsReducer from './useRecordsReducer';
import { useEffect } from '@wordpress/element';
import { isUndefined } from 'lodash';
/**
* Returns list of authors.
*
* @param {Object} query The query params.
* @return {{isResolving: boolean, records: Object}} The result set.
*/
export default function useAuthors( query = {} ) {
return useSelect( ( select ) => {
const {
getEntityRecords,
isResolving,
} = select( coreStore );
const queryParams = Object.assign( {
per_page: -1,
who: 'authors',
}, query );
// We have to check for undefined "include" here and delete it
// because somehow core does not return results if hasOwnProperty( 'include' ) returns true
if ( queryParams.hasOwnProperty( 'include' ) && isUndefined( queryParams.include ) ) {
delete queryParams.include;
}
const entityParams = [ 'root', 'user', queryParams ];
return {
records: getEntityRecords( ...entityParams ) || [],
isResolving: isResolving( 'getEntityRecords', entityParams ),
};
}, [ JSON.stringify( query ) ] );
}
/**
* Return author records persisting previous calls.
*
* @param {Object} queryParams The query params.
* @return {{isLoading: boolean, records: Object}} The result set.
*/
export function usePersistentAuthors( queryParams = {} ) {
const {
records,
setRecords,
query,
setQuery,
isLoading,
setIsLoading,
} = useRecordsReducer( { query: queryParams } );
useEffect( () => {
setQuery( queryParams );
setIsLoading( true );
}, [ JSON.stringify( queryParams ) ] );
const { records: authors, isResolving } = useAuthors( query );
useEffect( () => {
setIsLoading( isResolving );
}, [ isResolving ] );
useEffect( () => {
setRecords( authors );
setIsLoading( false );
}, [ JSON.stringify( authors ) ] );
return {
records,
isLoading,
};
}