<?php
/**
 * Generate a Contributors block.
 *
 * @package p2020
 */

namespace Automattic\P2\Themes\P2020;

function get_contributors( int $post_author, int $limit = 3 ): array {
	global $wpdb;
	$post_id                       = get_queried_object_id();
	$query                         = $wpdb->prepare(
		"SELECT post_author, COUNT(post_author) as contributions
		FROM $wpdb->posts
		WHERE
			post_parent = %d
			AND post_type = 'revision'
			AND post_status = 'inherit'
			AND post_name NOT LIKE ( %s )
		GROUP BY post_author
		ORDER BY contributions DESC
		LIMIT %d",
		$post_id,
		'%' . $post_id . '-autosave%',
		$limit
	);
	$revisions_counts_result       = $wpdb->get_results( $query );
	$authors_by_contribution_count = array();

	foreach ( (array) $revisions_counts_result as $revisions_count ) {
		$contributions_count = intval( $revisions_count->contributions );
		$authors_by_contribution_count[ $revisions_count->post_author ] = $contributions_count;
	}

	// Add the current post to the contributions count.
	if ( isset( $authors_by_contribution_count[ $post_author ] ) ) {
		$authors_by_contribution_count[ $post_author ]++;
	} else {
		$authors_by_contribution_count[ $post_author ] = 1;
	}

	return $authors_by_contribution_count;
}

function get_contributors_block(): string {
	$post_author  = get_post()->post_author;
	$contributors = get_contributors( $post_author );

	if ( count( $contributors ) === 0 ) {
		return null;
	}

	$contributor_list_items = array_map(
		function ( int $contributor_id, int $count ): string {
			$userdata   = get_userdata( $contributor_id );
			$avatar     = get_avatar( $contributor_id, 72, '', '', [ 'height' => 36, 'width' => 36 ] );
			$link       = esc_url( get_author_posts_url( $contributor_id ) );
			$link_title = esc_attr(
				sprintf(
				/* translators: %1$s is replaced with the user's display name; %2$s is the user's nicename */
					__( 'Posts by %1$s ( @%2$s )', 'p2020' ),
					$userdata->display_name,
					$userdata->user_nicename
				)
			);
			$name = esc_html( $userdata->display_name );

			$revisions = esc_html(
			/* translators: %s is replaced with the number of revisions */
				sprintf( _n( '%s revision', '%s revisions', $count, 'p2020' ), $count )
			);

			return <<<LISTITEM
		<li class="p2020-contributors__item">
			<div class="p2020-contributors__item-avatar">
				$avatar
			</div>
			<div>
				<a
					href="$link"
					class="p2020-contributors__item-name"
					title="$link_title"
				>
					$name
				</a>
				<div class="p2020-contributors__item-revisions">
					$revisions
				</div>
			</div>
		</li>
LISTITEM;
		},
		array_keys( $contributors ),
		$contributors
	);

	$contributor_list_items_imploded = implode( '', $contributor_list_items );

	$heading_text = esc_html__( 'Contributors', 'p2020' );

	return <<<CONTRIBUTORS
	<aside class="p2020-contributors">
		<h2 class="p2020-contributors__heading">
			<span class="p2020-contributors__heading-text">
				$heading_text
			</span>
		</h2>
		<ol class="p2020-contributors__list">
			$contributor_list_items_imploded
		</ol>
	</aside>
CONTRIBUTORS;
}