Pass by reference code refactor

While adopting my Manager History Extension to the new MainWP Namespace I started a new feature that has been on my todo list for a long time.

From time to time a plugin update on a child site causes the updated plugin to get deactivated. Most likely this is caused by bad development of the plugins. However the update succeeds and MainWP reports no failure. From MainWP’s point of view this is correct. But you can imagine the chaos if that happens.

Therefore i decided to implement a plugins activation status check to get notified if the status changes after an update. I fetch the status before an update from the action hook “mainwp_website_before_updated” and the status after the update from the action hook “mainwp_website_updated”. In both cases I use the “plugins” property of the $website object passed to the action hooks.

Now i had to realize that the $website object does not change. I would have expected an updated $website object at “mainwp_website_updated” because the action hook fires after MainWP_Sync::sync_information_array is called. I can see the updated information in the database but even the action hook “mainwp_site_synced” does not reflect the changes although $pWebsite is passed by reference. There is not much sense in passing a reference if you do not intend to modify the object. In fact there is no single attempt to update the $website object at all.

I get arround the issue now by fetching the updated information for that website (“get_website_by_id()”) from with in the action hook “mainwp_website_updated”.

On the one hand it is convenient to have the old $website object at hand even after an update. But if this is the intention there is no need to pass an object by reference because you are always in risk for extension developers to manipulate that object in a “harmful” way. While debugging the code i found many places where objects, arrays and variables are passed by reference and from my experience with $website object i guess many of this references are not needed. And as you are refactoring the mainwp code it seems to me a good idea to check those references and refactor the function calls if no reference is needed.


1 Like

Hi Michael,

thanks for the detailed report. I will have our dev team review this and see what can be done.