function match()

in web/wp-content/plugins/acf-extended/includes/template-tags.php [873:1013]


    function match(&$string){
        
        // direct tags
        // match field_65671b1d11f07 current_post etc...
        $direct_tags = $this->get_tags(array('direct' => true));
        
        // loop
        foreach($direct_tags as $tag){
            
            // default condition
            $condition = $string === $tag['name'];
            
            // tag condition
            if(is_callable($tag['condition'])){
                $condition = call_user_func_array($tag['condition'], array($string));
            }
            
            // check condition
            if($condition && is_callable($tag['resolver'])){
                
                // replace string
                $string = call_user_func_array($tag['resolver'], array($string));
                
                if($this->get_context('return') !== 'raw'){
                    $string = acfe_array_to_string($string);
                }
                
                // stop loop
                return false;
                
            }
            
        }
        
        // tags
        // match {name} {name:value} {name:value:value2} etc...
        preg_match_all('/[{\w: +-\\\]*(?<full>{(?<name>[\w]+)(?!:})(?::(?<value>.*?))?})}*/', $string, $matches);
        
        // keys
        $keys = current($matches);
        
        // stop loop
        if(empty($keys)){
            return false;
        }
        
        // loop matches
        foreach(array_keys($keys) as $i){
        
            // vars
            $replace = '';
            $search  = $matches['full'][ $i ];        // {field:my_field}
            $name    = trim($matches['name'][ $i ]);  // field
            $value   = trim($matches['value'][ $i ]); // my_field
            
            // get tags
            $tags = $this->get_tags(array('direct' => false, 'name' => $name));
            
            if($tags){
                
                // var
                $resolver_executed = false;
                
                // loop tags
                foreach($tags as $tag){
                    
                    // default condition
                    $condition = true;
        
                    // check condition
                    if(is_callable($tag['condition'])){
                        $condition = call_user_func_array($tag['condition'], array($value));
                    }
                    
                    // condition ok: call resolver
                    if($condition && is_callable($tag['resolver'])){
                        
                        // execute resolver
                        $replace = call_user_func_array($tag['resolver'], array($value));
                        
                        if($this->get_context('return') !== 'raw'){
                            
                            // convert array value into string
                            $replace = acfe_array_to_string($replace);
                            
                            // escape brackets
                            if(!empty($replace) && is_string($replace)){
                                $replace = str_replace('{', '{ ', $replace);
                                $replace = str_replace('}', ' }', $replace);
                            }
                            
                        }
                        
                        // marker
                        $resolver_executed = true;
                        
                    }
                    
                    // resolver executed: stop tags loop
                    if($resolver_executed){
                        break;
                    }
                    
                }
                
                // condition/resolver not executed: add as excluded tag
                // this will prevent current tag from being parsed again by next loop call
                if(!$resolver_executed){
                    
                    $replace = $search;
                    $replace = str_replace('{', '{!!', $replace);
                    $replace = str_replace('}', '!!}', $replace);
                    
                }
                
            }
            
            // raw context + array
            if($this->get_context('return') === 'raw' && is_array($replace)){
                
                // return array
                $string = $replace;
                
                // stop loop
                return false;
                
            }
            
            if($replace === null || $replace === false){
                $replace = '';
            }
            
            // replace
            $string = str_replace($search, $replace, $string);
        
        }
        
        // keep looping
        return true;
        
    }