block-editor
1 year ago
config-validator
11 months ago
css
11 months ago
js
1 year ago
swv
11 months ago
capabilities.php
7 years ago
contact-form-functions.php
11 months ago
contact-form-template.php
11 months ago
contact-form.php
11 months ago
controller.php
11 months ago
file.php
11 months ago
filesystem.php
11 months ago
form-tag.php
11 months ago
form-tags-manager.php
11 months ago
formatting.php
11 months ago
functions.php
11 months ago
html-formatter.php
11 months ago
integration.php
11 months ago
l10n.php
11 months ago
mail-tag.php
11 months ago
mail.php
11 months ago
pipe.php
11 months ago
pocket-holder.php
3 years ago
rest-api.php
11 months ago
shortcodes.php
11 months ago
special-mail-tags.php
1 year ago
submission.php
11 months ago
upgrade.php
11 months ago
validation-functions.php
11 months ago
validation.php
11 months ago
pipe.php
150 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 = wpcf7_strip_whitespaces( $text ); |
| 24 | } else { |
| 25 | $this->before = wpcf7_strip_whitespaces( substr( $text, 0, $pipe_pos ) ); |
| 26 | $this->after = wpcf7_strip_whitespaces( 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( $texts = null ) { |
| 40 | foreach ( (array) $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 merge( self $another ) { |
| 51 | $this->pipes = array_merge( $this->pipes, $another->pipes ); |
| 52 | } |
| 53 | |
| 54 | public function do_pipe( $input ) { |
| 55 | $input_canonical = wpcf7_canonicalize( $input, array( |
| 56 | 'strto' => 'as-is', |
| 57 | ) ); |
| 58 | |
| 59 | foreach ( $this->pipes as $pipe ) { |
| 60 | $before_canonical = wpcf7_canonicalize( $pipe->before, array( |
| 61 | 'strto' => 'as-is', |
| 62 | ) ); |
| 63 | |
| 64 | if ( $input_canonical === $before_canonical ) { |
| 65 | return $pipe->after; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return $input; |
| 70 | } |
| 71 | |
| 72 | public function collect_befores() { |
| 73 | $befores = array(); |
| 74 | |
| 75 | foreach ( $this->pipes as $pipe ) { |
| 76 | $befores[] = $pipe->before; |
| 77 | } |
| 78 | |
| 79 | return $befores; |
| 80 | } |
| 81 | |
| 82 | public function collect_afters() { |
| 83 | $afters = array(); |
| 84 | |
| 85 | foreach ( $this->pipes as $pipe ) { |
| 86 | $afters[] = $pipe->after; |
| 87 | } |
| 88 | |
| 89 | return $afters; |
| 90 | } |
| 91 | |
| 92 | public function zero() { |
| 93 | return empty( $this->pipes ); |
| 94 | } |
| 95 | |
| 96 | public function random_pipe() { |
| 97 | if ( $this->zero() ) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | return $this->pipes[array_rand( $this->pipes )]; |
| 102 | } |
| 103 | |
| 104 | public function to_array() { |
| 105 | return array_map( |
| 106 | static function ( WPCF7_Pipe $pipe ) { |
| 107 | return array( |
| 108 | $pipe->before, |
| 109 | $pipe->after, |
| 110 | ); |
| 111 | }, |
| 112 | $this->pipes |
| 113 | ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | /** |
| 119 | * Trait for classes that hold cross-tag WPCF7_Pipes object. |
| 120 | */ |
| 121 | trait WPCF7_PipesHolder { |
| 122 | |
| 123 | protected $pipes; |
| 124 | |
| 125 | public function get_pipes( $field_name ) { |
| 126 | if ( isset( $this->pipes[$field_name] ) ) { |
| 127 | return $this->pipes[$field_name]; |
| 128 | } |
| 129 | |
| 130 | $result = new WPCF7_Pipes(); |
| 131 | |
| 132 | $tags = $this->scan_form_tags( array( |
| 133 | 'name' => $field_name, |
| 134 | ) ); |
| 135 | |
| 136 | foreach ( $tags as $tag ) { |
| 137 | if ( $tag->pipes instanceof WPCF7_Pipes ) { |
| 138 | $result->merge( $tag->pipes ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $this->pipes[$field_name] = $result; |
| 143 | } |
| 144 | |
| 145 | public function scan_form_tags() { |
| 146 | return array(); |
| 147 | } |
| 148 | |
| 149 | } |
| 150 |