forked from benbalter/wordpress-plugin-sniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.plugin-sniffer.php
143 lines (106 loc) · 3.33 KB
/
class.plugin-sniffer.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* WordPress Plugin Sniffer
*
* @author Benjamin J. Balter <[email protected]>
* @package
*/
class WP_Plugin_Sniffer {
public $api = 'http://api.wordpress.org/plugins/info/1.0/';
public $per_page = 50;
public $count = 100;
public $timeout = 30;
public $found = array();
public $plugins = array();
public $home = null;
public $dir = 'wp-content/plugins/';
public $ttl = 86400;
public $path_to_wp = '../3.3.1/';
public $num_found = 0;
/**
* Bookstrap WP
*/
function __construct( $home = null, $count = null ) {
//bootstrap
include $this->path_to_wp . 'wp-load.php';
include $this->path_to_wp . 'wp-admin/includes/plugin-install.php';
require_once( 'tlc-transients/tlc-transients.php' );
set_time_limit( 0 );
if ( $home != null )
$this->home = esc_url( $home );
if ( $count != null )
$this->count = (int) $count;
add_filter( 'http_request_timeout', array( &$this, 'timeout_filter' ) );
}
/**
* WP API server is slow to query lots of plugins, give it more time
*/
function timeout_filter() {
return $this->timeout;
}
/**
* Grab a specific page of results
* Note: > 100, things get a bit iffy on WP's side
* @param int $num the page number to retrieve
* @retun array an array of plugin slugs
*/
function fetch_page( $page = 1 ) {
$plugins = plugins_api( 'query_plugins', array(
'browse' => 'popular',
'per_page' => $this->per_page,
'fields' => array(),
'page' => $page,
)
);
if ( is_wp_error( $plugins ) )
wp_die( "Can't retrieve plugin list" );
return wp_list_pluck( $plugins->plugins, 'slug' );
}
/**
* Get the top N plugin slugs
* Note: we fetch full pages of 100 and
* then slice down the array as necessary
* to allow for cleaner caching
* @param int $num the number of plugins to retrieve
* @return array an array of the top $num plugin slugs
*/
function get_plugins( $num = null ) {
if ( $num == null )
$num = $this->count;
$num = (int) $num;
$page = 1;
$pages = (int) ( ceil( $num / $this->per_page ) );
$plugins = array();
for( $page = 1; $page <= $pages; $page++ ) {
$this_page = tlc_transient( 'plugin_sniffer_' . $this->per_page . 'x' . $page )
->updates_with( array( &$this, 'fetch_page' ), array( $page ) )
->expires_in( $this->ttl )
->get();
$plugins = array_merge( $plugins, $this_page );
}
$this->plugins = array_slice( $plugins, 0, $num );
return $this->plugins;
}
/**
* Check a given site
* @param string $home the home URL of the site
* @param int $num number of plugins to check
*/
function sniff( $home = null, $num = null ) {
if ( $home == null )
$home = $this->home;
$this->home = trailingslashit( esc_url( $home ) );
$this->found = array();
$plugins = $this->get_plugins( $num );
foreach ( $plugins as $plugin ) {
$response = wp_remote_head( trailingslashit( $this->home . $this->dir . $plugin ), array( 'timeout' => $this->timeout ) );
//either the directory exists, in which case we'd get either a 403 (denied) or 200 (a listing)
//or it doesn't exist, in which case the plugin doesn't exist
if ( wp_remote_retrieve_response_code( $response ) == 404 )
continue;
$this->found[] = $plugin;
}
$this->num_found = count( $this->found );
return $this->found;
}
}