block-editor
3 years ago
css
3 years ago
js
3 years ago
swv
3 years ago
capabilities.php
7 years ago
config-validator.php
3 years ago
contact-form-functions.php
3 years ago
contact-form-template.php
3 years ago
contact-form.php
3 years ago
controller.php
3 years ago
file.php
3 years ago
form-tag.php
3 years ago
form-tags-manager.php
3 years ago
formatting.php
3 years ago
functions.php
3 years ago
html-formatter.php
3 years ago
integration.php
3 years ago
l10n.php
3 years ago
mail.php
3 years ago
pipe.php
4 years ago
pocket-holder.php
3 years ago
rest-api.php
4 years ago
shortcodes.php
3 years ago
special-mail-tags.php
3 years ago
submission.php
3 years ago
upgrade.php
7 years ago
validation-functions.php
3 years ago
validation.php
3 years ago
pipe.php
112 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Pipe-related classes. |
| 4 | * |
| 5 | * @link https://contactform7.com/selectable-recipient-with-pipes/ |
| 6 | */ |
| 7 | |
| 8 | |
| 9 | /** |
| 10 | * Class representing a pair of pipe. |
| 11 | */ |
| 12 | class WPCF7_Pipe { |
| 13 | |
| 14 | public $before = ''; |
| 15 | public $after = ''; |
| 16 | |
| 17 | public function __construct( $text ) { |
| 18 | $text = (string) $text; |
| 19 | |
| 20 | $pipe_pos = strpos( $text, '|' ); |
| 21 | |
| 22 | if ( false === $pipe_pos ) { |
| 23 | $this->before = $this->after = trim( $text ); |
| 24 | } else { |
| 25 | $this->before = trim( substr( $text, 0, $pipe_pos ) ); |
| 26 | $this->after = trim( substr( $text, $pipe_pos + 1 ) ); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Class representing a list of pipes. |
| 34 | */ |
| 35 | class WPCF7_Pipes { |
| 36 | |
| 37 | private $pipes = array(); |
| 38 | |
| 39 | public function __construct( array $texts ) { |
| 40 | foreach ( $texts as $text ) { |
| 41 | $this->add_pipe( $text ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | private function add_pipe( $text ) { |
| 46 | $pipe = new WPCF7_Pipe( $text ); |
| 47 | $this->pipes[] = $pipe; |
| 48 | } |
| 49 | |
| 50 | public function do_pipe( $input ) { |
| 51 | $input_canonical = wpcf7_canonicalize( $input, array( |
| 52 | 'strto' => 'as-is', |
| 53 | ) ); |
| 54 | |
| 55 | foreach ( $this->pipes as $pipe ) { |
| 56 | $before_canonical = wpcf7_canonicalize( $pipe->before, array( |
| 57 | 'strto' => 'as-is', |
| 58 | ) ); |
| 59 | |
| 60 | if ( $input_canonical === $before_canonical ) { |
| 61 | return $pipe->after; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return $input; |
| 66 | } |
| 67 | |
| 68 | public function collect_befores() { |
| 69 | $befores = array(); |
| 70 | |
| 71 | foreach ( $this->pipes as $pipe ) { |
| 72 | $befores[] = $pipe->before; |
| 73 | } |
| 74 | |
| 75 | return $befores; |
| 76 | } |
| 77 | |
| 78 | public function collect_afters() { |
| 79 | $afters = array(); |
| 80 | |
| 81 | foreach ( $this->pipes as $pipe ) { |
| 82 | $afters[] = $pipe->after; |
| 83 | } |
| 84 | |
| 85 | return $afters; |
| 86 | } |
| 87 | |
| 88 | public function zero() { |
| 89 | return empty( $this->pipes ); |
| 90 | } |
| 91 | |
| 92 | public function random_pipe() { |
| 93 | if ( $this->zero() ) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | return $this->pipes[array_rand( $this->pipes )]; |
| 98 | } |
| 99 | |
| 100 | public function to_array() { |
| 101 | return array_map( |
| 102 | function( WPCF7_Pipe $pipe ) { |
| 103 | return array( |
| 104 | $pipe->before, |
| 105 | $pipe->after, |
| 106 | ); |
| 107 | }, |
| 108 | $this->pipes |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 |