PluginProbe
Plugin Check (PCP) / 1.5.0
Plugin Check (PCP) v1.5.0
2.1.0 trunk 0.1 0.2.0 0.2.1 0.2.2 0.2.3 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.3.0 1.3.1 1.4.0 1.5.0 1.6.0 1.7.0 1.8.0 1.9.0 2.0.0 ci-artifacts
plugin-check / includes / Traits / URL_Aware.php

URL_Aware.php in Plugin Check (PCP) 1.5.0, at includes/Traits/URL_Aware.php

207 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class WordPress\Plugin_Check\Traits\URL_Aware
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Traits;
9
10 /**
11 * URL Aware trait.
12 *
13 * @since 1.0.0
14 */
15 trait URL_Aware {
16
17 /**
18 * List of relevant global query variables to modify.
19 *
20 * @since 1.0.0
21 * @var array
22 */
23 private $query_globals = array(
24 'query_string',
25 'id',
26 'postdata',
27 'authordata',
28 'day',
29 'currentmonth',
30 'page',
31 'pages',
32 'multipage',
33 'more',
34 'numpages',
35 'pagenow',
36 'current_screen',
37 );
38
39 /**
40 * List of relevant WP global variables to modify.
41 *
42 * @since 1.0.0
43 * @var array
44 */
45 private $wp_globals = array(
46 'wp_query',
47 'wp_the_query',
48 'wp',
49 );
50
51 /**
52 * Array of original values for the global state.
53 *
54 * @since 1.0.0
55 * @var array
56 */
57 private $global_values = array(
58 'get' => array(),
59 'post' => array(),
60 'server' => array(),
61 'global_vars' => array(),
62 );
63
64 /**
65 * Backups the original values for any global state that may be modified to be restored later.
66 *
67 * @since 1.0.0
68 */
69 protected function backup_globals() {
70 $this->global_values = array(
71 'get' => $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
72 'post' => $_POST, // phpcs:ignore WordPress.Security.NonceVerification.Missing
73 'server' => $_SERVER,
74 );
75
76 $global_vars = array();
77 $global_keys = array_merge( $this->query_globals, $this->wp_globals );
78
79 foreach ( $global_keys as $query_global ) {
80 if ( isset( $GLOBALS[ $query_global ] ) ) {
81 $global_vars[ $query_global ] = $GLOBALS[ $query_global ];
82 }
83 }
84
85 $this->global_values['global_vars'] = $global_vars;
86 }
87
88 /**
89 * Restores the original values for any global state that may have been modified.
90 *
91 * @since 1.0.0
92 */
93 protected function restore_globals() {
94 $_GET = $this->global_values['get'];
95 $_POST = $this->global_values['post'];
96 $_SERVER = $this->global_values['server'];
97
98 $global_vars = $this->global_values['global_vars'];
99 $global_keys = array_merge( $this->query_globals, $this->wp_globals );
100
101 foreach ( $global_keys as $query_global ) {
102 if ( isset( $global_vars[ $query_global ] ) ) {
103 $GLOBALS[ $query_global ] = $global_vars[ $query_global ];
104 } else {
105 unset( $GLOBALS[ $query_global ] );
106 }
107 }
108 }
109
110 /**
111 * Sets the global state to as if a given URL has been requested.
112 *
113 * @since 1.0.0
114 *
115 * @param string $url URL to simulate request for.
116 *
117 * @SuppressWarnings(PHPMD.NPathComplexity)
118 */
119 protected function go_to( $url ) {
120 /*
121 * Note: the WP and WP_Query classes like to silently fetch parameters
122 * from all over the place (globals, GET, etc), which makes it tricky
123 * to run them more than once without very carefully clearing everything.
124 */
125 $_GET = array();
126 $_POST = array();
127
128 foreach ( $this->query_globals as $v ) {
129 if ( isset( $GLOBALS[ $v ] ) ) {
130 unset( $GLOBALS[ $v ] );
131 }
132 }
133
134 $parts = wp_parse_url( $url );
135 if ( isset( $parts['scheme'] ) ) {
136 $req = isset( $parts['path'] ) ? $parts['path'] : '';
137 if ( isset( $parts['query'] ) ) {
138 $req .= '?' . $parts['query'];
139 // Parse the URL query vars into $_GET.
140 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
141 parse_str( $parts['query'], $_GET );
142 }
143 } else {
144 $req = $url;
145 }
146
147 if ( ! isset( $parts['query'] ) ) {
148 $parts['query'] = '';
149 }
150
151 $_SERVER['REQUEST_URI'] = $req;
152 unset( $_SERVER['PATH_INFO'] );
153
154 unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] );
155 $GLOBALS['wp_the_query'] = new \WP_Query();
156 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
157
158 $public_query_vars = $GLOBALS['wp']->public_query_vars;
159 $private_query_vars = $GLOBALS['wp']->private_query_vars;
160
161 $GLOBALS['wp'] = new \WP();
162 $GLOBALS['wp']->public_query_vars = $public_query_vars;
163 $GLOBALS['wp']->private_query_vars = $private_query_vars;
164
165 // Clean up query vars.
166 foreach ( $GLOBALS['wp']->public_query_vars as $v ) {
167 unset( $GLOBALS[ $v ] );
168 }
169
170 foreach ( $GLOBALS['wp']->private_query_vars as $v ) {
171 unset( $GLOBALS[ $v ] );
172 }
173
174 // Set up query vars for taxonomies and post types.
175 foreach ( get_taxonomies( array(), 'objects' ) as $t ) {
176 if ( $t->publicly_queryable && ! empty( $t->query_var ) ) {
177 $GLOBALS['wp']->add_query_var( $t->query_var );
178 }
179 }
180
181 foreach ( get_post_types( array(), 'objects' ) as $t ) {
182 if ( is_post_type_viewable( $t ) && ! empty( $t->query_var ) ) {
183 $GLOBALS['wp']->add_query_var( $t->query_var );
184 }
185 }
186
187 $GLOBALS['wp']->main( $parts['query'] );
188 }
189
190 /**
191 * Simulate all the urls like WP and run the cleanup function.
192 *
193 * @since 1.0.0
194 *
195 * @param array $urls An array of URLs to run.
196 * @param callable $callback Callback function to run for each URL.
197 */
198 protected function run_for_urls( $urls, $callback ) {
199 if ( ! empty( $urls ) ) {
200 foreach ( $urls as $url ) {
201 $this->go_to( $url );
202 $callback( $url );
203 }
204 }
205 }
206 }
207