Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of ALL plugins

Tags:

php

wordpress

I would like to get a list of ALL Wordpress plugins.

There is a function called get_plugins() but it will return all plugins that I have installed. What I need is a list of all plugins, no matter if I installed them before or not.

Is there a function that I could use? If not, is there a JSON, database, API or anything that I could use?

Edit:

var_dump(plugins_api('query_plugins', array(
    'per_page' => 100,
    'tag' => 'contact form 7',
    'number' => 5,
    'page' => 1,
    'fields' =>
        array(
            'short_description' => false,
            'description' => false,
            'sections' => false,
            'tested' => false,
            'requires' => false,
            'rating' => false,
            'ratings' => false,
            'downloaded' => false,
            'downloadlink' => false,
            'last_updated' => false,
            'added' => false,
            'tags' => false,
            'compatibility' => false,
            'homepage' => false,
            'versions' => false,
            'donate_link' => false,
            'reviews' => false,
            'banners' => false,
            'icons' => false,
            'active_installs' => false,
            'group' => false,
            'contributors' => false
        ))));

This returns a full of data that I don't need:

The only data that I need are the yellow marked keys below: name and slug

enter image description here

I know that I could get them out from the array but it would be very bad for the performance.

Even when I try it with a loop, I'll get 45 plugins but not more. Where is the rest???

foreach ($plugins as $plugin) { // $plugins is the variable of my code above but without 'tag' => 'contact form 7',
    foreach ($plugin as $p) {
        if ($p != null) {
            echo $p->name . "<br>";
        }
    }
}
like image 892
Reza Saadati Avatar asked Aug 23 '18 16:08

Reza Saadati


People also ask

How do I see what plugins I have?

Google Chrome has several hidden chrome:// pages you can access. To view the plug-ins installed in Chrome, type chrome://plugins into Chrome's address bar and press Enter.

How do I list plugins in WordPress?

WordPress only supports plugin files in the base plugins directory (wp-content/plugins) and in one directory above the plugins directory (wp-content/plugins/my-plugin). The file it looks for has the plugin data and must be found in those two locations.

How do I find out what plugins are installed on my website?

Open up the page source for the website and press Ctrl + F to search the code quickly. Type in wp-content/plugins/ and start the search. You will find all the plugins used on the site. Some add-in have comments.

Where are plugins stored?

Static files like images, JS, CSS used by the plugin are stored in the plugins directory. The users' files uploaded when working with the plugin are stored in the plugin-specific folder inside the wp-content/uploads directory.


2 Answers

Not the best answer but I tried to solve my own problem the best way I could.

Getting a list of plugins

This will not return ALL plugins but it will return the top rated ones:

$plugins = plugins_api('query_plugins', array(
    'per_page' => 100,
    'browse' => 'top-rated',
    'fields' =>
        array(
            'short_description' => false,
            'description' => false,
            'sections' => false,
            'tested' => false,
            'requires' => false,
            'rating' => false,
            'ratings' => false,
            'downloaded' => false,
            'downloadlink' => false,
            'last_updated' => false,
            'added' => false,
            'tags' => false,
            'compatibility' => false,
            'homepage' => false,
            'versions' => false,
            'donate_link' => false,
            'reviews' => false,
            'banners' => false,
            'icons' => false,
            'active_installs' => false,
            'group' => false,
            'contributors' => false
        )));

Save the data as JSON

Since the data that we get is huge and it will be bad for performance, we try to get the name and the slug out of the array and then we write it in a JSON file:

$plugins_json = '{' . PHP_EOL;
// Get only the name and the slug
foreach ($plugins as $plugin) {
    foreach ($plugin as $key => $p) {
        if ($p->name != null) {
            // Let's beautify the JSON
            $plugins_json .= '  "'. $p->name . '": {' . PHP_EOL;
            $plugins_json .= '      "slug": "' . $p->slug . '"' . PHP_EOL;
            end($plugin);
            $plugins_json .= ($key !== key($plugin)) ? '    },' . PHP_EOL : '   }' . PHP_EOL;
        }
    }
}
$plugins_json .= '}';
file_put_contents('plugins.json', $plugins_json);

Now we have a slim JSON file with only the data that we need.

To keep updating the JSON file, we run that script to create a JSON file every 24 hours by setting up a Cron Job.

like image 164
Reza Saadati Avatar answered Oct 26 '22 16:10

Reza Saadati


Because getting all plugins at once will be too heavy for the server, it is a better idea to do it in steps.

You could do as many plugins at once as the server can handle. For the example I use a safe 100 plugins at once.

Everytime the script runs, it increments the "page" number with 1. So the next time the script runs the next 100 plugins are retrieved. The contents of the existing plugins.json will be parsed. The new plugins will be added (or overwritten if the plugin already is present) to the existing data, before encoding and saving it again.

If the page number is past the last, no results will be returned. This way the script knows there are no more plugins next. It then resets the page to 1, so it starts over.

I use the wp_options table to keep track of the pages, simply because it's the quickest way. It would be better to use some kind of filesystem caching. That will be easier to reset manually if needed.

You can set a cronjob to execute the script every x minutes. Now the plugins.json file will build up and grow step by step, every time it runs.

// get the current "page", or if the option not exists, set page to 1.
$page = get_option( 'plugins_page' ) ? (int)get_option( 'plugins_page' ) : 1;

// get the plugin objects
$plugins = plugins_api( 'query_plugins', [
    'per_page' => 100,
    'page'     => $page,
    'fields'   => [
       //.........
    ]
] );

// increment the page, or when no results, reset to 1.
update_option( 'plugins_page', count( $plugins ) > 0 ? ++ $page : 1 );

// build up the data array
$newData = [];
foreach ( $plugins as $plugin ) {
    foreach ( $plugin as $key => $p ) {
        if ( $p->name != null ) {
            $newData[ $p->name ] = [ 'slug' => $p->slug ];
        }
    }
}

// get plugin data already in file.
// The last argument (true) is important. It makes json objects into
// associative arrays so they can be merged with array_merge.
$existingData = json_decode( file_get_contents( 'plugins.json' ), true );

// merge existing data with new data
$pluginData = array_merge( $existingData, $newData );

file_put_contents( 'plugins.json', json_encode( $pluginData ) );
like image 43
jrswgtr Avatar answered Oct 26 '22 18:10

jrswgtr