GravityView  2.10.1
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-widget-gravityforms.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Widget to display a Gravity Forms form
5  */
7 
8  public $icon = 'data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB3aWR0aD0iMTE0IiBoZWlnaHQ9IjEyMy42Ij48c3R5bGU-LnN0MHtmaWxsOm5vbmV9PC9zdHlsZT48cGF0aCBjbGFzcz0ic3QwIiBkPSJNMzguOCA0NC44Yy0yLjMgMS00LjMgMi40LTYuMSA0LjMtNC4yIDQuNS02LjUgMTAtNi44IDE2LjQtLjMgNi40LS40IDkuOC0uMyAxMC4xbC40IDUuMWg2Mi41VjY0SDc3LjZ2NS44SDM2LjVjLjEtMS44LjQtNCAxLTYuN3MxLjYtNC45IDMuMS02LjZjLjgtLjcgMS42LTEuMiAyLjUtMS42LjktLjQgMi0uNiAzLjEtLjZoNDIuNnYtMTFINDYuNGMtMi44LjEtNS4zLjYtNy42IDEuNXoiLz48cGF0aCBkPSJNMTEwLjEgMzEuNmMtMS43LTMtMy44LTUuMS02LjItNi42TDY2IDMuMUM2My42IDEuNyA2MC42IDEgNTcuMiAxYy0zLjUgMC02LjQuNy04LjggMi4xTDEwLjUgMjVjLTIuNCAxLjQtNC41IDMuNi02LjIgNi42LTEuOCAzLTIuNiA1LjktMi42IDguN1Y4NGMwIDIuOC45IDUuNiAyLjYgOC42czMuOCA1LjIgNi4yIDYuNmwzNy45IDIxLjljMi40IDEuMyA1LjQgMiA4LjggMiAzLjUgMCA2LjQtLjcgOC44LTJsMzcuOS0yMS45YzIuNC0xLjQgNC41LTMuNiA2LjItNi42IDEuNy0zIDIuNi01LjkgMi42LTguNlY0MC4yYy0uMS0yLjgtLjktNS43LTIuNi04LjZ6TTg4LjkgNTQuNEg0Ni4yYy0xLjIgMC0yLjIuMi0zLjEuNi0uOS40LTEuOC45LTIuNSAxLjYtMS41IDEuNy0yLjUgMy45LTMuMSA2LjYtLjYgMi43LS45IDQuOS0xIDYuN2g0MS4xVjY0aDEwLjl2MTYuOEgyNmwtLjQtNS4xYy0uMS0uMyAwLTMuNy4zLTEwLjEuMy02LjQgMi42LTExLjkgNi44LTE2LjQgMS44LTEuOSAzLjgtMy40IDYuMS00LjMgMi4zLTEgNC44LTEuNCA3LjYtMS40aDQyLjV2MTAuOXoiLz48L3N2Zz4';
9 
10  /**
11  * Does this get displayed on a single entry?
12  * @var boolean
13  */
14  protected $show_on_single = true;
15 
16  function __construct() {
17 
18  $this->widget_description = __('Display a Gravity Forms form.', 'gravityview' );
19 
20  $default_values = array(
21  'header' => 1,
22  'footer' => 1,
23  );
24 
25  $settings = array(
26  'form_id' => array(
27  'type' => 'select',
28  'label' => __( 'Form to display', 'gravityview' ),
29  'value' => '',
30  'options' => $this->_get_form_choices(),
31  ),
32  'title' => array(
33  'type' => 'checkbox',
34  'label' => __( 'Show form title?', 'gravityview' ),
35  'value' => 1,
36  ),
37  'description' => array(
38  'type' => 'checkbox',
39  'label' => __( 'Show form description?', 'gravityview' ),
40  'value' => 1,
41  ),
42  'ajax' => array(
43  'type' => 'checkbox',
44  'label' => __( 'Enable AJAX', 'gravityview' ),
45  'desc' => '',
46  'value' => 1,
47  ),
48  'field_values' => array(
49  'type' => 'text',
50  'class' => 'code widefat',
51  'label' => __( 'Field value parameters', 'gravityview' ),
52  'desc' => '<a href="https://docs.gravityforms.com/using-dynamic-population/" rel="external">' . esc_html__( 'Learn how to dynamically populate a field.', 'gravityview' ) . '</a>',
53  'value' => '',
54  ),
55  );
56 
57  add_filter( 'gravityview/widget/hide_until_searched/whitelist', array( $this, 'add_to_allowlist' ) );
58 
59  parent::__construct( __( 'Gravity Forms', 'gravityview' ) , 'gravityforms', $default_values, $settings );
60  }
61 
62  /**
63  * Returns an array of active forms to show as choices for the widget
64  *
65  * @since 2.9.0.1
66  *
67  * @return array Array with key set to Form ID => Form Title, with `0` as default placeholder.
68  */
69  private function _get_form_choices() {
70 
71  $choices = array(
72  0 => '&mdash; ' . esc_html__( 'list of forms', 'gravityview' ) . '&mdash;',
73  );
74 
75  if ( ! class_exists( 'GFAPI' ) ) {
76  return $choices;
77  }
78 
79  // Inside GV's widget AJAX request
80  $doing_ajax = defined( 'DOING_AJAX' ) && 'gv_field_options' === \GV\Utils::_POST( 'action' );
81 
82  /**
83  * gravityview_get_forms() is currently running too early as widgets_init runs before init and
84  * when most Gravity Forms plugins register their own fields like GP Terms of Service.
85  */
86  if( $doing_ajax || ( \GV\Admin_Request::is_admin() && ! GFForms::is_gravity_page() ) ) {
87 
88  // check for available gravity forms
90 
91  foreach ( $forms as $form ) {
92  $choices[ $form['id'] ] = $form['title'];
93  }
94  }
95 
96  return $choices;
97  }
98 
99  /**
100  * Add widget to a list of allowed "Hide Until Searched" items
101  *
102  * @param array $allowlist Array of widgets to show before a search is performed, if the setting is enabled.
103  *
104  * @return array
105  */
106  function add_to_allowlist( $allowlist ) {
107 
108  $allowlist[] = 'gravityforms';
109 
110  return $allowlist;
111  }
112 
113  /**
114  * @param array $widget_args
115  * @param string $content
116  * @param string $context
117  */
118  public function render_frontend( $widget_args, $content = '', $context = '') {
119 
120  if ( ! $this->pre_render_frontend() ) {
121  return;
122  }
123 
124  $form_id = \GV\Utils::get( $widget_args, 'form_id' );
125 
126  if ( empty( $form_id ) ) {
127  return;
128  }
129 
130  $title = \GV\Utils::get( $widget_args, 'title' );
131  $description = \GV\Utils::get( $widget_args, 'description' );
132  $field_values = \GV\Utils::get( $widget_args, 'field_values' );
133  $ajax = \GV\Utils::get( $widget_args, 'ajax' );
134 
135  gravity_form( $form_id, ! empty( $title ), ! empty( $description ), false, $field_values, $ajax );
136 
137  // If the form has been submitted, show the confirmation above the form, then show the form again below.
138  if ( isset( GFFormDisplay::$submission[ $form_id ] ) ) {
139 
140  unset( GFFormDisplay::$submission[ $form_id ] );
141 
142  gravity_form( $form_id, ! empty( $description ), ! empty( $title ) );
143  }
144  }
145 
146 }
147 
$forms
Definition: data-source.php:19
pre_render_frontend()
General validations when rendering the widget.
Widget to display a Gravity Forms form.
add_to_allowlist( $allowlist)
Add widget to a list of allowed "Hide Until Searched" items.
if(gravityview() ->plugin->is_GF_25()) $form
If this file is called directly, abort.
gravityview_get_forms( $active=true, $trash=false, $order_by='date_created', $order='ASC')
Alias of GFAPI::get_forms()
render_frontend( $widget_args, $content='', $context='')
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
if(empty( $created_by)) $form_id
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
_get_form_choices()
Returns an array of active forms to show as choices for the widget.
static _POST( $name, $default=null)
Grab a value from the _POST superglobal or default.
$description
Definition: post_image.php:28
$title