@@ -137,6 +137,8 @@ public function upgrade_plugin_theme() {
137137 );
138138 MainWP_Child_Actions::get_instance ()->init_custom_hooks ( $ hooks );
139139
140+ static ::enable_pre_auto_rollback_hooking ();
141+
140142 $ plugin_update = false ;
141143 // phpcs:disable WordPress.Security.NonceVerification
142144 if ( isset ( $ _POST ['type ' ] ) && 'plugin ' === $ _POST ['type ' ] ) {
@@ -382,8 +384,9 @@ private function to_update_plugins( &$information, $plugins ) { //phpcs:ignore -
382384
383385 $ updated_plugins = array ();
384386 foreach ( $ result as $ plugin => $ info ) {
385- $ success = false ;
386- $ error = '' ;
387+ $ success = false ;
388+ $ problematic_update = false ;
389+ $ error = '' ;
387390 if ( empty ( $ info ) ) {
388391
389392 $ information ['upgrades ' ][ $ plugin ] = false ;
@@ -408,6 +411,10 @@ private function to_update_plugins( &$information, $plugins ) { //phpcs:ignore -
408411 } elseif ( is_wp_error ( $ info ) ) {
409412 $ error = $ info ->get_error_message ();
410413 $ information ['upgrades_error ' ][ $ plugin ] = $ error ;
414+ $ errors_codes = $ info ->get_error_codes ();
415+ if ( is_array ( $ errors_codes ) && in_array ( 'mainwp_update_error_code ' , $ errors_codes ) ) {
416+ $ problematic_update = true ;
417+ }
411418 } else {
412419 $ information ['upgrades ' ][ $ plugin ] = true ;
413420 $ success = true ;
@@ -427,6 +434,10 @@ private function to_update_plugins( &$information, $plugins ) { //phpcs:ignore -
427434 $ info ['error ' ] = $ error ;
428435 }
429436
437+ if ( $ problematic_update ) {
438+ $ info ['rollback ' ] = 1 ;
439+ }
440+
430441 $ current_info = get_plugin_data ( WP_PLUGIN_DIR . '/ ' . $ plugin );
431442 if ( ! is_array ( $ current_info ) ) {
432443 $ current_info = array ();
@@ -582,9 +593,18 @@ private function to_upgrade_themes( &$information, $themes, $last_update ) { //p
582593 $ themes_info = MainWP_Child_Actions::get_instance ()->get_current_themes_info ();
583594 $ updated_themes = array ();
584595 foreach ( $ result as $ theme => $ value ) {
585- $ success = false ;
596+ $ success = false ;
597+ $ error = '' ;
598+ $ problematic_update = false ;
586599 if ( empty ( $ value ) ) {
587600 $ information ['upgrades ' ][ $ theme ] = false ;
601+ } elseif ( is_wp_error ( $ value ) ) {
602+ $ error = $ value ->get_error_message ();
603+ $ information ['upgrades ' ][ $ theme ] = false ;
604+ $ errors_codes = $ value ->get_error_codes ();
605+ if ( is_array ( $ errors_codes ) && in_array ( 'mainwp_update_error_code ' , $ errors_codes ) ) {
606+ $ problematic_update = true ;
607+ }
588608 } else {
589609 $ information ['upgrades ' ][ $ theme ] = true ;
590610 $ success = true ;
@@ -601,6 +621,14 @@ private function to_upgrade_themes( &$information, $themes, $last_update ) { //p
601621 'success ' => $ success ? 1 : 0 ,
602622 );
603623
624+ if ( $ problematic_update ) {
625+ $ info ['rollback ' ] = 1 ;
626+ }
627+
628+ if ( ! empty ( $ error ) ) {
629+ $ info ['error ' ] = $ error ;
630+ }
631+
604632 $ current_info = wp_get_theme ( $ theme );
605633 if ( ! is_array ( $ current_info ) ) {
606634 $ current_info = array ();
@@ -650,33 +678,206 @@ private function to_upgrade_themes( &$information, $themes, $last_update ) { //p
650678 * @param array $mwp_premium_updates_to_do_slugs An array containing the list of premium themes slugs to update.
651679 */
652680 private function update_premiums_to_do ( &$ information , $ premiumUpgrader , $ mwp_premium_updates_to_do , $ mwp_premium_updates_to_do_slugs ) { //phpcs:ignore -- NOSONAR - complex.
681+
682+ if ( ! isset ( $ information ['other_data ' ]['updated_data ' ] ) ) {
683+ $ information ['other_data ' ]['updated_data ' ] = array ();
684+ }
685+
686+ $ plugins_info = MainWP_Child_Actions::get_instance ()->get_current_plugins_info ();
687+ $ updated_plugins = array ();
688+
653689 // Upgrade via WP.
654690 // @see wp-admin/update.php.
655- $ result = $ premiumUpgrader ->bulk_upgrade ( $ mwp_premium_updates_to_do_slugs );
656- if ( ! empty ( $ result ) ) {
657- foreach ( $ result as $ plugin => $ info ) {
658- if ( ! empty ( $ info ) ) {
659- $ information ['upgrades ' ][ $ plugin ] = true ;
691+ $ results = $ premiumUpgrader ->bulk_upgrade ( $ mwp_premium_updates_to_do_slugs );
692+ if ( ! empty ( $ results ) ) {
693+ foreach ( $ results as $ plugin => $ result ) {
694+ if ( ! empty ( $ result ) ) {
695+
696+ if ( is_wp_error ( $ result ) ) {
697+ $ slug = $ plugin ;
698+
699+ $ update_info = array ();
700+ if ( is_array ( $ mwp_premium_updates_to_do ) ) {
701+ $ update_info = array_filter (
702+ $ mwp_premium_updates_to_do ,
703+ function ( $ e ) use ( $ slug ) {
704+ return isset ( $ e ['slug ' ] ) && $ e ['slug ' ] === $ slug ;
705+ }
706+ );
707+ if ( $ update_info ) {
708+ $ update_info = current ( $ update_info );
709+ }
710+ if ( ! is_array ( $ update_info ) ) {
711+ $ update_info = array ();
712+ }
713+ }
714+
715+ $ problematic_update = false ;
716+ $ error = $ result ->get_error_message ();
717+ $ errors_codes = $ result ->get_error_codes ();
718+ if ( is_array ( $ errors_codes ) && in_array ( 'mainwp_update_error_code ' , $ errors_codes ) ) {
719+ $ problematic_update = true ;
720+ }
721+
722+ $ old_info = isset ( $ plugins_info [ $ slug ] ) ? $ plugins_info [ $ slug ] : array ();
723+
724+ if ( ! is_array ( $ old_info ) ) {
725+ $ old_info = array ();
726+ }
727+ $ name = isset ( $ old_info ['Name ' ] ) ? $ old_info ['Name ' ] : '' ;
728+
729+ $ info = array (
730+ 'name ' => $ name ,
731+ 'old_version ' => isset ( $ old_info ['Version ' ] ) ? $ old_info ['Version ' ] : '' ,
732+ 'slug ' => $ slug ,
733+ 'success ' => 0 ,
734+ );
735+
736+ if ( empty ( $ info ['old_version ' ] ) && ! empty ( $ update_info ['Version ' ] ) ) {
737+ $ info ['old_version ' ] = $ update_info ['Version ' ];
738+ }
739+
740+ if ( $ problematic_update ) {
741+ $ info ['rollback ' ] = 1 ;
742+ }
743+
744+ if ( ! empty ( $ error ) ) {
745+ $ info ['error ' ] = $ error ;
746+ }
747+
748+ $ current_info = get_plugin_data ( WP_PLUGIN_DIR . '/ ' . $ slug );
749+ if ( ! is_array ( $ current_info ) ) {
750+ $ current_info = array ();
751+ }
752+
753+ if ( ! empty ( $ current_info ['Version ' ] ) ) {
754+ $ info ['version ' ] = $ current_info ['Version ' ];
755+ }
756+
757+ if ( empty ( $ info ['name ' ] ) && ! empty ( $ current_info ['Name ' ] ) ) {
758+ $ info ['name ' ] = $ current_info ['Name ' ];
759+ }
760+
761+ if ( empty ( $ info ['name ' ] ) ) {
762+ $ info ['name ' ] = $ slug ;
763+ }
764+
765+ $ information ['other_data ' ]['updated_data ' ][ $ slug ] = $ info ;
766+
767+ } else {
768+ $ information ['upgrades ' ][ $ plugin ] = true ;
769+ }
770+ $ updated_plugins [ $ slug ] = 1 ; // to prevent try next and incorrect.
660771 }
661772 }
662773 }
663774
664775 // Upgrade via callback.
665776 foreach ( $ mwp_premium_updates_to_do as $ update ) {
777+
778+ if ( isset ( $ update ['slug ' ] ) && isset ( $ updated_plugins [ $ update ['slug ' ] ] ) ) {
779+ continue ;
780+ }
781+
666782 $ slug = ( isset ( $ update ['slug ' ] ) ? $ update ['slug ' ] : $ update ['Name ' ] );
667783
668784 if ( isset ( $ update ['url ' ] ) ) {
669- $ installer = new \WP_Upgrader ();
670- $ result = $ installer ->run (
785+
786+ $ installer = new \WP_Upgrader ();
787+
788+ $ hook_extra = array ();
789+
790+ if ( 'plugin ' === $ update ['type ' ] && false !== strpos ( $ slug , '/ ' ) ) {
791+ $ hook_extra = array (
792+ 'plugin ' => $ slug ,
793+ 'temp_backup ' => array (
794+ 'slug ' => dirname ( $ slug ),
795+ 'src ' => WP_PLUGIN_DIR ,
796+ 'dir ' => 'plugins ' ,
797+ ),
798+ );
799+ }
800+
801+ $ result = $ installer ->run (
671802 array (
672803 'package ' => $ update ['url ' ],
673804 'destination ' => ( 'plugin ' === $ update ['type ' ] ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/themes ' ),
674805 'clear_destination ' => true ,
675806 'clear_working ' => true ,
676- 'hook_extra ' => array () ,
807+ 'hook_extra ' => $ hook_extra ,
677808 )
678809 );
679- $ information ['upgrades ' ][ $ slug ] = ( ! is_wp_error ( $ result ) && ! empty ( $ result ) );
810+
811+ $ success = ! is_wp_error ( $ result ) && ! empty ( $ result );
812+ $ information ['upgrades ' ][ $ slug ] = $ success ;
813+
814+ $ problematic_update = false ;
815+
816+ $ error = '' ;
817+
818+ if ( is_wp_error ( $ result ) ) {
819+ $ error = $ result ->get_error_message ();
820+ $ errors_codes = $ result ->get_error_codes ();
821+ if ( is_array ( $ errors_codes ) && in_array ( 'mainwp_update_error_code ' , $ errors_codes ) ) {
822+ $ problematic_update = true ;
823+ }
824+ }
825+
826+ $ name = ! empty ( $ update ['name ' ] ) ? $ update ['name ' ] : '' ;
827+
828+ if ( empty ( $ name ) ) {
829+ $ name = ! empty ( $ update ['Name ' ] ) ? $ update ['Name ' ] : '' ;
830+ }
831+
832+ $ old_info = array ();
833+
834+ if ( 'plugin ' === $ update ['type ' ] ) {
835+ $ old_info = isset ( $ plugins_info [ $ slug ] ) ? $ plugins_info [ $ slug ] : array ();
836+ if ( ! is_array ( $ old_info ) ) {
837+ $ old_info = array ();
838+ }
839+ }
840+
841+ $ info = array (
842+ 'name ' => $ name ,
843+ 'old_version ' => isset ( $ old_info ['version ' ] ) ? $ old_info ['version ' ] : '' ,
844+ 'slug ' => $ slug ,
845+ 'success ' => $ success ? 1 : 0 ,
846+ );
847+
848+ if ( $ problematic_update ) {
849+ $ info ['rollback ' ] = 1 ;
850+ }
851+
852+ if ( ! empty ( $ error ) ) {
853+ $ info ['error ' ] = $ error ;
854+ }
855+
856+ if ( 'plugin ' === $ update ['type ' ] ) {
857+ $ current_info = get_plugin_data ( WP_PLUGIN_DIR . '/ ' . $ slug );
858+ if ( ! is_array ( $ current_info ) ) {
859+ $ current_info = array ();
860+ }
861+
862+ if ( ! empty ( $ current_info ['Version ' ] ) ) {
863+ $ info ['version ' ] = $ current_info ['Version ' ];
864+ }
865+
866+ if ( empty ( $ info ['name ' ] ) && ! empty ( $ current_info ['Name ' ] ) ) {
867+ $ info ['name ' ] = $ current_info ['Name ' ];
868+ }
869+ }
870+
871+ if ( empty ( $ info ['version ' ] ) && ! empty ( $ update ['version ' ] ) ) {
872+ $ info ['version ' ] = $ update ['version ' ];
873+ }
874+
875+ if ( empty ( $ info ['name ' ] ) ) {
876+ $ info ['name ' ] = $ slug ;
877+ }
878+
879+ $ information ['other_data ' ]['updated_data ' ][ $ slug ] = $ info ;
880+
680881 } elseif ( isset ( $ update ['callback ' ] ) ) {
681882 if ( is_array ( $ update ['callback ' ] ) && isset ( $ update ['callback ' ][0 ] ) && isset ( $ update ['callback ' ][1 ] ) ) {
682883 $ update_result = call_user_func (
@@ -1191,4 +1392,25 @@ public function upgrade_translation() { //phpcs:ignore -- NOSONAR - complex.
11911392 $ information ['sync ' ] = MainWP_Child_Stats::get_instance ()->get_site_stats ( array (), false );
11921393 MainWP_Helper::write ( $ information );
11931394 }
1395+
1396+
1397+ /**
1398+ * Method enable_pre_auto_rollback_hooking().
1399+ */
1400+ public static function enable_pre_auto_rollback_hooking () {
1401+ add_filter ( 'upgrader_install_package_result ' , array ( static ::get_class_name (), 'upgrader_auto_rollback_hooking ' ), 99 , 2 );
1402+ }
1403+
1404+ /**
1405+ * Method upgrader_auto_rollback_hooking().
1406+ *
1407+ * @param array|WP_Error $result Result from WP_Upgrader::install_package().
1408+ * @param array $hook_extra Extra arguments passed to hooked filters.
1409+ */
1410+ public static function upgrader_auto_rollback_hooking ( $ result , $ hook_extra = array () ) {
1411+ if ( is_wp_error ( $ result ) && is_array ( $ hook_extra ) && ! empty ( $ hook_extra ['temp_backup ' ] ) ) {
1412+ $ result ->add ( 'mainwp_update_error_code ' , 'Update error. ' );
1413+ }
1414+ return $ result ;
1415+ }
11941416}
0 commit comments