Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* After creating the necessary table, run with `wp eval-file plugin-stats.php`
*/
use WP_CLI\Utils;
global $wpdb;
$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 );
$wpdb->query( $wpdb->prepare( 'INSERT INTO plugins (name, active_installs, last_updated) VALUES(%s, %s, %s) ON DUPLICATE KEY UPDATE active_installs=%s, last_updated=%s', $plugin_name, $plugin['active_installs'], $plugin['last_updated'], $plugin['active_installs'], $plugin['last_updated'] ) );
}
} else {
$request_url = false;
}
$paged++;
} while( $request_url );
WP_CLI::success( 'All done' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment