-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-org-plugins.php
265 lines (227 loc) · 6.05 KB
/
class-wp-org-plugins.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
/**
* @copyright (C) Copyright Bobbing Wide 2019-2023
* @package wp-top12
*
*/
class WP_org_plugins {
private $plugins; // Loaded file_or_url
private $limit; // Maximum rows to return ( not incl. header )
private $file_or_url; // Source of information
private $includes; // CSV string of words to search for
private $excludes; // CSV string of words to exclude
private $matches; // CSV string or words to match exactly with a plugin name
private $included;
private $excluded;
private $total_downloads;
private $both;
function __construct() {
$this->file_or_url = 'wporg_plugins.csv';
$this->limit = 12;
$this->includes = null;
$this->excludes = null;
$this->matches = null;
$this->both = false;
$this->total_downloads = 0;
//$this->plugin_info_v2( "oik" );
}
function atts( $atts ) {
$matches = bw_array_get_from( $atts, 'matches,0', null );
$this->limit = bw_array_get_from( $atts, 'limit,0', 12 );
$this->matches = [];
if ( null !== $matches ) {
if ( is_numeric( $matches ) ) {
//$this->limit = $matches;
$matches = null;
} else {
$this->matches = bw_as_array( $matches );
if ( is_numeric( $this->limit ) ) {
// go with that
} else {
$this->limit = count( $this->matches );
}
}
}
$includes = bw_array_get( $atts, 'includes', null );
$this->includes = bw_as_array( $includes );
$excludes = bw_array_get( $atts, 'excludes', null );
$this->excludes = bw_as_array( $excludes );
if ( null === $includes && null === $excludes && null === $matches && null === $this->limit ) {
}
$this->limit = min( $this->limit, 1000 );
$both = bw_array_get( $atts, 'both', 'N' );
$this->both = bw_validate_torf( $both );
}
function full_file_or_url() {
$file = oik_path( $this->file_or_url, 'wp-top12');
if ( !file_exists( $file ))
echo "You need to get the file";
return $file;
}
function load_plugins() {
$this->plugins = file( $this->full_file_or_url() );
//unset( $this->plugins[0] );
//array_shift( $this->plugins );
}
function count_plugins() {
$count = count( $this->plugins );
$count--;
return $count;
}
function plugin_info_v2( ) {
$this->load_plugins();
$total = $this->count_plugins();
p( "Total plugins: " . $total );
$this->filter();
$this->report();
if ( $this->both ) {
$this->included = $this->excluded;
$this->report();
}
}
function filter() {
$this->included = [];
$this->excluded = [];
$count = 0;
foreach ( $this->plugins as $key => $info ) {
if ( $key === 0 ) {
continue;
}
if ( $this->match( $key, $info )) {
$this->included[] = $this->build_include_or_exclude( $key, $info );
$count++;
} elseif ( $this->include( $key, $info ) ) {
if ( $this->exclude( $key, $info ) ) {
$this->excluded[] = $this->build_include_or_exclude( $key, $info );
} else {
$this->included[] = $this->build_include_or_exclude( $key, $info );
$count++;
}
}
if ( $count >= $this->limit ) {
e( 'Limit reached: ' . $count . ' at ' . $key );
break;
}
}
}
/**
* Find exact matches with the plugin name
*
*/
function match( $key, $info ) {
if ( 0 === count( $this->matches ) ) {
return false;
}
$matched = false;
foreach ( $this->matches as $plugin ) {
$pos = stripos( $info, $plugin . ',' );
if ( 0 === $pos ) {
$matched = true;
}
}
return $matched;
}
function include( $key, $info ) {
//bw_trace2();
if ( 0 === count( $this->includes ) && 0 === count( $this->matches ) ) {
return true;
}
$included = false;
foreach ( $this->includes as $plugin ) {
//$pos = stripos( $info, $plugin );
$pos = $this->check_word_match( $info, $plugin);
if ( false !== $pos ) {
$included = true;
}
}
return $included;
}
function check_word_match( $haystack, $needle ) {
$matched = false;
$haystack = str_replace( ',', ' ', $haystack);
$words = explode( ' ', $haystack);
foreach ( $words as $word ) {
if ( 0 === strcasecmp( $word, $needle)) {
$matched = true;
}
}
return $matched;
}
function exclude( $key, $info ) {
if ( 0 === count( $this->excludes ) ) {
return false;
}
$excluded = false;
foreach ( $this->excludes as $plugin ) {
//$pos = stripos( $info, $plugin );
$pos = $this->check_word_match( $info, $plugin);
if ( false !== $pos ) {
$excluded = true;
}
}
return $excluded;
}
function build_include_or_exclude( $key, $info ) {
$included = $key;
$included .= ',';
$info = str_replace( " ", ' ', $info );
$included .= $info;
return $included;
}
/**
* Report the headings for the table
* These may need moving around
*/
function report_header() {
$th = [ "Position","Plugin","Total downloads","Active","Star Rating","Tested up to" ];
bw_tablerow( $th, "tr", "th" );
}
function report() {
stag( "table", "wp-top12" );
$this->report_header();
//bw_tablerow( "Posn,Plugin,");
foreach ( $this->included as $key => $included ) {
$plugin = bw_as_array( $included );
$row = [];
$row[] = $plugin[0];
$row[] = $this->plugin_link( $plugin );
$row[] = $this->plugin_total_downloads( $plugin );
$row[] = $this->plugin_active( $plugin );
$row[] = $this->plugin_rating( $plugin );
$row[] = $this->plugin_tested_up_to( $plugin );
bw_tablerow( $row );
}
bw_tablerow( [ count( $this->included ), "TOTALS", $this->total_downloads ]);
etag( "table");
}
/** Mapping of key,info to fields *
* index | contains
* 0 | key = position in table
* 1 | Slug
* 2 | Name
* 3 | Rating
* 4 | Downloaded
* 5 | Installed = active
* 6 | Tested
* 7 | Requires
* 8 | LastUpdate
*/
function plugin_link( $plugin ) {
$url = 'https://wordpress.org/plugins/';
$url .= $plugin[1];
return retlink( null, $url, str_replace( ' ', ' ', $plugin[2] ));
}
function plugin_total_downloads( $plugin ) {
$this->total_downloads += $plugin[4];
return $plugin[4];
}
function plugin_active( $plugin ) {
return $plugin[5];
}
function plugin_rating( $plugin ) {
return $plugin[3];
}
function plugin_tested_up_to( $plugin ) {
return $plugin[6];
}
}