<?php
/**
 * Per-instance visibility controls for site title and site tagline blocks.
 *
 * @package wuppi
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Whether a block is one of the site branding blocks managed by Wuppi.
 *
 * @param string $block_name Block name.
 * @return bool
 */
function wuppi_is_site_branding_block_name( $block_name ) {
	return in_array( (string) $block_name, array( 'core/site-title', 'core/site-tagline' ), true );
}

/**
 * Add the Wuppi visibility attribute to site branding blocks.
 *
 * @param array  $args       Block type registration args.
 * @param string $block_type Block name.
 * @return array
 */
function wuppi_register_site_branding_visibility_attribute( $args, $block_type ) {
	if ( ! wuppi_is_site_branding_block_name( $block_type ) ) {
		return $args;
	}

	if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
		$args['attributes'] = array();
	}

	$args['attributes']['wuppiHidden'] = array(
		'type'    => 'boolean',
		'default' => false,
	);

	return $args;
}
add_filter( 'register_block_type_args', 'wuppi_register_site_branding_visibility_attribute', 10, 2 );

/**
 * Whether a parsed block is hidden by the Wuppi visibility toggle.
 *
 * @param array $block Parsed block data.
 * @return bool
 */
function wuppi_is_site_branding_block_hidden( $block ) {
	if ( ! is_array( $block ) ) {
		return false;
	}

	$block_name = isset( $block['blockName'] ) ? (string) $block['blockName'] : '';

	if ( ! wuppi_is_site_branding_block_name( $block_name ) ) {
		return false;
	}

	return ! empty( $block['attrs']['wuppiHidden'] );
}

/**
 * Remove hidden site branding blocks from front-end rendering.
 *
 * @param string    $block_content Block markup.
 * @param array     $block         Parsed block data.
 * @param WP_Block  $instance      Block instance.
 * @return string
 */
function wuppi_filter_hidden_site_branding_block( $block_content, $block, $instance ) {
	unset( $instance );

	if ( ! wuppi_is_site_branding_block_hidden( $block ) ) {
		return $block_content;
	}

	return '';
}
add_filter( 'render_block_core/site-title', 'wuppi_filter_hidden_site_branding_block', 10, 3 );
add_filter( 'render_block_core/site-tagline', 'wuppi_filter_hidden_site_branding_block', 10, 3 );

/**
 * Whether block attributes contain a given CSS class.
 *
 * @param array  $attributes Block attributes.
 * @param string $class_name CSS class to check.
 * @return bool
 */
function wuppi_block_attributes_have_class( $attributes, $class_name ) {
	if ( ! is_array( $attributes ) || empty( $attributes['className'] ) ) {
		return false;
	}

	$classes = preg_split( '/\s+/', trim( (string) $attributes['className'] ) );

	if ( ! is_array( $classes ) ) {
		return false;
	}

	return in_array( (string) $class_name, array_filter( $classes ), true );
}

/**
 * Whether a branding group contains only hidden site branding blocks.
 *
 * @param array $block Parsed group block data.
 * @return bool
 */
function wuppi_group_contains_only_hidden_site_branding_blocks( $block ) {
	if ( ! is_array( $block ) || empty( $block['innerBlocks'] ) || ! is_array( $block['innerBlocks'] ) ) {
		return false;
	}

	$has_site_branding_block = false;

	foreach ( $block['innerBlocks'] as $inner_block ) {
		if ( ! is_array( $inner_block ) ) {
			return false;
		}

		$inner_block_name = isset( $inner_block['blockName'] ) ? (string) $inner_block['blockName'] : '';

		if ( '' === $inner_block_name ) {
			return false;
		}

		if ( ! wuppi_is_site_branding_block_name( $inner_block_name ) ) {
			return false;
		}

		$has_site_branding_block = true;

		if ( ! wuppi_is_site_branding_block_hidden( $inner_block ) ) {
			return false;
		}
	}

	return $has_site_branding_block;
}

/**
 * Remove the branding text wrapper when all site branding blocks are hidden.
 *
 * @param string    $block_content Block markup.
 * @param array     $block         Parsed block data.
 * @param WP_Block  $instance      Block instance.
 * @return string
 */
function wuppi_filter_empty_site_branding_group( $block_content, $block, $instance ) {
	unset( $instance );

	if ( ! is_array( $block ) || empty( $block['attrs'] ) || ! is_array( $block['attrs'] ) ) {
		return $block_content;
	}

	if ( ! wuppi_block_attributes_have_class( $block['attrs'], 'calm-branding-text' ) ) {
		return $block_content;
	}

	if ( ! wuppi_group_contains_only_hidden_site_branding_blocks( $block ) ) {
		return $block_content;
	}

	return '';
}
add_filter( 'render_block_core/group', 'wuppi_filter_empty_site_branding_group', 10, 3 );
