Example #1
0
/**
 * Output field based on a certain html markup
 *
 *   markup - string to be used on a sprintf statement.
 *      Use:
 *         {{label}} - field label
 *         {{value}} - entry field value
 *         {{class}} - field class
 *
 *   wpautop - true will filter the value using wpautop function
 *
 * @since  1.1.5
 * @param  array $passed_args Associative array with field data. `field` and `form` are required.
 * @return string
 */
function gravityview_field_output($passed_args)
{
    $defaults = array('entry' => NULL, 'field' => NULL, 'form' => NULL, 'hide_empty' => true, 'markup' => '<div class="{{class}}">{{label}}{{value}}</div>', 'label_markup' => '', 'wpautop' => false, 'zone_id' => NULL);
    $args = wp_parse_args($passed_args, $defaults);
    /**
     * Modify the args before generation begins
     *
     * @since 1.7
     *
     * @param array $args Associative array; `field` and `form` is required.
     * @param array $passed_args Original associative array with field data. `field` and `form` are required.
     *
     */
    $args = apply_filters('gravityview/field_output/args', $args, $passed_args);
    // Required fields.
    if (empty($args['field']) || empty($args['form'])) {
        do_action('gravityview_log_error', '[gravityview_field_output] Field or form are empty.', $args);
        return '';
    }
    $entry = empty($args['entry']) ? array() : $args['entry'];
    $value = gv_value($entry, $args['field']);
    // If the value is empty and we're hiding empty, return empty.
    if ($value === '' && !empty($args['hide_empty'])) {
        return '';
    }
    if ($value !== '' && !empty($args['wpautop'])) {
        $value = wpautop($value);
    }
    // Get width setting, if exists
    $width = GravityView_API::field_width($args['field']);
    //If replacing with CSS inline formatting, let's do it.
    $width_style = GravityView_API::field_width($args['field'], 'width:' . $width . '%%;');
    $class = gv_class($args['field'], $args['form'], $entry);
    // get field label if needed
    if (!empty($args['label_markup']) || false !== strpos($args['markup'], '{{label}}')) {
        $label = gv_label($args['field'], $entry);
    } else {
        $label = '';
    }
    if (!empty($label)) {
        // If the label markup is overridden
        if (!empty($args['label_markup'])) {
            $label = str_replace('{{label}}', '<span class="gv-field-label">' . $label . '</span>', $args['label_markup']);
        } else {
            $args['markup'] = str_replace('{{label}}', '<span class="gv-field-label">{{label}}</span>', $args['markup']);
        }
    }
    $html = $args['markup'];
    $html = str_replace('{{width}}', $width, $html);
    $html = str_replace('{{width:style}}', $width_style, $html);
    $html = str_replace('{{class}}', $class, $html);
    $html = str_replace('{{label}}', $label, $html);
    $html = str_replace('{{value}}', $value, $html);
    /**
     * Modify the output
     * @param string $html Existing HTML output
     * @param array $args Arguments passed to the function
     */
    $html = apply_filters('gravityview_field_output', $html, $args);
    unset($value, $label, $class, $width, $width_style, $args, $passed_args, $entry);
    return $html;
}
Example #2
0
/**
 * Output field based on a certain html markup
 *
 *   markup - string to be used on a sprintf statement.
 *      Use:
 *         {{label}} - field label
 *         {{value}} - entry field value
 *         {{class}} - field class
 *
 *   wpautop - true will filter the value using wpautop function
 *
 * @since  1.1.5
 * @param  array $passed_args Associative array with field data. `field` and `form` are required.
 * @return string Field output. If empty value and hide empty is true, return empty.
 */
function gravityview_field_output($passed_args)
{
    $defaults = array('entry' => null, 'field' => null, 'form' => null, 'hide_empty' => true, 'markup' => '<div id="{{ field_id }}" class="{{ class }}">{{label}}{{value}}</div>', 'label_markup' => '', 'wpautop' => false, 'zone_id' => null);
    $args = wp_parse_args($passed_args, $defaults);
    /**
     * @filter `gravityview/field_output/args` Modify the args before generation begins
     * @since 1.7
     * @param array $args Associative array; `field` and `form` is required.
     * @param array $passed_args Original associative array with field data. `field` and `form` are required.
     */
    $args = apply_filters('gravityview/field_output/args', $args, $passed_args);
    // Required fields.
    if (empty($args['field']) || empty($args['form'])) {
        do_action('gravityview_log_error', '[gravityview_field_output] Field or form are empty.', $args);
        return '';
    }
    $entry = empty($args['entry']) ? array() : $args['entry'];
    /**
     * Create the Context for replacing.
     * @since 1.11
     */
    $context = array();
    $context['value'] = gv_value($entry, $args['field']);
    // If the value is empty and we're hiding empty, return empty.
    if ($context['value'] === '' && !empty($args['hide_empty'])) {
        return '';
    }
    if ($context['value'] !== '' && !empty($args['wpautop'])) {
        $context['value'] = wpautop($context['value']);
    }
    // Get width setting, if exists
    $context['width'] = GravityView_API::field_width($args['field']);
    // If replacing with CSS inline formatting, let's do it.
    $context['width:style'] = GravityView_API::field_width($args['field'], 'width:' . $context['width'] . '%;');
    // Grab the Class using `gv_class`
    $context['class'] = gv_class($args['field'], $args['form'], $entry);
    $context['field_id'] = GravityView_API::field_html_attr_id($args['field'], $args['form'], $entry);
    // Get field label if needed
    if (!empty($args['label_markup'])) {
        $context['label'] = str_replace(array('{{label}}', '{{ label }}'), '<span class="gv-field-label">{{ label_value }}</span>', $args['label_markup']);
    }
    if (empty($context['label'])) {
        $context['label'] = '<span class="gv-field-label">{{ label_value }}</span>';
    }
    // Default Label value
    $context['label_value'] = gv_label($args['field'], $entry);
    /**
     * @filter `gravityview/field_output/pre_html` Allow Pre filtering of the HTML
     * @since 1.11
     * @param string $markup The HTML for the markup
     * @param array $args All args for the field output
     */
    $html = apply_filters('gravityview/field_output/pre_html', $args['markup'], $args);
    /**
     * @filter `gravityview/field_output/open_tag` Modify the opening tags for the template content placeholders
     * @since 1.11
     * @param string $open_tag Open tag for template content placeholders. Default: `{{`
     */
    $open_tag = apply_filters('gravityview/field_output/open_tag', '{{', $args);
    /**
     * @filter `gravityview/field_output/close_tag` Modify the closing tags for the template content placeholders
     * @since 1.11
     * @param string $close_tag Close tag for template content placeholders. Default: `}}`
     */
    $close_tag = apply_filters('gravityview/field_output/close_tag', '}}', $args);
    /**
     * Loop through each of the tags to replace and replace both `{{tag}}` and `{{ tag }}` with the values
     * @since 1.11
     */
    foreach ($context as $tag => $value) {
        // If the tag doesn't exist just skip it
        if (false === strpos($html, $open_tag . $tag . $close_tag) && false === strpos($html, $open_tag . ' ' . $tag . ' ' . $close_tag)) {
            continue;
        }
        // Array to search
        $search = array($open_tag . $tag . $close_tag, $open_tag . ' ' . $tag . ' ' . $close_tag);
        /**
         * `gravityview/field_output/context/{$tag}` Allow users to filter content on context
         * @since 1.11
         * @param string $value The content to be shown instead of the {{tag}} placeholder
         * @param array $args Arguments passed to the function
         */
        $value = apply_filters('gravityview/field_output/context/' . $tag, $value, $args);
        // Finally do the replace
        $html = str_replace($search, $value, $html);
    }
    /**
     * @todo  Depricate `gravityview_field_output`
     */
    $html = apply_filters('gravityview_field_output', $html, $args);
    /**
     * @filter `gravityview/field_output/html` Modify field HTML output
     * @param string $html Existing HTML output
     * @param array $args Arguments passed to the function
     */
    $html = apply_filters('gravityview/field_output/html', $html, $args);
    // Just free up a tiny amount of memory
    unset($value, $args, $passed_args, $entry, $context, $search, $open_tag, $tag, $close_tag);
    return $html;
}
 /**
  * @group field_width
  * @covers GravityView_API::field_width()
  */
 public function test_field_width()
 {
     $field = array();
     // Empty $field['width'] returns NULL
     $width = GravityView_API::field_width($field);
     $this->assertNull($width);
     // Default: convert to %
     $field['width'] = 10;
     $width = GravityView_API::field_width($field);
     $this->assertEquals('10%', $width);
     // Limit to 100% when using default % formatting
     $field['width'] = 200;
     $width = GravityView_API::field_width($field);
     $this->assertEquals('100%', $width);
     // Check other formats
     $format = '%dpx';
     $field['width'] = 200;
     $width = GravityView_API::field_width($field, $format);
     $this->assertEquals('200px', $width);
     $format = '%d';
     $field['width'] = 500000;
     $width = GravityView_API::field_width($field, $format);
     $this->assertEquals('500000', $width);
 }