Completed
Push — develop ( 0586aa...5d54b4 )
by Zack
15:10
created

Request   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 72.72%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 188
ccs 40
cts 55
cp 0.7272
rs 9
c 2
b 1
f 0
wmc 35
lcom 0
cbo 4

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A is_admin() 0 6 4
A is_frontend() 0 3 1
A is_add_oembed_preview() 0 4 4
A is_ajax() 0 3 2
A is_rest() 0 3 1
A is_view() 0 7 3
A is_entry() 0 20 4
A is_edit_entry() 0 11 3
B is_search() 0 20 7
B _has_field_key() 0 22 5
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 66
	public function __construct() {
16 66
	}
17
18
	/**
19
	 * Check if WordPress is_admin(), and make sure not DOING_AJAX.
20
	 *
21
	 * @return boolean
22
	 */
23 16
	public static function is_admin() {
24 16
		$doing_ajax = defined( 'DOING_AJAX' ) ? DOING_AJAX : false;
25 16
		$load_scripts_styles = preg_match( '#^/wp-admin/load-(scripts|styles).php$#', Utils::_SERVER( 'SCRIPT_NAME' ) );
26
27 16
		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 58
	public function is_view() {
79 58
		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 58
		if ( $post && get_post_type( $post ) == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
81 7
			return \GV\View::from_post( $post );
82
		}
83 53
		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
	 * @return \GV\GF_Entry|false The entry requested or false.
94
	 */
95 12
	public function is_entry() {
96
97 12
		if ( $id = get_query_var( \GV\Entry::get_endpoint_name() ) ) {
98
99
			static $entries = array();
100
101
			if ( isset( $entries[ $id ] ) ) {
102
				return $entries[ $id ];
103
			}
104
105
			if ( $entry = \GV\GF_Entry::by_id( $id ) ) {
106
				$entries[ $id ] = $entry;
107
				return $entry;
108
			}
109
110
			$entries[ $id ] = false;
111
		}
112
113 12
		return false;
114
	}
115
116
	/**
117
	 * Checks whether this an edit entry request.
118
	 *
119
	 * @api
120
	 * @since 2.0
121
	 * @todo tests
122
	 *
123
	 * @return \GV\Entry|false The entry requested or false.
124
	 */
125 7
	public function is_edit_entry() {
126
		/**
127
		* @filter `gravityview_is_edit_entry` Whether we're currently on the Edit Entry screen \n
128
		* The Edit Entry functionality overrides this value.
129
		* @param boolean $is_edit_entry
130
		*/
131 7
		if ( ( $entry = $this->is_entry() ) && apply_filters( 'gravityview_is_edit_entry', false ) ) {
132
			return $entry;
133
		}
134 7
		return false;
135
	}
136
137
	/**
138
	 * Checks whether this an entry search request.
139
	 *
140
	 * @api
141
	 * @since 2.0
142
	 * @todo implementation
143
	 *
144
	 * @return boolean True if this is a search request.
145
	 */
146 8
	public function is_search() {
147
148 8
		$search_method = apply_filters( 'gravityview/search/method', 'get' );
149
150 8
		if ( 'post' === $search_method ) {
151 1
			$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...
152
		} else {
153 8
			$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...
154
		}
155
156 8
		unset( $get['mode'] );
157
158 8
		$get = array_filter( $get, 'gravityview_is_not_empty_string' );
159
160 8
		if( $has_field_key = $this->_has_field_key( $get ) ) {
161 1
			return true;
162
		}
163
164 8
		return isset( $get['gv_search'] ) || isset( $get['gv_start'] ) || isset( $get['gv_end'] ) || isset( $get['gv_by'] ) || isset( $get['gv_id'] );
165
	}
166
167
	/**
168
	 * Calculate whether the $_REQUEST has a GravityView field
169
	 *
170
	 * @internal
171
	 * @todo Roll into the future Search refactor
172
	 *
173
	 * @since 2.0.7
174
	 *
175
	 * @param array $get $_POST or $_GET array
176
	 *
177
	 * @return bool True: GravityView-formatted field detected; False: not detected
178
	 */
179 7
	private function _has_field_key( $get ) {
180
181 7
		$has_field_key = false;
182
183 7
		$fields = \GravityView_Fields::get_all();
184
185 7
		$meta = array();
186 7
		foreach ( $fields as $field ) {
187 7
			if( empty( $field->_gf_field_class_name ) ) {
188 7
				$meta[] = preg_quote( $field->name );
189
			}
190
		}
191
192 7
		foreach ( $get as $key => $value ) {
193
			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...
194
				$has_field_key = true;
195
				break;
196
			}
197
		}
198
199 7
		return $has_field_key;
200
	}
201
}
202
203
/** Load implementations. */
204
require gravityview()->plugin->dir( 'future/includes/class-gv-request-frontend.php' );
205
require gravityview()->plugin->dir( 'future/includes/class-gv-request-admin.php' );
206
require gravityview()->plugin->dir( 'future/includes/rest/class-gv-request-rest.php' );
207
require gravityview()->plugin->dir( 'future/includes/class-gv-request-mock.php' );
208