GravityView  2.1.1
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-html-elements.php
Go to the documentation of this file.
1 <?php
2 /**
3  * GravityView HTML elements that are commonly used
4  *
5  * Thanks to EDD
6  * @see https://github.com/easydigitaldownloads/easy-digital-downloads/blob/master/includes/class-edd-html-elements.php
7  *
8  * @package GravityView
9  * @license GPL2+
10  * @author Katz Web Services, Inc.
11  * @link http://gravityview.co
12  * @copyright Copyright 2016, Katz Web Services, Inc.
13  * @since 1.22.1
14  */
15 
17 
18 
19  /**
20  * Renders an HTML Dropdown of all the Products (Downloads)
21  *
22  * @access public
23  * @since 1.22.1
24  * @param array $args Arguments for the dropdown
25  * @return string $output Dropdown of forms
26  */
27  public function form_dropdown( $args = array() ) {
28 
29  $defaults = array(
30  'active' => true,
31  'trash' => false,
32  'options' => array(),
33  'exclude' => array(),
34  'name' => 'gravityview_form_id',
35  'id' => 'gravityview_form_id',
36  'class' => '',
37  'multiple' => false,
38  'selected' => 0,
39  'show_option_none' => sprintf( '&mdash; %s &mdash;', esc_html__( 'list of forms', 'gravityview' ) ),
40  'data' => array( 'search-type' => 'form' ),
41  );
42 
43  $args = wp_parse_args( $args, $defaults );
44 
45  $forms = gravityview_get_forms( (bool) $args['active'], (bool) $args['trash'] );
46 
47  if( array() === $args['options'] ) {
48  foreach ( $forms as $form ) {
49 
50  if ( in_array( $form['id'], $args['exclude'] ) ) {
51  continue;
52  }
53 
54  $args['options'][ $form['id'] ] = esc_html( $form['title'] );
55  }
56  }
57 
58  $output = $this->select( $args );
59 
60  return $output;
61  }
62 
63  /**
64  * Renders an HTML Dropdown of all the fields in a form
65  *
66  * @access public
67  * @param array $args Arguments for the dropdown
68  * @return string $output Product dropdown
69  */
70  public function field_dropdown( $args = array() ) {
71 
72  $defaults = array(
73  'form_id' => 0,
74  'options' => array(),
75  'name' => 'gravityview_form_fields',
76  'id' => 'gravityview_form_fields',
77  'class' => '',
78  'multiple' => false,
79  'selected' => 0,
80  'show_option_none' => __( 'Select a field', 'gravityview' ),
81  'data' => array( 'search-type' => 'form' ),
82  );
83 
84  $args = wp_parse_args( $args, $defaults );
85 
86  if( empty( $args['form_id'] ) ) {
87  return '';
88  }
89 
90  $fields = GVCommon::get_sortable_fields_array( $args['form_id'] );
91 
92  if( array() === $args['options'] ) {
93  foreach ( $fields as $field_id => $field ) {
94  $args['options'][ $field_id ] = esc_html( $field['label'] );
95  }
96  }
97 
98  $output = $this->select( $args );
99 
100  return $output;
101  }
102 
103  /**
104  * Renders an HTML Dropdown
105  *
106  * @since 1.22.1
107  *
108  * @param array $args
109  *
110  * @return string
111  */
112  public function select( $args = array() ) {
113  $defaults = array(
114  'options' => array(),
115  'name' => null,
116  'class' => '',
117  'id' => '',
118  'selected' => 0,
119  'placeholder' => null,
120  'multiple' => false,
121  'disabled' => false,
122  'show_option_all' => _x( 'All', 'all dropdown items', 'gravityview' ),
123  'show_option_none' => _x( 'None', 'no dropdown items', 'gravityview' ),
124  'data' => array(),
125  );
126 
127  $args = wp_parse_args( $args, $defaults );
128 
129  $data_elements = '';
130  foreach ( $args['data'] as $key => $value ) {
131  $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
132  }
133 
134  if( $args['multiple'] ) {
135  $multiple = ' MULTIPLE';
136  } else {
137  $multiple = '';
138  }
139 
140  if( $args['placeholder'] ) {
141  $placeholder = $args['placeholder'];
142  } else {
143  $placeholder = '';
144  }
145 
146  $disabled = $args['disabled'] ? ' disabled="disabled"' : '';
147  $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
148  $output = '<select name="' . esc_attr( $args['name'] ) . '" id="' . esc_attr( str_replace( '-', '_', $args['id'] ) ) . '" class="gravityview-select ' . $class . '"' . $multiple . $disabled . ' data-placeholder="' . $placeholder . '"'. $data_elements . '>';
149 
150  if ( ! empty( $args['options'] ) ) {
151 
152  if ( $args['show_option_none'] ) {
153  if( $args['multiple'] ) {
154  $selected = selected( true, in_array( -1, $args['selected'] ), false );
155  } else {
156  $selected = selected( $args['selected'], -1, false );
157  }
158  $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
159  }
160 
161  foreach( $args['options'] as $key => $option ) {
162 
163  if( $args['multiple'] && is_array( $args['selected'] ) ) {
164  $selected = selected( true, in_array( $key, $args['selected'], true ), false );
165  } else {
166  $selected = selected( $args['selected'], $key, false );
167  }
168 
169  $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
170  }
171  }
172 
173  $output .= '</select>';
174 
175  return $output;
176  }
177 }
$forms
Definition: data-source.php:19
form_dropdown( $args=array())
Renders an HTML Dropdown of all the Products (Downloads)
$class
field_dropdown( $args=array())
Renders an HTML Dropdown of all the fields in a form.
gravityview_get_forms( $active=true, $trash=false)
Alias of GFAPI::get_forms()
$field_id
Definition: time.php:17
static get_sortable_fields_array( $formid, $blacklist=array( 'list', 'textarea'))
select( $args=array())
Renders an HTML Dropdown.
$field
Definition: gquiz_grade.php:11