This repository has been archived by the owner on Oct 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin-stats.php
34 lines (28 loc) · 1.48 KB
/
plugin-stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
use WP_CLI\Utils;
global $wpdb;
// This table must exist with the correct schema.
$wpdb->select( 'gutenberg_plugins', $wpdb->dbh );
$original_request_url = 'https://wordpress.org/plugins/wp-json/plugins/v1/query-plugins?s=&posts_per_page=100';
$paged = 1;
do {
$request_url = $original_request_url . '&paged=' . $paged;
WP_CLI::log( 'Requesting: ' . $request_url );
$response = Utils\http_request( 'GET', $request_url );
$list_body = json_decode( $response->body, true );
if ( ! empty( $list_body['plugins'] ) ) {
foreach ( $list_body['plugins'] as $plugin_name ) {
WP_CLI::log( ' -> ' . $plugin_name );
$plugin_url = 'https://wordpress.org/plugins/wp-json/plugins/v1/plugin/' . $plugin_name;
$response = Utils\http_request( 'GET', $plugin_url );
$plugin = json_decode( $response->body, true );
$tags = ! empty( $plugin['tags'] ) ? implode( ', ', $plugin['tags'] ) : '';
$last_updated = human_time_diff( strtotime( $plugin['last_updated'] ), time() );
$wpdb->query( $wpdb->prepare( 'INSERT INTO plugins (plugin_slug, active_installs, last_updated, short_description, tags) VALUES(%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE active_installs=%s, last_updated=%s, short_description=%s, tags=%s', $plugin_name, $plugin['active_installs'], $last_updated, $plugin['short_description'], $tags, $plugin['active_installs'], $last_updated, $plugin['short_description'], $tags ) );
}
} else {
$request_url = false;
}
$paged++;
} while( $request_url );
WP_CLI::success( 'All done' );