403Webshell
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/hoc/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /www/html/apt.mitrofanov.ru/wordpress/wp-content/plugins/generateblocks/src/hoc/withUniqueId.js
import { useEffect } from '@wordpress/element';
import getEditorBlocks from '../utils/get-editor-blocks';
import { useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
 * Search all blocks for uniqueIds
 *
 * @param {Array} blocks The blocks array
 * @return {Array} The array of uniqueIds
 */
export const getUniqueIdFromBlocks = ( blocks ) => blocks
	.reduce( ( result, block ) => {
		if (
			( block.name && block.name.includes( 'generateblocks' ) ) &&
			( block.attributes && block.attributes.uniqueId )
		) {
			result.uniqueIds.push( block.attributes.uniqueId );
			result.clientIds.push( block.clientId );
		}

		if ( block.innerBlocks ) {
			const { uniqueIds, clientIds } = getUniqueIdFromBlocks( block.innerBlocks );
			result.uniqueIds = result.uniqueIds.concat( uniqueIds );
			result.clientIds = result.clientIds.concat( clientIds );
		}

		return result;
	}, { uniqueIds: [], clientIds: [] } );

/**
 * Generates a unique id based on the clientId
 *
 * @param {string} clientId The block clientId
 * @return {string} The uniqueId
 */
export const generateUniqueId = ( clientId ) => clientId.substr( 2, 9 ).replace( '-', '' );

/**
 * Checks if the array contains duplicates of the value
 *
 * @param {Array}  arr          The array to check the values
 * @param {any}    value        The value to check if has duplicates
 * @param {number} currentIndex The current index
 * @return {boolean} If the array has duplicates
 */
export const hasDuplicates = ( arr, value, currentIndex ) => (
	arr.filter( ( el ) => ( el === value ) ).length > 1 &&
	currentIndex === arr.lastIndexOf( value )
);

/**
 * It will enhance a block component with the attributes.uniqueId property
 *
 * @param {any} WrappedComponent The component to add the uniqueId
 * @return {Function} The wrapped component
 */
export default ( WrappedComponent ) => ( ( props ) => {
	const { clientId, attributes } = props;
	const { updateBlockAttributes } = useDispatch( blockEditorStore );

	useEffect( () => {
		const { uniqueIds, clientIds } = getUniqueIdFromBlocks( getEditorBlocks() );

		if (
			! attributes.uniqueId ||
			hasDuplicates( uniqueIds, attributes.uniqueId, clientIds.indexOf( clientId ) )
		) {
			const uniqueId = generateUniqueId( clientId );

			updateBlockAttributes( clientId, { uniqueId } );
		}
	}, [ clientId ] );

	return ( <WrappedComponent { ...props } /> );
} );

Youez - 2016 - github.com/yon3zu
LinuXploit