<?php

/**
 * Render shorter user interface id for any block that has style or js attribute
 * $block = array(5) {
  ["blockName"]=> string(21) "dragblock/wrapper"
  ["attrs"]=> array(2) {}
  ["innerBlocks"]=>array(0) {}
  ["innerHTML"]=>string(113) ""
 */
global $dragblock_uids;
$dragblock_uids = array();
global $dragblock_preload;
$dragblock_preload = array();
/**
Parse the UIDs using render block data so other render process can use later
 */
add_filter('render_block_data', 'dragblock_uid_parser', 1, 2);
function dragblock_uid_parser($parsed_block, $source_block) {
	// var_dump('dragblock_uid_parser()');

    if (
        'core/null' === $parsed_block['blockName'] ||
        empty($parsed_block['attrs']['dragBlockClientId']) ||

        /**
        To keep the output source simple, we use this line but please
        @remove this when you need UIDs for other things than CSS and JS
        */
        
        (empty($parsed_block['attrs']['dragBlockCSS']) && empty($parsed_block['attrs']['dragBlockJS']))
        // stop here please
    ) {
        return $parsed_block;
    }

    global $dragblock_uids;

    /**     
     * We added $parsed_block['blockName'] as a prefix because some block like core/post-template
     * will have a clone as core/null which have the same attribute but will not be rendered
     */
    // $uid_key = $parsed_block['blockName'] . $parsed_block['attrs']['dragBlockClientId'];
    $uid_key = dragblock_uid_key($parsed_block);

    /*        
    Some blocks repeat themselves in block templates, ex: Comment Template block
    so this is to prevent collect the same block two times
    which will increase the count of $dragblock_uids incorrectly        
    */
    if (!empty($dragblock_uids[$uid_key])) {
        return $parsed_block;
    }

    $short_class = 'i' . count($dragblock_uids);
    $dragblock_uids[$uid_key] = $short_class;

    return $parsed_block;
}

/**
 * insert block uids
 * @var string $block_content the entire block html including itself's tag
 * @var array $parsed_block the block data saved in editor, ex: attributes, blockName...
 * @var WP_Block $block the block PHP object
 */
add_filter('render_block', 'dragblock_uid_inserter', 10, 3);
function dragblock_uid_inserter($block_content, $parsed_block, $block) {

	// var_dump('dragblock_uid_inserter()');
	


    if (
        'core/null' === $parsed_block['blockName'] ||
        empty($parsed_block['attrs']['dragBlockClientId']) ||

        /**
        To keep the output source simple, we use this line but please
        @remove this when you need UIDs for other things than CSS and JS
        */
        // stop here
        (empty($parsed_block['attrs']['dragBlockCSS']) && empty($parsed_block['attrs']['dragBlockJS']))

    ) {
        return $block_content;
    }
    global $dragblock_uids;
    // $uid_key = $parsed_block['blockName'] . $parsed_block['attrs']['dragBlockClientId'];
    $uid_key = dragblock_uid_key($parsed_block);

    // insert the class into the content class name attribute
    $class_key = 'class="';
    $class_start = strpos($block_content, $class_key);
    $class_end = strpos($block_content, '>');

    // this is not a normal block
    if (false === $class_end) {
        return $block_content;
    }

    // this has no classname attribute, we insert by ourselves
    if (false === $class_start) {        
        return (
            substr($block_content, 0, $class_end) .
            ' class="' . ($dragblock_uids[$uid_key]) . '"' .
            substr($block_content, $class_end)
        );
    }

    if ($class_start >= $class_end) {
        return $block_content;
    }

    return (
        substr($block_content, 0, $class_start + strlen($class_key)) .
        ($dragblock_uids[$uid_key]) . ' ' .
        substr($block_content, $class_start + strlen($class_key))
    );
}