GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-request.php
Go to the documentation of this file.
1 <?php
2 namespace GV;
3 
4 /** If this file is called directly, abort. */
5 if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6  die();
7 }
8 
9 /**
10  * The Request abstract class.
11  *
12  * Knows more about the request than anyone else.
13  */
14 abstract class Request {
15 
16  public function __construct() {}
17 
18  /**
19  * Whether this request is something that is renderable.
20  *
21  * @since 2.5.2
22  *
23  * @return bool Yes or no.
24  */
25  public function is_renderable() {
26 
27  $is_renderable = in_array( get_class( $this ), array(
28  'GV\Frontend_Request',
29  'GV\Mock_Request',
30  'GV\REST\Request',
31  ), true );
32 
33  /**
34  * @filter `gravityview/request/is_renderable` Is this request renderable?
35  * @since 2.5.2
36  * @param boolean $is_renderable Huh?
37  * @param \GV\Request $this This.
38  */
39  return apply_filters( 'gravityview/request/is_renderable', $is_renderable, $this );
40  }
41 
42  /**
43  * Check if WordPress is_admin(), and make sure not DOING_AJAX.
44  *
45  * @return boolean
46  */
47  public static function is_admin() {
48  $doing_ajax = defined( 'DOING_AJAX' ) ? DOING_AJAX : false;
49  $load_scripts_styles = preg_match( '#^/wp-admin/load-(scripts|styles).php$#', Utils::_SERVER( 'SCRIPT_NAME' ) );
50 
51  return is_admin() && ! ( $doing_ajax || $load_scripts_styles );
52  }
53 
54  /**
55  * This is the frontend.
56  *
57  * @return boolean True or false.
58  */
59  public static function is_frontend() {
60  return ! is_admin();
61  }
62 
63  /**
64  * Is this the Add Media / From URL preview request?
65  *
66  * Will not work in WordPress 4.8+
67  *
68  * @return boolean
69  */
70  public static function is_add_oembed_preview() {
71  /** The preview request is a parse-embed AJAX call without a type set. */
72  return ( self::is_ajax() && ! empty( $_POST['action'] ) && $_POST['action'] == 'parse-embed' && ! isset( $_POST['type'] ) );
73  }
74 
75  /**
76  * Is this an AJAX call in progress?
77  *
78  * @return boolean
79  */
80  public static function is_ajax() {
81  return defined( 'DOING_AJAX' ) && DOING_AJAX;
82  }
83 
84  /**
85  * Is this a REST request? Call after parse_request.
86  *
87  * @return boolean
88  */
89  public static function is_rest() {
90  return ! empty( $GLOBALS['wp']->query_vars['rest_route'] );
91  }
92 
93  /**
94  * The current $post is a View, no?
95  *
96  * @api
97  * @since 2.0
98  * @since 2.16 Added $return_view parameter.
99  *
100  * @param bool $return_view Whether to return a View object or boolean.
101  *
102  * @return \GV\View|bool If the global $post is a View, returns the View or true, depending on $return_view. If not a View, returns false.
103  */
104  public function is_view( $return_view = true ) {
105  global $post;
106  if ( $post && 'gravityview' === get_post_type( $post ) ) {
107  return ( $return_view ) ? \GV\View::from_post( $post ) : true;
108  }
109  return false;
110  }
111 
112  /**
113  * Checks whether this is a single entry request
114  *
115  * @api
116  * @since 2.0
117  * @todo tests
118  *
119  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
120  *
121  * @return \GV\GF_Entry|false The entry requested or false.
122  */
123  public function is_entry( $form_id = 0 ) {
124  global $wp_query;
125 
126  if ( ! $wp_query ) {
127  return false;
128  }
129 
130  $id = get_query_var( Entry::get_endpoint_name() );
131 
132  if ( ! $id ) {
133  return false;
134  }
135 
136  static $entries = array();
137 
138  if ( isset( $entries[ "$form_id:$id" ] ) ) {
139  return $entries[ "$form_id:$id" ];
140  }
141 
142  $view = $this->is_view();
143 
144  /**
145  * Not CPT, so probably a shortcode
146  */
147  if ( ! $view ) {
148  $view = gravityview()->views->get();
149  }
150 
151  /**
152  * A joined request.
153  */
154  if ( $view && ( $joins = $view->joins ) ) {
155  $forms = array_merge( wp_list_pluck( $joins, 'join' ), wp_list_pluck( $joins, 'join_on' ) );
156  $valid_forms = array_unique( wp_list_pluck( $forms, 'ID' ) );
157 
158  $multientry = array();
159  foreach ( $ids = explode( ',', $id ) as $i => $id ) {
160 
161  $valid_form = \GV\Utils::get( $valid_forms, $i, 0 );
162 
163  if ( ! $e = GF_Entry::by_id( $id, $valid_form ) ) {
164  return false;
165  }
166 
167  if ( ! in_array( $e['form_id'], $valid_forms ) ) {
168  return false;
169  }
170 
171  array_push( $multientry, $e );
172  }
173 
174  // Allow Edit Entry to only edit a single entry on a multi-entry
175  $is_edit_entry = apply_filters( 'gravityview_is_edit_entry', false );
176 
177  // Edit entry links are single-entry based
178  if ( $is_edit_entry && 1 !== count( $multientry ) ) {
179  return false;
180  }
181 
182  $entry = Multi_Entry::from_entries( array_filter( $multientry ) );
183  } else {
184  /**
185  * A regular one.
186  */
187  $entry = GF_Entry::by_id( $id, $form_id );
188  }
189 
190  $entries[ "$form_id:$id" ] = $entry;
191 
192  return $entry;
193  }
194 
195  /**
196  * Checks whether this an edit entry request.
197  *
198  * @api
199  * @since 2.0
200  * @todo tests
201  *
202  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
203  *
204  * @return \GV\Entry|false The entry requested or false.
205  */
206  public function is_edit_entry( $form_id = 0 ) {
207  /**
208  * @filter `gravityview_is_edit_entry` Whether we're currently on the Edit Entry screen \n
209  * The Edit Entry functionality overrides this value.
210  * @param boolean $is_edit_entry
211  */
212  if ( ( $entry = $this->is_entry( $form_id ) ) && apply_filters( 'gravityview_is_edit_entry', false ) ) {
213  if ( $entry->is_multi() ) {
214  return array_pop( $entry->entries );
215  }
216 
217  return $entry;
218  }
219  return false;
220  }
221 
222  /**
223  * Checks whether this an entry search request.
224  *
225  * @api
226  * @since 2.0
227  * @todo implementation
228  *
229  * @return boolean True if this is a search request.
230  */
231  public function is_search() {
232 
233  $search_method = apply_filters( 'gravityview/search/method', 'get' );
234 
235  if ( 'post' === $search_method ) {
236  $get = $_POST;
237  } else {
238  $get = $_GET;
239  }
240 
241  unset( $get['mode'] );
242 
243  $get = array_filter( $get, 'gravityview_is_not_empty_string' );
244 
245  if( $this->_has_field_key( $get ) ) {
246  return true;
247  }
248 
249  return isset( $get['gv_search'] ) || isset( $get['gv_start'] ) || isset( $get['gv_end'] ) || isset( $get['gv_by'] ) || isset( $get['gv_id'] );
250  }
251 
252  /**
253  * Calculate whether the $_REQUEST has a GravityView field
254  *
255  * @internal
256  * @todo Roll into the future Search refactor
257  *
258  * @since 2.0.7
259  *
260  * @param array $get $_POST or $_GET array
261  *
262  * @return bool True: GravityView-formatted field detected; False: not detected
263  */
264  private function _has_field_key( $get ) {
265 
266  $has_field_key = false;
267 
268  $fields = \GravityView_Fields::get_all();
269 
270  $meta = array();
271  foreach ( $fields as $field ) {
272  if( empty( $field->_gf_field_class_name ) ) {
273  $meta[] = preg_quote( $field->name );
274  }
275  }
276 
277  foreach ( $get as $key => $value ) {
278  if ( preg_match('/^(filter|input)_(([0-9_]+)|'. implode( '|', $meta ) .')$/sm', $key ) ) {
279  $has_field_key = true;
280  break;
281  }
282  }
283 
284  return $has_field_key;
285  }
286 }
287 
288 /** Load implementations. */
289 require gravityview()->plugin->dir( 'future/includes/class-gv-request-frontend.php' );
290 require gravityview()->plugin->dir( 'future/includes/class-gv-request-admin.php' );
291 require gravityview()->plugin->dir( 'future/includes/rest/class-gv-request-rest.php' );
292 require gravityview()->plugin->dir( 'future/includes/class-gv-request-mock.php' );
If this file is called directly, abort.
$forms
Definition: data-source.php:19
static is_rest()
Is this a REST request? Call after parse_request.
is_search()
Checks whether this an entry search request.
static is_add_oembed_preview()
Is this the Add Media / From URL preview request?
$entries
$search_method
static is_admin()
Check if WordPress is_admin(), and make sure not DOING_AJAX.
global $post
Definition: delete-entry.php:7
is_entry( $form_id=0)
Checks whether this is a single entry request.
is_renderable()
Whether this request is something that is renderable.
static is_ajax()
Is this an AJAX call in progress?
if(empty( $created_by)) $form_id
is_view( $return_view=true)
The current $post is a View, no?
_has_field_key( $get)
Calculate whether the $_REQUEST has a GravityView field.
static is_frontend()
This is the frontend.
static get( $array, $key, $default=null)
Grab a value from an array or an object or default.
gravityview()
The main GravityView wrapper function.
static get_all( $groups='')
Get all fields.
$entry
Definition: notes.php:27
is_edit_entry( $form_id=0)
Checks whether this an edit entry request.
static from_post( $post)
Construct a instance from a .