From a754b4bb8f39b19cebdb7ea91e05cdcc90b520d1 Mon Sep 17 00:00:00 2001 From: saifsultanc Date: Sat, 20 Sep 2025 11:29:37 +0530 Subject: [PATCH] `gw-gravity-forms-rounding.php`: Added deprecation notice to the snippet. --- gravity-forms/gw-gravity-forms-rounding.php | 467 +------------------- 1 file changed, 3 insertions(+), 464 deletions(-) diff --git a/gravity-forms/gw-gravity-forms-rounding.php b/gravity-forms/gw-gravity-forms-rounding.php index 9fa989fac..e95d8cf63 100644 --- a/gravity-forms/gw-gravity-forms-rounding.php +++ b/gravity-forms/gw-gravity-forms-rounding.php @@ -1,467 +1,6 @@ =' ) ) { - return; - } - - // time for hooks - add_filter( 'gform_pre_render', array( $this, 'prepare_form_and_load_script' ), 10, 2 ); - add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ) ); - add_filter( 'gform_enqueue_scripts', array( $this, 'enqueue_form_scripts' ) ); - - add_action( 'gform_pre_submission', array( $this, 'override_submitted_value' ), 10, 5 ); - add_filter( 'gform_calculation_result', array( $this, 'override_submitted_calculation_value' ), 10, 5 ); - - } - - public function prepare_form_and_load_script( $form, $is_ajax_enabled ) { - - if ( ! $this->is_applicable_form( $form ) ) { - return $form; - } - - if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { - add_action( 'wp_footer', array( $this, 'output_script' ) ); - add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); - add_action( 'admin_footer', array( $this, 'output_script' ) ); - } - - foreach ( $form['fields'] as &$field ) { - if ( preg_match( $this->get_class_regex(), $field['cssClass'] ) ) { - $field['cssClass'] .= ' gw-rounding'; - } - } - - return $form; - } - - public function is_ajax_submission( $form_id, $is_ajax_enabled ) { - return class_exists( 'GFFormDisplay' ) && isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled; - } - - function output_script() { - ?> - - - - is_applicable_form( $form ) ) { - return; - } - - $args = array( - 'formId' => $form['id'], - 'classRegex' => $this->class_regex, - ); - $script = 'new GWRounding( ' . json_encode( $args ) . ' );'; - - GFFormDisplay::add_init_script( $form['id'], 'gw_rounding', GFFormDisplay::ON_PAGE_RENDER, $script ); - - } - - function enqueue_form_scripts( $form ) { - - if ( $this->is_applicable_form( $form ) ) { - wp_enqueue_script( 'gform_gravityforms' ); - } - - } - - function override_submitted_value( $form ) { - - foreach ( $form['fields'] as $field ) { - if ( $this->is_applicable_field( $field ) && ! is_a( $field, 'GF_Field_Calculation' ) ) { - - $value = rgpost( "input_{$field['id']}" ); - - $is_currency = $field->get_input_type() == 'number' && $field->numberFormat == 'currency'; - if ( $is_currency ) { - $value = GFCommon::to_number( $value ); - } - - $value = $this->process_rounding_actions( $value, $this->get_rounding_actions( $field ) ); - - $_POST[ "input_{$field['id']}" ] = $value; - - } - } - - } - - function override_submitted_calculation_value( $result, $formula, $field, $form, $entry ) { - - if ( $this->is_applicable_field( $field ) ) { - $result = $this->process_rounding_actions( $result, $this->get_rounding_actions( $field ) ); - } - - return $result; - } - - function process_rounding_actions( $value, $actions ) { - - foreach ( $actions as $action ) { - $value = $this->round( $value, $action['action'], $action['action_value'] ); - } - - return $value; - } - - function round( $value, $action, $action_value ) { - - $value = floatval( $value ); - $action_value = floatval( $action_value ); - - switch ( $action ) { - case 'min': - $min = $action_value; - if ( $value < $min ) { - $value = $min; - } - break; - case 'max': - $max = $action_value; - if ( $value > $max ) { - $value = $max; - } - break; - case 'up': - $interval = $action_value; - $base = ceil( $value / $interval ); - $value = $base * $interval; - break; - case 'down': - $interval = $action_value; - $base = floor( $value / $interval ); - $value = $base * $interval; - break; - case 'round': - $interval = $action_value; - $base = round( $value / $interval ); - $value = $base * $interval; - break; - default: - /** - * Custom rounding filter - * - * Use this filter to implement your own rounding method. The filter's name is based on - * the CSS class set on the field. - * - * Example: - * CSS Class: gw-round-mycustomroundroundingfunc-10 - * Filter: gw_round_mycustomroundingfunc - * - * @param int $value Current input value to be rounded - * @param int $actionValue Custom value passed in CSS class name (e.g. gw-round-custom-10, actionValue = 10) - */ - // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores - $value = apply_filters( sprintf( 'gw_round_%s', $action ), $value, $action_value ); - break; - } - - return floatval( $value ); - } - - // # HELPERS - - function is_applicable_form( $form ) { - - foreach ( $form['fields'] as $field ) { - if ( $this->is_applicable_field( $field ) ) { - return true; - } - } - - return false; - } - - function is_applicable_field( $field ) { - return preg_match( $this->get_class_regex(), rgar( $field, 'cssClass' ) ) == true; - } - - function get_class_regex() { - return "/{$this->class_regex}/"; - } - - function get_rounding_actions( $field ) { - - $actions = array(); - - preg_match_all( $this->get_class_regex(), rgar( $field, 'cssClass' ), $matches, PREG_SET_ORDER ); - - foreach ( $matches as $match ) { - - list( $full_match, $action, $action_value ) = array_pad( $match, 3, false ); - - if ( $action_value === false && is_numeric( $action ) ) { - $action_value = $action; - $action = 'round'; - } - - $action = array( - 'action' => $action, - 'action_value' => $action_value, - ); - - $actions[] = $action; - - } - - return $actions; - } - -} - -function gw_rounding() { - return GW_Rounding::get_instance(); -} - -gw_rounding();