<?php
/**
 * Register & render dynamic block.
 *
 * @package {{phpClassName}}
 */

namespace {{phpClassName}};

/**
 * Register & render dynamic block in php.
 *
 * @since {{version}}
 */
class DynamicBlock {
	/**
	 * Register class with appropriate WordPress hooks
	 */
	public static function register() {
		$instance = new self();
		add_action( 'init', array( $instance, 'register_block_type' ) );
	}

	/**
	 * Registers the `{{slug}}/dynamic-block` block on server.
	 */
	public function register_block_type() {
		// These scripts and styles are registered in includes/class-register-blocks.php.
		register_block_type_from_metadata(
			__DIR__,
			array(
				'render_callback' => array(
					$this,
					'render_block',
				),
			)
		);
	}

	/**
	 * Render dynamic block markup.
	 *
	 * @param Array  $attributes block attributes.
	 * @param String $content block inner content.
	 * @return string The dynamic block markup.
	 */
	public function render_block( $attributes, $content ) {

		$args = array(
			'numberposts'      => $attributes['postsToShow'],
			'post_status'      => 'publish',
			'order'            => $attributes['order'],
			'orderby'          => $attributes['orderBy'],
			'suppress_filters' => false,
		);

		if ( isset( $attributes['categories'] ) ) {
			$args['category__in'] = array_column( $attributes['categories'], 'id' );
		}

		$recent_posts = get_posts( $args );

		if ( count( $recent_posts ) === 0 ) {
			return 'No posts';
		}

		$markup = '';

		foreach ( $recent_posts as $post ) {
			$markup .= '<li>';

			// Add the featured image.
			if ( $attributes['showFeaturedImage'] && has_post_thumbnail( $post ) ) {
				$markup .= sprintf(
					'<div><a href="%1$s">%2$s</a></div>',
					esc_url( get_permalink( $post ) ),
					get_the_post_thumbnail(
						$post,
						$attributes['featuredImageSize']
					)
				);
			}

			// Add the post title.
			if ( isset( $attributes['showPostTitle'] ) && $attributes['showPostTitle'] ) {
				$markup .= sprintf(
					'<h3><a href="%1$s">%2$s</a></h3>',
					esc_url( get_permalink( $post ) ),
					esc_html( get_the_title( $post ) )
				);
			}

			// Add the post author.
			if ( isset( $attributes['showPostAuthor'] ) && $attributes['showPostAuthor'] ) {
				$markup .= sprintf(
					'<p>By: %1$s</p>',
					get_the_author_meta( 'display_name', $post->post_author )
				);
			}

			// Add the post date.
			if ( isset( $attributes['showPostDate'] ) && $attributes['showPostDate'] ) {
				$markup .= sprintf(
					'<p><time datetime="%1$s">%2$s</time></p>',
					esc_attr( get_the_date( 'c', $post ) ),
					esc_html( get_the_date( '', $post ) )
				);
			}

			// Add the post excerpt.
			if ( isset( $attributes['showPostExcerpt'] ) && $attributes['showPostExcerpt'] ) {
				$markup .= sprintf(
					'<div>%1$s</div>',
					get_the_excerpt( $post )
				);
			}

			$markup .= '</li>';
		}

		$class = 'wp-block-{{slug}}-dynamic-block';
		if ( isset( $attributes['align'] ) ) {
			$class .= ' align' . $attributes['align'];
		}

		return sprintf(
			'<div class="%1$s"><ul class="{{#tailwindcss}}list-none{{/tailwindcss}}{{#bootstrap}}list-unstyled{{/bootstrap}}">%2$s</ul></div>',
			$class,
			$markup
		);
	}
}

/**
 * Initialize class.
 *
 * @since {{version}}
 */
DynamicBlock::register();
