@@ -156,10 +156,10 @@ public function __invoke( $args, $assoc_args ) {
156156 // Process must-use plugins if not excluded.
157157 $ mu_plugins = array ();
158158 if ( ! $ exclude_mu ) {
159- $ mu_plugins = $ this -> get_mu_plugin_list ();
159+ $ mu_plugins = get_mu_plugins ();
160160
161161 foreach ( $ mu_plugins as $ mu_file => $ mu_plugin ) {
162- $ plugin_name = $ this ->get_plugin_slug_from_header ( $ mu_file, $ mu_plugin );
162+ $ plugin_name = $ this ->get_plugin_slug_from_path ( $ mu_file );
163163
164164 if ( in_array ( $ plugin_name , $ exclude_list , true ) ) {
165165 ++$ skips ;
@@ -309,6 +309,33 @@ private function check_file_checksum( $path, $checksums ) {
309309 return in_array ( $ md5 , (array ) $ checksums ['md5 ' ], true );
310310 }
311311
312+ /**
313+ * Checks the integrity of a single MU plugin file by comparing it to the
314+ * officially provided checksum.
315+ *
316+ * @param string $path Relative path to the MU plugin file to check the
317+ * integrity of.
318+ * @param array $checksums Array of provided checksums to compare against.
319+ *
320+ * @return bool|string
321+ */
322+ private function check_mu_file_checksum ( $ path , $ checksums ) {
323+ if ( $ this ->supports_sha256 ()
324+ && array_key_exists ( 'sha256 ' , $ checksums )
325+ ) {
326+ $ sha256 = $ this ->get_sha256 ( WPMU_PLUGIN_DIR . '/ ' . $ path );
327+ return in_array ( $ sha256 , (array ) $ checksums ['sha256 ' ], true );
328+ }
329+
330+ if ( ! array_key_exists ( 'md5 ' , $ checksums ) ) {
331+ return 'No matching checksum algorithm found ' ;
332+ }
333+
334+ $ md5 = $ this ->get_md5 ( WPMU_PLUGIN_DIR . '/ ' . $ path );
335+
336+ return in_array ( $ md5 , (array ) $ checksums ['md5 ' ], true );
337+ }
338+
312339 /**
313340 * Checks whether the current environment supports 256-bit SHA-2.
314341 *
@@ -384,37 +411,43 @@ private function is_soft_change_file( $file ) {
384411 }
385412
386413 /**
387- * Gets the list of all must-use plugins.
414+ * Extracts the plugin slug from the plugin file path.
415+ *
416+ * For MU plugins that are actually standard plugins moved to mu-plugins folder,
417+ * we extract the plugin slug from the file path to look up checksums.
388418 *
389- * @return array Array of MU plugins with their file path as key.
419+ * @param string $plugin_file Path to the plugin file.
420+ *
421+ * @return string Plugin slug.
390422 */
391- private function get_mu_plugin_list () {
392- if ( ! function_exists ( 'get_mu_plugins ' ) ) {
393- return array ();
423+ private function get_plugin_slug_from_path ( $ plugin_file ) {
424+ // If it's in a subdirectory, use the directory name as slug.
425+ if ( false !== strpos ( $ plugin_file , '/ ' ) ) {
426+ return dirname ( $ plugin_file );
394427 }
395428
396- return get_mu_plugins ();
429+ // For single files, extract the slug from the filename.
430+ return basename ( $ plugin_file , '.php ' );
397431 }
398432
399433 /**
400- * Extracts the plugin slug from the plugin header data.
434+ * Gets the version for a plugin from its header data or the version argument .
401435 *
402- * For MU plugins that are actually standard plugins moved to mu-plugins folder,
403- * we try to extract the plugin slug to look up checksums .
436+ * @param string $version_arg Version argument from command line.
437+ * @param array $plugin_data Plugin header data .
404438 *
405- * @param string $mu_file Path to the MU plugin file.
406- * @param array $mu_plugin Plugin header data.
407- *
408- * @return string Plugin slug.
439+ * @return string|false Plugin version, or false if not found.
409440 */
410- private function get_plugin_slug_from_header ( $ mu_file , $ mu_plugin ) {
411- // If it's in a subdirectory, use the directory name as slug.
412- if ( false !== strpos ( $ mu_file , '/ ' ) ) {
413- return dirname ( $ mu_file );
441+ private function get_plugin_version_for_verification ( $ version_arg , $ plugin_data ) {
442+ if ( ! empty ( $ version_arg ) ) {
443+ return $ version_arg ;
414444 }
415445
416- // For single files, extract the slug from the filename.
417- return basename ( $ mu_file , '.php ' );
446+ if ( ! empty ( $ plugin_data ['Version ' ] ) ) {
447+ return $ plugin_data ['Version ' ];
448+ }
449+
450+ return false ;
418451 }
419452
420453 /**
@@ -432,14 +465,9 @@ private function verify_mu_plugin( $mu_file, $mu_plugin, $plugin_name, $version_
432465 $ is_single_file = false === strpos ( $ mu_file , '/ ' );
433466
434467 // Get version from the plugin header.
435- $ version = '' ;
436- if ( ! empty ( $ version_arg ) ) {
437- $ version = $ version_arg ;
438- } elseif ( ! empty ( $ mu_plugin ['Version ' ] ) ) {
439- $ version = $ mu_plugin ['Version ' ];
440- }
468+ $ version = $ this ->get_plugin_version_for_verification ( $ version_arg , $ mu_plugin );
441469
442- if ( empty ( $ version ) ) {
470+ if ( false === $ version ) {
443471 WP_CLI ::warning ( "Could not retrieve the version for must-use plugin {$ plugin_name }, skipping. " );
444472 ++$ skips ;
445473 return ;
@@ -485,11 +513,10 @@ private function verify_mu_plugin( $mu_file, $mu_plugin, $plugin_name, $version_
485513 continue ;
486514 }
487515
488- $ absolute_path = $ is_single_file
489- ? WPMU_PLUGIN_DIR . '/ ' . $ file
490- : WPMU_PLUGIN_DIR . '/ ' . dirname ( $ mu_file ) . '/ ' . $ file ;
516+ // Build the relative path for MU plugins.
517+ $ relative_path = $ is_single_file ? $ file : dirname ( $ mu_file ) . '/ ' . $ file ;
491518
492- $ result = $ this ->check_file_checksum ( $ absolute_path , $ checksums [ $ file ] );
519+ $ result = $ this ->check_mu_file_checksum ( $ relative_path , $ checksums [ $ file ] );
493520 if ( true !== $ result ) {
494521 $ this ->add_error ( $ plugin_name , $ file , is_string ( $ result ) ? $ result : 'Checksum does not match ' );
495522 }
0 commit comments