-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaypal-donations.php
363 lines (333 loc) · 11.2 KB
/
paypal-donations.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
<?php
/*
Plugin Name: PayPal Donations
Plugin URI: https://www.tipsandtricks-hq.com/paypal-donations-widgets-plugin
Description: Easy and simple setup and insertion of PayPal donate buttons with a shortcode or through a sidebar Widget. Donation purpose can be set for each button. A few other customization options are available as well.
Author: Tips and Tricks HQ, Johan Steen
Author URI: https://www.tipsandtricks-hq.com/
Version: 1.9.3
License: GPLv2 or later
Text Domain: paypal-donations
*/
include_once('paypal_utility.php');
/** Load all of the necessary class files for the plugin */
spl_autoload_register('PayPalDonations::autoload');
/**
* Init Singleton Class for PayPal Donations.
*
* @package PayPal Donations
* @author Johan Steen <artstorm at gmail dot com>
*/
class PayPalDonations
{
/** Holds the plugin instance */
private static $instance = false;
/** Define plugin constants */
const MIN_PHP_VERSION = '5.2.4';
const MIN_WP_VERSION = '3.0';
const OPTION_DB_KEY = 'paypal_donations_options';
const TEXT_DOMAIN = 'paypal-donations';
const FILE = __FILE__;
// -------------------------------------------------------------------------
// Define constant data arrays
// -------------------------------------------------------------------------
private $donate_buttons = array(
'small' => 'https://www.paypal.com/en_US/i/btn/btn_donate_SM.gif',
'large' => 'https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif',
'cards' => 'https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif'
);
private $currency_codes = array(
'AUD' => 'Australian Dollars (A $)',
'BRL' => 'Brazilian Real',
'CAD' => 'Canadian Dollars (C $)',
'CZK' => 'Czech Koruna',
'DKK' => 'Danish Krone',
'EUR' => 'Euros (€)',
'HKD' => 'Hong Kong Dollar ($)',
'HUF' => 'Hungarian Forint',
'ILS' => 'Israeli New Shekel',
'JPY' => 'Yen (¥)',
'MYR' => 'Malaysian Ringgit',
'MXN' => 'Mexican Peso',
'NOK' => 'Norwegian Krone',
'NZD' => 'New Zealand Dollar ($)',
'PHP' => 'Philippine Peso',
'PLN' => 'Polish Zloty',
'GBP' => 'Pounds Sterling (£)',
'RUB' => 'Russian Ruble',
'SGD' => 'Singapore Dollar ($)',
'SEK' => 'Swedish Krona',
'CHF' => 'Swiss Franc',
'TWD' => 'Taiwan New Dollar',
'THB' => 'Thai Baht',
'TRY' => 'Turkish Lira',
'USD' => 'U.S. Dollars ($)',
);
private $localized_buttons = array(
'en_AU' => 'Australia - Australian English',
'de_DE/AT' => 'Austria - German',
'nl_NL/BE' => 'Belgium - Dutch',
'fr_XC' => 'Canada - French',
'zh_XC' => 'China - Simplified Chinese',
'fr_FR/FR' => 'France - French',
'de_DE/DE' => 'Germany - German',
'it_IT/IT' => 'Italy - Italian',
'ja_JP/JP' => 'Japan - Japanese',
'es_XC' => 'Mexico - Spanish',
'nl_NL/NL' => 'Netherlands - Dutch',
'pl_PL/PL' => 'Poland - Polish',
'es_ES/ES' => 'Spain - Spanish',
'de_DE/CH' => 'Switzerland - German',
'fr_FR/CH' => 'Switzerland - French',
'en_US' => 'United States - U.S. English'
);
private $checkout_languages = array(
'AU' => 'Australia',
'AT' => 'Austria',
'BE' => 'Belgium',
'BR' => 'Brazil',
'CA' => 'Canada',
'CN' => 'China',
'FR' => 'France',
'DE' => 'Germany',
'IT' => 'Italy',
'NL' => 'Netherlands',
'PL' => 'Poland',
'PR' => 'Portugal',
'RU' => 'Russia',
'ES' => 'Spain',
'SE' => 'Sweden',
'CH' => 'Switzerland',
'GB' => 'United Kingdom',
'US' => 'United States',
);
/**
* Singleton class
*/
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Initializes the plugin by setting localization, filters, and
* administration functions.
*/
private function __construct()
{
if (!$this->testHost()) {
return;
}
add_action('init', array($this, 'textDomain'));
register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
$admin = new PayPalDonations_Admin();
$admin->setOptions(
get_option(self::OPTION_DB_KEY),
$this->currency_codes,
$this->donate_buttons,
$this->localized_buttons,
$this->checkout_languages
);
add_filter('widget_text', 'do_shortcode');
add_shortcode('paypal-donation', array(&$this,'paypalShortcode'));
add_action('wp_head', array($this, 'addCss'), 999);
add_action(
'widgets_init',
create_function('', 'register_widget("PayPalDonations_Widget");')
);
}
/**
* PSR-0 compliant autoloader to load classes as needed.
*
* @param string $classname The name of the class
* @return null Return early if the class name does not start with the
* correct prefix
*/
public static function autoload($className)
{
if (__CLASS__ !== mb_substr($className, 0, strlen(__CLASS__))) {
return;
}
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
$fileName .= DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, 'src_'.$className);
$fileName .='.php';
require $fileName;
}
/**
* Loads the plugin text domain for translation
*/
public function textDomain()
{
$domain = self::TEXT_DOMAIN;
$locale = apply_filters('plugin_locale', get_locale(), $domain);
load_textdomain(
$domain,
WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo'
);
load_plugin_textdomain(
$domain,
false,
dirname(plugin_basename(__FILE__)).'/lang/'
);
}
/**
* Fired when the plugin is uninstalled.
*/
public function uninstall()
{
delete_option('paypal_donations_options');
delete_option('widget_paypal_donations');
}
/**
* Adds inline CSS code to the head section of the html pages to center the
* PayPal button.
*/
public function addCss()
{
$opts = get_option(self::OPTION_DB_KEY);
if (isset($opts['center_button']) and $opts['center_button'] == true) {
echo '<style type="text/css">'."\n";
echo '.paypal-donations { text-align: center !important }'."\n";
echo '</style>'."\n";
}
}
/**
* Create and register the PayPal shortcode
*/
public function paypalShortcode($atts)
{
extract(
shortcode_atts(
array(
'purpose' => '',
'reference' => '',
'amount' => '',
'show_amount' => '',
'return_page' => '',
'button_url' => '',
'button_text' => '',
'validate_ipn' => '',
),
$atts
)
);
return $this->generateHtml(
$purpose,
$reference,
$amount,
$show_amount,
$return_page,
$button_url,
$button_text,
$validate_ipn
);
}
/**
* Generate the PayPal button HTML code
*/
public function generateHtml(
$purpose = null,
$reference = null,
$amount = null,
$show_amount = null,
$return_page = null,
$button_url = null,
$button_text = null,
$validate_ipn = ''
) {
$pd_options = get_option(self::OPTION_DB_KEY);
// Set overrides for purpose and reference if defined
$purpose = (!$purpose) ? $pd_options['purpose'] : $purpose;
$reference = (!$reference) ? $pd_options['reference'] : $reference;
$amount = (!$amount) ? $pd_options['amount'] : $amount;
$show_amount = (!$show_amount) ? $pd_options['show_amount'] : $show_amount;
$return_page = (!$return_page) ? $pd_options['return_page'] : $return_page;
$button_url = (!$button_url) ? $pd_options['button_url'] : $button_url;
$button_text = (!$button_text) ? $pd_options['button_text'] : $button_text;
$data = array(
'pd_options' => $pd_options,
'return_page' => $return_page,
'purpose' => $purpose,
'reference' => $reference,
'amount' => $amount,
'show_amount' => $show_amount,
'button_url' => $button_url,
'button_text' => $button_text,
'donate_buttons' => $this->donate_buttons,
'validate_ipn' => $validate_ipn,
);
return PayPalDonations_View::render('paypal-button', $data);
}
// -------------------------------------------------------------------------
// Environment Checks
// -------------------------------------------------------------------------
/**
* Checks PHP and WordPress versions.
*/
private function testHost()
{
// Check if PHP is too old
if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) {
// Display notice
add_action('admin_notices', array(&$this, 'phpVersionError'));
return false;
}
// Check if WordPress is too old
global $wp_version;
if (version_compare($wp_version, self::MIN_WP_VERSION, '<')) {
add_action('admin_notices', array(&$this, 'wpVersionError'));
return false;
}
return true;
}
/**
* Displays a warning when installed on an old PHP version.
*/
public function phpVersionError()
{
echo '<div class="error"><p><strong>';
printf(
'Error: %3$s requires PHP version %1$s or greater.<br/>'.
'Your installed PHP version: %2$s',
self::MIN_PHP_VERSION,
PHP_VERSION,
$this->getPluginName()
);
echo '</strong></p></div>';
}
/**
* Displays a warning when installed in an old Wordpress version.
*/
public function wpVersionError()
{
echo '<div class="error"><p><strong>';
printf(
'Error: %2$s requires WordPress version %1$s or greater.',
self::MIN_WP_VERSION,
$this->getPluginName()
);
echo '</strong></p></div>';
}
/**
* Get the name of this plugin.
*
* @return string The plugin name.
*/
private function getPluginName()
{
$data = get_plugin_data(self::FILE);
return $data['Name'];
}
}
add_action('plugins_loaded', array('PayPalDonations', 'getInstance'));