← All changes
|
includes/blocks/render/calculated-field-render.php
+70
-78
1.1.0
→
3.6.6
View file →
| @@ -1,78 +1,70 @@ | ||
| 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 | -} | |
| 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 | +} | |