attributes-trait.php
4 years ago
base-attributes-trait.php
4 years ago
builder-helper.php
4 years ago
condition-helper.php
4 years ago
gallery.php
4 years ago
get-icon-trait.php
4 years ago
get-template-trait.php
4 years ago
html-attributes-trait.php
4 years ago
instance-trait.php
4 years ago
listing-filter-manager.php
4 years ago
listing-filter.php
4 years ago
macros-parser.php
4 years ago
messages-helper-trait.php
4 years ago
tools.php
4 years ago
macros-parser.php
128 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Classes; |
| 5 | |
| 6 | |
| 7 | use Jet_Form_Builder\Exceptions\Action_Exception; |
| 8 | use Jet_Form_Builder\Request\Request_Handler; |
| 9 | |
| 10 | class Macros_Parser { |
| 11 | |
| 12 | /** @var Listing_Filter */ |
| 13 | private $filter; |
| 14 | |
| 15 | private $content; |
| 16 | private $replacements; |
| 17 | |
| 18 | public function __construct() { |
| 19 | $this->set_default_filter(); |
| 20 | } |
| 21 | |
| 22 | public function set_default_filter() { |
| 23 | $this->set_custom_filter( ( new Listing_Filter_Manager() )->get_filter() ); |
| 24 | } |
| 25 | |
| 26 | public function set_custom_filter( $filter ) { |
| 27 | $this->filter = $filter; |
| 28 | |
| 29 | return $this; |
| 30 | } |
| 31 | |
| 32 | public function set_content( $content ) { |
| 33 | if ( ! $content ) { |
| 34 | return $this; |
| 35 | } |
| 36 | $this->content = $content; |
| 37 | |
| 38 | return $this; |
| 39 | } |
| 40 | |
| 41 | public function set_replacements( $replacements ) { |
| 42 | if ( ! $replacements ) { |
| 43 | return $this; |
| 44 | } |
| 45 | $this->replacements = $replacements; |
| 46 | |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | public function parse_macros( $content = false, $replacements = false ) { |
| 51 | $this->set_content( $content )->set_replacements( $replacements ); |
| 52 | |
| 53 | if ( ! $this->filter || ! $this->content || ! $this->replacements ) { |
| 54 | return $this->content ?: ''; |
| 55 | } |
| 56 | |
| 57 | return $this->macros_replace(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Parse macros in content |
| 62 | * |
| 63 | * @param [type] $content [description] |
| 64 | * |
| 65 | * @return [type] [description] |
| 66 | */ |
| 67 | public function macros_replace() { |
| 68 | |
| 69 | return preg_replace_callback( '/%(.*?)(\|([a-zA-Z0-9\(\)\.\,\:\/\s_-]+))?%/', function ( $match ) { |
| 70 | |
| 71 | if ( isset( $this->replacements[ $match[1] ] ) ) { |
| 72 | |
| 73 | if ( ! empty( $match[3] ) ) { |
| 74 | return $this->filter->apply_filters( |
| 75 | $this->replacements[ $match[1] ], $match[3] |
| 76 | ); |
| 77 | } else { |
| 78 | if ( is_array( $this->replacements[ $match[1] ] ) ) { |
| 79 | if ( ! empty( $this->replacements[ Request_Handler::REPEATERS_SETTINGS ][ $match[1] ] ) ) { |
| 80 | return $this->verbose_repeater( $this->replacements[ $match[1] ] ); |
| 81 | } else { |
| 82 | return implode( ', ', $this->replacements[ $match[1] ] ); |
| 83 | } |
| 84 | } else { |
| 85 | return $this->replacements[ $match[1] ]; |
| 86 | } |
| 87 | } |
| 88 | } else { |
| 89 | return $match[0]; |
| 90 | } |
| 91 | }, $this->content ); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Verbose repeater items array |
| 97 | * |
| 98 | * @param array $items |
| 99 | * |
| 100 | * @return string |
| 101 | */ |
| 102 | public function verbose_repeater( $items = array() ) { |
| 103 | |
| 104 | $result = ''; |
| 105 | $index = 1; |
| 106 | |
| 107 | foreach ( $items as $item ) { |
| 108 | $item_data = array(); |
| 109 | |
| 110 | foreach ( $item as $key => $value ) { |
| 111 | $item_data[] = sprintf( '%1$s: %2$s', $key, $this->maybe_parse_if_array( $value ) ); |
| 112 | } |
| 113 | $result .= $index ++ . ') ' . implode( ', ', $item_data ) . ';<br>'; |
| 114 | } |
| 115 | |
| 116 | return $result; |
| 117 | |
| 118 | } |
| 119 | |
| 120 | private function maybe_parse_if_array( $value ) { |
| 121 | if ( is_array( $value ) ) { |
| 122 | return implode( ', ', $value ); |
| 123 | } |
| 124 | |
| 125 | return $value; |
| 126 | } |
| 127 | |
| 128 | } |