-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.php
149 lines (113 loc) · 2.56 KB
/
pipe.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* Pipe-related classes.
*
* @link https://contactform7.com/selectable-recipient-with-pipes/
*/
/**
* Class representing a pair of pipe.
*/
class WPCF7_Pipe {
public $before = '';
public $after = '';
public function __construct( $text ) {
$text = (string) $text;
$pipe_pos = strpos( $text, '|' );
if ( false === $pipe_pos ) {
$this->before = $this->after = trim( $text );
} else {
$this->before = trim( substr( $text, 0, $pipe_pos ) );
$this->after = trim( substr( $text, $pipe_pos + 1 ) );
}
}
}
/**
* Class representing a list of pipes.
*/
class WPCF7_Pipes {
private $pipes = array();
public function __construct( $texts = null ) {
foreach ( (array) $texts as $text ) {
$this->add_pipe( $text );
}
}
private function add_pipe( $text ) {
$pipe = new WPCF7_Pipe( $text );
$this->pipes[] = $pipe;
}
public function merge( self $another ) {
$this->pipes = array_merge( $this->pipes, $another->pipes );
}
public function do_pipe( $input ) {
$input_canonical = wpcf7_canonicalize( $input, array(
'strto' => 'as-is',
) );
foreach ( $this->pipes as $pipe ) {
$before_canonical = wpcf7_canonicalize( $pipe->before, array(
'strto' => 'as-is',
) );
if ( $input_canonical === $before_canonical ) {
return $pipe->after;
}
}
return $input;
}
public function collect_befores() {
$befores = array();
foreach ( $this->pipes as $pipe ) {
$befores[] = $pipe->before;
}
return $befores;
}
public function collect_afters() {
$afters = array();
foreach ( $this->pipes as $pipe ) {
$afters[] = $pipe->after;
}
return $afters;
}
public function zero() {
return empty( $this->pipes );
}
public function random_pipe() {
if ( $this->zero() ) {
return null;
}
return $this->pipes[array_rand( $this->pipes )];
}
public function to_array() {
return array_map(
static function ( WPCF7_Pipe $pipe ) {
return array(
$pipe->before,
$pipe->after,
);
},
$this->pipes
);
}
}
/**
* Trait for classes that hold cross-tag WPCF7_Pipes object.
*/
trait WPCF7_PipesHolder {
protected $pipes;
public function get_pipes( $field_name ) {
if ( isset( $this->pipes[$field_name] ) ) {
return $this->pipes[$field_name];
}
$result = new WPCF7_Pipes;
$tags = $this->scan_form_tags( array(
'name' => $field_name,
) );
foreach ( $tags as $tag ) {
if ( $tag->pipes instanceof WPCF7_Pipes ) {
$result->merge( $tag->pipes );
}
}
return $this->pipes[$field_name] = $result;
}
public function scan_form_tags() {
return array();
}
}