GravityView  2.10.1
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[in,out] 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  * @todo tests
99  *
100  * @return \GV\View|false The view requested or false
101  */
102  public function is_view() {
103  global $post;
104  if ( $post && get_post_type( $post ) == 'gravityview' ) {
105  return \GV\View::from_post( $post );
106  }
107  return false;
108  }
109 
110  /**
111  * Checks whether this is a single entry request
112  *
113  * @api
114  * @since 2.0
115  * @todo tests
116  *
117  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
118  *
119  * @return \GV\GF_Entry|false The entry requested or false.
120  */
121  public function is_entry( $form_id = 0 ) {
122  global $wp_query;
123 
124  if ( ! $wp_query ) {
125  return false;
126  }
127 
128  $id = get_query_var( Entry::get_endpoint_name() );
129 
130  if ( ! $id ) {
131  return false;
132  }
133 
134  static $entries = array();
135 
136  if ( isset( $entries[ "$form_id:$id" ] ) ) {
137  return $entries[ "$form_id:$id" ];
138  }
139 
140  $view = $this->is_view();
141 
142  /**
143  * Not CPT, so probably a shortcode
144  */
145  if ( ! $view ) {
146  $view = gravityview()->views->get();
147  }
148 
149  /**
150  * A joined request.
151  */
152  if ( $view && ( $joins = $view->joins ) ) {
153  $forms = array_merge( wp_list_pluck( $joins, 'join' ), wp_list_pluck( $joins, 'join_on' ) );
154  $valid_forms = array_unique( wp_list_pluck( $forms, 'ID' ) );
155 
156  $multientry = array();
157  foreach ( $ids = explode( ',', $id ) as $i => $id ) {
158 
159  $valid_form = \GV\Utils::get( $valid_forms, $i, 0 );
160 
161  if ( ! $e = GF_Entry::by_id( $id, $valid_form ) ) {
162  return false;
163  }
164 
165  if ( ! in_array( $e['form_id'], $valid_forms ) ) {
166  return false;
167  }
168 
169  array_push( $multientry, $e );
170  }
171 
172  // Allow Edit Entry to only edit a single entry on a multi-entry
173  $is_edit_entry = apply_filters( 'gravityview_is_edit_entry', false );
174 
175  // Edit entry links are single-entry based
176  if ( $is_edit_entry && 1 !== count( $multientry ) ) {
177  return false;
178  }
179 
180  $entry = Multi_Entry::from_entries( array_filter( $multientry ) );
181  } else {
182  /**
183  * A regular one.
184  */
185  $entry = GF_Entry::by_id( $id, $form_id );
186  }
187 
188  $entries[ "$form_id:$id" ] = $entry;
189 
190  return $entry;
191  }
192 
193  /**
194  * Checks whether this an edit entry request.
195  *
196  * @api
197  * @since 2.0
198  * @todo tests
199  *
200  * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
201  *
202  * @return \GV\Entry|false The entry requested or false.
203  */
204  public function is_edit_entry( $form_id = 0 ) {
205  /**
206  * @filter `gravityview_is_edit_entry` Whether we're currently on the Edit Entry screen \n
207  * The Edit Entry functionality overrides this value.
208  * @param boolean $is_edit_entry
209  */
210  if ( ( $entry = $this->is_entry( $form_id ) ) && apply_filters( 'gravityview_is_edit_entry', false ) ) {
211  if ( $entry->is_multi() ) {
212  return array_pop( $entry->entries );
213  }
214 
215  return $entry;
216  }
217  return false;
218  }
219 
220  /**
221  * Checks whether this an entry search request.
222  *
223  * @api
224  * @since 2.0
225  * @todo implementation
226  *
227  * @return boolean True if this is a search request.
228  */
229  public function is_search() {
230 
231  $search_method = apply_filters( 'gravityview/search/method', 'get' );
232 
233  if ( 'post' === $search_method ) {
234  $get = $_POST;
235  } else {
236  $get = $_GET;
237  }
238 
239  unset( $get['mode'] );
240 
241  $get = array_filter( $get, 'gravityview_is_not_empty_string' );
242 
243  if( $has_field_key = $this->_has_field_key( $get ) ) {
244  return true;
245  }
246 
247  return isset( $get['gv_search'] ) || isset( $get['gv_start'] ) || isset( $get['gv_end'] ) || isset( $get['gv_by'] ) || isset( $get['gv_id'] );
248  }
249 
250  /**
251  * Calculate whether the $_REQUEST has a GravityView field
252  *
253  * @internal
254  * @todo Roll into the future Search refactor
255  *
256  * @since 2.0.7
257  *
258  * @param array $get $_POST or $_GET array
259  *
260  * @return bool True: GravityView-formatted field detected; False: not detected
261  */
262  private function _has_field_key( $get ) {
263 
264  $has_field_key = false;
265 
266  $fields = \GravityView_Fields::get_all();
267 
268  $meta = array();
269  foreach ( $fields as $field ) {
270  if( empty( $field->_gf_field_class_name ) ) {
271  $meta[] = preg_quote( $field->name );
272  }
273  }
274 
275  foreach ( $get as $key => $value ) {
276  if ( preg_match('/^filter_(([0-9_]+)|'. implode( '|', $meta ) .')$/sm', $key ) ) {
277  $has_field_key = true;
278  break;
279  }
280  }
281 
282  return $has_field_key;
283  }
284 }
285 
286 /** Load implementations. */
287 require gravityview()->plugin->dir( 'future/includes/class-gv-request-frontend.php' );
288 require gravityview()->plugin->dir( 'future/includes/class-gv-request-admin.php' );
289 require gravityview()->plugin->dir( 'future/includes/rest/class-gv-request-rest.php' );
290 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.
gravityview()
Definition: _stubs.php:26
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
_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.
is_view()
The current $post is a View, no?
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.
$field
Definition: gquiz_grade.php:11