'alphagf_read_only', 'src' => $this->get_base_url() . '/js/alpha_read_only.js', 'version' => $this->_version, 'deps' => array('jquery'), 'enqueue' => array(array($this, 'frontend_script_callback')) ), ); return array_merge(parent::scripts(), $scripts); } public function add_read_only_custom_class($classes, $field, $form) { if (! empty($field->alphagf_read_only) && in_array($field->type, $this->_supported_field_type, true)) { $classes .= ' read_only_field'; } return $classes; } /** * Check if a field is a supported field with read only enabled. * * @param GF_Field $field * * @return bool */ private function is_read_only_field($field) { return $field instanceof GF_Field && ! empty($field->alphagf_read_only) && in_array($field->type, $this->_supported_field_type, true); } /** * Get the dynamic population values as Gravity Forms writes them to the gform_field_values input. * * @param array|string|null $field_values * * @return string */ private function get_field_values_string($field_values) { return is_array($field_values) ? http_build_query($field_values) : (string) $field_values; } /** * Remember the dynamic population values used to render the form so they can be signed in the form state. * * @param array $form * @param bool $ajax * @param array|string|null $field_values * * @return array */ public function remember_field_values($form, $ajax = false, $field_values = null) { if (! empty($form['id'])) { $this->_rendered_field_values[absint($form['id'])] = $this->get_field_values_string($field_values); } return $form; } /** * Add a hash of the gform_field_values input to the form state, so tampered values can be detected on submission. * * @param object $state_handler Gravity Forms state handler. * @param array $keys * @param int $form_id * * @return void */ public function sign_field_values($state_handler, $keys, $form_id) { $form_id = absint($form_id); if (! isset($this->_rendered_field_values[$form_id]) || ! method_exists($state_handler, 'add_hashes')) { return; } $state_handler->add_hashes($form_id, 'alphagf_read_only_field_values', wp_hash($this->_rendered_field_values[$form_id])); } /** * Get the submitted dynamic population values, ignoring them if they don't match the signed form state. * * @param int $form_id * * @return array */ private function get_submitted_field_values($form_id) { $field_values = rgpost('gform_field_values'); if (! is_string($field_values) || $field_values === '') { return array(); } // The form state is only available from Gravity Forms 3.0. Without it the submitted values can't be trusted. if (! method_exists('GFFormDisplay', 'get_state_handler')) { return array(); } $state_handler = GFFormDisplay::get_state_handler(); if (! is_object($state_handler) || ! method_exists($state_handler, 'is_valid_additional_value') || ! $state_handler->is_valid_additional_value($form_id, 'alphagf_read_only_field_values', $field_values)) { return array(); } parse_str($field_values, $parsed); return is_array($parsed) ? $parsed : array(); } /** * Replace the submitted values of read only fields with the values the form was displayed with. * * Runs before validation, so validation, calculations, notifications and the saved entry all use the enforced values. * * @param array $form * * @return array */ public function enforce_read_only_values($form) { if (empty($form['fields']) || ! is_array($form['fields']) || empty($_POST['is_submit_' . absint(rgar($form, 'id'))])) { // phpcs:ignore WordPress.Security.NonceVerification.Missing return $form; } $field_values = null; foreach ($form['fields'] as $field) { if (! $this->is_read_only_field($field)) { continue; } if ($field_values === null) { $field_values = $this->get_submitted_field_values(absint($form['id'])); } $field_id = absint($field->id); // These change the value on the server side, so drop them before the value is worked out. unset($_POST['input_' . $field_id . '_copy_values_activated'], $_POST['input_' . $field_id . '_other']); $this->set_submitted_value($field, $this->get_read_only_value($field, $field_values)); } return $form; } /** * Get the value a read only field is displayed with: its dynamic population value, default value or preselected choices. * * @param GF_Field $field * @param array $field_values * * @return array|string */ private function get_read_only_value($field, $field_values) { $value = GFFormsModel::get_field_value($field, $field_values, false); $value = $field->get_value_default_if_empty($value); if (! is_array($field->choices)) { return $value; } // Checkboxes are displayed with their preselected choices checked as well as any populated ones. if (is_array($field->get_entry_inputs())) { return array_merge(is_array($value) ? $value : array(), $this->get_selected_choices_value($field)); } // Radio buttons and drop downs only use their preselected choice when there is no other value. if (GFCommon::is_empty_array($value)) { $value = $this->get_selected_choices_value($field); } return $value; } /** * Get the value of the choices marked as selected by default. * * @param GF_Field $field * * @return array|string */ private function get_selected_choices_value($field) { $inputs = $field->get_entry_inputs(); if (! is_array($inputs)) { foreach ($field->choices as $choice) { if (rgar($choice, 'isSelected')) { return $this->get_choice_value($field, $choice); } } // Browsers submit the first option of a drop down that has no selected option or placeholder. if ($field->get_input_type() === 'select' && rgblank($field->placeholder) && ! empty($field->choices)) { return $this->get_choice_value($field, reset($field->choices)); } return ''; } $choices_by_key = array(); foreach ($field->choices as $choice) { if (rgar($choice, 'key')) { $choices_by_key[$choice['key']] = $choice; } } $value = array(); foreach (array_values((array) $field->inputs) as $index => $input) { $choice = rgar($input, 'key') ? rgar($choices_by_key, $input['key']) : rgar($field->choices, $index); if (rgar($choice, 'isSelected')) { $value[(string) $input['id']] = $this->get_choice_value($field, $choice); } } return $value; } /** * Get the value a choice is submitted with. * * @param GF_Field $field * @param array $choice * * @return string */ private function get_choice_value($field, $choice) { if (method_exists($field, 'get_choice_option_value')) { return $field->get_choice_option_value($choice); } return rgar($choice, 'value'); } /** * Write a field value to $_POST using the input names the field reads on submission. * * @param GF_Field $field * @param array|string $value * * @return void */ private function set_submitted_value($field, $value) { $field_id = absint($field->id); $inputs = $field->get_entry_inputs(); // Name, address, checkbox and other fields that submit one input per sub-field. if (is_array($inputs)) { $value = is_array($value) ? $value : array(); foreach ($inputs as $input) { $input_id = (string) $input['id']; $this->set_post_value('input_' . str_replace('.', '_', $input_id), rgar($value, $input_id)); } return; } if ($field->get_input_type() === 'email' && $field->emailConfirmEnabled) { $value = is_array($value) ? array_values($value) : array($value, $value); $this->set_post_value('input_' . $field_id, rgar($value, 0)); $this->set_post_value('input_' . $field_id . '_2', rgar($value, 1, rgar($value, 0))); return; } // Time, list and multi-input date fields submit a plain array under the field ID. $this->set_post_value('input_' . $field_id, is_array($value) ? array_values($value) : $value); } /** * Set or remove a $_POST value. Values are slashed to match the rest of $_POST. * * @param string $name * @param array|string $value * * @return void */ private function set_post_value($name, $value) { if (GFCommon::is_empty_array($value)) { unset($_POST[$name]); return; } $_POST[$name] = wp_slash($value); } public function read_only_field_settings($position) { if ((int) $position !== 20) { return; } ?>