Numbers

Adds and maintains filters providing number functionality to the alphabet


Description


Source

File: src/Numbers.php

class Numbers {
	/**
	 * Where to place the numbers.
	 *
	 * @var string
	 */
	protected $position = 'hide';

	/**
	 * Whether to group the numbers in a single entry.
	 *
	 * @var boolean
	 */
	protected $group = false;

	/**
	 * Add filters to append or prepend numbers to the alphabet with optional grouping
	 *
	 * @since 2.0.0
	 * @param string $position Can be either "before" or "after" indicating where to place the numbers respective to the alphabet.
	 * @param bool   $group Whether to group the numbers into a single heading or individually.
	 */
	public function __construct( $position = 'hide', $group = false ) {
		if ( 'before' === $position || 'after' === $position ) {
			$this->position = $position;
			$this->group    = a_z_listing_is_truthy( $group );
			add_filter( 'a-z-listing-alphabet', array( $this, 'add_to_alphabet' ) );
			add_filter( 'the-a-z-letter-title', array( $this, 'title' ) );
		}
	}

	/**
	 * Remove the numbers filters we added previously
	 *
	 * @since 2.0.0
	 */
	public function teardown() {
		remove_filter( 'a-z-listing-alphabet', array( $this, 'add_to_alphabet' ) );
		remove_filter( 'the-a-z-letter-title', array( $this, 'title' ) );
	}

	/**
	 * Add numbers to the alphabet
	 *
	 * @since 2.0.0
	 * @param string $alphabet The alphabet to add numbers into.
	 * @return string The alphabet with numbers either prepended or appended
	 */
	public function add_to_alphabet( $alphabet ) {
		if ( 'hide' === $this->position ) {
			return $alphabet;
		}

		if ( true === $this->group ) {
			$numbers = '0123456789';
		} else {
			$numbers = '0,1,2,3,4,5,6,7,8,9';
		}

		if ( 'before' === $this->position ) {
			return join( ',', array( $numbers, $alphabet ) );
		}
		return join( ',', array( $alphabet, $numbers ) );
	}

	/**
	 * Override the group title for grouped numbers
	 *
	 * @since 2.0.0
	 * @param string $letter The original title of the group.
	 * @return string The new title for the group
	 */
	public function title( $letter ) {
		if ( '0' === $letter && true === $this->group ) {
			return '0-9';
		}
		return $letter;
	}
}

Changelog

Changelog
Version Description
2.0.0 Introduced.

More Information

You are currently logged out. To view tickets you must first login.

I have read and understood the Terms of Service


Methods

  • __construct — Add filters to append or prepend numbers to the alphabet with optional grouping
  • add_to_alphabet — Add numbers to the alphabet
  • teardown — Remove the numbers filters we added previously
  • title — Override the group title for grouped numbers

User Contributed Notes

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.