How to list all child plugins (code)

Hi there,
I am working on building my own MainWP extension. What I need is a way to list all child site plugins (with details). Is there a hook to do this? Or should I use the REST API route?
Kind regards,
hrbrt

Hi Herbert,

thanks for reaching out. Here is some help:

if you need to list fetch all sites and installed plugins here is how you can do it:

$websites = apply_filters( 'mainwp_getsites', $theExtensionActivator->get_child_file(), $theExtensionActivator->get_child_key(), null );

$sites_ids = array();
if ( is_array( $websites ) ) {
    foreach ( $websites as $website ) {
        $sites_ids[] = $website['id'];
    }
}

$option = array(
    'plugins'         => true,
);

$dbwebsites = apply_filters( 'mainwp_getdbsites', $theExtensionActivator->get_child_file(), $theExtensionActivator->get_child_key(), $sites_ids, array(), $option );


foreach ( $dbwebsites as $website ) {
    if ( $website->plugins != '' ) {
        $plugins = json_decode( $website->plugins, 1 );
        if ( is_array( $plugins ) && count( $plugins ) != 0 ) {
            // do something here.
        }
    }
}

If you need to do it just for one site, you can do it this way:

$sites_ids = array( ONE_SITE_ID );        
$option = array(
    'plugins'         => true,
);

$dbwebsites = apply_filters( 'mainwp_getdbsites', $theExtensionActivator->get_child_file(), $theExtensionActivator->get_child_key(), $sites_ids, array(), $option );


foreach ( $dbwebsites as $website ) {
    if ( $website->plugins != '' ) {
        $plugins = json_decode( $website->plugins, 1 );
        if ( is_array( $plugins ) && count( $plugins ) != 0 ) {
            // do something here.
        }
    }
}
3 Likes

Great! Thank you Bogdan. That is working.

3 Likes

Hi Bogdan,
Not really mainwp related… Do you have any suggestions on how to parse a large json file (45mb). I am running in to timeouts now.
I tried using halaxa/jsonmachine and cerbero/json-parser but both are still timing out.
Any suggestions are welcome!
Kind regards, hrbrt

Hi Herbert,

Can you please share a bit more details so we can look into different approaches?
Where do you get JSON from?
Is all data in it required? Maybe you can export only the needed data to get smaller JSON?
How exactly do you parse it now?
Have you tried to increase the PHP memory limit and execution time?

2 Likes

Hi,
The file has a lot of data because it is a vulnarability database, so data cannot really be smaller.
Now I am using halaxa/jsonmachine in a foreach loop to find the vuln data foreach plugin.

Execution time is 30 sec. Memory is no issue. Error gives a execution timeout.
I was hoping you had any tips where to look. I was thinking of buffering output and releasing it for each plugin. Would that be a solution?
Kind regards, hrbrt

UPDATE: I might be able to shrink the file size by getting only new vulnerabilties from the file and using them. I will work on that one.

@mwp-haha-hrbrt-com “Error gives a execution timeout.” is related to the set_time_limit value. The script is not being aloud to run past 30 seconds & you would need to increase it until it doesn’t fail.

https://www.php.net/manual/en/function.set-time-limit.php

1 Like

I did as Bogdan suggested: cleaning up the base file. I don’t really need all vulnerabilities, so I create a new file after download with just the last 2 years of updated vulnerabilities. That should be enough. Now it runs very fast. Thank you for al the tips and suggestions.

1 Like

@mwp-haha-hrbrt-com Perfect! Glad everything is working to your liking now :slight_smile:

FYI
I made this plugin because I don’t like to use WF plugin on my websites. I have good hosting and good security measures on client websites so I dont need the WF plugin, but I wanted to easily check if there are any vulnerable plugins on client sites. Now I can do this without using the WF plugin and using their open vulnerability database.

I will probably put this on my github repo so anyone who want to can use it. It still can use some more work but for now it is working.

Thanks for all the help.
Kind regards, hrbrt

2 Likes

I still want to add some options. Maybe you can tell me how I can make a direct link for updating a plugin from my vulnerable listing. That would be a nice addition.

2 Likes

For now I put it in my repo on GitHub. If anyone wants to use it it can be downloaded there. Or maybe offer suggestions or improvements. You can find it here:

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.