| 1 |
<?php |
| 2 |
|
| 3 |
namespace Jet_Form_Builder\Blocks\Render; |
| 4 |
|
| 5 |
// If this file is called directly, abort. |
| 6 |
use Jet_Form_Builder\Classes\Tools; |
| 7 |
|
| 8 |
if ( ! defined( 'WPINC' ) ) { |
| 9 |
die; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Define text field renderer class |
| 14 |
*/ |
| 15 |
class Calculated_Field_Render extends Base { |
| 16 |
|
| 17 |
public function get_name() { |
| 18 |
return 'calculated-field'; |
| 19 |
} |
| 20 |
|
| 21 |
public function render_editor_placeholder() { |
| 22 |
return Tools::is_editor() ? '25.00' : ''; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get calulation formula for calculated field |
| 27 |
* |
| 28 |
* @return [type] [description] |
| 29 |
*/ |
| 30 |
public function get_calculated_data( $args ) { |
| 31 |
|
| 32 |
if ( empty( $args['calc_formula'] ) ) { |
| 33 |
return ''; |
| 34 |
} |
| 35 |
|
| 36 |
$listen_fields = array(); |
| 37 |
|
| 38 |
$formula = preg_replace_callback( |
| 39 |
'/%([a-zA-Z-_]+)::([a-zA-Z0-9-_]+)%/', |
| 40 |
function ( $matches ) use ( &$listen_fields ) { |
| 41 |
|
| 42 |
switch ( strtolower( $matches[1] ) ) { |
| 43 |
case 'field': |
| 44 |
|
| 45 |
$listen_fields[] = $matches[2]; |
| 46 |
|
| 47 |
return '%' . $matches[2] . '%'; |
| 48 |
|
| 49 |
case 'meta': |
| 50 |
|
| 51 |
return get_post_meta( $this->post->ID, $matches[2], true ); |
| 52 |
|
| 53 |
default: |
| 54 |
$macros_name = $matches[1]; |
| 55 |
$field_key = isset( $matches[2] ) ? $matches[2] : ''; |
| 56 |
|
| 57 |
if ( $field_key ) { |
| 58 |
$listen_fields[] = $field_key; |
| 59 |
} |
| 60 |
|
| 61 |
return apply_filters( "jet-engine/calculated-data/$macros_name", $matches[0], $matches ); |
| 62 |
} |
| 63 |
|
| 64 |
}, |
| 65 |
$args['calc_formula'] |
| 66 |
); |
| 67 |
|
| 68 |
$formula = str_replace( '^', '**', $formula ); |
| 69 |
|
| 70 |
return array( |
| 71 |
'formula' => $formula, |
| 72 |
'listen_fields' => $listen_fields, |
| 73 |
'listen_to' => $listen_fields, |
| 74 |
); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|