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