GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
class-gv-utils.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  * Generic utilities.
11  */
12 class Utils {
13  /**
14  * Grab a value from the _GET superglobal or default.
15  *
16  * @param string $name The key name (will be prefixed).
17  * @param mixed $default The default value if not found (Default: null)
18  *
19  * @return mixed The value or $default if not found.
20  */
21  public static function _GET( $name, $default = null ) {
22  return self::get( $_GET, $name, $default );
23  }
24 
25  /**
26  * Grab a value from the _POST superglobal or default.
27  *
28  * @param string $name The key name (will be prefixed).
29  * @param mixed $default The default value if not found (Default: null)
30  *
31  * @return mixed The value or $default if not found.
32  */
33  public static function _POST( $name, $default = null ) {
34  return self::get( $_POST, $name, $default );
35  }
36 
37  /**
38  * Grab a value from the _REQUEST superglobal or default.
39  *
40  * @param string $name The key name (will be prefixed).
41  * @param mixed $default The default value if not found (Default: null)
42  *
43  * @return mixed The value or $default if not found.
44  */
45  public static function _REQUEST( $name, $default = null ) {
46  return self::get( $_REQUEST, $name, $default );
47  }
48 
49  /**
50  * Grab a value from the _SERVER superglobal or default.
51  *
52  * @param string $name The key name (will be prefixed).
53  * @param mixed $default The default value if not found (Default: null)
54  *
55  * @return mixed The value or $default if not found.
56  */
57  public static function _SERVER( $name, $default = null ) {
58  return self::get( $_SERVER, $name, $default );
59  }
60 
61  /**
62  * Grab a value from an array or an object or default.
63  *
64  * Supports nested arrays, objects via / key delimiters.
65  *
66  * @param array|object|mixed $array The array (or object). If not array or object, returns $default.
67  * @param string $key The key.
68  * @param mixed $default The default value. Default: null
69  *
70  * @return mixed The value or $default if not found.
71  */
72  public static function get( $array, $key, $default = null ) {
73  if ( ! is_array( $array ) && ! is_object( $array ) ) {
74  return $default;
75  }
76 
77  /**
78  * Try direct key.
79  */
80  if ( is_array( $array ) || $array instanceof \ArrayAccess ) {
81  if ( isset( $array[ $key ] ) ) {
82  return $array[ $key ];
83  }
84  } else if ( is_object( $array ) ) {
85  if ( isset( $array->$key ) ) {
86  return $array->$key;
87  }
88  }
89 
90  /**
91  * Try subkeys after split.
92  */
93  if ( count( $parts = explode( '/', $key, 2 ) ) > 1 ) {
94  return self::get( self::get( $array, $parts[0] ), $parts[1], $default );
95  }
96 
97  return $default;
98  }
99 
100  /**
101  * Sanitizes Excel formulas inside CSV output
102  *
103  * @internal
104  * @since 2.1
105  *
106  * @param string $value The cell value to strip formulas from.
107  *
108  * @return string The sanitized value.
109  */
110  public static function strip_excel_formulas( $value ) {
111 
112  if ( strpos( $value, '=' ) === 0 ) {
113  $value = "'" . $value;
114  }
115 
116  return $value;
117  }
118 
119  /**
120  * Return a value by call.
121  *
122  * Use for quick hook callback returns and whatnot.
123  *
124  * @internal
125  * @since 2.1
126  *
127  * @param mixed $value The value to return from the closure.
128  *
129  * @return Closure The closure with the $value bound.
130  */
131  public static function _return( $value ) {
132  return function() use ( $value ) { return $value; };
133  }
134 
135  /**
136  * Output an associative array represenation of the query parameters.
137  *
138  * @internal
139  * @since 2.1
140  *
141  * @param \GF_Query The query object to dump.
142  *
143  * @return array An associative array of parameters.
144  */
145  public static function gf_query_debug( $query ) {
146  $introspect = $query->_introspect();
147  return array(
148  'where' => $query->_where_unwrap( $introspect['where'] )
149  );
150  }
151 
152  /**
153  * Strips aliases in columns
154  *
155  * @see https://github.com/gravityview/GravityView/issues/1308#issuecomment-617075190
156  *
157  * @internal
158  *
159  * @since 2.8.1
160  *
161  * @param \GF_Query_Condition $condition The condition to strip column aliases from.
162  *
163  * @return \GF_Query_Condition
164  */
165  public static function gf_query_strip_condition_column_aliases( $condition ) {
166  if ( $condition->expressions ) {
167  $conditions = array();
168  foreach ( $condition->expressions as $expression ) {
169  $conditions[] = self::gf_query_strip_condition_column_aliases( $expression );
170  }
171  return call_user_func_array(
172  array( '\GF_Query_Condition', $condition->operator == 'AND' ? '_and' : '_or' ),
173  $conditions
174  );
175  } else {
176  if ( $condition->left instanceof \GF_Query_Column ) {
177  return new \GF_Query_Condition(
178  new \GF_Query_Column( $condition->left->field_id ),
179  $condition->operator,
180  $condition->right
181  );
182  }
183  }
184 
185  return $condition;
186  }
187 }
static _GET( $name, $default=null)
Grab a value from the _GET superglobal or default.
static _SERVER( $name, $default=null)
Grab a value from the _SERVER superglobal or default.
static _REQUEST( $name, $default=null)
Grab a value from the _REQUEST superglobal or default.
static strip_excel_formulas( $value)
Sanitizes Excel formulas inside CSV output.
static _return( $value)
Return a value by call.
static gf_query_strip_condition_column_aliases( $condition)
Strips aliases in columns.
static gf_query_debug( $query)
Output an associative array represenation of the query parameters.
If this file is called directly, abort.
static _POST( $name, $default=null)
Grab a value from the _POST superglobal or default.