PluginProbe
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management / 0.0.1
SureDonation – Donation Forms, Fundraising Campaigns & Donor Management v0.0.1
1.5.1 1.5.0 1.4.0 1.3.0 trunk 0.0.1 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0
suredonation / plugin-loader.php

plugin-loader.php in SureDonation – Donation Forms, Fundraising Campaigns & Donor Management 0.0.1, at plugin-loader.php

191 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Loader
4 *
5 * @package SureDonation
6 */
7
8 namespace SureDonation;
9
10 use SureDonation\Inc\Admin\Admin;
11 use SureDonation\Inc\Ajax\Donation_Handler;
12 use SureDonation\Inc\Assets\Register as Assets_Register;
13 use SureDonation\Inc\Blocks\Register as Blocks_Register;
14 use SureDonation\Inc\Campaigns\Campaign_Cpt;
15 use SureDonation\Inc\Database\Register as Database_Register;
16 use SureDonation\Inc\FormEditor\Assets as FormEditor_Assets;
17 use SureDonation\Inc\Payments\Stripe\Stripe_Frontend;
18 use SureDonation\Inc\Payments\Stripe\Stripe_Settings;
19 use SureDonation\Inc\Payments\Stripe\Stripe_Webhook;
20 use SureDonation\Inc\Post_Types\Donation_Form;
21 use SureDonation\Inc\Rest_Api;
22 use SureDonation\Inc\Shortcodes\Donation_Form as Donation_Form_Shortcode;
23
24 // Exit if accessed directly.
25 if ( ! defined( 'ABSPATH' ) ) {
26 exit;
27 }
28
29 /**
30 * Main Plugin Class
31 *
32 * @since 0.0.1
33 */
34 final class Plugin_Loader {
35 /**
36 * Instance of this class.
37 *
38 * @var Plugin_Loader
39 */
40 private static $instance = null;
41
42 /**
43 * Constructor
44 */
45 private function __construct() {
46 $this->setup_autoloader();
47 $this->init_hooks();
48 }
49
50 /**
51 * Get instance of this class.
52 *
53 * @return Plugin_Loader
54 */
55 public static function get_instance() {
56 if ( null === self::$instance ) {
57 self::$instance = new self();
58 }
59 return self::$instance;
60 }
61
62 /**
63 * Autoload classes.
64 *
65 * @param string $class_name Class name.
66 * @return void
67 */
68 public function autoload( $class_name ) {
69 // Check if class belongs to our namespace.
70 if ( strpos( $class_name, 'SureDonation\\' ) !== 0 ) {
71 return;
72 }
73
74 // Remove namespace prefix.
75 $class_name = str_replace( 'SureDonation\\', '', $class_name );
76
77 // Remove 'Inc\' prefix if exists.
78 $class_name = str_replace( 'Inc\\', '', $class_name );
79
80 // Convert namespace separators to directory separators.
81 $class_name = str_replace( '\\', DIRECTORY_SEPARATOR, $class_name );
82
83 // Convert class name parts to lowercase with hyphens.
84 $parts = explode( DIRECTORY_SEPARATOR, $class_name );
85 $parts = array_map(
86 static function ( $part ) {
87 // Convert CamelCase to kebab-case.
88 $converted = preg_replace( '/([a-z])([A-Z])/', '$1-$2', $part );
89 return strtolower( str_replace( '_', '-', is_string( $converted ) ? $converted : $part ) );
90 },
91 $parts
92 );
93
94 $class_name = implode( DIRECTORY_SEPARATOR, $parts );
95
96 // Build file path.
97 $file = SUREDONATION_DIR . 'inc/' . $class_name . '.php';
98
99 // Load file if exists.
100 if ( file_exists( $file ) ) {
101 require_once $file;
102 }
103 }
104
105 /**
106 * Load plugin components.
107 *
108 * @return void
109 */
110 public function load_plugin() {
111 // Initialize database tables.
112 Database_Register::init();
113
114 // Initialize Campaign CPT.
115 Campaign_Cpt::get_instance();
116
117 // Initialize Donation Form CPT.
118 Donation_Form::get_instance();
119
120 // Initialize Admin.
121 if ( is_admin() ) {
122 Admin::get_instance();
123 }
124
125 // Initialize REST API.
126 Rest_Api::get_instance();
127
128 // Initialize blocks (must be before init hook fires).
129 Blocks_Register::get_instance();
130
131 // Initialize frontend assets.
132 Assets_Register::get_instance();
133
134 // Initialize form editor assets (sidebar, meta, etc.).
135 FormEditor_Assets::get_instance();
136
137 // Initialize AJAX handlers.
138 Donation_Handler::get_instance();
139
140 // Initialize shortcodes.
141 Donation_Form_Shortcode::get_instance();
142
143 // Initialize Stripe payment gateway.
144 Stripe_Settings::get_instance();
145 Stripe_Webhook::get_instance();
146 Stripe_Frontend::get_instance();
147 }
148
149 /**
150 * Setup autoloader for plugin classes.
151 *
152 * @return void
153 */
154 private function setup_autoloader() {
155 spl_autoload_register( [ $this, 'autoload' ] );
156 }
157
158 /**
159 * Initialize plugin hooks.
160 *
161 * @return void
162 */
163 private function init_hooks() {
164 // Activation hook.
165 register_activation_hook(
166 SUREDONATION_FILE,
167 static function () {
168 // Reset rewrite rules.
169 delete_option( 'rewrite_rules' );
170
171 // Set redirect flag for onboarding.
172 update_option( '__suredonation_do_redirect', true );
173 }
174 );
175
176 // Deactivation hook.
177 register_deactivation_hook(
178 SUREDONATION_FILE,
179 static function () {
180 update_option( '__suredonation_do_redirect', false );
181 }
182 );
183
184 // Initialize plugin after WordPress loads.
185 add_action( 'plugins_loaded', [ $this, 'load_plugin' ], 99 );
186 }
187 }
188
189 // Initialize plugin.
190 Plugin_Loader::get_instance();
191