| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JFB_Modules\Rich_Content; |
| 5 |
|
| 6 |
use Jet_Form_Builder\Classes\Filters\Filters_Manager; |
| 7 |
use Jet_Form_Builder\Classes\Macro_Constants\Constants_Manager; |
| 8 |
use Jet_Form_Builder\Classes\Tools; |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
class Macros_Parser { |
| 16 |
|
| 17 |
private $content = ''; |
| 18 |
private $replacements = array(); |
| 19 |
private $current_macro = ''; |
| 20 |
|
| 21 |
public function set_content( $content ): Macros_Parser { |
| 22 |
if ( is_array( $content ) ) { |
| 23 |
$content = $content[0] ?? ''; |
| 24 |
} |
| 25 |
|
| 26 |
$this->content = Tools::to_string( $content ); |
| 27 |
|
| 28 |
return $this; |
| 29 |
} |
| 30 |
|
| 31 |
public function set_replacements( $replacements ): Macros_Parser { |
| 32 |
if ( ! $replacements ) { |
| 33 |
return $this; |
| 34 |
} |
| 35 |
$this->replacements = $replacements; |
| 36 |
|
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
public function parse_macros( $content = false, $replacements = false ): string { |
| 41 |
$this->set_content( $content )->set_replacements( $replacements ); |
| 42 |
|
| 43 |
if ( ! $this->content || ( ! $this->replacements && ! jet_fb_context()->has_request() ) ) { |
| 44 |
return $this->content ?: ''; |
| 45 |
} |
| 46 |
|
| 47 |
return $this->macros_replace(); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Parse macros in content |
| 52 |
* |
| 53 |
* @return string |
| 54 |
*/ |
| 55 |
public function macros_replace(): string { |
| 56 |
Constants_Manager::instance(); |
| 57 |
|
| 58 |
$content = preg_replace_callback( |
| 59 |
'/%([\w\-].*?\S?)%/', |
| 60 |
function ( $replace_match ) { |
| 61 |
$filters = explode( '|', $replace_match[1] ); |
| 62 |
$name = $filters[0]; |
| 63 |
array_shift( $filters ); |
| 64 |
|
| 65 |
if ( false !== strpos( $name, '::' ) ) { |
| 66 |
$value = apply_filters( |
| 67 |
'jet-form-builder/custom-macro', |
| 68 |
null, |
| 69 |
$name, |
| 70 |
$this |
| 71 |
); |
| 72 |
|
| 73 |
if ( is_null( $value ) ) { |
| 74 |
return $replace_match[0]; |
| 75 |
} |
| 76 |
|
| 77 |
return Filters_Manager::instance()->apply( $value, $filters ); |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! $this->has_replace( $name ) ) { |
| 81 |
return $replace_match[0]; |
| 82 |
} |
| 83 |
|
| 84 |
$this->current_macro = $name; |
| 85 |
|
| 86 |
$value = $this->get_replace( $name ); |
| 87 |
|
| 88 |
if ( ! empty( $filters ) ) { |
| 89 |
return Filters_Manager::instance()->apply( $value, $filters ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! is_array( $value ) ) { |
| 93 |
return $value; |
| 94 |
} |
| 95 |
|
| 96 |
if ( 'repeater-field' === jet_fb_context()->get_field_type( $name ) ) { |
| 97 |
return $this->verbose_repeater( $value, $name ); |
| 98 |
} else { |
| 99 |
return implode( ', ', $value ); |
| 100 |
} |
| 101 |
}, |
| 102 |
$this->content |
| 103 |
); |
| 104 |
|
| 105 |
return is_string( $content ) ? $content : ''; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Verbose repeater items array |
| 110 |
* |
| 111 |
* @param array $items |
| 112 |
* @param $name |
| 113 |
* |
| 114 |
* @return string |
| 115 |
*/ |
| 116 |
protected function verbose_repeater( array $items, $name ) { |
| 117 |
|
| 118 |
$result = apply_filters( 'jet-form-builder/send-email/template-repeater', '', $items, $this ); |
| 119 |
$counter = 1; |
| 120 |
|
| 121 |
if ( $result ) { |
| 122 |
return $result; |
| 123 |
} |
| 124 |
|
| 125 |
foreach ( $items as $index => $item ) { |
| 126 |
$item_data = array(); |
| 127 |
|
| 128 |
foreach ( $item as $key => $value ) { |
| 129 |
$label = jet_fb_context()->get_setting( 'label', array( $name, $index, $key ) ); |
| 130 |
|
| 131 |
$item_data[] = sprintf( |
| 132 |
'%1$s: %2$s', |
| 133 |
$label ?: $key, |
| 134 |
$this->maybe_parse_if_array( $value ) |
| 135 |
); |
| 136 |
} |
| 137 |
$result .= ( $counter++ ) . ') ' . implode( ', ', $item_data ) . ';<br>'; |
| 138 |
} |
| 139 |
|
| 140 |
return $result; |
| 141 |
} |
| 142 |
|
| 143 |
private function maybe_parse_if_array( $value ) { |
| 144 |
if ( is_array( $value ) ) { |
| 145 |
return implode( ', ', $value ); |
| 146 |
} |
| 147 |
|
| 148 |
return $value; |
| 149 |
} |
| 150 |
|
| 151 |
protected function has_replace( string $name ): bool { |
| 152 |
if ( ! empty( $this->replacements ) ) { |
| 153 |
return isset( $this->replacements[ $name ] ); |
| 154 |
} |
| 155 |
|
| 156 |
return jet_fb_context()->has_field( $name ); |
| 157 |
} |
| 158 |
|
| 159 |
protected function get_replace( string $name ) { |
| 160 |
if ( ! empty( $this->replacements ) ) { |
| 161 |
return $this->replacements[ $name ] ?? ''; |
| 162 |
} |
| 163 |
|
| 164 |
return jet_fb_context()->get_value( $name ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* @return string |
| 169 |
*/ |
| 170 |
public function get_current_macro(): string { |
| 171 |
return $this->current_macro; |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|