Completed
Push — master ( 18187e...a1257b )
by Zack
19:22 queued 17:03
created

Request::is_add_oembed_preview()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

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