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 / Checker / Runtime_Environment_Setup.php

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

276 lines 8.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 foreach ( $tables as $table ) {
114 $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
115 }
116
117 // Restore the old prefix.
118 $prefix_cleanup();
119
120 // Return early if the plugin check object cache does not exist.
121 if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
122 return;
123 }
124
125 // Remove the object-cache.php file.
126 if ( $wp_filesystem || WP_Filesystem() ) {
127 if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
128 return;
129 }
130
131 // Check the drop-in file matches the copy.
132 $original_content = $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' );
133 $copy_content = $wp_filesystem->get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php' );
134
135 if ( $original_content && $original_content === $copy_content ) {
136 $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
137 }
138 }
139 }
140
141 /**
142 * Tests if the runtime environment is currently set up.
143 *
144 * This returns true when the plugin's object-cache.php drop-in is active in the current request and/or when the
145 * custom runtime environment database tables are present.
146 *
147 * @since 1.3.0
148 *
149 * @global wpdb $wpdb WordPress database abstraction object.
150 *
151 * @return bool True if the runtime environment is set up, false if not.
152 */
153 public function is_set_up() {
154 global $wpdb;
155
156 if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) ) {
157 return true;
158 }
159
160 // Set the custom prefix to check for the runtime environment tables.
161 $prefix_cleanup = $this->amend_db_base_prefix();
162
163 $tables_present = $wpdb->posts === $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->posts ) );
164
165 // Restore the old prefix.
166 $prefix_cleanup();
167
168 return $tables_present;
169 }
170
171 /**
172 * Checks if the WordPress Environment can be set up for runtime checks.
173 *
174 * @since 1.0.0
175 *
176 * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
177 *
178 * @return bool Returns true if the runtime environment can be set up, false if not.
179 */
180 public function can_set_up() {
181 global $wp_filesystem;
182
183 require_once ABSPATH . '/wp-admin/includes/upgrade.php';
184
185 if ( ! is_object( $wp_filesystem ) && ! WP_Filesystem() ) {
186 return false;
187 }
188
189 // Check if the object-cache.php file exists.
190 if ( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
191 // Check If the object-cache.php file is the Plugin Check version.
192 if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) {
193 return true;
194 }
195 } else {
196 // Get the correct Plugin Check directory when run too early.
197 if ( ! defined( 'WP_PLUGIN_CHECK_PLUGIN_DIR_PATH' ) ) {
198 $object_cache_copy = dirname( __DIR__, 2 ) . '/plugin-check/drop-ins/object-cache.copy.php';
199 } else {
200 $object_cache_copy = WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'drop-ins/object-cache.copy.php';
201 }
202
203 // If the file does not exist, check if we can place it.
204 $wp_filesystem->copy( $object_cache_copy, WP_CONTENT_DIR . '/object-cache.php' );
205
206 /**
207 * PHPStan ignore reason: PHPStan raised an issue because we have redundant file existence checks in our code.
208 * We perform this double check because we want to ensure that we can write the file we're testing.
209 *
210 * @phpstan-ignore-next-line
211 */
212 if ( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) {
213 // Remove the file before returning.
214 $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' );
215
216 return true;
217 }
218 }
219
220 return false;
221 }
222
223 /**
224 * Installs WordPress, while providing tweaks to allow for early execution of the install process.
225 *
226 * @since 1.3.0
227 *
228 * @param string $active_siteurl The actual site's site URL.
229 * @param string $active_theme The actual site's theme slug.
230 * @param string[] $active_plugins The actual site's list of plugin basenames.
231 */
232 private function install_wordpress( string $active_siteurl, string $active_theme, array $active_plugins ): void {
233 if ( ! isset( $_SERVER['HTTP_HOST'] ) ) {
234 $site_url = $active_siteurl;
235 $_SERVER['HTTP_HOST'] = preg_replace( '#^https?://#', '', rtrim( $site_url, '/' ) );
236 }
237
238 // Do not send post-install notification email, see https://github.com/WordPress/plugin-check/issues/424.
239 add_filter( 'pre_wp_mail', '__return_false' );
240
241 // The `wp_install()` function requires the WP_DEFAULT_THEME constant to be set.
242 if ( ! defined( 'WP_DEFAULT_THEME' ) ) {
243 define( 'WP_DEFAULT_THEME', $active_theme );
244 }
245
246 // The `wp_install()` function requires some pluggable functions like `get_user_by()` to be loaded.
247 if ( ! function_exists( 'get_user_by' ) ) {
248 require_once ABSPATH . '/wp-includes/pluggable.php';
249 }
250
251 /*
252 * Cookie constants need to be set before installation, which normally happens immediately after
253 * 'muplugins_loaded', which is when the logic here typically runs. It is therefore safe to call these
254 * functions here already.
255 */
256 if ( doing_action( 'muplugins_loaded' ) || ! did_action( 'muplugins_loaded' ) ) {
257 if ( is_multisite() ) {
258 ms_cookie_constants();
259 }
260 wp_cookie_constants();
261 }
262
263 wp_install(
264 'Plugin Check',
265 'plugincheck',
266 'demo@plugincheck.test',
267 false
268 );
269
270 remove_filter( 'pre_wp_mail', '__return_false' );
271
272 // Activate the same plugins in the test environment.
273 update_option( 'active_plugins', $active_plugins );
274 }
275 }
276