Plural_Forms::execute()publicWP 4.9.0

Execute the plural form function.

Method of the class: Plural_Forms{}

No Hooks.

Return

Int. Plural form value.

Usage

$Plural_Forms = new Plural_Forms();
$Plural_Forms->execute( $n );
$n(int) (required)
Variable "n" to substitute.

Changelog

Since 4.9.0 Introduced.

Plural_Forms::execute() code WP 6.5.2

public function execute( $n ) {
	$stack = array();
	$i     = 0;
	$total = count( $this->tokens );
	while ( $i < $total ) {
		$next = $this->tokens[ $i ];
		++$i;
		if ( 'var' === $next[0] ) {
			$stack[] = $n;
			continue;
		} elseif ( 'value' === $next[0] ) {
			$stack[] = $next[1];
			continue;
		}

		// Only operators left.
		switch ( $next[1] ) {
			case '%':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 % $v2;
				break;

			case '||':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 || $v2;
				break;

			case '&&':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 && $v2;
				break;

			case '<':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 < $v2;
				break;

			case '<=':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 <= $v2;
				break;

			case '>':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 > $v2;
				break;

			case '>=':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 >= $v2;
				break;

			case '!=':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 != $v2;
				break;

			case '==':
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 == $v2;
				break;

			case '?:':
				$v3      = array_pop( $stack );
				$v2      = array_pop( $stack );
				$v1      = array_pop( $stack );
				$stack[] = $v1 ? $v2 : $v3;
				break;

			default:
				throw new Exception( sprintf( 'Unknown operator "%s"', $next[1] ) );
		}
	}

	if ( count( $stack ) !== 1 ) {
		throw new Exception( 'Too many values remaining on the stack' );
	}

	return (int) $stack[0];
}