block-editor
5 years ago
css
5 years ago
js
5 years ago
capabilities.php
7 years ago
config-validator.php
5 years ago
contact-form-functions.php
5 years ago
contact-form-template.php
5 years ago
contact-form.php
5 years ago
controller.php
7 years ago
form-tag.php
5 years ago
form-tags-manager.php
6 years ago
formatting.php
5 years ago
functions.php
5 years ago
integration.php
7 years ago
l10n.php
5 years ago
mail.php
5 years ago
pipe.php
5 years ago
rest-api.php
5 years ago
shortcodes.php
9 years ago
special-mail-tags.php
5 years ago
submission.php
5 years ago
upgrade.php
7 years ago
validation.php
7 years ago
pipe.php
91 lines
| 1 | <?php |
| 2 | |
| 3 | class WPCF7_Pipe { |
| 4 | |
| 5 | public $before = ''; |
| 6 | public $after = ''; |
| 7 | |
| 8 | public function __construct( $text ) { |
| 9 | $text = (string) $text; |
| 10 | |
| 11 | $pipe_pos = strpos( $text, '|' ); |
| 12 | |
| 13 | if ( false === $pipe_pos ) { |
| 14 | $this->before = $this->after = trim( $text ); |
| 15 | } else { |
| 16 | $this->before = trim( substr( $text, 0, $pipe_pos ) ); |
| 17 | $this->after = trim( substr( $text, $pipe_pos + 1 ) ); |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | class WPCF7_Pipes { |
| 23 | |
| 24 | private $pipes = array(); |
| 25 | |
| 26 | public function __construct( array $texts ) { |
| 27 | foreach ( $texts as $text ) { |
| 28 | $this->add_pipe( $text ); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | private function add_pipe( $text ) { |
| 33 | $pipe = new WPCF7_Pipe( $text ); |
| 34 | $this->pipes[] = $pipe; |
| 35 | } |
| 36 | |
| 37 | public function do_pipe( $before ) { |
| 38 | foreach ( $this->pipes as $pipe ) { |
| 39 | if ( $pipe->before == $before ) { |
| 40 | return $pipe->after; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $before; |
| 45 | } |
| 46 | |
| 47 | public function collect_befores() { |
| 48 | $befores = array(); |
| 49 | |
| 50 | foreach ( $this->pipes as $pipe ) { |
| 51 | $befores[] = $pipe->before; |
| 52 | } |
| 53 | |
| 54 | return $befores; |
| 55 | } |
| 56 | |
| 57 | public function collect_afters() { |
| 58 | $afters = array(); |
| 59 | |
| 60 | foreach ( $this->pipes as $pipe ) { |
| 61 | $afters[] = $pipe->after; |
| 62 | } |
| 63 | |
| 64 | return $afters; |
| 65 | } |
| 66 | |
| 67 | public function zero() { |
| 68 | return empty( $this->pipes ); |
| 69 | } |
| 70 | |
| 71 | public function random_pipe() { |
| 72 | if ( $this->zero() ) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | return $this->pipes[array_rand( $this->pipes )]; |
| 77 | } |
| 78 | |
| 79 | public function to_array() { |
| 80 | return array_map( |
| 81 | function( WPCF7_Pipe $pipe ) { |
| 82 | return array( |
| 83 | $pipe->before, |
| 84 | $pipe->after, |
| 85 | ); |
| 86 | }, |
| 87 | $this->pipes |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 |