GravityView  2.17
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 use GV\View;
4 
5 /**
6  * Widget to display a Gravity Forms form
7  */
9 
10  public $icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwJSIgaGVpZ2h0PSIxMDAlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MDguMyA1NTkuNSIgZm9jdXNhYmxlPSJmYWxzZSIgYXJpYS1oaWRkZW49InRydWUiIGNsYXNzPSJkYXNoaWNvbiBkYXNoaWNvbi1ncmF2aXR5Zm9ybXMiIHJvbGU9ImltZyI+PGc+PHBhdGggY2xhc3M9InN0MCIgZD0iTTQ2OCwxMDkuOEwyOTQuNCw5LjZjLTIyLjEtMTIuOC01OC40LTEyLjgtODAuNSwwTDQwLjMsMTA5LjhDMTguMiwxMjIuNiwwLDE1NCwwLDE3OS41VjM4MAljMCwyNS42LDE4LjEsNTYuOSw0MC4zLDY5LjdsMTczLjYsMTAwLjJjMjIuMSwxMi44LDU4LjQsMTIuOCw4MC41LDBMNDY4LDQ0OS44YzIyLjItMTIuOCw0MC4zLTQ0LjIsNDAuMy02OS43VjE3OS42CUM1MDguMywxNTQsNDkwLjIsMTIyLjYsNDY4LDEwOS44eiBNMzk5LjMsMjQ0LjRsLTE5NS4xLDBjLTExLDAtMTkuMiwzLjItMjUuNiwxMGMtMTQuMiwxNS4xLTE4LjIsNDQuNC0xOS4zLDYwLjdIMzQ4di0yNi40aDQ5LjkJdjc2LjNIMTExLjNsLTEuOC0yM2MtMC4zLTMuMy01LjktODAuNywzMi44LTEyMS45YzE2LjEtMTcuMSwzNy4xLTI1LjgsNjIuNC0yNS44aDE5NC43VjI0NC40eiI+PC9wYXRoPjwvZz48L3N2Zz4=';
11 
12  /**
13  * Does this get displayed on a single entry?
14  * @var boolean
15  */
16  protected $show_on_single = true;
17 
18  function __construct() {
19  // Initialize widget in the frontend or when editing a View/performing widget AJAX action
20  $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX && 'gv_field_options' === \GV\Utils::_POST( 'action' );
21  $editing_view = 'edit' === \GV\Utils::_GET( 'action' ) && 'gravityview' === get_post_type( \GV\Utils::_GET( 'post' ) );
22  $is_frontend = gravityview()->request->is_frontend();
23 
24  if ( ! $doing_ajax && ! $editing_view && ! $is_frontend ) {
25  return;
26  }
27 
28  $this->widget_description = __('Display a Gravity Forms form.', 'gk-gravityview' );
29 
30  $default_values = array(
31  'header' => 1,
32  'footer' => 1,
33  );
34 
35  $settings = array(
36  'widget_form_id' => array(
37  'type' => 'select',
38  'label' => __( 'Form to display', 'gk-gravityview' ),
39  'value' => '',
40  'options' => $this->_get_form_choices(),
41  ),
42  'title' => array(
43  'type' => 'checkbox',
44  'label' => __( 'Show form title?', 'gk-gravityview' ),
45  'value' => 1,
46  ),
47  'description' => array(
48  'type' => 'checkbox',
49  'label' => __( 'Show form description?', 'gk-gravityview' ),
50  'value' => 1,
51  ),
52  'ajax' => array(
53  'type' => 'checkbox',
54  'label' => __( 'Enable AJAX', 'gk-gravityview' ),
55  'desc' => '',
56  'value' => 1,
57  ),
58  'field_values' => array(
59  'type' => 'text',
60  'class' => 'code widefat',
61  'label' => __( 'Field value parameters', 'gk-gravityview' ),
62  'desc' => '<a href="https://docs.gravityforms.com/using-dynamic-population/" rel="external">' . esc_html__( 'Learn how to dynamically populate a field.', 'gk-gravityview' ) . '</a>',
63  'value' => '',
64  ),
65  );
66 
67  add_filter( 'gravityview/widget/hide_until_searched/allowlist', array( $this, 'add_to_allowlist' ) );
68 
69  parent::__construct( __( 'Gravity Forms', 'gk-gravityview' ) , 'gravityforms', $default_values, $settings );
70  }
71 
72  /**
73  * Returns an array of active forms to show as choices for the widget
74  *
75  * @since 2.9.0.1
76  *
77  * @return array Array with key set to Form ID => Form Title, with `0` as default placeholder.
78  */
79  private function _get_form_choices() {
80 
81  $choices = array(
82  0 => '&mdash; ' . esc_html__( 'list of forms', 'gk-gravityview' ) . '&mdash;',
83  );
84 
85  if ( ! class_exists( 'GFAPI' ) ) {
86  return $choices;
87  }
88 
89  if( gravityview()->request->is_frontend() ) {
90  return $choices;
91  }
92 
93  global $wpdb;
94 
95  $table = GFFormsModel::get_form_table_name();
96 
97  $results = $wpdb->get_results( "SELECT id, title FROM ${table} WHERE is_active = 1" );
98 
99  if ( ! empty( $results ) ) {
100  foreach ( $results as $form ) {
101  $choices[ $form->id ] = $form->title;
102  }
103  }
104 
105  return $choices;
106  }
107 
108  /**
109  * Add widget to a list of allowed "Hide Until Searched" items
110  *
111  * @param array $allowlist Array of widgets to show before a search is performed, if the setting is enabled.
112  *
113  * @return array
114  */
115  function add_to_allowlist( $allowlist ) {
116 
117  $allowlist[] = 'gravityforms';
118 
119  return $allowlist;
120  }
121 
122  /**
123  * @param array $widget_args
124  * @param string $content
125  * @param string $context
126  */
127  public function render_frontend( $widget_args, $content = '', $context = '') {
128 
129  if ( ! $this->pre_render_frontend() ) {
130  return;
131  }
132 
133  $form_id = \GV\Utils::get( $widget_args, 'widget_form_id', \GV\Utils::get( $widget_args, 'form_id' ) );
134 
135  if ( empty( $form_id ) ) {
136  return;
137  }
138 
139  $title = \GV\Utils::get( $widget_args, 'title' );
140  $description = \GV\Utils::get( $widget_args, 'description' );
141  $field_values = \GV\Utils::get( $widget_args, 'field_values' );
142  $ajax = \GV\Utils::get( $widget_args, 'ajax' );
143 
144  gravity_form( $form_id, ! empty( $title ), ! empty( $description ), false, $field_values, $ajax );
145 
146  // If the form has been submitted, show the confirmation above the form, then show the form again below.
147  if ( isset( GFFormDisplay::$submission[ $form_id ] ) ) {
148 
149  unset( GFFormDisplay::$submission[ $form_id ] );
150 
151  gravity_form( $form_id, ! empty( $title ), ! empty( $description ), false, $field_values, $ajax );
152  }
153  }
154 
155 }
156 
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
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.
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.
gravityview()
The main GravityView wrapper function.
_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