<?php
/**
 * Wrap the post counts that list widgets print.
 *
 * The Categories and Archives widgets print their count as bare text after the
 * link, like "Travel (10)". There is no element around it, so a theme cannot
 * style the number on its own. These filters put it in a span so the stylesheet
 * can set it as a badge, and mark it up so a screen reader reads something
 * meaningful rather than a stray number in brackets.
 *
 * @package Intimate
 */

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

if ( ! function_exists( 'intimate_wrap_list_counts' ) ) :

	/**
	 * Put a span around the "(12)" a list widget prints after each link.
	 *
	 * @param string $html Widget markup.
	 * @return string
	 */
	function intimate_wrap_list_counts( $html ) {

		if ( ! is_string( $html ) || '' === $html ) {
			return $html;
		}

		return preg_replace_callback(
			'#</a>\s*\((\d+)\)#',
			function ( $matches ) {
				return sprintf(
					'</a> <span class="post-count"><span class="screen-reader-text">%1$s </span>%2$s</span>',
					esc_html__( 'Posts:', 'intimate' ),
					absint( $matches[1] )
				);
			},
			$html
		);
	}

endif;

add_filter( 'wp_list_categories', 'intimate_wrap_list_counts' );
add_filter( 'get_archives_link', 'intimate_wrap_list_counts' );
