transformer.php
41 lines
| 1 | <?php |
| 2 | namespace ElementsKit_Lite\Libs\Template; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | class Transformer { |
| 7 | |
| 8 | private $prefix; |
| 9 | |
| 10 | public function render( $str, $prefix ) { |
| 11 | $str = trim( $str ); |
| 12 | $this->prefix = $prefix; |
| 13 | |
| 14 | $fn_length = explode( '(', $str ); |
| 15 | if ( count( $fn_length ) == 2 ) { |
| 16 | $method = $fn_length[0]; // backward support |
| 17 | |
| 18 | if ( method_exists( $this, $method ) ) { |
| 19 | return $this->$method( rtrim( $fn_length[1], ')' ) ); |
| 20 | } |
| 21 | return $str; |
| 22 | } |
| 23 | |
| 24 | return $this->variable( $str ); |
| 25 | } |
| 26 | |
| 27 | private function variable( $str ) { |
| 28 | $str_var_set = explode( '.', $str ); |
| 29 | $array_parts = ''; |
| 30 | foreach ( $str_var_set as $i => $var ) { |
| 31 | $array_parts .= '["' . ( $i > 0 ? '' : $this->prefix ) . $var . '"]'; |
| 32 | } |
| 33 | |
| 34 | return '<?php echo isset($settings' . $array_parts . ') ? $settings' . $array_parts . ' : ""; ?>'; |
| 35 | } |
| 36 | |
| 37 | private function icon( $str ) { |
| 38 | return '<?php Icons_Manager::render_icon($settings["' . $this->prefix . trim( $str ) . '"]); ?>'; |
| 39 | } |
| 40 | } |
| 41 |