GravityView  2.10.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 GravityView <[email protected]>
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  * @since 1.22.1
23  * @param array $args Arguments for the dropdown
24  * @return string $output Dropdown of forms
25  */
26  public function form_dropdown( $args = array() ) {
27 
28  $defaults = array(
29  'active' => true,
30  'trash' => false,
31  'options' => array(),
32  'exclude' => array(),
33  'name' => 'gravityview_form_id',
34  'id' => 'gravityview_form_id',
35  'class' => '',
36  'multiple' => false,
37  'selected' => 0,
38  'show_option_none' => sprintf( '&mdash; %s &mdash;', esc_html__( 'list of forms', 'gravityview' ) ),
39  'data' => array( 'search-type' => 'form' ),
40  );
41 
42  $args = wp_parse_args( $args, $defaults );
43 
44  $forms = gravityview_get_forms( (bool) $args['active'], (bool) $args['trash'] );
45 
46  if( array() === $args['options'] ) {
47  foreach ( $forms as $form ) {
48 
49  if ( in_array( $form['id'], $args['exclude'] ) ) {
50  continue;
51  }
52 
53  $args['options'][ $form['id'] ] = esc_html( $form['title'] );
54  }
55  }
56 
57  $output = $this->select( $args );
58 
59  return $output;
60  }
61 
62  /**
63  * Renders an HTML Dropdown of all the fields in a form
64  *
65  * @param array $args Arguments for the dropdown
66  * @return string $output Product dropdown
67  */
68  public function field_dropdown( $args = array() ) {
69 
70  $defaults = array(
71  'form_id' => 0,
72  'options' => array(),
73  'name' => 'gravityview_form_fields',
74  'id' => 'gravityview_form_fields',
75  'class' => '',
76  'multiple' => false,
77  'selected' => 0,
78  'show_option_none' => __( 'Select a field', 'gravityview' ),
79  'data' => array( 'search-type' => 'form' ),
80  );
81 
82  $args = wp_parse_args( $args, $defaults );
83 
84  if( empty( $args['form_id'] ) ) {
85  return '';
86  }
87 
88  $fields = GVCommon::get_sortable_fields_array( $args['form_id'] );
89 
90  if( array() === $args['options'] ) {
91  foreach ( $fields as $field_id => $field ) {
92  $args['options'][ $field_id ] = esc_html( $field['label'] );
93  }
94  }
95 
96  $output = $this->select( $args );
97 
98  return $output;
99  }
100 
101  /**
102  * Renders an HTML Dropdown
103  *
104  * @since 1.22.1
105  *
106  * @param array $args
107  *
108  * @return string
109  */
110  public function select( $args = array() ) {
111  $defaults = array(
112  'options' => array(),
113  'name' => null,
114  'class' => '',
115  'id' => '',
116  'selected' => 0,
117  'placeholder' => null,
118  'multiple' => false,
119  'disabled' => false,
120  'show_option_all' => _x( 'All', 'all dropdown items', 'gravityview' ),
121  'show_option_none' => _x( 'None', 'no dropdown items', 'gravityview' ),
122  'data' => array(),
123  );
124 
125  $args = wp_parse_args( $args, $defaults );
126 
127  $data_elements = '';
128  foreach ( $args['data'] as $key => $value ) {
129  $data_elements .= ' data-' . esc_attr( $key ) . '="' . esc_attr( $value ) . '"';
130  }
131 
132  if( $args['multiple'] ) {
133  $multiple = ' MULTIPLE';
134  } else {
135  $multiple = '';
136  }
137 
138  if( $args['placeholder'] ) {
139  $placeholder = $args['placeholder'];
140  } else {
141  $placeholder = '';
142  }
143 
144  $disabled = $args['disabled'] ? ' disabled="disabled"' : '';
145  $class = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $args['class'] ) ) );
146  $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 . '>';
147 
148  if ( ! empty( $args['options'] ) ) {
149 
150  if ( $args['show_option_none'] ) {
151  if( $args['multiple'] ) {
152  $selected = selected( true, in_array( -1, $args['selected'] ), false );
153  } else {
154  $selected = selected( $args['selected'], -1, false );
155  }
156  $output .= '<option value="-1"' . $selected . '>' . esc_html( $args['show_option_none'] ) . '</option>';
157  }
158 
159  foreach( $args['options'] as $key => $option ) {
160 
161  if( $args['multiple'] && is_array( $args['selected'] ) ) {
162  $selected = selected( true, in_array( $key, $args['selected'], true ), false );
163  } else {
164  $selected = selected( $args['selected'], $key, false );
165  }
166 
167  $output .= '<option value="' . esc_attr( $key ) . '"' . $selected . '>' . esc_html( $option ) . '</option>';
168  }
169  }
170 
171  $output .= '</select>';
172 
173  return $output;
174  }
175 }
$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.
if(gravityview() ->plugin->is_GF_25()) $form
gravityview_get_forms( $active=true, $trash=false, $order_by='date_created', $order='ASC')
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