-
Notifications
You must be signed in to change notification settings - Fork 0
/
wporg-stats.php
339 lines (267 loc) · 8.38 KB
/
wporg-stats.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* Plugin Name: WPOrg Stats
* Plugin URI: https://github.com/digitalchild/wporg-stats
* Description: Display WordPress.org plugin or theme information on your site.
* Author: Jamie Madden (https://github.com/digitalchild)
* Author URI: https://github.com/digitalchild
* GitHub Plugin URI: https://github.com/digitalchild/wporg-stats
*
* Version: 1.0.0
* Requires at least: 4.4.0
* Tested up to: 4.9.1
*
* Text Domain: wporgstats
* Domain Path: /languages/
*
* @category Plugin
* @copyright Copyright © 2017 Jamie Madden
* @author Jamie Madden
* @package WPOrg_Stats
* @license GPL2
WPOrg Stats is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
WPOrg Statss is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with WPOrg Stats. If not, see http://www.gnu.org/licenses/gpl-2.0.txt.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
final class WPOrg_Stats {
/**
* WPOrg Stats version.
*
* @var string
*/
public $version = '1.0.0';
/**
* The single instance of the class.
*
* @var WPOrg_Stat
* @since 2.0
*/
protected static $instance = null;
/**
* @var WC_Logger Reference to logging class.
*/
private static $log;
/**
* @var bool Enable debug logging.
*/
public static $enable_logging;
/**
* Main WPOrg Stats Instance.
*
* Ensures only one instance of WPOrg Stats is loaded or can be loaded.
*
* @since 2.0
* @static
* @return WPOrg Stats - Main instance.
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* WPOrg Stats Constructor.
*/
public function __construct() {
$this->define_constants();
$this->init_hooks();
self::$enable_logging = apply_filters( 'wpps_enable_logging', true );
do_action( 'wpps_loaded' );
}
/**
* Cloning is forbidden.
* @since 2.0
*/
public function __clone() {
self::log( __( 'No cloning allowed', 'wporgstats' ) );
}
/**
* Unserializing instances of this class is forbidden.
* @since 2.0
*/
public function __wakeup() {
self::log( __( 'No wakeup allowed', 'wporgstats' ) );
}
/**
* Hook into actions and filters.
* @since 2.0
*/
private function init_hooks() {
add_shortcode( 'wpps_show_plugin_info', array( $this, 'wpps_show_plugin_info' ) );
add_shortcode( 'wpps_show_theme_info', array( $this, 'wpps_show_theme_info' ) );
}
/**
* Define WC Constants.
*/
private function define_constants() {
$this->define( 'WPPS_PLUGIN_FILE', __FILE__ );
$this->define( 'WPPS_ABSPATH', dirname( __FILE__ ) . '/' );
$this->define( 'WPPS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
$this->define( 'WPPS_VERSION', $this->version );
}
/**
* Define constant if not already set.
*
* @param string $name
* @param string|bool $value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Get the plugin url.
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Get the template path.
* @return string
*/
public function template_path() {
return apply_filters( 'wpps_template_path', 'wporg-stats/' );
}
/*
* Show plugin stats from wordpress.org
*/
public function wpps_show_plugin_info( $atts ){
extract( shortcode_atts( array(
'slug' => '',
'info' => 'downloaded'
), $atts ) );
// No slug provided, bail
if ( ! $slug ) return;
$stats = $this->get_wp_plugin_info( $slug );
if ( is_numeric( $stats->$info ) ){
return number_format( $stats->$info );
} else {
return $stats->$info;
}
}
/*
* Show theme stats from wordpress.org
*/
public function wpps_show_theme_info( $atts ){
extract( shortcode_atts( array(
'slug' => '',
'info' => 'downloaded'
), $atts ) );
// No slug provided, bail
if ( ! $slug ) return;
$stats = $this->get_wp_theme_info( $slug );
if ( $all ){
return print_r( $stats );
}
if ( is_numeric( $stats->$info ) ){
return number_format( $stats->$info );
} else {
return $stats->$info;
}
}
/**
* Get the plugin information from wp.org
* Cache the results to ensure fast access
*
*/
public function get_wp_plugin_info( $slug ){
// $url = 'https://api.wordpress.org/plugins/info/1.0/' . $slug . '.json';
$url = 'https://wordpress.org/plugins/wp-json/plugins/v1/plugin/'. $slug;
// Get any existing copy of our transient data
if ( false === ( $wp_org_response = get_transient( 'wpps_plugin_' . $slug . '_results' ) ) ) {
$wp_org_response = wp_remote_request( $url, array( 'method' => 'GET' ) );
if ( is_wp_error( $wp_org_response ) ) {
return $wp_org_response->get_error_message();
}
set_transient( 'wpps_plugin_' . $slug . '_results', $wp_org_response, 12 * HOUR_IN_SECONDS );
}
return json_decode( wp_remote_retrieve_body( $wp_org_response ) );
}
/**
* Get theme information from wp.org
* Cache the results to ensure fast access.
*/
public function get_wp_theme_info( $slug ){
$url = 'https://api.wordpress.org/themes/info/1.1/?action=theme_information&request[slug]=' . $slug;
// Get any existing copy of our transient data
if ( false === ( $wp_org_response = get_transient( 'wpps_theme_' . $slug . '_results' ) ) ) {
$wp_org_response = wp_remote_request( $url, array( 'method' => 'GET' ) );
if ( is_wp_error( $wp_org_response ) ) {
return $wp_org_response->get_error_message();
}
set_transient( 'wpps_theme_' . $slug . '_results', $wp_org_response, 12 * HOUR_IN_SECONDS );
}
self::log( json_decode( wp_remote_retrieve_body( $wp_org_response ) ) );
return json_decode( wp_remote_retrieve_body( $wp_org_response ) );
}
/**
* Load Localisation files.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Locales found in:
* - WP_LANG_DIR/wporg-stats/wporg-stats-LOCALE.mo
* - WP_LANG_DIR/plugins/wporg-stats-LOCALE.mo
*/
public function load_plugin_textdomain() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'wporgstats' );
load_textdomain( 'wporgstats', WP_LANG_DIR . '/wporg-stats/wporgstats-' . $locale . '.mo' );
load_plugin_textdomain( 'wporgstats', false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Class logger so that we can keep our debug and logging information cleaner
*
* @since 1.0.0
* @access public
*
* @param mixed - $data the data to go to the error log could be string, array or object
*/
public function log( $data = '', $pre = '' ){
$trace = debug_backtrace( false, 2 );
$caller = ( isset( $trace[ 1 ] ) ) ? array_key_exists( 'class', $trace[ 1 ] ) ? $trace[ 1 ][ 'class' ] : '' : '';
if ( self::$enable_logging ){
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
if ( is_array( $data ) || is_object( $data ) ) {
// Output to the error log
error_log( '=================== ' . $pre .' : ' . $caller . ' ======================' );
error_log( $caller . ' : ' . print_r( $data, true ) );
error_log( '===============================================================');
} else {
// Output to debugging log
error_log( '=================== ' . $pre .' : ' . $caller . ' ======================' );
error_log( $caller . ' : ' . $data );
error_log( '===============================================================');
}
}
}
}
} // End final class
/**
* Main instance of WPOrg Stats
*
*/
function WPOrg_Stats(){
return WPOrg_Stats::instance();
}
add_action( 'plugins_loaded', 'WPOrg_Stats' );