Skip to content

Instantly share code, notes, and snippets.

@Apina
Forked from danielbachhuber/plugin-stats.php
Created October 29, 2018 17:58
Show Gist options
  • Save Apina/05dee04f0e86bc1e9e5f8f32238f0fc7 to your computer and use it in GitHub Desktop.
Save Apina/05dee04f0e86bc1e9e5f8f32238f0fc7 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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment