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