-
Notifications
You must be signed in to change notification settings - Fork 1
/
pastebin.php
401 lines (337 loc) · 9.18 KB
/
pastebin.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
<?php
/**
* @package Awesome Support Pastebin
* @author ThemeAvenue <[email protected]>
* @license GPL-2.0+
* @link http://themeavenue.net
* @copyright 2015 ThemeAvenue
*
* @boilerplate-version 0.1.3
*
* Plugin Name: Awesome Support: Pastebin
* Plugin URI: http://getawesomesupport.com/addons/?utm_source=internal&utm_medium=plugin_meta&utm_campaign=Addons_Boilerplate
* Description: Use Pastebin to share code snippets from any ticket.
* Version: 1.0
* Author: ThemeAvenue
* Author URI: http://themeavenue.net/?utm_source=internal&utm_medium=plugin_meta&utm_campaign=Addons_Boilerplate
* Text Domain: as-pastebin
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/*----------------------------------------------------------------------------*
* Instantiate the plugin
*----------------------------------------------------------------------------*/
/**
* Register the activation hook
*/
register_activation_hook( __FILE__, array( 'AS_Pastebin_Loader', 'activate' ) );
add_action( 'plugins_loaded', array( 'AS_Pastebin_Loader', 'get_instance' ) );
/**
* Instanciate the addon.
*
* This method runs a few checks to make sure that the addon
* can indeed be used in the current context, after what it
* registers the addon to the core plugin for it to be loaded
* when the entire core is ready.
*
* @since 0.1.0
* @return void
*/
class AS_Pastebin_Loader {
/**
* ID of the item.
*
* The item ID must match teh post ID on the e-commerce site.
* Using the item ID instead of its name has the huge advantage of
* allowing changes in the item name.
*
* If the ID is not set the class will fall back on the plugin name instead.
*
* @since 0.1.3
* @var int
*/
protected $item_id;
/**
* Required version of the core.
*
* The minimum version of the core that's required
* to properly run this addon. If the minimum version
* requirement isn't met an error message is displayed
* and the addon isn't registered.
*
* @since 0.1.0
* @var string
*/
protected $version_required = '3.1.6';
/**
* Required version of PHP.
*
* Follow WordPress latest requirements and require
* PHP version 5.4 at least.
*
* @var string
*/
protected $php_version_required = '5.4';
/**
* Plugin slug.
*
* @since 0.1.0
* @var string
*/
protected $slug = 'pastebin';
/**
* Instance of this loader class.
*
* @since 0.1.0
* @var object
*/
protected static $instance = null;
/**
* Instance of the addon itself.
*
* @since 0.1.0
* @var object
*/
public $addon = null;
/**
* Possible error message.
*
* @var null|WP_Error
*/
protected $error = null;
public function __construct() {
$this->declare_constants();
$this->init();
}
/**
* Return an instance of this class.
*
* @since 3.0.0
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Return an instance of the addon.
*
* @since 0.1.0
* @return object
*/
public function scope() {
return $this->addon;
}
public function declare_constants() {
define( 'AS_PASTEBIN_VERSION', '0.1.0' );
define( 'AS_PASTEBIN_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
define( 'AS_PASTEBIN_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
}
/**
* Activate the plugin.
*
* The activation method just checks if the main plugin
* Awesome Support is installed (active or inactive) on the site.
* If not, the addon installation is aborted and an error message is displayed.
*
* @since 0.1.0
* @return void
*/
public static function activate() {
if ( ! class_exists( 'Awesome_Support' ) ) {
deactivate_plugins( basename( __FILE__ ) );
wp_die(
sprintf( __( 'You need Awesome Support to activate this addon. Please <a href="%s" target="_blank">install Awesome Support</a> before continuing.', 'as-pastebin' ), esc_url( 'http://getawesomesupport.com/?utm_source=internal&utm_medium=addon_loader&utm_campaign=Pastebin' ) )
);
}
}
/**
* Initialize the addon.
*
* This method is the one running the checks and
* registering the addon to the core.
*
* @since 0.1.0
* @return boolean Whether or not the addon was registered
*/
public function init() {
$plugin_name = $this->plugin_data( 'Name' );
if ( ! $this->is_core_active() ) {
$this->add_error( sprintf( __( '%s requires Awesome Support to be active. Please activate the core plugin first.', 'as-pastebin' ), $plugin_name ) );
}
if ( ! $this->is_php_version_enough() ) {
$this->add_error( sprintf( __( 'Unfortunately, %s can not run on PHP versions older than %s. Read more information about <a href="%s" target="_blank">how you can update</a>.', 'as-pastebin' ), $plugin_name, $this->php_version_required, esc_url( 'http://www.wpupdatephp.com/update/' ) ) );
}
if ( ! $this->is_version_compatible() ) {
$this->add_error( sprintf( __( '%s requires Awesome Support version %s or greater. Please update the core plugin first.', 'as-pastebin' ), $plugin_name, $this->version_required ) );
}
if ( is_a( $this->error, 'WP_Error' ) ) {
add_action( 'admin_notices', array( $this, 'display_error' ), 10, 0 );
add_action( 'admin_init', array( $this, 'deactivate' ), 10, 0 );
return false;
}
/**
* Register the addon
*/
wpas_register_addon( $this->slug, array( $this, 'load' ) );
return true;
}
/**
* Get the plugin data.
*
* @since 0.1.0
* @param string $data Plugin data to retrieve
* @return string Data value
*/
protected function plugin_data( $data ) {
if ( ! function_exists( 'get_plugin_data' ) ) {
$admin_path = str_replace( get_site_url() . '/', ABSPATH, get_admin_url() );
require_once( $admin_path . 'includes/plugin.php' );
}
$plugin = get_plugin_data( __FILE__, false, false );
if ( array_key_exists( $data, $plugin ) ){
return $plugin[$data];
} else {
return '';
}
}
/**
* Check if core is active.
*
* Checks if the core plugin is listed in the acitve
* plugins in the WordPress database.
*
* @since 0.1.0
* @return boolean Whether or not the core is active
*/
protected function is_core_active() {
if ( in_array( 'awesome-support/awesome-support.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
return true;
} else {
return false;
}
}
/**
* Check if the core version is compatible with this addon.
*
* @since 0.1.0
* @return boolean
*/
protected function is_version_compatible() {
/**
* Return true if the core is not active so that this message won't show.
* We already have the error saying the plugin is disabled, no need to add this one.
*/
if ( ! $this->is_core_active() ) {
return true;
}
if ( empty( $this->version_required ) ) {
return true;
}
if ( ! defined( 'WPAS_VERSION' ) ) {
return false;
}
if ( version_compare( WPAS_VERSION, $this->version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Check if the version of PHP is compatible with this addon.
*
* @since 0.1.0
* @return boolean
*/
protected function is_php_version_enough() {
/**
* No version set, we assume everything is fine.
*/
if ( empty( $this->php_version_required ) ) {
return true;
}
if ( version_compare( phpversion(), $this->php_version_required, '<' ) ) {
return false;
}
return true;
}
/**
* Add error.
*
* Add a new error to the WP_Error object
* and create the object if it doesn't exist yet.
*
* @since 0.1.0
* @param string $message Error message to add
* @return void
*/
public function add_error( $message ) {
if ( ! is_object( $this->error ) || ! is_a( $this->error, 'WP_Error' ) ) {
$this->error = new WP_Error();
}
$this->error->add( 'addon_error', $message );
}
/**
* Display error.
*
* Get all the error messages and display them
* in the admin notices.
*
* @since 0.1.0
* @return void
*/
public function display_error() {
if ( ! is_a( $this->error, 'WP_Error' ) ) {
return;
}
$message = $this->error->get_error_messages(); ?>
<div class="error">
<p>
<?php
if ( count( $message ) > 1 ) {
echo '<ul>';
foreach ( $message as $msg ) {
echo "<li>$msg</li>";
}
echo '</li>';
} else {
echo $message[0];
}
?>
</p>
</div>
<?php
}
/**
* Deactivate the addon.
*
* If the requirements aren't met we try to
* deactivate the addon completely.
*
* @return void
*/
public function deactivate() {
if ( function_exists( 'deactivate_plugins' ) ) {
deactivate_plugins( basename( __FILE__ ) );
}
}
/**
* Load the addon.
*
* Include all necessary files and instanciate the addon.
*
* @since 0.1.0
* @return void
*/
public function load() {
require( AS_PASTEBIN_PATH . 'includes/settings.php' );
require( AS_PASTEBIN_PATH . 'includes/functions-pastebin.php' );
}
}