| 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 |
use Jet_Form_Builder\Live_Form; |
| 8 |
|
| 9 |
if ( ! defined( 'WPINC' ) ) { |
| 10 |
die; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Define text field renderer class |
| 15 |
*/ |
| 16 |
class Calculated_Field_Render extends Base { |
| 17 |
|
| 18 |
public function get_name() { |
| 19 |
return 'calculated-field'; |
| 20 |
} |
| 21 |
|
| 22 |
public function render_editor_placeholder() { |
| 23 |
return Tools::is_editor() ? '25.00' : '0.00'; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* We do not need a <label> tag in any case, |
| 28 |
* because there is no visual field in this field |
| 29 |
* that can be referenced through the "for" attribute. |
| 30 |
* |
| 31 |
* @see https://github.com/Crocoblock/jetformbuilder/issues/356 |
| 32 |
*/ |
| 33 |
protected function get_fields_label_tag(): string { |
| 34 |
return 'div'; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Get calulation formula for calculated field |
| 39 |
* |
| 40 |
* @return [type] [description] |
| 41 |
*/ |
| 42 |
public function get_calculated_data( $args ) { |
| 43 |
|
| 44 |
if ( empty( $args['calc_formula'] ) ) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
|
| 48 |
$formula = preg_replace_callback( |
| 49 |
'/%([a-zA-Z-_]+)::([a-zA-Z0-9-_]+)%/', |
| 50 |
function ( $matches ) { |
| 51 |
switch ( strtolower( $matches[1] ) ) { |
| 52 |
case 'field': |
| 53 |
return '%' . $matches[2] . '%'; |
| 54 |
|
| 55 |
case 'meta': |
| 56 |
return get_post_meta( Live_Form::instance()->post->ID, $matches[2], true ); |
| 57 |
|
| 58 |
default: |
| 59 |
$macros_name = $matches[1]; |
| 60 |
|
| 61 |
return apply_filters( "jet-engine/calculated-data/$macros_name", $matches[0], $matches ); |
| 62 |
} |
| 63 |
}, |
| 64 |
$args['calc_formula'] |
| 65 |
); |
| 66 |
|
| 67 |
return str_replace( '^', '**', $formula ); |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|