ID | get_the_ID())) -> has_ancestor(N) * P2 $post->post_parent ==|=== N (either side; N int or quoted digits) -> has_parent(N) * P3 get_post_type() ==|=== 'x' (either side) -> post_type_is('x') */ final class Patterns { /** * Try to match a pattern on tokens [lo, hi). * * @param array $t Tokens (comments removed). * @param int $lo Start. * @param int $hi End (exclusive). * @return array|null {cond, args, pattern} */ public static function match( array $t, $lo, $hi ) { $sig = array(); for ( $i = $lo; $i < $hi; $i++ ) { $sig[] = $t[ $i ]; } $r = self::p1( $sig ); if ( $r ) { return $r; } $r = self::p2( $sig ); if ( $r ) { return $r; } return self::p3( $sig ); } /** * Token equals. * * @param array $tok Token. * @param string $type Type. * @param mixed $value Value. * @return bool */ private static function is( $tok, $type, $value = null ) { return is_array( $tok ) && $tok['type'] === $type && ( null === $value || $tok['value'] === $value ); } /** * P1. * * @param array $s Tokens. * @return array|null */ private static function p1( array $s ) { $n = count( $s ); if ( $n < 8 || ! self::is( $s[0], 'ident', 'in_array' ) || ! self::is( $s[1], 'punct', '(' ) || ! self::is( $s[2], 'int' ) || ! self::is( $s[3], 'punct', ',' ) || ! self::is( $s[4], 'ident', 'get_post_ancestors' ) || ! self::is( $s[5], 'punct', '(' ) ) { return null; } $inner = array_slice( $s, 6, $n - 6 - 2 ); if ( ! self::is( $s[ $n - 2 ], 'punct', ')' ) || ! self::is( $s[ $n - 1 ], 'punct', ')' ) ) { return null; } $ok = ( 1 === count( $inner ) && self::is( $inner[0], 'var', '$post' ) ) || ( 3 === count( $inner ) && self::is( $inner[0], 'var', '$post' ) && self::is( $inner[1], 'op', '->' ) && self::is( $inner[2], 'ident', 'id' ) ) || ( 3 === count( $inner ) && self::is( $inner[0], 'ident', 'get_the_id' ) && self::is( $inner[1], 'punct', '(' ) && self::is( $inner[2], 'punct', ')' ) ); if ( ! $ok ) { return null; } return array( 'cond' => 'has_ancestor', 'args' => array( $s[2]['value'] ), 'pattern' => 'P1', ); } /** * Integer value of an int token or a quoted all-digit string. * * @param array $tok Token. * @return int|null */ private static function int_value( $tok ) { if ( self::is( $tok, 'int' ) ) { return $tok['value']; } if ( ( self::is( $tok, 'sq' ) || ( self::is( $tok, 'dq' ) && empty( $tok['foreign'] ) ) ) && preg_match( '/^(0|[1-9][0-9]{0,17})$/', $tok['value'] ) ) { return (int) $tok['value']; } return null; } /** * P2. * * @param array $s Tokens. * @return array|null */ private static function p2( array $s ) { if ( 5 !== count( $s ) ) { return null; } $cmp = function ( $tok ) { return self::is( $tok, 'op', '==' ) || self::is( $tok, 'op', '===' ); }; $is_parent = function ( $a, $b, $c ) { return self::is( $a, 'var', '$post' ) && self::is( $b, 'op', '->' ) && self::is( $c, 'ident', 'post_parent' ); }; if ( $is_parent( $s[0], $s[1], $s[2] ) && $cmp( $s[3] ) ) { $v = self::int_value( $s[4] ); if ( null !== $v && ( '===' !== $s[3]['value'] || self::is( $s[4], 'int' ) ) ) { return array( 'cond' => 'has_parent', 'args' => array( $v ), 'pattern' => 'P2', ); } } if ( $cmp( $s[1] ) && $is_parent( $s[2], $s[3], $s[4] ) ) { $v = self::int_value( $s[0] ); if ( null !== $v && ( '===' !== $s[1]['value'] || self::is( $s[0], 'int' ) ) ) { return array( 'cond' => 'has_parent', 'args' => array( $v ), 'pattern' => 'P2', ); } } return null; } /** * P3. * * @param array $s Tokens. * @return array|null */ private static function p3( array $s ) { if ( 5 !== count( $s ) ) { return null; } $str = function ( $tok ) { return self::is( $tok, 'sq' ) || ( self::is( $tok, 'dq' ) && empty( $tok['foreign'] ) ); }; $cmp = function ( $tok ) { return self::is( $tok, 'op', '==' ) || self::is( $tok, 'op', '===' ); }; $gpt = function ( $a, $b, $c ) { return self::is( $a, 'ident', 'get_post_type' ) && self::is( $b, 'punct', '(' ) && self::is( $c, 'punct', ')' ); }; if ( $gpt( $s[0], $s[1], $s[2] ) && $cmp( $s[3] ) && $str( $s[4] ) && '' !== $s[4]['value'] ) { return array( 'cond' => 'post_type_is', 'args' => array( $s[4]['value'] ), 'pattern' => 'P3', ); } if ( $str( $s[0] ) && '' !== $s[0]['value'] && $cmp( $s[1] ) && $gpt( $s[2], $s[3], $s[4] ) ) { return array( 'cond' => 'post_type_is', 'args' => array( $s[0]['value'] ), 'pattern' => 'P3', ); } return null; } }