Completed
Push — develop ( 08a1ab...5dbd9d )
by Zack
07:41
created

Request::is_view()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 101
	public function is_renderable() {
26
27 101
		$is_renderable = in_array( get_class( $this ), array(
28 101
			'GV\Frontend_Request',
29
			'GV\Mock_Request',
30
			'GV\REST\Request',
31 101
		), 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 101
		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 43
	public static function is_admin() {
48 43
		$doing_ajax = defined( 'DOING_AJAX' ) ? DOING_AJAX : false;
49 43
		$load_scripts_styles = preg_match( '#^/wp-admin/load-(scripts|styles).php$#', Utils::_SERVER( 'SCRIPT_NAME' ) );
50
51 43
		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 3
	public static function is_add_oembed_preview() {
71
		/** The preview request is a parse-embed AJAX call without a type set. */
72 3
		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 3
	public static function is_ajax() {
81 3
		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 90
	public function is_view() {
103 90
		global $post;
104 90
		if ( $post && get_post_type( $post ) == 'gravityview' ) {
105 11
			return \GV\View::from_post( $post );
106
		}
107 81
		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 22
	public function is_entry( $form_id = 0 ) {
122
123 22
		$id = get_query_var( Entry::get_endpoint_name() );
124
125 22
		if ( ! $id ) {
126 21
			return false;
127
		}
128
129 1
		static $entries = array();
130
131 1
		if ( isset( $entries[ "$form_id:$id" ] ) ) {
132 1
			return $entries[ "$form_id:$id" ];
133
		}
134
135 1
		$view = $this->is_view();
136
137
		/**
138
		 * Not CPT, so probably a shortcode
139
		 */
140 1
		if ( ! $view ) {
141 1
			$view = gravityview()->views->get();
0 ignored issues
show
Documentation introduced by
The property views does not exist on object<GV\Core>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
142
		}
143
144
		/**
145
		 * A joined request.
146
		 */
147 1
		if ( $view && ( $joins = $view->joins ) ) {
148
			$forms = array_merge( wp_list_pluck( $joins, 'join' ), wp_list_pluck( $joins, 'join_on' ) );
149
			$valid_forms = array_unique( wp_list_pluck( $forms, 'ID' ) );
150
151
			$multientry = array();
152
			foreach ( $ids = explode( ',', $id ) as $i => $id ) {
153
154
				$valid_form = \GV\Utils::get( $valid_forms, $i, 0 );
155
156
				if ( ! $e = GF_Entry::by_id( $id, $valid_form ) ) {
157
					return false;
158
				}
159
160
				if ( ! in_array( $e['form_id'], $valid_forms ) ) {
161
					return false;
162
				}
163
164
				array_push( $multientry, $e );
165
			}
166
167
			// Allow Edit Entry to only edit a single entry on a multi-entry
168
			$is_edit_entry = apply_filters( 'gravityview_is_edit_entry', false );
169
170
			// Edit entry links are single-entry based
171
			if ( $is_edit_entry && 1 !== count( $multientry ) ) {
172
				return false;
173
			}
174
175
			$entry = Multi_Entry::from_entries( array_filter( $multientry ) );
176
		}  else {
177
			/**
178
			 * A regular one.
179
			 */
180 1
			$entry = GF_Entry::by_id( $id, $form_id );
181
		}
182
183 1
		$entries[ "$form_id:$id" ] = $entry;
184
185 1
		return $entry;
186
	}
187
188
	/**
189
	 * Checks whether this an edit entry request.
190
	 *
191
	 * @api
192
	 * @since 2.0
193
	 * @todo tests
194
	 *
195
	 * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
196
	 *
197
	 * @return \GV\Entry|false The entry requested or false.
198
	 */
199 13
	public function is_edit_entry( $form_id = 0 ) {
200
		/**
201
		* @filter `gravityview_is_edit_entry` Whether we're currently on the Edit Entry screen \n
202
		* The Edit Entry functionality overrides this value.
203
		* @param boolean $is_edit_entry
204
		*/
205 13
		if ( ( $entry = $this->is_entry( $form_id ) ) && apply_filters( 'gravityview_is_edit_entry', false ) ) {
206
			if ( $entry->is_multi() ) {
207
				return array_pop( $entry->entries );
0 ignored issues
show
Bug introduced by
The property entries does not seem to exist in GV\GF_Entry.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
208
			}
209
210
			return $entry;
211
		}
212 13
		return false;
213
	}
214
215
	/**
216
	 * Checks whether this an entry search request.
217
	 *
218
	 * @api
219
	 * @since 2.0
220
	 * @todo implementation
221
	 *
222
	 * @return boolean True if this is a search request.
223
	 */
224 15
	public function is_search() {
225
226 15
		$search_method = apply_filters( 'gravityview/search/method', 'get' );
227
228 15
		if ( 'post' === $search_method ) {
229 1
			$get = $_POST;
230
		} else {
231 15
			$get = $_GET;
232
		}
233
234 15
		unset( $get['mode'] );
235
236 15
		$get = array_filter( $get, 'gravityview_is_not_empty_string' );
237
238 15
		if( $has_field_key = $this->_has_field_key( $get ) ) {
239 1
			return true;
240
		}
241
242 15
		return isset( $get['gv_search'] ) || isset( $get['gv_start'] ) || isset( $get['gv_end'] ) || isset( $get['gv_by'] ) || isset( $get['gv_id'] );
243
	}
244
245
	/**
246
	 * Calculate whether the $_REQUEST has a GravityView field
247
	 *
248
	 * @internal
249
	 * @todo Roll into the future Search refactor
250
	 *
251
	 * @since 2.0.7
252
	 *
253
	 * @param array $get $_POST or $_GET array
254
	 *
255
	 * @return bool True: GravityView-formatted field detected; False: not detected
256
	 */
257 14
	private function _has_field_key( $get ) {
258
259 14
		$has_field_key = false;
260
261 14
		$fields = \GravityView_Fields::get_all();
262
263 14
		$meta = array();
264 14
		foreach ( $fields as $field ) {
265 14
			if( empty( $field->_gf_field_class_name ) ) {
266 14
				$meta[] = preg_quote( $field->name );
267
			}
268
		}
269
270 14
		foreach ( $get as $key => $value ) {
271 3
			if ( preg_match('/^filter_(([0-9_]+)|'. implode( '|', $meta ) .')$/sm', $key ) ) {
272
				$has_field_key = true;
273
				break;
274
			}
275
		}
276
277 14
		return $has_field_key;
278
	}
279
}
280
281
/** Load implementations. */
282
require gravityview()->plugin->dir( 'future/includes/class-gv-request-frontend.php' );
283
require gravityview()->plugin->dir( 'future/includes/class-gv-request-admin.php' );
284
require gravityview()->plugin->dir( 'future/includes/rest/class-gv-request-rest.php' );
285
require gravityview()->plugin->dir( 'future/includes/class-gv-request-mock.php' );
286