|
Server IP : 89.26.249.46 / Your IP : 216.73.216.150 Web Server : Apache System : Linux a.cp.cloudlink.pt 4.18.0-553.121.1.lve.el8.x86_64 #1 SMP Thu Apr 30 16:40:41 UTC 2026 x86_64 User : eticalga ( 1129) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0755) : /home/eticalga/www/files/plugins/acfml/classes/FieldGroup/ |
| [ Home ] | [ C0mmand ] | [ Upload File ] |
|---|
<?php
namespace ACFML\FieldGroup;
use WPML\FP\Str;
class FieldNamePatterns {
const OPTION_KEY = 'acfml_field_name_patterns';
/**
* @var array $cachedMatches
*/
private $cachedMatches = [];
/**
* @param int $groupId
* @param array $groupPatterns
*
* @return void
*/
public function updateGroup( $groupId, $groupPatterns ) {
$allPatterns = $this->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, [] );
}
}