PluginProbe
Donations via PayPal / trunk
Donations via PayPal vtrunk
trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.4.9.1 1.4.9.2 1.4.9.3 1.4.9.4 1.4.9.5 1.4.9.6 1.5 All 37 releases
paypal-donations / paypal-donations.php

paypal-donations.php in Donations via PayPal trunk, at paypal-donations.php

364 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Donations via PayPal
4 Plugin URI: https://www.tipsandtricks-hq.com/paypal-donations-widgets-plugin
5 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.
6 Author: Tips and Tricks HQ, Johan Steen
7 Author URI: https://www.tipsandtricks-hq.com/
8 Version: 1.9.11
9 License: GPLv2 or later
10 Text Domain: paypal-donations
11 */
12
13 include_once('paypal_utility.php');
14
15 /** Load all of the necessary class files for the plugin */
16 spl_autoload_register('PayPalDonations::autoload');
17
18 /**
19 * Init Singleton Class for PayPal Donations.
20 *
21 * @package PayPal Donations
22 * @author Johan Steen <artstorm at gmail dot com>
23 */
24 class PayPalDonations
25 {
26 /** Holds the plugin instance */
27 private static $instance = false;
28
29 /** Define plugin constants */
30 const MIN_PHP_VERSION = '5.2.4';
31 const MIN_WP_VERSION = '5.0';
32 const OPTION_DB_KEY = 'paypal_donations_options';
33 const TEXT_DOMAIN = 'paypal-donations';
34 const FILE = __FILE__;
35
36
37 // -------------------------------------------------------------------------
38 // Define constant data arrays
39 // -------------------------------------------------------------------------
40 private $donate_buttons = array(
41 'small' => 'https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif',
42 'large' => 'https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif',
43 'cards' => 'https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif'
44 );
45 private $currency_codes = array(
46 'AUD' => 'Australian Dollars (A $)',
47 'BRL' => 'Brazilian Real',
48 'CAD' => 'Canadian Dollars (C $)',
49 'CZK' => 'Czech Koruna',
50 'DKK' => 'Danish Krone',
51 'EUR' => 'Euros (&euro;)',
52 'HKD' => 'Hong Kong Dollar ($)',
53 'HUF' => 'Hungarian Forint',
54 'ILS' => 'Israeli New Shekel',
55 'JPY' => 'Yen (&yen;)',
56 'MYR' => 'Malaysian Ringgit',
57 'MXN' => 'Mexican Peso',
58 'NOK' => 'Norwegian Krone',
59 'NZD' => 'New Zealand Dollar ($)',
60 'PHP' => 'Philippine Peso',
61 'PLN' => 'Polish Zloty',
62 'GBP' => 'Pounds Sterling (&pound;)',
63 'RUB' => 'Russian Ruble',
64 'SGD' => 'Singapore Dollar ($)',
65 'SEK' => 'Swedish Krona',
66 'CHF' => 'Swiss Franc',
67 'TWD' => 'Taiwan New Dollar',
68 'THB' => 'Thai Baht',
69 'TRY' => 'Turkish Lira',
70 'USD' => 'U.S. Dollars ($)',
71 );
72 private $localized_buttons = array(
73 'en_AU' => 'Australia - Australian English',
74 'de_DE/AT' => 'Austria - German',
75 'nl_NL/BE' => 'Belgium - Dutch',
76 'fr_XC' => 'Canada - French',
77 'zh_XC' => 'China - Simplified Chinese',
78 'fr_FR/FR' => 'France - French',
79 'de_DE/DE' => 'Germany - German',
80 'it_IT/IT' => 'Italy - Italian',
81 'ja_JP/JP' => 'Japan - Japanese',
82 'es_XC' => 'Mexico - Spanish',
83 'nl_NL/NL' => 'Netherlands - Dutch',
84 'pl_PL/PL' => 'Poland - Polish',
85 'es_ES/ES' => 'Spain - Spanish',
86 'de_DE/CH' => 'Switzerland - German',
87 'fr_FR/CH' => 'Switzerland - French',
88 'en_US' => 'United States - U.S. English'
89 );
90 private $checkout_languages = array(
91 'AU' => 'Australia',
92 'AT' => 'Austria',
93 'BE' => 'Belgium',
94 'BR' => 'Brazil',
95 'CA' => 'Canada',
96 'CN' => 'China',
97 'FR' => 'France',
98 'DE' => 'Germany',
99 'IT' => 'Italy',
100 'NL' => 'Netherlands',
101 'PL' => 'Poland',
102 'PR' => 'Portugal',
103 'RU' => 'Russia',
104 'ES' => 'Spain',
105 'SE' => 'Sweden',
106 'CH' => 'Switzerland',
107 'GB' => 'United Kingdom',
108 'US' => 'United States',
109 );
110
111 /**
112 * Singleton class
113 */
114 public static function getInstance()
115 {
116 if (!self::$instance) {
117 self::$instance = new self();
118 }
119 return self::$instance;
120 }
121
122 /**
123 * Constructor.
124 * Initializes the plugin by setting localization, filters, and
125 * administration functions.
126 */
127 private function __construct()
128 {
129 if (!$this->testHost()) {
130 return;
131 }
132
133 add_action('init', array($this, 'textDomain'));
134 register_uninstall_hook(__FILE__, array(__CLASS__, 'uninstall'));
135
136 $admin = new PayPalDonations_Admin();
137 $admin->setOptions(
138 get_option(self::OPTION_DB_KEY),
139 $this->currency_codes,
140 $this->donate_buttons,
141 $this->localized_buttons,
142 $this->checkout_languages
143 );
144
145 add_filter('widget_text', 'do_shortcode');
146 add_shortcode('paypal-donation', array(&$this,'paypalShortcode'));
147 add_action('wp_head', array($this, 'addCss'), 999);
148 add_action('widgets_init', array($this, 'handle_widgets_init'));
149 }
150
151 public function handle_widgets_init(){
152 register_widget("PayPalDonations_Widget");
153 }
154
155 /**
156 * PSR-0 compliant autoloader to load classes as needed.
157 *
158 * @param string $classname The name of the class
159 * @return null Return early if the class name does not start with the
160 * correct prefix
161 */
162 public static function autoload($className)
163 {
164 if (__CLASS__ !== mb_substr($className, 0, strlen(__CLASS__))) {
165 return;
166 }
167 $className = ltrim($className, '\\');
168 $fileName = '';
169 $namespace = '';
170 if ($lastNsPos = strrpos($className, '\\')) {
171 $namespace = substr($className, 0, $lastNsPos);
172 $className = substr($className, $lastNsPos + 1);
173 $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
174 $fileName .= DIRECTORY_SEPARATOR;
175 }
176 $fileName .= str_replace('_', DIRECTORY_SEPARATOR, 'src_'.$className);
177 $fileName .='.php';
178
179 require $fileName;
180 }
181
182 /**
183 * Loads the plugin text domain for translation
184 */
185 public function textDomain()
186 {
187 $domain = self::TEXT_DOMAIN;
188 $locale = apply_filters('plugin_locale', get_locale(), $domain);
189 load_textdomain(
190 $domain,
191 WP_LANG_DIR.'/'.$domain.'/'.$domain.'-'.$locale.'.mo'
192 );
193 load_plugin_textdomain(
194 $domain,
195 false,
196 dirname(plugin_basename(__FILE__)).'/lang/'
197 );
198 }
199
200 /**
201 * Fired when the plugin is uninstalled.
202 */
203 public function uninstall()
204 {
205 delete_option('paypal_donations_options');
206 delete_option('widget_paypal_donations');
207 }
208
209 /**
210 * Adds inline CSS code to the head section of the html pages to center the
211 * PayPal button.
212 */
213 public function addCss()
214 {
215 $opts = get_option(self::OPTION_DB_KEY);
216 if (isset($opts['center_button']) and $opts['center_button'] == true) {
217 echo '<style type="text/css">'."\n";
218 echo '.paypal-donations { text-align: center !important }'."\n";
219 echo '</style>'."\n";
220 }
221 }
222
223 /**
224 * Create and register the PayPal shortcode
225 */
226 public function paypalShortcode($atts)
227 {
228 extract(
229 shortcode_atts(
230 array(
231 'purpose' => '',
232 'reference' => '',
233 'amount' => '',
234 'return_page' => '',
235 'button_url' => '',
236 'validate_ipn' => '',
237 ),
238 $atts
239 )
240 );
241
242 return $this->generateHtml(
243 $purpose,
244 $reference,
245 $amount,
246 $return_page,
247 $button_url,
248 $validate_ipn
249 );
250 }
251
252 /**
253 * Generate the PayPal button HTML code
254 */
255 public function generateHtml(
256 $purpose = null,
257 $reference = null,
258 $amount = null,
259 $return_page = null,
260 $button_url = null,
261 $validate_ipn = ''
262 ) {
263 $pd_options = get_option(self::OPTION_DB_KEY);
264
265 // Set overrides for purpose and reference if defined
266 $purpose = (!$purpose) ? $pd_options['purpose'] : $purpose;
267 $reference = (!$reference) ? $pd_options['reference'] : $reference;
268 $amount = (!$amount) ? $pd_options['amount'] : $amount;
269 $return_page = (!$return_page) ? $pd_options['return_page'] : $return_page;
270 $button_url = (!$button_url) ? $pd_options['button_url'] : $button_url;
271
272 $data = array(
273 'pd_options' => $pd_options,
274 'return_page' => $return_page,
275 'purpose' => $purpose,
276 'reference' => $reference,
277 'amount' => $amount,
278 'button_url' => $button_url,
279 'donate_buttons' => $this->donate_buttons,
280 'validate_ipn' => $validate_ipn,
281 );
282
283 return PayPalDonations_View::render('paypal-button', $data);
284 }
285
286 // -------------------------------------------------------------------------
287 // Environment Checks
288 // -------------------------------------------------------------------------
289
290 /**
291 * Checks PHP and WordPress versions.
292 */
293 private function testHost()
294 {
295 // Check if PHP is too old
296 if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<')) {
297 // Display notice
298 add_action('admin_notices', array(&$this, 'phpVersionError'));
299 return false;
300 }
301
302 // Check if WordPress is too old
303 global $wp_version;
304 if (version_compare($wp_version, self::MIN_WP_VERSION, '<')) {
305 add_action('admin_notices', array(&$this, 'wpVersionError'));
306 return false;
307 }
308 return true;
309 }
310
311 /**
312 * Displays a warning when installed on an old PHP version.
313 */
314 public function phpVersionError()
315 {
316 echo '<div class="error"><p><strong>';
317 printf(
318 'Error: %3$s requires PHP version %1$s or greater.<br/>'.
319 'Your installed PHP version: %2$s',
320 self::MIN_PHP_VERSION,
321 PHP_VERSION,
322 $this->getPluginName()
323 );
324 echo '</strong></p></div>';
325 }
326
327 /**
328 * Displays a warning when installed in an old Wordpress version.
329 */
330 public function wpVersionError()
331 {
332 echo '<div class="error"><p><strong>';
333 printf(
334 'Error: %2$s requires WordPress version %1$s or greater.',
335 self::MIN_WP_VERSION,
336 $this->getPluginName()
337 );
338 echo '</strong></p></div>';
339 }
340
341 /**
342 * Get the name of this plugin.
343 *
344 * @return string The plugin name.
345 */
346 private function getPluginName()
347 {
348 $data = get_plugin_data(self::FILE);
349 return $data['Name'];
350 }
351 }
352
353 add_action('plugins_loaded', array('PayPalDonations', 'getInstance'));
354
355 //Add the settings link
356 function wp_ppd_add_settings_link( $links, $file ) {
357 if ( $file == plugin_basename( __FILE__ ) ) {
358 $settings_link = '<a href="options-general.php?page=paypal-donations-options">' . (__( "Settings", "paypal-donations" )) . '</a>';
359 array_unshift( $links, $settings_link );
360 }
361 return $links;
362 }
363 add_filter( 'plugin_action_links', 'wp_ppd_add_settings_link', 10, 2 );
364