getAllPatterns(); if ( $groupPatterns ) { $allPatterns[ $groupId ] = $groupPatterns; } else { unset( $allPatterns[ $groupId ] ); } update_option( self::OPTION_KEY, $allPatterns, false ); } /** * @param string $fieldName * * @return int|null */ public function findMatchingGroup( $fieldName ) { if ( array_key_exists( $fieldName, $this->cachedMatches ) ) { return $this->cachedMatches[ $fieldName ]; } $this->cachedMatches[ $fieldName ] = null; foreach ( $this->getAllPatterns() as $groupId => $patterns ) { if ( $this->matches( $fieldName, $patterns ) ) { $this->cachedMatches[ $fieldName ] = (int) $groupId; break; } } return $this->cachedMatches[ $fieldName ]; } /** * @param string $fieldName * @param string[] $patterns * * @return bool */ private function matches( $fieldName, $patterns ) { foreach ( $patterns as $pattern ) { if ( Str::match( '/^' . $pattern . '$/', $fieldName ) ) { return true; } } return false; } /** * @return array */ private function getAllPatterns() { return (array) get_option( self::OPTION_KEY, [] ); } }