/** * @since 1.15.1 * @covers ::gv_empty() */ public function test_gv_empty() { $this->assertTrue(gv_empty(array())); $this->assertTrue(gv_empty(false)); $this->assertTrue(gv_empty(null)); $this->assertTrue(gv_empty(new stdClass())); $this->assertTrue(gv_empty('')); $this->assertTrue(gv_empty($not_defined)); // Test $zero_is_empty $this->assertTrue(gv_empty(0)); $this->assertTrue(gv_empty('0')); $this->assertTrue(gv_empty(floatval(0))); $this->assertFalse(gv_empty('0.0')); $this->assertFalse(gv_empty(0, false)); $this->assertFalse(gv_empty('0', false)); $this->assertFalse(gv_empty(floatval(0), false)); $this->assertFalse(gv_empty('0.0', false)); // Test $allow_string_booleans $this->assertTrue(gv_empty('false')); $this->assertTrue(gv_empty('no')); $this->assertFalse(gv_empty('false', true, false)); $this->assertFalse(gv_empty('no', true, false)); $this->assertFalse(gv_empty('true', true, false)); $this->assertFalse(gv_empty('yes', true, false)); }
<?php /** * Display Gravity Forms Quiz output * * @package GravityView * @subpackage GravityView/templates/fields */ $gravityview_view = GravityView_View::getInstance(); $field = $gravityview_view->getCurrentField(); // If there's no grade, don't continue if (gv_empty($field['value'])) { return; } if (!class_exists('GFQuiz')) { do_action('gravityview_log_error', __FILE__ . ': GFQuiz class does not exist.'); return; } // Get the setting for show/hide explanation $show_answer = rgars($field, 'field_settings/quiz_show_explanation'); // Update the quiz field so GF generates the output properly $field['field']->gquizShowAnswerExplanation = !empty($show_answer); // Generate the output echo GFQuiz::get_instance()->display_quiz_on_entry_detail($field['value'], $field['field'], $field['entry'], $field['form']);
<?php /** * Display the search text input field * * @see class-search-widget.php */ $gravityview_view = GravityView_View::getInstance(); $search_field = $gravityview_view->search_field; ?> <div class="gv-search-box"> <?php if (!gv_empty($search_field['label'], false)) { ?> <label for=search-box-<?php echo esc_attr($search_field['name']); ?> ><?php echo esc_html($search_field['label']); ?> </label> <?php } ?> <p> <input type="text" name="<?php echo esc_attr($search_field['name']); ?> " id="search-box-<?php echo esc_attr($search_field['name']);
/** * Display the search all input box * * @see class-search-widget.php */ $gravityview_view = GravityView_View::getInstance(); $view_id = $gravityview_view->getViewId(); $value = $gravityview_view->search_field['value']; $label = $gravityview_view->search_field['label']; ?> <div class="gv-search-box gv-search-field-text gv-search-field-search_all"> <div class="gv-search"> <?php if (!gv_empty($label, false)) { ?> <label for="gv_search_<?php echo $view_id; ?> "><?php echo esc_html($label); ?> </label> <?php } ?> <p><input type="text" name="gv_search" id="gv_search_<?php echo $view_id; ?> " value="<?php
<?php /** * Display Gravity Forms Quiz value letter grade * * @package GravityView * @subpackage GravityView/templates/fields */ $gravityview_view = GravityView_View::getInstance(); $field = $gravityview_view->getCurrentField(); // If there's no grade, don't continue if (gv_empty($field['value'], false, false)) { return; } // Check if grading is enabled for the form. If not set, default to false. $grading_type_enabled = !empty($field['form']['gravityformsquiz']['grading']) ? $field['form']['gravityformsquiz']['grading'] : 'none'; if ('letter' === $grading_type_enabled) { echo $field['value']; } elseif (GVCommon::has_cap('gravityforms_edit_forms')) { $grade_type = __('Letter', 'gravityview'); printf(esc_html_x('%s grading is disabled for this form. %sChange the setting%s', '%s is the current Quiz field type ("Letter" or "Pass/Fail")', 'gravityview'), $grade_type, '<a href="' . admin_url('admin.php?page=gf_edit_forms&view=settings&subview=gravityformsquiz&id=' . $gravityview_view->getFormId()) . '">', '</a>'); }
/** * Given an entry and a form field id, calculate the entry value for that field. * * @access public * @param array $entry * @param array $field * @return null|string */ public static function field_value($entry, $field_settings, $format = 'html') { if (empty($entry['form_id']) || empty($field_settings['id'])) { return NULL; } $gravityview_view = GravityView_View::getInstance(); $field_id = $field_settings['id']; $form = $gravityview_view->getForm(); $field = gravityview_get_field($form, $field_id); if ($field && is_numeric($field_id)) { // Used as file name of field template in GV. // Don't use RGFormsModel::get_input_type( $field ); we don't care if it's a radio input; we want to know it's a 'quiz' field $field_type = $field->type; $value = RGFormsModel::get_lead_field_value($entry, $field); } else { $field = GravityView_Fields::get_associated_field($field_id); $field_type = $field_id; // Used as file name of field template in GV } // If a Gravity Forms Field is found, get the field display if ($field) { // Prevent any PHP warnings that may be generated ob_start(); $display_value = GFCommon::get_lead_field_display($field, $value, $entry["currency"], false, $format); if ($errors = ob_get_clean()) { do_action('gravityview_log_error', 'GravityView_API[field_value] Errors when calling GFCommon::get_lead_field_display()', $errors); } $display_value = apply_filters("gform_entry_field_value", $display_value, $field, $entry, $form); // prevent the use of merge_tags for non-admin fields if (!empty($field->adminOnly)) { $display_value = self::replace_variables($display_value, $form, $entry); } } else { $value = $display_value = rgar($entry, $field_id); $display_value = $value; } // Check whether the field exists in /includes/fields/{$field_type}.php // This can be overridden by user template files. $field_path = $gravityview_view->locate_template("fields/{$field_type}.php"); // Set the field data to be available in the templates $gravityview_view->setCurrentField(array('form' => $form, 'field_id' => $field_id, 'field' => $field, 'field_settings' => $field_settings, 'value' => $value, 'display_value' => $display_value, 'format' => $format, 'entry' => $entry, 'field_type' => $field_type, 'field_path' => $field_path)); if (!empty($field_path)) { do_action('gravityview_log_debug', sprintf('[field_value] Rendering %s', $field_path)); ob_start(); load_template($field_path, false); $output = ob_get_clean(); } else { // Backup; the field template doesn't exist. $output = $display_value; } // Get the field settings again so that the field template can override the settings $field_settings = $gravityview_view->getCurrentField('field_settings'); /** * @filter `gravityview_field_entry_value_{$field_type}_pre_link` Modify the field value output for a field type before Show As Link setting is applied. Example: `gravityview_field_entry_value_number_pre_link` * @since 1.16 * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field Field array, as fetched from GravityView_View::getCurrentField() */ $output = apply_filters('gravityview_field_entry_value_' . $field_type . '_pre_link', $output, $entry, $field_settings, $gravityview_view->getCurrentField()); /** * Link to the single entry by wrapping the output in an anchor tag * * Fields can override this by modifying the field data variable inside the field. See /templates/fields/post_image.php for an example. * */ if (!empty($field_settings['show_as_link']) && !gv_empty($output, false, false)) { $link_atts = empty($field_settings['new_window']) ? array() : array('target' => '_blank'); $output = self::entry_link_html($entry, $output, $link_atts, $field_settings); } /** * @filter `gravityview_field_entry_value_{$field_type}` Modify the field value output for a field type. Example: `gravityview_field_entry_value_number` * @since 1.6 * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field Current field being displayed */ $output = apply_filters('gravityview_field_entry_value_' . $field_type, $output, $entry, $field_settings, $gravityview_view->getCurrentField()); /** * @filter `gravityview_field_entry_value` Modify the field value output for all field types * @param string $output HTML value output * @param array $entry The GF entry array * @param array $field_settings Settings for the particular GV field * @param array $field_data {@since 1.6} */ $output = apply_filters('gravityview_field_entry_value', $output, $entry, $field_settings, $gravityview_view->getCurrentField()); return $output; }
/** * Validate attributes passed to the [gravityview] shortcode. Supports {get} Merge Tags values. * * Attributes passed to the shortcode are compared to registered attributes {@see GravityView_View_Data::get_default_args} * Only attributes that are defined will be allowed through. * * Then, {get} merge tags are replaced with their $_GET values, if passed * * Then, attributes are sanitized based on the type of setting (number, checkbox, select, radio, text) * * @since 1.15.1 * * @see GravityView_View_Data::get_default_args Only attributes defined in get_default_args() are valid to be passed via the shortcode * * @param array $passed_atts Attribute pairs defined to render the View * * @return array Valid and sanitized attribute pairs */ private function parse_and_sanitize_atts($passed_atts) { $defaults = GravityView_View_Data::get_default_args(true); $supported_atts = array_fill_keys(array_keys($defaults), ''); // Whittle down the attributes to only valid pairs $filtered_atts = shortcode_atts($supported_atts, $passed_atts, 'gravityview'); // Only keep the passed attributes after making sure that they're valid pairs $filtered_atts = function_exists('array_intersect_key') ? array_intersect_key($passed_atts, $filtered_atts) : $filtered_atts; $atts = array(); foreach ($filtered_atts as $key => $passed_value) { // Allow using GravityView merge tags in shortcode attributes, like {get} and {created_by} $passed_value = GravityView_Merge_Tags::replace_variables($passed_value); switch ($defaults[$key]['type']) { /** * Make sure number fields are numeric. * Also, convert mixed number strings to numbers * @see http://php.net/manual/en/function.is-numeric.php#107326 */ case 'number': if (is_numeric($passed_value)) { $atts[$key] = $passed_value + 0; } break; // Checkboxes should be 1 or 0 // Checkboxes should be 1 or 0 case 'checkbox': $atts[$key] = gv_empty($passed_value) ? 0 : 1; break; /** * Only allow values that are defined in the settings */ /** * Only allow values that are defined in the settings */ case 'select': case 'radio': $options = isset($defaults[$key]['choices']) ? $defaults[$key]['choices'] : $defaults[$key]['options']; if (in_array($passed_value, array_keys($options))) { $atts[$key] = $passed_value; } break; case 'text': default: $atts[$key] = $passed_value; break; } } return $atts; }
/** * Modify the value for the current field input * * @param GF_Field $field * * @return array|mixed|string|void */ private function get_field_value($field) { /** * @filter `gravityview/edit_entry/pre_populate/override` Allow the pre-populated value to override saved value in Edit Entry form. By default, pre-populate mechanism only kicks on empty fields. * @param boolean True: override saved values; False: don't override (default) * @param $field GF_Field object Gravity Forms field object * @since 1.13 */ $override_saved_value = apply_filters('gravityview/edit_entry/pre_populate/override', false, $field); // We're dealing with multiple inputs (e.g. checkbox) but not time or date (as it doesn't store data in input IDs) if (isset($field->inputs) && is_array($field->inputs) && !in_array($field->type, array('time', 'date'))) { $field_value = array(); // only accept pre-populated values if the field doesn't have any choice selected. $allow_pre_populated = $field->allowsPrepopulate; foreach ((array) $field->inputs as $input) { $input_id = strval($input['id']); if (isset($this->entry[$input_id]) && !gv_empty($this->entry[$input_id], false, false)) { $field_value[$input_id] = 'post_category' === $field->type ? GFCommon::format_post_category($this->entry[$input_id], true) : $this->entry[$input_id]; $allow_pre_populated = false; } } $pre_value = $field->get_value_submission(array(), false); $field_value = !$allow_pre_populated && !($override_saved_value && !gv_empty($pre_value, false, false)) ? $field_value : $pre_value; } else { $id = intval($field->id); // get pre-populated value if exists $pre_value = $field->allowsPrepopulate ? GFFormsModel::get_parameter_value($field->inputName, array(), $field) : ''; // saved field entry value (if empty, fallback to the pre-populated value, if exists) // or pre-populated value if not empty and set to override saved value $field_value = !gv_empty($this->entry[$id], false, false) && !($override_saved_value && !gv_empty($pre_value, false, false)) ? $this->entry[$id] : $pre_value; // in case field is post_category but inputType is select, multi-select or radio, convert value into array of category IDs. if ('post_category' === $field->type && !gv_empty($field_value, false, false)) { $categories = array(); foreach (explode(',', $field_value) as $cat_string) { $categories[] = GFCommon::format_post_category($cat_string, true); } $field_value = 'multiselect' === $field->get_input_type() ? $categories : implode('', $categories); } } // if value is empty get the default value if defined $field_value = $field->get_value_default_if_empty($field_value); return $field_value; }