@@ -396,6 +396,183 @@ function () {
396396 );
397397 }
398398
399+ /**
400+ * @group ms-excluded
401+ */
402+ public function test_single_site_bulk_deactivation_skips_sqlite () {
403+ $ this ->set_single_site_user ( 'administrator ' );
404+ wp_get_current_user ()->add_cap ( 'manage_options ' , false );
405+ $ plugins = array ( 'another-plugin/plugin.php ' , plugin_basename ( SQLITE_MAIN_FILE ) );
406+
407+ foreach ( $ plugins as $ index => $ plugin ) {
408+ if ( ! current_user_can ( 'deactivate_plugin ' , $ plugin ) ) {
409+ unset( $ plugins [ $ index ] );
410+ }
411+ }
412+
413+ $ this ->assertSame ( array ( 'another-plugin/plugin.php ' ), array_values ( $ plugins ) );
414+ }
415+
416+ /**
417+ * @group ms-required
418+ */
419+ public function test_network_bulk_deactivation_skips_sqlite_and_continues () {
420+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
421+ $ other_plugin = 'another-plugin/plugin.php ' ;
422+ $ plugins = array ( $ other_plugin , $ sqlite_plugin );
423+
424+ $ this ->run_as_delegated_network_plugin_manager (
425+ function () use ( $ plugins , $ sqlite_plugin , $ other_plugin ) {
426+ $ this ->run_with_network_active_plugins (
427+ $ plugins ,
428+ function () use ( $ plugins , $ sqlite_plugin , $ other_plugin ) {
429+ $ dropin_before = file_get_contents ( WP_CONTENT_DIR . '/db.php ' );
430+
431+ $ this ->set_bulk_deactivation_request ( $ plugins );
432+ $ this ->run_bulk_deactivation_preflight ( 'plugins-network ' );
433+
434+ $ this ->assertSame ( array ( $ other_plugin ), $ _POST ['checked ' ] );
435+
436+ deactivate_plugins ( $ _POST ['checked ' ], false , true );
437+
438+ $ this ->assertTrue ( is_plugin_active_for_network ( $ sqlite_plugin ) );
439+ $ this ->assertFalse ( is_plugin_active_for_network ( $ other_plugin ) );
440+ $ this ->assertSame ( $ dropin_before , file_get_contents ( WP_CONTENT_DIR . '/db.php ' ) );
441+ }
442+ );
443+ }
444+ );
445+ }
446+
447+ /**
448+ * @group ms-required
449+ */
450+ public function test_super_admin_can_bulk_deactivate_sqlite () {
451+ $ this ->set_multisite_user ( true );
452+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
453+
454+ $ this ->run_with_network_active_plugins (
455+ array ( $ sqlite_plugin ),
456+ function () use ( $ sqlite_plugin ) {
457+ $ this ->set_bulk_deactivation_request ( array ( $ sqlite_plugin ) );
458+ $ this ->run_bulk_deactivation_preflight ( 'plugins-network ' );
459+
460+ $ this ->assertSame ( array ( $ sqlite_plugin ), $ _POST ['checked ' ] );
461+
462+ $ filesystem = $ this ->run_with_deactivation_filesystem (
463+ function () use ( $ sqlite_plugin ) {
464+ deactivate_plugins ( $ sqlite_plugin , false , true );
465+ }
466+ );
467+
468+ $ this ->assertFalse ( is_plugin_active_for_network ( $ sqlite_plugin ) );
469+ $ this ->assertSame ( WP_CONTENT_DIR . '/db.php ' , $ filesystem ->deleted_path );
470+ }
471+ );
472+ }
473+
474+ /**
475+ * @group ms-required
476+ */
477+ public function test_site_admin_bulk_preflight_does_not_modify_request () {
478+ $ this ->set_multisite_user ( false );
479+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
480+ $ plugins = array ( 'another-plugin/plugin.php ' , $ sqlite_plugin );
481+
482+ $ this ->set_bulk_deactivation_request ( $ plugins );
483+ $ this ->run_bulk_deactivation_preflight ( 'plugins ' );
484+
485+ $ this ->assertSame ( $ plugins , $ _POST ['checked ' ] );
486+ }
487+
488+ /**
489+ * @group ms-required
490+ */
491+ public function test_network_bulk_preflight_preserves_slashed_plugin_names () {
492+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
493+ $ other_plugin = "another-plugin/plugin's.php " ;
494+
495+ $ this ->run_as_delegated_network_plugin_manager (
496+ function () use ( $ sqlite_plugin , $ other_plugin ) {
497+ $ this ->set_bulk_deactivation_request ( array ( $ other_plugin , $ sqlite_plugin ) );
498+ $ _POST ['checked ' ] = wp_slash ( $ _POST ['checked ' ] );
499+
500+ $ this ->run_bulk_deactivation_preflight ( 'plugins-network ' );
501+
502+ $ this ->assertSame ( wp_slash ( array ( $ other_plugin ) ), $ _POST ['checked ' ] );
503+ }
504+ );
505+ }
506+
507+ /**
508+ * @group ms-required
509+ */
510+ public function test_network_bulk_preflight_ignores_non_string_action () {
511+ $ this ->set_multisite_user ( false );
512+ $ plugins = array ( plugin_basename ( SQLITE_MAIN_FILE ) );
513+
514+ $ this ->set_bulk_deactivation_request ( $ plugins );
515+ $ _REQUEST ['action ' ] = array ( 'deactivate-selected ' );
516+
517+ $ this ->run_bulk_deactivation_preflight ( 'plugins-network ' );
518+
519+ $ this ->assertSame ( $ plugins , $ _POST ['checked ' ] );
520+ }
521+
522+ /**
523+ * @group ms-required
524+ */
525+ public function test_network_bulk_deactivation_allows_sqlite_without_dropin () {
526+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
527+
528+ $ this ->run_as_delegated_network_plugin_manager (
529+ function () use ( $ sqlite_plugin ) {
530+ $ this ->run_without_dropin (
531+ function () use ( $ sqlite_plugin ) {
532+ $ this ->set_bulk_deactivation_request ( array ( $ sqlite_plugin ) );
533+ $ this ->run_bulk_deactivation_preflight ( 'plugins-network ' );
534+
535+ $ this ->assertSame ( array ( $ sqlite_plugin ), $ _POST ['checked ' ] );
536+ }
537+ );
538+ }
539+ );
540+ }
541+
542+ /**
543+ * @group ms-required
544+ */
545+ public function test_deactivation_hook_preserves_dropin_when_bulk_preflight_is_bypassed () {
546+ $ sqlite_plugin = plugin_basename ( SQLITE_MAIN_FILE );
547+
548+ $ this ->run_as_delegated_network_plugin_manager (
549+ function () use ( $ sqlite_plugin ) {
550+ $ this ->run_with_network_active_plugins (
551+ array ( $ sqlite_plugin ),
552+ function () use ( $ sqlite_plugin ) {
553+ $ filesystem = $ this ->run_with_deactivation_filesystem (
554+ function () use ( $ sqlite_plugin ) {
555+ deactivate_plugins ( $ sqlite_plugin , false , true );
556+ }
557+ );
558+
559+ $ this ->assertFalse ( is_plugin_active_for_network ( $ sqlite_plugin ) );
560+ $ this ->assertNull ( $ filesystem ->deleted_path );
561+ }
562+ );
563+ }
564+ );
565+ }
566+
567+ /**
568+ * @group ms-excluded
569+ */
570+ public function test_deactivation_hook_allows_programmatic_call_without_user () {
571+ wp_set_current_user ( 0 );
572+
573+ $ this ->assert_deactivation_removes_dropin ();
574+ }
575+
399576 private function set_single_site_user ( $ role ) {
400577 $ user_id = self ::factory ()->user ->create ( array ( 'role ' => $ role ) );
401578
@@ -428,6 +605,46 @@ private function run_as_delegated_network_plugin_manager( $callback ) {
428605 }
429606 }
430607
608+ private function run_with_network_active_plugins ( $ plugins , $ callback ) {
609+ $ active_before = get_site_option ( 'active_sitewide_plugins ' , array () );
610+ $ active = $ active_before ;
611+
612+ foreach ( $ plugins as $ plugin ) {
613+ $ active [ $ plugin ] = time ();
614+ }
615+ update_site_option ( 'active_sitewide_plugins ' , $ active );
616+
617+ try {
618+ return call_user_func ( $ callback );
619+ } finally {
620+ update_site_option ( 'active_sitewide_plugins ' , $ active_before );
621+ }
622+ }
623+
624+ private function set_bulk_deactivation_request ( $ plugins ) {
625+ $ _POST = array ( 'checked ' => $ plugins );
626+ $ _REQUEST = array (
627+ 'action ' => 'deactivate-selected ' ,
628+ );
629+ }
630+
631+ private function run_bulk_deactivation_preflight ( $ screen_id ) {
632+ $ screen_was_set = array_key_exists ( 'current_screen ' , $ GLOBALS );
633+ $ screen_before = $ screen_was_set ? $ GLOBALS ['current_screen ' ] : null ;
634+
635+ $ GLOBALS ['current_screen ' ] = WP_Screen ::get ( $ screen_id );
636+
637+ try {
638+ sqlite_plugin_filter_network_bulk_deactivation ();
639+ } finally {
640+ if ( $ screen_was_set ) {
641+ $ GLOBALS ['current_screen ' ] = $ screen_before ;
642+ } else {
643+ unset( $ GLOBALS ['current_screen ' ] );
644+ }
645+ }
646+ }
647+
431648 private function assert_deactivation_removes_dropin () {
432649 $ filesystem = $ this ->run_with_deactivation_filesystem ( 'sqlite_plugin_remove_db_file ' );
433650
0 commit comments