/var/www/html_uk/wp-content/plugins/automatewoo/includes/Replace_Helper.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

/**
 * @class Replace_Helper
 * @since 2.1.9
 */
class Replace_Helper {

    
/** @var array */
    
public $patterns = [
        
'text_urls' => [
            
'match' => 0,
            
'expression' => "/(?<!a href=\")(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i"
        
],
        
'href_urls' => [
            
'match' => 1,
            
'expression' => '/href=["\']?([^"\'>]+)["\']?/'
        
],
        
'variables' => [
            
'match' => 1,
            
'expression' => '/{{(.*?)}}/'
        
]
    ];

    
/** @var string */
    
public $selected_pattern;

    
/** @var string */
    
public $string;

    
/** @var callable */
    
public $callback;


    
/**
     * @param $string
     * @param callable $callback
     * @param string $pattern_name
     */
    
function __construct$string$callback$pattern_name '' ) {

        
$this->string $string;
        
$this->callback $callback;

        if ( 
$pattern_name && isset( $this->patterns[$pattern_name] ) ) {
            
$this->selected_pattern $this->patterns[$pattern_name];
        }
    }


    
/**
     * @return mixed
     */
    
function process() {

        if ( ! 
$this->selected_pattern )
            return 
false;

        return 
preg_replace_callback$this->selected_pattern['expression'], [ $this'callback' ] , $this->string );
    }


    
/**
     * Pre process match before using the actual callback
     * @param $match
     * @return string
     */
    
function callback$match ) {
        if ( 
is_array$match ) ) {
            
$match $match$this->selected_pattern['match'] ];
        }
        return 
call_user_func$this->callback$match );
    }

}