Skip to content

Instantly share code, notes, and snippets.

@danielbachhuber
Created January 5, 2018 00:37
Show Gist options
  • Save danielbachhuber/eb918f1a9238761e6bc9ad994426eb51 to your computer and use it in GitHub Desktop.
Save danielbachhuber/eb918f1a9238761e6bc9ad994426eb51 to your computer and use it in GitHub Desktop.
<?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' );
@kadimi
Copy link

kadimi commented Feb 5, 2019

Hi

Is there a way to get plugins sorted by popularity?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment