forked from b1cep/Charitable
/
charitable.php
725 lines (618 loc) · 23.6 KB
/
charitable.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
<?php
/**
* Plugin Name: Charitable
* Plugin URI: https://www.wpcharitable.com
* Description: The WordPress fundraising alternative for non-profits, created to help non-profits raise money on their own website.
* Version: 1.2.2
* Author: WP Charitable
* Author URI: https://wpcharitable.com
* Requires at least: 4.1
* Tested up to: 4.3.1
*
* Text Domain: charitable
* Domain Path: /i18n/languages/
*
* @package Charitable
* @author Eric Daams
* @copyright Copyright (c) 2015, Studio 164a
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'Charitable' ) ) :
/**
* Main Charitable class
*
* @class Charitable
* @version 1.2.2
*/
class Charitable {
/**
* @var string
*/
const VERSION = '1.2.2';
/**
* @var string A date in the format: YYYYMMDD
*/
const DB_VERSION = '20150615';
/**
* @var string The Campaign post type.
*/
const CAMPAIGN_POST_TYPE = 'campaign';
/**
* @var string The Donation post type.
*/
const DONATION_POST_TYPE = 'donation';
/**
* @var Charitable
* @access private
*/
private static $instance = null;
/**
* @var string Directory path for the plugin.
* @access private
*/
private $directory_path;
/**
* @var string Directory url for the plugin.
* @access private
*/
private $directory_url;
/**
* @var string Directory path for the includes folder of the plugin.
* @access private
*/
private $includes_path;
/**
* @var array Store of registered objects.
* @access private
*/
private $registry;
/**
* Create class instance.
*
* @since 1.0.0
*/
public function __construct() {
$this->directory_path = plugin_dir_path( __FILE__ );
$this->directory_url = plugin_dir_url( __FILE__ );
$this->includes_path = $this->directory_path . 'includes/';
$this->load_dependencies();
register_activation_hook( __FILE__, array( $this, 'activate') );
register_deactivation_hook( __FILE__, array( $this, 'deactivate') );
add_action( 'plugins_loaded', array( $this, 'start' ), 1 );
}
/**
* Returns the original instance of this class.
*
* @return Charitable
* @since 1.0.0
*/
public static function get_instance() {
return self::$instance;
}
/**
* Run the startup sequence.
*
* This is only ever executed once.
*
* @return void
* @access public
* @since 1.0.0
*/
public function start() {
// If we've already started (i.e. run this function once before), do not pass go.
if ( $this->started() ) {
return;
}
// Set static instance
self::$instance = $this;
$this->maybe_start_ajax();
$this->maybe_upgrade();
$this->attach_hooks_and_filters();
$this->maybe_start_admin();
$this->maybe_start_public();
Charitable_Addons::load( $this );
}
/**
* Include necessary files.
*
* @return void
* @access private
* @since 1.0.0
*/
private function load_dependencies() {
$includes_path = $this->get_path( 'includes' );
/* Abstracts */
require_once( $includes_path . 'abstracts/class-charitable-form.php' );
require_once( $includes_path . 'abstracts/class-charitable-query.php' );
require_once( $includes_path . 'abstracts/class-charitable-start-object.php' );
/* Functions & Core Classes */
require_once( $includes_path . 'charitable-core-functions.php' );
require_once( $includes_path . 'charitable-utility-functions.php' );
require_once( $includes_path . 'class-charitable-locations.php' );
require_once( $includes_path . 'class-charitable-notices.php' );
require_once( $includes_path . 'class-charitable-post-types.php' );
require_once( $includes_path . 'class-charitable-request.php' );
require_once( $includes_path . 'class-charitable-cron.php' );
require_once( $includes_path . 'class-charitable-i18n.php' );
/* Addons */
require_once( $includes_path . 'addons/class-charitable-addons.php' );
/* Campaigns */
require_once( $includes_path . 'campaigns/charitable-campaign-functions.php' );
require_once( $includes_path . 'campaigns/class-charitable-campaign.php' );
require_once( $includes_path . 'campaigns/class-charitable-campaigns.php' );
/* Currency */
require_once( $includes_path . 'currency/charitable-currency-functions.php' );
require_once( $includes_path . 'currency/class-charitable-currency.php' );
/* Donations */
require_once( $includes_path . 'donations/interface-charitable-donation-form.php' );
require_once( $includes_path . 'donations/class-charitable-donation-processor.php' );
require_once( $includes_path . 'donations/class-charitable-donation.php' );
require_once( $includes_path . 'donations/class-charitable-donations.php' );
require_once( $includes_path . 'donations/class-charitable-donation-form.php' );
require_once( $includes_path . 'donations/class-charitable-donation-amount-form.php' );
require_once( $includes_path . 'donations/charitable-donation-hooks.php' );
require_once( $includes_path . 'donations/charitable-donation-functions.php' );
/* Users */
require_once( $includes_path . 'users/charitable-user-functions.php' );
require_once( $includes_path . 'users/charitable-user-hooks.php' );
require_once( $includes_path . 'users/class-charitable-user.php' );
require_once( $includes_path . 'users/class-charitable-roles.php' );
require_once( $includes_path . 'users/class-charitable-donor.php' );
require_once( $includes_path . 'users/class-charitable-donor-query.php' );
require_once( $includes_path . 'users/class-charitable-registration-form.php' );
require_once( $includes_path . 'users/class-charitable-profile-form.php' );
/* Gateways */
include_once( $includes_path . 'gateways/interface-charitable-gateway.php' );
require_once( $includes_path . 'gateways/class-charitable-gateways.php' );
include_once( $includes_path . 'gateways/abstract-class-charitable-gateway.php' );
include_once( $includes_path . 'gateways/class-charitable-gateway-offline.php' );
include_once( $includes_path . 'gateways/class-charitable-gateway-paypal.php' );
/* Emails */
include_once( $includes_path . 'emails/charitable-email-hooks.php' );
include_once( $includes_path . 'emails/interface-charitable-email.php' );
require_once( $includes_path . 'emails/class-charitable-emails.php' );
include_once( $includes_path . 'emails/abstract-class-charitable-email.php' );
include_once( $includes_path . 'emails/class-charitable-email-new-donation.php' );
include_once( $includes_path . 'emails/class-charitable-email-donation-receipt.php' );
include_once( $includes_path . 'emails/class-charitable-email-campaign-end.php' );
/* Database */
require_once( $includes_path . 'db/abstract-class-charitable-db.php' );
require_once( $includes_path . 'db/class-charitable-campaign-donations-db.php' );
require_once( $includes_path . 'db/class-charitable-donors-db.php' );
/* Licensing */
require_once( $includes_path . 'licensing/class-charitable-licenses.php' );
require_once( $includes_path . 'licensing/class-charitable-plugin-updater.php' );
/* Public */
require_once( $includes_path . 'public/charitable-page-functions.php' );
require_once( $includes_path . 'public/charitable-template-functions.php' );
require_once( $includes_path . 'public/charitable-template-helpers.php' );
require_once( $includes_path . 'public/charitable-template-hooks.php' );
require_once( $includes_path . 'public/class-charitable-session.php' );
require_once( $includes_path . 'public/class-charitable-template.php' );
require_once( $includes_path . 'public/class-charitable-template-part.php' );
require_once( $includes_path . 'public/class-charitable-templates.php' );
require_once( $includes_path . 'public/class-charitable-ghost-page.php' );
require_once( $includes_path . 'public/class-charitable-user-dashboard.php' );
/* Shortcodes */
require_once( $includes_path . 'shortcodes/class-charitable-campaigns-shortcode.php' );
require_once( $includes_path . 'shortcodes/class-charitable-my-donations-shortcode.php' );
require_once( $includes_path . 'shortcodes/class-charitable-donation-receipt-shortcode.php' );
require_once( $includes_path . 'shortcodes/class-charitable-login-shortcode.php' );
require_once( $includes_path . 'shortcodes/class-charitable-registration-shortcode.php' );
require_once( $includes_path . 'shortcodes/class-charitable-profile-shortcode.php' );
require_once( $includes_path . 'shortcodes/charitable-shortcodes-hooks.php' );
/* Widgets */
require_once( $includes_path . 'widgets/class-charitable-widgets.php' );
require_once( $includes_path . 'widgets/class-charitable-campaign-terms-widget.php' );
require_once( $includes_path . 'widgets/class-charitable-campaigns-widget.php' );
require_once( $includes_path . 'widgets/class-charitable-donors-widget.php' );
require_once( $includes_path . 'widgets/class-charitable-donate-widget.php' );
require_once( $includes_path . 'widgets/class-charitable-donation-stats-widget.php' );
/* Customizer */
require_once( $includes_path . 'admin/customizer/class-charitable-customizer.php' );
/* Deprecated */
require_once( $includes_path . 'deprecated/charitable-deprecated-functions.php' );
/**
* We are registering this object only for backwards compatibility. It
* will be removed in or after Charitable 1.3.
*
* @deprecated
*/
$this->register_object( Charitable_Emails::get_instance() );
$this->register_object( Charitable_Request::get_instance() );
$this->register_object( Charitable_Gateways::get_instance() );
$this->register_object( Charitable_i18n::get_instance() );
$this->register_object( Charitable_Post_Types::get_instance() );
$this->register_object( Charitable_Cron::get_instance() );
$this->register_object( Charitable_Widgets::get_instance() );
$this->register_object( Charitable_Licenses::get_instance() );
$this->register_object( Charitable_User_Dashboard::get_instance() );
}
/**
* Set up hook and filter callback functions.
*
* @return void
* @access private
* @since 1.0.0
*/
private function attach_hooks_and_filters() {
add_action('plugins_loaded', array( $this, 'charitable_install' ), 100 );
add_action('plugins_loaded', array( $this, 'charitable_start' ), 100 );
add_action( 'setup_theme', array( 'Charitable_Customizer', 'start' ) );
/**
* We do this on priority 20 so that any functionality that is loaded on init (such
* as addons) has a chance to run before the event.
*/
add_action('init', array( $this, 'do_charitable_actions' ), 20 );
add_filter('charitable_sanitize_campaign_meta', array( 'Charitable_Campaign', 'sanitize_meta' ), 10, 3 );
add_filter('charitable_sanitize_donation_meta', array( 'Charitable_Donation', 'sanitize_meta' ), 10, 2 );
add_filter('charitable_after_insert_user', array( 'Charitable_User', 'signon' ), 10, 2 );
}
/**
* Checks whether we're in the admin area and if so, loads the admin-only functionality.
*
* @return void
* @access private
* @since 1.0.0
*/
private function maybe_start_admin() {
if ( ! is_admin() ) {
return;
}
require_once( $this->get_path( 'admin' ) . 'class-charitable-admin.php' );
/**
* We are registering this object only for backwards compatibility. It
* will be removed in or after Charitable 1.3.
*
* @deprecated
*/
$this->register_object( Charitable_Admin::get_instance() );
}
/**
* Checks whether we're on the public-facing side and if so, loads the public-facing functionality.
*
* @return void
* @access private
* @since 1.0.0
*/
private function maybe_start_public() {
if ( is_admin() ) {
return;
}
require_once( $this->get_path( 'public' ) . 'class-charitable-public.php' );
/**
* We are registering this object only for backwards compatibility. It
* will be removed in or after Charitable 1.3.
*
* @deprecated
*/
$this->register_object( Charitable_Public::get_instance() );
}
/**
* Checks whether we're executing an AJAX hook and if so, loads some AJAX functionality.
*
* @return void
* @access private
* @since 1.0.0
*/
private function maybe_start_ajax() {
if ( false === ( defined('DOING_AJAX') && DOING_AJAX ) ) {
return;
}
/**
* We are registering this object only for backwards compatibility. It
* will be removed in or after Charitable 1.3.
*
* @deprecated
*/
$this->register_object( Charitable_Session::get_instance() );
}
/**
* This method is fired after all plugins are loaded and simply fires the charitable_start hook.
*
* Extensions can use the charitable_start event to load their own functionality.
*
* @return void
* @access public
* @since 1.0.0
*/
public function charitable_start() {
do_action( 'charitable_start', $this );
}
/**
* Fires off an action right after Charitable is installed, allowing other
* plugins/themes to do something at this point.
*
* @return void
* @access public
* @since 1.0.1
*/
public function charitable_install() {
$install = get_transient( 'charitable_install' );
if ( ! $install ) {
return;
}
do_action( 'charitable_install' );
delete_transient( 'charitable_install' );
}
/**
* Returns whether we are currently in the start phase of the plugin.
*
* @return bool
* @access public
* @since 1.0.0
*/
public function is_start() {
return current_filter() == 'charitable_start';
}
/**
* Returns whether the plugin has already started.
*
* @return bool
* @access public
* @since 1.0.0
*/
public function started() {
return did_action( 'charitable_start' ) || current_filter() == 'charitable_start';
}
/**
* Returns whether the plugin is being activated.
*
* @return bool
* @access public
* @since 1.0.0
*/
public function is_activation() {
return current_filter() == 'activate_charitable/charitable.php';
}
/**
* Returns whether the plugin is being deactivated.
*
* @return bool
* @access public
* @since 1.0.0
*/
public function is_deactivation() {
return current_filter() == 'deactivate_charitable/charitable.php';
}
/**
* Stores an object in the plugin's registry.
*
* @param mixed $object
* @return void
* @access public
* @since 1.0.0
*/
public function register_object($object) {
if ( ! is_object( $object ) ) {
return;
}
$class = get_class( $object );
$this->registry[$class] = $object;
}
/**
* Returns a registered object.
*
* @param string $class The type of class you want to retrieve.
* @return mixed The object if its registered. Otherwise false.
* @access public
* @since 1.0.0
*/
public function get_registered_object($class) {
return isset( $this->registry[$class] ) ? $this->registry[$class] : false;
}
/**
* Returns plugin paths.
*
* @param string $type If empty, returns the path to the plugin.
* @param bool $absolute_path If true, returns the file system path. If false, returns it as a URL.
* @return string
* @since 1.0.0
*/
public function get_path($type = '', $absolute_path = true ) {
$base = $absolute_path ? $this->directory_path : $this->directory_url;
switch( $type ) {
case 'includes' :
$path = $base . 'includes/';
break;
case 'admin' :
$path = $base . 'includes/admin/';
break;
case 'public' :
$path = $base . 'includes/public/';
break;
case 'assets' :
$path = $base . 'assets/';
break;
case 'templates' :
$path = $base . 'templates/';
break;
case 'directory' :
$path = $base;
break;
default :
$path = __FILE__;
}
return $path;
}
/**
* Returns the plugin's version number.
*
* @return string
* @access public
* @since 1.0.0
*/
public function get_version() {
return self::VERSION;
}
/**
* Returns the public class.
*
* @return Charitable_Public
* @access public
* @since 1.0.0
*/
public function get_public() {
return $this->get_registered_object( 'Charitable_Public' );
}
/**
* Returns the admin class.
*
* @return Charitable_Admin
* @access public
* @since 1.0.0
*/
public function get_admin() {
return $this->get_registered_object( 'Charitable_Admin' );
}
/**
* Return the current request object.
*
* @return Charitable_Request
* @access public
* @since 1.0.0
*/
public function get_request() {
$request = $this->get_registered_object('Charitable_Request');
if ( $request === false ) {
$request = new Charitable_Request();
$this->register_object( $request );
}
return $request;
}
/**
* Return an instance of the currency helper.
*
* @return Charitable_Currency
* @access public
* @since 1.0.0
*/
public function get_currency_helper() {
$currency_helper = $this->get_registered_object('Charitable_Currency');
if ( false === $currency_helper ) {
$currency_helper = new Charitable_Currency();
$this->register_object( $currency_helper );
}
return $currency_helper;
}
/**
* Returns the model for one of Charitable's database tables.
*
* @param string $table
* @return Charitable_DB
* @access public
* @since 1.0.0
*/
public function get_db_table( $table ) {
$tables = $this->get_tables();
if ( ! isset( $tables[ $table ] ) ) {
_doing_it_wrong( __METHOD__, sprintf( 'Invalid table %s passed', $table ), '1.0.0' );
return null;
}
$class_name = $tables[ $table ];
$db_table = $this->get_registered_object( $class_name );
if ( false === $db_table ) {
$db_table = new $class_name;
$this->register_object( $db_table );
}
return $db_table;
}
/**
* Return the filtered list of registered tables.
*
* @return string[]
* @access private
* @since 1.0.0
*/
private function get_tables() {
$default_tables = array(
'campaign_donations' => 'Charitable_Campaign_Donations_DB',
'donors' => 'Charitable_Donors_DB'
);
return apply_filters( 'charitable_db_tables', $default_tables );
}
/**
* Runs on plugin activation.
*
* @see register_activation_hook
*
* @return void
* @access public
* @since 1.0.0
*/
public function activate() {
require_once( $this->get_path( 'includes' ) . 'class-charitable-install.php' );
new Charitable_Install();
}
/**
* Runs on plugin deactivation.
*
* @see register_deactivation_hook
*
* @return void
* @access public
* @since 1.0.0
*/
public function deactivate() {
require_once( $this->get_path( 'includes' ) . 'class-charitable-uninstall.php' );
new Charitable_Uninstall();
}
/**
* Perform upgrade routine if necessary.
*
* @return void
* @access private
* @since 1.0.0
*/
private function maybe_upgrade() {
$db_version = get_option( 'charitable_version' );
if ( $db_version !== self::VERSION ) {
require_once( $this->get_path( 'includes' ) . 'class-charitable-upgrade.php' );
Charitable_Upgrade::upgrade_from( $db_version, self::VERSION );
}
}
/**
* If a charitable_action event is triggered, delegate the event using do_action.
*
* @return void
* @access public
* @since 1.0.0
*/
public function do_charitable_actions() {
if ( isset( $_REQUEST['charitable_action'] ) ) {
$action = $_REQUEST[ 'charitable_action' ];
do_action( 'charitable_' . $action, 20 );
}
}
/**
* Throw error on object clone.
*
* This class is specifically designed to be instantiated once. You can retrieve the instance using charitable()
*
* @since 1.0.0
* @access public
* @return void
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'charitable' ), '1.0.0' );
}
/**
* Disable unserializing of the class.
*
* @since 1.0.0
* @access public
* @return void
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'charitable' ), '1.0.0' );
}
/**
* @deprecated
*/
public function get_location_helper() {
return charitable_get_location_helper();
}
}
$charitable = new Charitable();
endif; // End if class_exists check