获取所有插件的列表 [英] Getting a list of ALL plugins

查看:28
本文介绍了获取所有插件的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得所有 Wordpress 插件的列表.

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

有一个名为 get_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.

有我可以使用的功能吗?如果没有,是否有 JSON、数据库、API 或任何我可以使用的东西?

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

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:

我需要的唯一数据是下面的黄色标记键:nameslug

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

我知道我可以将它们从数组中取出,但这对性能会非常不利.

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

即使我尝试循环使用,我也会得到 45 个插件,但不会更多.剩下的呢???

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>";
        }
    }
}

推荐答案

因为一次获取​​所有插件对服务器来说太重了,所以最好分步完成.

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

你可以一次做尽可能多的插件,只要服务器可以处理.例如,我一次使用了 100 个安全的插件.

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

每次脚本运行时,它都会将页面"编号增加 1.因此,下次脚本运行时,将检索接下来的 100 个插件.现有 plugins.json 的内容将被解析.在重新编码和保存数据之前,新插件将被添加(如果插件已经存在,则覆盖)到现有数据中.

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.

如果页码超过最后一个,则不会返回任何结果.这样脚本就知道接下来没有插件了.然后它将页面重置为 1,因此它重新开始.

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.

我使用 wp_options 表来跟踪页面,因为这是最快的方式.最好使用某种文件系统缓存.如果需要,手动重置会更容易.

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.

您可以设置 cronjob每 x 分钟执行脚本.现在 plugins.json 文件将在每次运行时逐步建立和增长.

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 ) );

这篇关于获取所有插件的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆