Completed
Push — develop ( d80f5d...0bccf3 )
by Zack
32:13 queued 12:16
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 81
	public function __construct() {
16 81
	}
17
18
	/**
19
	 * Check if WordPress is_admin(), and make sure not DOING_AJAX.
20
	 *
21
	 * @return boolean
22
	 */
23 19
	public static function is_admin() {
24 19
		$doing_ajax = defined( 'DOING_AJAX' ) ? DOING_AJAX : false;
25 19
		$load_scripts_styles = preg_match( '#^/wp-admin/load-(scripts|styles).php$#', Utils::_SERVER( 'SCRIPT_NAME' ) );
26
27 19
		return is_admin() && ! ( $doing_ajax || $load_scripts_styles );
28
	}
29
30
	/**
31
	 * This is the frontend.
32
	 *
33
	 * @return boolean True or false.
34
	 */
35
	public static function is_frontend() {
36
		return ! is_admin();
37
	}
38
39
	/**
40
	 * Is this the Add Media / From URL preview request?
41
	 *
42
	 * Will not work in WordPress 4.8+
43
	 *
44
	 * @return boolean
45
	 */
46 2
	public static function is_add_oembed_preview() {
47
		/** The preview request is a parse-embed AJAX call without a type set. */
48 2
		return ( self::is_ajax() && ! empty( $_POST['action'] ) && $_POST['action'] == 'parse-embed' && ! isset( $_POST['type'] ) );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
49
	}
50
51
	/**
52
	 * Is this an AJAX call in progress?
53
	 *
54
	 * @return boolean
55
	 */
56 2
	public static function is_ajax() {
57 2
		return defined( 'DOING_AJAX' ) && DOING_AJAX;
58
	}
59
60
	/**
61
	 * Is this a REST request? Call after parse_request.
62
	 *
63
	 * @return boolean
64
	 */
65
	public static function is_rest() {
66
		return ! empty( $GLOBALS['wp']->query_vars['rest_route'] );
67
	}
68
69
	/**
70
	 * The current $post is a View, no?
71
	 *
72
	 * @api
73
	 * @since 2.0
74
	 * @todo tests
75
	 *
76
	 * @return \GV\View|false The view requested or false
77
	 */
78 68
	public function is_view() {
79 68
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
80 68
		if ( $post && get_post_type( $post ) == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
81 8
			return \GV\View::from_post( $post );
82
		}
83 62
		return false;
84
	}
85
86
	/**
87
	 * Checks whether this is a single entry request
88
	 *
89
	 * @api
90
	 * @since 2.0
91
	 * @todo tests
92
	 *
93
	 * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
94
	 *
95
	 * @return \GV\GF_Entry|false The entry requested or false.
96
	 */
97 18
	public function is_entry( $form_id = 0 ) {
98 18
		$entry = false;
99
100 18
		if ( $id = get_query_var( Entry::get_endpoint_name() ) ) {
101
102 1
			static $entries = array();
103
104 1
			if ( isset( $entries[ "$form_id:$id" ] ) ) {
105 1
				return $entries[ "$form_id:$id" ];
106
			}
107
108 1
			if ( ! $view = $this->is_view() ) {
109
				/**
110
				 * A shortcode probably.
111
				 */
112 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...
113
			}
114
115
			/**
116
			 * A joined request.
117
			 */
118 1
			if ( $view && ( $joins = $view->joins ) ) {
119
				$forms = array_merge( wp_list_pluck( $joins, 'join' ), wp_list_pluck( $joins, 'join_on' ) );
120
				$valid_forms = array_unique( wp_list_pluck( $forms, 'ID' ) );
121
				$needs_forms = array_flip( $valid_forms );
122
123
				$multientry = array();
124
				foreach ( $ids = explode( ',', $id ) as $i => $id ) {
125
					if ( ! $e = GF_Entry::by_id( $id, $forms[ $i ] ) ) {
126
						return false;
127
					}
128
129
					if ( ! in_array( $e['form_id'], $valid_forms ) ) {
130
						return false;
131
					}
132
133
					unset( $needs_forms[ $e['form_id'] ] );
134
135
					array_push( $multientry, $e );
136
				}
137
138
				// Allow Edit Entry to only edit a single entry on a multi-entry
139
				$is_edit_entry = apply_filters( 'gravityview_is_edit_entry', false );
140
141
				/**
142
				 * Not all forms have been requested.
143
				 */
144
				if ( count( $needs_forms ) && ! $is_edit_entry ) {
145
					return false;
146
				}
147
148
				if ( ( count( $multientry ) - 1 ) != count( $joins ) && ! $is_edit_entry ) {
149
					return false;
150
				}
151
152
				if ( $is_edit_entry && 1 === count( $multientry ) ) {
153
					$entry = $multientry[0];
154 1
				} else {
155
					$entry = Multi_Entry::from_entries( array_filter( $multientry ) );
156
				}
157 1
				
158
			}  else {
159
				/**
160 18
				 * A regular one.
161
				 */
162
				$entry = GF_Entry::by_id( $id, $form_id );
163
			}
164
165
			$entries[ "$form_id:$id" ] = $entry;
166
		}
167
168
		return $entry;
169
	}
170
171
	/**
172
	 * Checks whether this an edit entry request.
173
	 *
174 10
	 * @api
175
	 * @since 2.0
176
	 * @todo tests
177
	 *
178
	 * @param int $form_id The form ID, since slugs can be non-unique. Default: 0.
179
	 *
180 10
	 * @return \GV\Entry|false The entry requested or false.
181
	 */
182
	public function is_edit_entry( $form_id = 0 ) {
183 10
		/**
184
		* @filter `gravityview_is_edit_entry` Whether we're currently on the Edit Entry screen \n
185
		* The Edit Entry functionality overrides this value.
186
		* @param boolean $is_edit_entry
187
		*/
188
		if ( ( $entry = $this->is_entry( $form_id ) ) && apply_filters( 'gravityview_is_edit_entry', false ) ) {
189
			return $entry;
190
		}
191
		return false;
192
	}
193
194
	/**
195 12
	 * Checks whether this an entry search request.
196
	 *
197 12
	 * @api
198
	 * @since 2.0
199 12
	 * @todo implementation
200 1
	 *
201
	 * @return boolean True if this is a search request.
202 12
	 */
203
	public function is_search() {
204
205 12
		$search_method = apply_filters( 'gravityview/search/method', 'get' );
206
207 12
		if ( 'post' === $search_method ) {
208
			$get = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
209 12
		} else {
210 1
			$get = $_GET;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
211
		}
212
213 12
		unset( $get['mode'] );
214
215
		$get = array_filter( $get, 'gravityview_is_not_empty_string' );
216
217
		if( $has_field_key = $this->_has_field_key( $get ) ) {
218
			return true;
219
		}
220
221
		return isset( $get['gv_search'] ) || isset( $get['gv_start'] ) || isset( $get['gv_end'] ) || isset( $get['gv_by'] ) || isset( $get['gv_id'] );
222
	}
223
224
	/**
225
	 * Calculate whether the $_REQUEST has a GravityView field
226
	 *
227
	 * @internal
228 11
	 * @todo Roll into the future Search refactor
229
	 *
230 11
	 * @since 2.0.7
231
	 *
232 11
	 * @param array $get $_POST or $_GET array
233
	 *
234 11
	 * @return bool True: GravityView-formatted field detected; False: not detected
235 11
	 */
236 11
	private function _has_field_key( $get ) {
237 11
238
		$has_field_key = false;
239
240
		$fields = \GravityView_Fields::get_all();
241 11
242 1
		$meta = array();
243
		foreach ( $fields as $field ) {
244 1
			if( empty( $field->_gf_field_class_name ) ) {
245
				$meta[] = preg_quote( $field->name );
246
			}
247
		}
248 11
249
		foreach ( $get as $key => $value ) {
250
			if ( preg_match('/^filter_(([0-9_]+)|'. implode( '|', $meta ) .')$/sm', $key ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
251
				$has_field_key = true;
252
				break;
253
			}
254
		}
255
256
		return $has_field_key;
257
	}
258
}
259
260
/** Load implementations. */
261
require gravityview()->plugin->dir( 'future/includes/class-gv-request-frontend.php' );
262
require gravityview()->plugin->dir( 'future/includes/class-gv-request-admin.php' );
263
require gravityview()->plugin->dir( 'future/includes/rest/class-gv-request-rest.php' );
264
require gravityview()->plugin->dir( 'future/includes/class-gv-request-mock.php' );
265