PluginProbe
Plugin Check (PCP) / trunk
Plugin Check (PCP) vtrunk
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 / Checker / Runtime_Environment_Setup.php

Runtime_Environment_Setup.php in Plugin Check (PCP) trunk, at includes/Checker/Runtime_Environment_Setup.php

302 lines 9.8 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\Checker\Runtime_Environment_Setup
4 *
5 * @package plugin-check
6 */
7
8 namespace WordPress\Plugin_Check\Checker;
9
10 use WordPress\Plugin_Check\Traits\Amend_DB_Base_Prefix;
11
12 /**
13 * Class to setup the Runtime Environment for Runtime checks.
14 *
15 * @since 1.0.0
16 */
17 final class Runtime_Environment_Setup {
18 use Amend_DB_Base_Prefix;
19
20 /**
21 * Sets up the WordPress environment for runtime checks
22 *
23 * @since 1.0.0
24 *
25 * @global wpdb $wpdb WordPress database abstraction object.
26 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
27 */
28 public function set_up() {
29 global $wpdb, $wp_filesystem;
30
31 require_once ABSPATH . '/wp-admin/includes/upgrade.php';
32
33 // Get the existing site URL.
34 $site_url = get_option( 'siteurl' );
35
36 // Get the existing active plugins.
37 $active_plugins = get_option( 'active_plugins' );
38
39 // Get the existing active theme.
40 $active_theme = get_option( 'stylesheet' );
41
42 // Get the existing permalink structure.
43 $permalink_structure = get_option( 'permalink_structure' );
44
45 // Set the new prefix.
46 $prefix_cleanup = $this->amend_db_base_prefix();
47
48 // Create and populate the test database tables if they do not exist.
49 if ( $wpdb->posts !== $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->posts ) ) ) {
50 /*
51 * Set the same permalink structure *before* install finishes,
52 * so that wp_install_maybe_enable_pretty_permalinks() does not flush rewrite rules.
53 *
54 * See https://github.com/WordPress/plugin-check/issues/330
55 */
56 add_action(
57 'populate_options',
58 static function () use ( $permalink_structure ) {
59 /*
60 * If pretty permalinks are not used, temporarily enable them by setting a permalink structure, to
61 * avoid flushing rewrite rules in wp_install_maybe_enable_pretty_permalinks().
62 * Afterwards, on the 'wp_install' action, set the original (empty) permalink structure.
63 */
64 if ( ! $permalink_structure ) {
65 add_action(
66 'wp_install',
67 static function () use ( $permalink_structure ) {
68 update_option( 'permalink_structure', $permalink_structure );
69 }
70 );
71 $permalink_structure = '/%postname%/';
72 }
73 add_option( 'permalink_structure', $permalink_structure );
74 }
75 );
76
77 $this->install_wordpress( $site_url, $active_theme, $active_plugins );
78 }
79
80 // Restore the old prefix.
81 $prefix_cleanup();
82
83 // Return early if the plugin check object cache already exists.
84 if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
85 return;
86 }
87
88 // Create the object-cache.php file.
89 if ( $wp_filesystem || WP_Filesystem() ) {
90 // Do not replace the object-cache.php file if it already exists.
91 if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
92 $wp_filesystem->copy( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php', WP_CONTENT_DIR . '/object-cache.php' );
93 }
94 }
95 }
96
97 /**
98 * Cleans up the runtime environment setup.
99 *
100 * @since 1.0.0
101 *
102 * @global wpdb $wpdb WordPress database abstraction object.
103 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
104 */
105 public function clean_up() {
106 global $wpdb, $wp_filesystem;
107
108 require_once ABSPATH . '/wp-admin/includes/upgrade.php';
109
110 $prefix_cleanup = $this->amend_db_base_prefix();
111 $tables = $wpdb->tables();
112
113 $tables = $this->ignore_custom_tables( $tables );
114
115 foreach ( $tables as $table ) {
116 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
117 }
118
119 // Restore the old prefix.
120 $prefix_cleanup();
121
122 // Return early if the plugin check object cache does not exist.
123 if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
124 return;
125 }
126
127 // Remove the object-cache.php file.
128 if ( $wp_filesystem || WP_Filesystem() ) {
129 if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
130 return;
131 }
132
133 // Check the drop-in file matches the copy.
134 $original_content = $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' );
135 $copy_content = $wp_filesystem->get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php' );
136
137 if ( $original_content && $original_content === $copy_content ) {
138 $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
139 }
140 }
141 }
142
143 /**
144 * Excludes the custom user and user meta tables from the list of tables to be deleted.
145 *
146 * @since 1.7.0
147 *
148 * @param array $tables List of WordPress database tables.
149 * @return array List of WordPress database tables to delete.
150 */
151 private function ignore_custom_tables( array $tables ): array {
152 // Do not remove custom tables (which by definition weren't duplicated because we cannot override constants).
153 if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) && CUSTOM_USER_TABLE === $tables['users'] ) {
154 unset( $tables['users'] );
155 }
156 if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) && CUSTOM_USER_META_TABLE === $tables['usermeta'] ) {
157 unset( $tables['usermeta'] );
158 }
159 return $tables;
160 }
161
162 /**
163 * Tests if the runtime environment is currently set up.
164 *
165 * This returns true when the plugin's object-cache.php drop-in is active in the current request and/or when the
166 * custom runtime environment database tables are present.
167 *
168 * @since 1.3.0
169 *
170 * @global wpdb $wpdb WordPress database abstraction object.
171 *
172 * @return bool True if the runtime environment is set up, false if not.
173 */
174 public function is_set_up() {
175 global $wpdb;
176
177 if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) ) {
178 return true;
179 }
180
181 // Set the custom prefix to check for the runtime environment tables.
182 $prefix_cleanup = $this->amend_db_base_prefix();
183
184 $tables_present = $wpdb->posts === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->posts ) );
185
186 // Restore the old prefix.
187 $prefix_cleanup();
188
189 return $tables_present;
190 }
191
192 /**
193 * Checks if the WordPress Environment can be set up for runtime checks.
194 *
195 * @since 1.0.0
196 *
197 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
198 *
199 * @return bool Returns true if the runtime environment can be set up, false if not.
200 */
201 public function can_set_up() {
202 global $wp_filesystem;
203
204 if ( defined( 'CUSTOM_USER_TABLE' ) || defined( 'CUSTOM_USER_META_TABLE' ) ) {
205 // When these constants are defined, we cannot duplicate the user tables for testing.
206 return false;
207 }
208
209 require_once ABSPATH . '/wp-admin/includes/upgrade.php';
210
211 if ( ! is_object( $wp_filesystem ) && ! WP_Filesystem() ) {
212 return false;
213 }
214
215 // Check if the object-cache.php file exists.
216 if ( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
217 // Check If the object-cache.php file is the Plugin Check version.
218 if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
219 return true;
220 }
221 } else {
222 // Get the correct Plugin Check directory when run too early.
223 if ( ! defined( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' ) ) {
224 $object_cache_copy = dirname( __DIR__, 2 ) . '/plugin-check/drop-ins/object-cache.copy.php';
225 } else {
226 $object_cache_copy = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php';
227 }
228
229 // If the file does not exist, check if we can place it.
230 $wp_filesystem->copy( $object_cache_copy, WP_CONTENT_DIR . '/object-cache.php' );
231
232 /**
233 * PHPStan ignore reason: PHPStan raised an issue because we have redundant file existence checks in our code.
234 * We perform this double check because we want to ensure that we can write the file we're testing.
235 *
236 * @phpstan-ignore-next-line
237 */
238 if ( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
239 // Remove the file before returning.
240 $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
241
242 return true;
243 }
244 }
245
246 return false;
247 }
248
249 /**
250 * Installs WordPress, while providing tweaks to allow for early execution of the install process.
251 *
252 * @since 1.3.0
253 *
254 * @param string $active_siteurl The actual site's site URL.
255 * @param string $active_theme The actual site's theme slug.
256 * @param string[] $active_plugins The actual site's list of plugin basenames.
257 */
258 private function install_wordpress( string $active_siteurl, string $active_theme, array $active_plugins ): void {
259 if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
260 $site_url = $active_siteurl;
261 $_SERVER['HTTP_HOST'] = preg_replace( '#^https?://#', '', rtrim( $site_url, '/' ) );
262 }
263
264 // Do not send post-install notification email, see https://github.com/WordPress/plugin-check/issues/424.
265 add_filter( 'pre_wp_mail', '__return_false' );
266
267 // The `wp_install()` function requires the WP_DEFAULT_THEME constant to be set.
268 if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
269 define( 'WP_DEFAULT_THEME', $active_theme );
270 }
271
272 // The `wp_install()` function requires some pluggable functions like `get_user_by()` to be loaded.
273 if ( ! function_exists( 'get_user_by' ) ) {
274 require_once ABSPATH . '/wp-includes/pluggable.php';
275 }
276
277 /*
278 * Cookie constants need to be set before installation, which normally happens immediately after
279 * 'muplugins_loaded', which is when the logic here typically runs. It is therefore safe to call these
280 * functions here already.
281 */
282 if ( doing_action( 'muplugins_loaded' ) || ! did_action( 'muplugins_loaded' ) ) {
283 if ( is_multisite() ) {
284 ms_cookie_constants();
285 }
286 wp_cookie_constants();
287 }
288
289 wp_install(
290 'Plugin Check',
291 'plugincheck',
292 'demo@plugincheck.test',
293 false
294 );
295
296 remove_filter( 'pre_wp_mail', '__return_false' );
297
298 // Activate the same plugins in the test environment.
299 update_option( 'active_plugins', $active_plugins );
300 }
301 }
302