/wp-content/plugins/contact-form-7/includes/pipe.php

https://bitbucket.org/devbctph/futura_wp · PHP · 78 lines · 58 code · 20 blank · 0 comment · 5 complexity · 454e28daaabb2c6840dbad4283b7f332 MD5 · raw file

  1. <?php
  2. class WPCF7_Pipe {
  3. public $before = '';
  4. public $after = '';
  5. public function __construct( $text ) {
  6. $text = (string) $text;
  7. $pipe_pos = strpos( $text, '|' );
  8. if ( false === $pipe_pos ) {
  9. $this->before = $this->after = trim( $text );
  10. } else {
  11. $this->before = trim( substr( $text, 0, $pipe_pos ) );
  12. $this->after = trim( substr( $text, $pipe_pos + 1 ) );
  13. }
  14. }
  15. }
  16. class WPCF7_Pipes {
  17. private $pipes = array();
  18. public function __construct( array $texts ) {
  19. foreach ( $texts as $text ) {
  20. $this->add_pipe( $text );
  21. }
  22. }
  23. private function add_pipe( $text ) {
  24. $pipe = new WPCF7_Pipe( $text );
  25. $this->pipes[] = $pipe;
  26. }
  27. public function do_pipe( $before ) {
  28. foreach ( $this->pipes as $pipe ) {
  29. if ( $pipe->before == $before ) {
  30. return $pipe->after;
  31. }
  32. }
  33. return $before;
  34. }
  35. public function collect_befores() {
  36. $befores = array();
  37. foreach ( $this->pipes as $pipe ) {
  38. $befores[] = $pipe->before;
  39. }
  40. return $befores;
  41. }
  42. public function collect_afters() {
  43. $afters = array();
  44. foreach ( $this->pipes as $pipe ) {
  45. $afters[] = $pipe->after;
  46. }
  47. return $afters;
  48. }
  49. public function zero() {
  50. return empty( $this->pipes );
  51. }
  52. public function random_pipe() {
  53. if ( $this->zero() ) {
  54. return null;
  55. }
  56. return $this->pipes[array_rand( $this->pipes )];
  57. }
  58. }