Skip to content

Commit

Permalink
Enable Bulk Plugin Management: Fix the updateable sites count display…
Browse files Browse the repository at this point in the history
…ed (#96603)

* filter out plugins that cannot be updated

* fix the correct number of updateable sites displayed in the update modal

---------

Co-authored-by: Paulo Trentin <paulo@paulotrentin.com.br>
  • Loading branch information
vykes-mac and paulopmt1 authored Nov 25, 2024
1 parent 27dda12 commit df5d3aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function usePluginVersionInfo(
currentVersionsRange: { min: string; max: string };
updatedVersions: string[];
hasUpdate: boolean;
updateableSites: number;
} {
const allSites = useSelector( getSites );

Expand All @@ -28,6 +29,10 @@ export default function usePluginVersionInfo(

const siteIds = siteObjectsToSiteIds( sites );

const updateableSites = sites.filter(
( site ) => site.canUpdateFiles && site.version !== plugin.update?.new_version
).length;

const pluginsOnSites: any = useSelector( ( state ) =>
getPluginOnSites( state, siteIds, plugin?.slug )
);
Expand All @@ -37,10 +42,20 @@ export default function usePluginVersionInfo(
return pluginsOnSites?.sites[ siteId ];
};

const hasUpdate = sites.some( ( site ) => {
const sitePlugin = getSitePlugin( site );
return sitePlugin?.update?.new_version && site.canUpdateFiles;
} );
let hasUpdate = false;

if ( selectedSiteId ) {
const selectedSite = sites.find( ( site ) => site.ID === selectedSiteId );
if ( selectedSite ) {
const sitePlugin = getSitePlugin( selectedSite );
hasUpdate = sitePlugin?.update?.new_version && selectedSite.canUpdateFiles;
}
} else {
hasUpdate = sites.some( ( site ) => {
const sitePlugin = getSitePlugin( site );
return sitePlugin?.update?.new_version && site.canUpdateFiles;
} );
}

const updatedVersions = sites
.map( ( site ) => {
Expand Down Expand Up @@ -79,6 +94,7 @@ export default function usePluginVersionInfo(
},
updatedVersions,
hasUpdate,
updateableSites,
};
}, [ currentVersions, hasUpdate, updatedVersions ] );
}, [ currentVersions, hasUpdate, updateableSites, updatedVersions ] );
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ export default function PluginRowFormatter( {
selectedSite={ selectedSite }
className={ className }
updatePlugin={ updatePlugin }
siteCount={ siteCount }
/>
);
case 'install':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ interface Props {
siteCount?: number;
}

export default function UpdatePlugin( {
plugin,
selectedSite,
className,
updatePlugin,
siteCount,
}: Props ) {
export default function UpdatePlugin( { plugin, selectedSite, className, updatePlugin }: Props ) {
const translate = useTranslate();
const state = useSelector( ( state ) => state );
const [ displayConfirmModal, setDisplayConfirmModal ] = useState( false );

const { currentVersionsRange, updatedVersions, hasUpdate } = usePluginVersionInfo(
plugin,
selectedSite?.ID
);
const { currentVersionsRange, updatedVersions, hasUpdate, updateableSites } =
usePluginVersionInfo( plugin, selectedSite?.ID );

const allowedActions = getAllowedPluginActions( plugin, state, selectedSite );

Expand Down Expand Up @@ -125,11 +117,11 @@ export default function UpdatePlugin( {
'You are about to update the %(plugin)s plugin to version %(version)s, on %(siteCount)d site. ',
'You are about to update the %(plugin)s plugin to version %(version)s, on %(siteCount)d sites. ',
{
count: siteCount ?? 1,
count: updateableSites ?? 1,
args: {
version: updatedVersions[ 0 ],
plugin: plugin.name ?? plugin.slug,
siteCount: String( siteCount ),
siteCount: String( updateableSites ),
},
}
) }
Expand Down
10 changes: 10 additions & 0 deletions client/my-sites/plugins/plugins-list/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ export class PluginsList extends Component {
[ PluginActions.DISABLE_AUTOUPDATES ]: this.unsetAutoupdateSelected,
};

if ( actionName === PluginActions.UPDATE ) {
//filter out sites that don't have an update available
selectedPlugins = selectedPlugins.map( ( plugin ) => {
const filteredSites = Object.fromEntries(
Object.entries( plugin.sites ).filter( ( [ , site ] ) => site.update?.new_version )
);
return { ...plugin, sites: filteredSites };
} );
}

const selectedActionCallback = ALL_ACTION_CALLBACKS[ actionName ];
showPluginActionDialog( actionName, selectedPlugins, allSites, selectedActionCallback );
};
Expand Down

0 comments on commit df5d3aa

Please sign in to comment.