| 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\Admin\Admin_Ajax; |
| 12 |
use SureDonation\Inc\Admin\Analytics; |
| 13 |
use SureDonation\Inc\Admin\Notices; |
| 14 |
use SureDonation\Inc\Ajax\Donation_Handler; |
| 15 |
use SureDonation\Inc\Assets\Register as Assets_Register; |
| 16 |
use SureDonation\Inc\Blocks\Register as Blocks_Register; |
| 17 |
use SureDonation\Inc\Campaigns\Campaign_Cpt; |
| 18 |
use SureDonation\Inc\Campaigns\Campaign_Meta_Tags; |
| 19 |
use SureDonation\Inc\Campaigns\Campaign_Page; |
| 20 |
use SureDonation\Inc\Database\Register as Database_Register; |
| 21 |
use SureDonation\Inc\Donor_User_Link; |
| 22 |
use SureDonation\Inc\FormEditor\Assets as FormEditor_Assets; |
| 23 |
use SureDonation\Inc\Onboarding; |
| 24 |
use SureDonation\Inc\Privacy\Privacy_Data; |
| 25 |
use SureDonation\Inc\Payments\Offline\Offline_Frontend; |
| 26 |
use SureDonation\Inc\Payments\Offline\Offline_Settings; |
| 27 |
use SureDonation\Inc\Payments\PayPal\PayPal_Frontend; |
| 28 |
use SureDonation\Inc\Payments\PayPal\PayPal_Settings; |
| 29 |
use SureDonation\Inc\Payments\PayPal\PayPal_Webhook_Listener; |
| 30 |
use SureDonation\Inc\Payments\Stripe\Stripe_Frontend; |
| 31 |
use SureDonation\Inc\Payments\Stripe\Stripe_Settings; |
| 32 |
use SureDonation\Inc\Payments\Stripe\Stripe_Webhook; |
| 33 |
use SureDonation\Inc\Page_Builders\Page_Builders; |
| 34 |
use SureDonation\Inc\Pdf\Pdf_Manager; |
| 35 |
use SureDonation\Inc\Post_Types\Donation_Form; |
| 36 |
use SureDonation\Inc\Rest_Api; |
| 37 |
use SureDonation\Inc\Shortcodes\Donation_Form as Donation_Form_Shortcode; |
| 38 |
|
| 39 |
// Exit if accessed directly. |
| 40 |
if ( ! defined( 'ABSPATH' ) ) { |
| 41 |
exit; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Main Plugin Class |
| 46 |
* |
| 47 |
* @since 0.0.1 |
| 48 |
*/ |
| 49 |
final class Plugin_Loader { |
| 50 |
/** |
| 51 |
* Instance of this class. |
| 52 |
* |
| 53 |
* @var Plugin_Loader |
| 54 |
*/ |
| 55 |
private static $instance = null; |
| 56 |
|
| 57 |
/** |
| 58 |
* Constructor |
| 59 |
*/ |
| 60 |
private function __construct() { |
| 61 |
$this->setup_autoloader(); |
| 62 |
$this->init_hooks(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get instance of this class. |
| 67 |
* |
| 68 |
* @return Plugin_Loader |
| 69 |
*/ |
| 70 |
public static function get_instance() { |
| 71 |
if ( null === self::$instance ) { |
| 72 |
self::$instance = new self(); |
| 73 |
} |
| 74 |
return self::$instance; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Autoload classes. |
| 79 |
* |
| 80 |
* @param string $class_name Class name. |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function autoload( $class_name ) { |
| 84 |
// Check if class belongs to our namespace. |
| 85 |
if ( strpos( $class_name, 'SureDonation\\' ) !== 0 ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
// Remove namespace prefix. |
| 90 |
$class_name = str_replace( 'SureDonation\\', '', $class_name ); |
| 91 |
|
| 92 |
// Remove 'Inc\' prefix if exists. |
| 93 |
$class_name = str_replace( 'Inc\\', '', $class_name ); |
| 94 |
|
| 95 |
// Convert namespace separators to directory separators. |
| 96 |
$class_name = str_replace( '\\', DIRECTORY_SEPARATOR, $class_name ); |
| 97 |
|
| 98 |
// Convert class name parts to lowercase with hyphens. |
| 99 |
$parts = explode( DIRECTORY_SEPARATOR, $class_name ); |
| 100 |
$parts = array_map( |
| 101 |
static function ( $part ) { |
| 102 |
// Convert CamelCase to kebab-case. |
| 103 |
$converted = preg_replace( '/([a-z])([A-Z])/', '$1-$2', $part ); |
| 104 |
return strtolower( str_replace( '_', '-', is_string( $converted ) ? $converted : $part ) ); |
| 105 |
}, |
| 106 |
$parts |
| 107 |
); |
| 108 |
|
| 109 |
$class_name = implode( DIRECTORY_SEPARATOR, $parts ); |
| 110 |
|
| 111 |
// Build file path. |
| 112 |
$file = SUREDONATION_DIR . 'inc/' . $class_name . '.php'; |
| 113 |
|
| 114 |
// Load file if exists. |
| 115 |
if ( file_exists( $file ) ) { |
| 116 |
require_once $file; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Load plugin components. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function load_plugin() { |
| 126 |
// Initialize database tables. |
| 127 |
Database_Register::init(); |
| 128 |
|
| 129 |
// Initialize Campaign CPT. |
| 130 |
Campaign_Cpt::get_instance(); |
| 131 |
|
| 132 |
// Initialize Campaign Page (default layout seeding + display block helpers). |
| 133 |
Campaign_Page::get_instance(); |
| 134 |
|
| 135 |
// Output Open Graph / Twitter Card tags on singular campaign pages. |
| 136 |
Campaign_Meta_Tags::get_instance(); |
| 137 |
|
| 138 |
// Page-builder integrations (Bricks now; each self-gates on the builder). |
| 139 |
// Page-builder integrations (Elementor now; each self-gates on the builder). |
| 140 |
Page_Builders::get_instance(); |
| 141 |
|
| 142 |
// Initialize Donation Form CPT. |
| 143 |
Donation_Form::get_instance(); |
| 144 |
|
| 145 |
// Initialize Admin. |
| 146 |
if ( is_admin() ) { |
| 147 |
Admin::get_instance(); |
| 148 |
Admin_Ajax::get_instance(); |
| 149 |
Pdf_Manager::get_instance(); |
| 150 |
Onboarding::get_instance(); |
| 151 |
Notices::get_instance(); |
| 152 |
} |
| 153 |
|
| 154 |
// Initialize Analytics. Loaded outside is_admin() so usage events |
| 155 |
// fired during REST requests (onboarding, campaign publish) are |
| 156 |
// captured; the class gates its own admin-only behavior internally. |
| 157 |
Analytics::get_instance(); |
| 158 |
|
| 159 |
// Initialize REST API. |
| 160 |
Rest_Api::get_instance(); |
| 161 |
|
| 162 |
// Initialize blocks (must be before init hook fires). |
| 163 |
Blocks_Register::get_instance(); |
| 164 |
|
| 165 |
// Initialize frontend assets. |
| 166 |
Assets_Register::get_instance(); |
| 167 |
|
| 168 |
// Initialize form editor assets (sidebar, meta, etc.). |
| 169 |
FormEditor_Assets::get_instance(); |
| 170 |
|
| 171 |
// Initialize AJAX handlers. |
| 172 |
Donation_Handler::get_instance(); |
| 173 |
|
| 174 |
// Initialize shortcodes. |
| 175 |
Donation_Form_Shortcode::get_instance(); |
| 176 |
|
| 177 |
// Register WordPress personal-data export/erase integration. |
| 178 |
Privacy_Data::get_instance(); |
| 179 |
|
| 180 |
// Initialize Stripe payment gateway. |
| 181 |
Stripe_Settings::get_instance(); |
| 182 |
Stripe_Webhook::get_instance(); |
| 183 |
Stripe_Frontend::get_instance(); |
| 184 |
|
| 185 |
// Initialize Offline payment gateway. |
| 186 |
Offline_Settings::get_instance(); |
| 187 |
Offline_Frontend::get_instance(); |
| 188 |
|
| 189 |
// Initialize PayPal payment gateway. |
| 190 |
PayPal_Settings::get_instance(); |
| 191 |
PayPal_Frontend::get_instance(); |
| 192 |
PayPal_Webhook_Listener::get_instance(); |
| 193 |
|
| 194 |
// Initialize donor-user linking. |
| 195 |
Donor_User_Link::get_instance(); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Setup autoloader for plugin classes. |
| 200 |
* |
| 201 |
* @return void |
| 202 |
*/ |
| 203 |
private function setup_autoloader() { |
| 204 |
spl_autoload_register( [ $this, 'autoload' ] ); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Initialize plugin hooks. |
| 209 |
* |
| 210 |
* @return void |
| 211 |
*/ |
| 212 |
private function init_hooks() { |
| 213 |
// Activation hook. |
| 214 |
register_activation_hook( |
| 215 |
SUREDONATION_FILE, |
| 216 |
static function () { |
| 217 |
// Reset rewrite rules. |
| 218 |
delete_option( 'rewrite_rules' ); |
| 219 |
|
| 220 |
// Set redirect flag for onboarding. |
| 221 |
update_option( '__suredonation_do_redirect', true ); |
| 222 |
|
| 223 |
// Record install time for the review-notice grace period. |
| 224 |
// add_option is a no-op if the value already exists, so it |
| 225 |
// never clobbers a real install time on re-activation. |
| 226 |
add_option( 'suredonation_install_time', time() ); |
| 227 |
|
| 228 |
// Register the donor role. |
| 229 |
if ( ! get_role( 'suredonation_donor' ) ) { |
| 230 |
add_role( |
| 231 |
'suredonation_donor', |
| 232 |
__( 'Donor', 'suredonation' ), |
| 233 |
[ 'read' => true ] |
| 234 |
); |
| 235 |
} |
| 236 |
} |
| 237 |
); |
| 238 |
|
| 239 |
// Deactivation hook. |
| 240 |
register_deactivation_hook( |
| 241 |
SUREDONATION_FILE, |
| 242 |
static function () { |
| 243 |
update_option( '__suredonation_do_redirect', false ); |
| 244 |
} |
| 245 |
); |
| 246 |
|
| 247 |
// Ensure donor role exists (for upgrades without re-activation). |
| 248 |
add_action( |
| 249 |
'init', |
| 250 |
static function () { |
| 251 |
if ( ! get_role( 'suredonation_donor' ) ) { |
| 252 |
add_role( |
| 253 |
'suredonation_donor', |
| 254 |
__( 'Donor', 'suredonation' ), |
| 255 |
[ 'read' => true ] |
| 256 |
); |
| 257 |
} |
| 258 |
// Load plugin text domain. |
| 259 |
load_plugin_textdomain( 'suredonation', false, dirname( plugin_basename( SUREDONATION_FILE ) ) . '/languages' ); |
| 260 |
} |
| 261 |
); |
| 262 |
|
| 263 |
// Initialize plugin after WordPress loads. |
| 264 |
add_action( 'plugins_loaded', [ $this, 'load_plugin' ], 99 ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
// Initialize plugin. |
| 269 |
Plugin_Loader::get_instance(); |
| 270 |
|