This repository has been archived by the owner on May 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
UrlToQuery.php
114 lines (99 loc) · 2.8 KB
/
UrlToQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* From Url To Query
* https://github.com/Giuseppe-Mazzapica/Url_To_Query
*/
namespace NodeifyWP;
class UrlToQuery {
/**
* Rewrite rules array
* @var array
*/
private $rewrite;
/**
* $wp_rewrite->use_verbose_page_rules
* @var bool
*/
private $use_verbose;
/**
* Global WP object
* @var \WP
*/
private $wp;
/**
* Extra variables to be added to any query resolved
* @var array
*/
private $extra_query_vars = [ ];
/**
* Array of the instantiated \NodeifyWP\UrlToQueryItem objects
* @var array
*/
private $items = [ ];
/**
* Constructor
* @param array $extra_query_vars Extra variables to be added to any query resolved
*/
function __construct( $extra_query_vars = [ ] ) {
if ( is_array( $extra_query_vars ) ) {
$this->extra_query_vars = $extra_query_vars;
} elseif ( is_string( $extra_query_vars ) && ! empty( $extra_query_vars ) ) {
parse_str( $extra_query_vars, $this->extra_query_vars );
}
$this->rewrite = $GLOBALS['wp_rewrite']->wp_rewrite_rules();
$this->wp = $GLOBALS['wp'];
$this->use_verbose = (bool) $GLOBALS['wp_rewrite']->use_verbose_page_rules;
}
/**
* Resolve an url to an array of WP_Query arguments for main query
*
* @param string $url Url to resolve
* @param type $query_string_vars Query variables to be added to the url
* @return array|\WP_Error Resolved query or WP_Error is something goes wrong
*/
function resolve( $url = '', $query_string_vars = [ ] ) {
$url = filter_var( $url, FILTER_SANITIZE_URL );
if ( ! empty( $url ) ) {
$id = md5( $url . serialize( $query_string_vars ) );
if ( ! isset( $this->items[$id] ) || ! $this->items[$id] instanceof UrlToQueryItem ) {
$this->items[$id] = new UrlToQueryItem( $this );
}
$result = $this->items[$id]->resolve( $url, $query_string_vars );
} else {
$result = new \WP_Error( 'url-to-query-bad-url' );
}
return $result;
}
/**
* Get the rewrite rules array
*
* @return array
*/
function getRewrite() {
return $this->rewrite;
}
/**
* Get the global WP object
*
* @return \WP
*/
function getWp() {
return $this->wp;
}
/**
* Get the array of extra variables to be added to resolved queries
*
* @return array
*/
function getExtraQueryVars() {
return $this->extra_query_vars;
}
/**
* Is true when $wp_rewrite->use_verbose_page_rules is true
*
* @return bool
*/
function isVerbose() {
return (bool) $this->use_verbose;
}
}