<?php
/**
 * Template part for displaying article
 *
 * @package errigal
 * @since 1.1.0
 *
 * @param array $args {
 *     Associative array of arguments for article.
 *
 *     @type callable        $render_callback Render callback. Default null.
 *     @type string|string[] $css_class       One or more classes to add to article class list.
 * }
 */

defined( 'ABSPATH' ) || exit;

use Errigal\Constant\{
	Errigal_Content_Hook as Content_Hook,
	Errigal_Html_Attr as Html_Attr,
	Errigal_Html_Option as Html_Option,
	Errigal_Template_Part as Template_Part,
};
use Errigal\Shared\{
	Errigal_Html_Builder as Html_Builder,
	Errigal_Validator as Validator,
};

$args = $args ?? array();
$args = wp_parse_args(
	$args,
	array(
		Template_Part::RENDER_CALLBACK => null,
		Template_Part::CSS_CLASS       => '',
	)
);

$render_callback = $args[ Template_Part::RENDER_CALLBACK ];
$css_class       = $args[ Template_Part::CSS_CLASS ];

if (
	! Validator::is_closure( $render_callback ) ||
	! ( is_string( $css_class ) || is_array( $css_class ) )
) {
	return;
}

Html_Builder::create()->create_article(
	array(
		Html_Option::ATTRS => array(
			Html_Attr::ID         => 'post-' . get_the_ID(),
			Html_Attr::CLASS_NAME => get_post_class( $css_class ),
		),
	)
)->render_element( $render_callback );
do_action( Content_Hook::AFTER_ARTICLE );
