forked from envato/wp-envato-market
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenvato-market.php
508 lines (462 loc) · 10.3 KB
/
envato-market.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/**
* Plugin Name: Envato Market
* Plugin URI: http://envato.github.io/wp-envato-market/
* Description: WordPress Theme & Plugin management for the Envato Market.
* Version: 1.0.0-RC2
* Author: Derek Herman
* Author URI: https://valendesigns.com/
* Requires at least: 4.2
* Tested up to: 4.4
* Text Domain: envato-market
* Domain Path: /languages/
*
* @package Envato_Market
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'Envato_Market' ) ) :
/**
* It's the main class that does all the things.
*
* @class Envato_Market
* @version 1.0.0
* @since 1.0.0
*/
final class Envato_Market {
/**
* The single class instance.
*
* @since 1.0.0
* @access private
*
* @var object
*/
private static $_instance = null;
/**
* Plugin data.
*
* @since 1.0.0
* @access private
*
* @var object
*/
private $data;
/**
* The slug.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $slug;
/**
* The version number.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $version;
/**
* The web URL to the plugin directory.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $plugin_url;
/**
* The server path to the plugin directory.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $plugin_path;
/**
* The web URL to the plugin admin page.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $page_url;
/**
* The setting option name.
*
* @since 1.0.0
* @access private
*
* @var string
*/
private $option_name;
/**
* Main Envato_Market Instance
*
* Ensures only one instance of this class exists in memory at any one time.
*
* @see Envato_Market()
* @uses Envato_Market::init_globals() Setup class globals.
* @uses Envato_Market::init_includes() Include required files.
* @uses Envato_Market::init_actions() Setup hooks and actions.
*
* @since 1.0.0
* @static
* @return object The one true Envato_Market.
* @codeCoverageIgnore
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
self::$_instance->init_globals();
self::$_instance->init_includes();
self::$_instance->init_actions();
}
return self::$_instance;
}
/**
* A dummy constructor to prevent this class from being loaded more than once.
*
* @see Envato_Market::instance()
*
* @since 1.0.0
* @access private
* @codeCoverageIgnore
*/
private function __construct() {
/* We do nothing here! */
}
/**
* You cannot clone this class.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'envato-market' ), '1.0.0' );
}
/**
* You cannot unserialize instances of this class.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'envato-market' ), '1.0.0' );
}
/**
* Setup the class globals.
*
* @since 1.0.0
* @access private
* @codeCoverageIgnore
*/
private function init_globals() {
$this->data = new stdClass();
$this->version = '1.0.0-RC2';
$this->slug = 'envato-market';
$this->option_name = self::sanitize_key( $this->slug );
$this->plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_path = plugin_dir_path( __FILE__ );
$this->page_url = admin_url( 'admin.php?page=' . $this->slug );
$this->data->admin = true;
// Set the current version for the Github updater to use.
if ( version_compare( get_option( 'envato_market_version' ), $this->version, '<' ) ) {
update_option( 'envato_market_version', $this->version );
}
}
/**
* Include required files.
*
* @since 1.0.0
* @access private
* @codeCoverageIgnore
*/
private function init_includes() {
require $this->plugin_path . '/inc/admin/admin.php';
require $this->plugin_path . '/inc/admin/functions.php';
require $this->plugin_path . '/inc/api.php';
require $this->plugin_path . '/inc/items.php';
require $this->plugin_path . '/inc/github.php';
}
/**
* Setup the hooks, actions and filters.
*
* @uses add_action() To add actions.
* @uses add_filter() To add filters.
*
* @since 1.0.0
* @access private
* @codeCoverageIgnore
*/
private function init_actions() {
// Activate plugin.
register_activation_hook( __FILE__, array( $this, 'activate' ) );
// Deactivate plugin.
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
// Load the textdomain.
add_action( 'init', array( $this, 'load_textdomain' ) );
// Load OAuth.
add_action( 'init', array( $this, 'admin' ) );
// Load Upgrader.
add_action( 'init', array( $this, 'items' ) );
}
/**
* Activate plugin.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function activate() {
self::set_plugin_state( true );
}
/**
* Deactivate plugin.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function deactivate() {
self::set_plugin_state( false );
}
/**
* Loads the plugin's translated strings.
*
* @since 1.0.0
* @codeCoverageIgnore
*/
public function load_textdomain() {
load_plugin_textdomain( 'envato-market', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Sanitize data key.
*
* @since 1.0.0
* @access private
*
* @param string $key An alpha numeric string to sanitize.
* @return string
*/
private function sanitize_key( $key ) {
return preg_replace( '/[^A-Za-z0-9\_]/i', '', str_replace( array( '-', ':' ), '_', $key ) );
}
/**
* Recursively converts data arrays to objects.
*
* @since 1.0.0
* @access private
*
* @param array $array An array of data.
* @return object
*/
private function convert_data( $array ) {
foreach ( (array) $array as $key => $value ) {
if ( is_array( $value ) ) {
$array[ $key ] = self::convert_data( $value );
}
}
return (object) $array;
}
/**
* Set the `is_plugin_active` option.
*
* This setting helps determine context. Since the plugin can be included in your theme root you
* might want to hide the admin UI when the plugin is not activated and implement your own.
*
* @since 1.0.0
* @access private
*
* @param bool $value Whether or not the plugin is active.
*/
private function set_plugin_state( $value ) {
self::set_option( 'is_plugin_active', $value );
}
/**
* Set option value.
*
* @since 1.0.0
* @access private
*
* @param string $name Option name.
* @param mixed $option Option data.
*/
private function set_option( $name, $option ) {
$options = self::get_options();
$name = self::sanitize_key( $name );
$options[ $name ] = esc_html( $option );
update_option( $this->option_name, $options );
}
/**
* Return the option settings array.
*
* @since 1.0.0
*/
public function get_options() {
return get_option( $this->option_name, array() );
}
/**
* Return a value from the option settings array.
*
* @since 1.0.0
*
* @param string $name Option name.
* @param mixed $default The default value if nothing is set.
* @return mixed
*/
public function get_option( $name, $default = '' ) {
$options = self::get_options();
$name = self::sanitize_key( $name );
return isset( $options[ $name ] ) ? $options[ $name ] : $default;
}
/**
* Set data.
*
* @since 1.0.0
*
* @param string $key Unique object key.
* @param mixed $data Any kind of data.
*/
public function set_data( $key, $data ) {
if ( ! empty( $key ) ) {
if ( is_array( $data ) ) {
$data = self::convert_data( $data );
}
$key = self::sanitize_key( $key );
$this->data->$key = $data;
}
}
/**
* Get data.
*
* @since 1.0.0
*
* @param string $key Unique object key.
* @return string|object
*/
public function get_data( $key ) {
return isset( $this->data->$key ) ? $this->data->$key : '';
}
/**
* Return the plugin slug.
*
* @since 1.0.0
*
* @return string
*/
public function get_slug() {
return $this->slug;
}
/**
* Return the plugin version number.
*
* @since 1.0.0
*
* @return string
*/
public function get_version() {
return $this->version;
}
/**
* Return the plugin URL.
*
* @since 1.0.0
*
* @return string
*/
public function get_plugin_url() {
return $this->plugin_url;
}
/**
* Return the plugin path.
*
* @since 1.0.0
*
* @return string
*/
public function get_plugin_path() {
return $this->plugin_path;
}
/**
* Return the plugin page URL.
*
* @since 1.0.0
*
* @return string
*/
public function get_page_url() {
return $this->page_url;
}
/**
* Return the option settings name.
*
* @since 1.0.0
*
* @return string
*/
public function get_option_name() {
return $this->option_name;
}
/**
* Admin UI class.
*
* @since 1.0.0
*
* @return Envato_Market_Admin
*/
public function admin() {
return Envato_Market_Admin::instance();
}
/**
* Envato API class.
*
* @since 1.0.0
*
* @return Envato_Market_API
*/
public function api() {
return Envato_Market_API::instance();
}
/**
* Items class.
*
* @since 1.0.0
*
* @return Envato_Market_Items
*/
public function items() {
return Envato_Market_Items::instance();
}
}
endif;
if ( ! function_exists( 'envato_market' ) ) :
/**
* The main function responsible for returning the one true
* Envato_Market Instance to functions everywhere.
*
* Use this function like you would a global variable, except
* without needing to declare the global.
*
* Example: <?php $envato_market = envato_market(); ?>
*
* @since 1.0.0
* @return Envato_Market The one true Envato_Market Instance
*/
function envato_market() {
return Envato_Market::instance();
}
endif;
/**
* Loads the main instance of Envato_Market to prevent
* the need to use globals.
*
* @since 1.0.0
* @return object Envato_Market
*/
add_action( 'after_setup_theme', 'envato_market', 11 );