| 1 |
<?php |
| 2 |
/** |
| 3 |
* Loader Class |
| 4 |
* |
| 5 |
* This file is responsible for initializing the SureMails plugin components. |
| 6 |
* |
| 7 |
* @package SureMails |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SureMails; |
| 11 |
|
| 12 |
use SureMails\Inc\Abilities\Ability; |
| 13 |
use SureMails\Inc\Admin\Activator; |
| 14 |
use SureMails\Inc\Admin\Crons; |
| 15 |
use SureMails\Inc\Admin\Plugin; |
| 16 |
use SureMails\Inc\Admin\Update; |
| 17 |
use SureMails\Inc\Ajax\Ajax; |
| 18 |
use SureMails\Inc\Analytics\Analytics; |
| 19 |
use SureMails\Inc\API\Api_Init; |
| 20 |
use SureMails\Inc\Controller\ContentGuard; |
| 21 |
use SureMails\Inc\Controller\WeeklySummary; |
| 22 |
use SureMails\Inc\Lib\Suremails_Nps_Survey; |
| 23 |
use SureMails\Inc\Nps_Notice; |
| 24 |
|
| 25 |
/** |
| 26 |
* Class Loader |
| 27 |
* |
| 28 |
* The Loader class is responsible for loading the SureMails plugin, handling translations, |
| 29 |
* autoloading, and initializing components like email settings, connections, and activation hooks. |
| 30 |
* |
| 31 |
* @package SureMails |
| 32 |
*/ |
| 33 |
class Loader { |
| 34 |
/** |
| 35 |
* Singleton instance of the Loader class. |
| 36 |
* |
| 37 |
* @var Loader|null |
| 38 |
*/ |
| 39 |
private static $instance = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Loader constructor. |
| 43 |
* Private to enforce singleton pattern. |
| 44 |
*/ |
| 45 |
private function __construct() { |
| 46 |
|
| 47 |
if ( ! defined( 'SUREMAILS_CONTENT_GUARD_MIDDLEWARE' ) ) { |
| 48 |
define( 'SUREMAILS_CONTENT_GUARD_MIDDLEWARE', 'https://credits.startertemplates.com/suremails/' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Register the autoloader. |
| 53 |
*/ |
| 54 |
$this->register_autoload(); |
| 55 |
|
| 56 |
/** |
| 57 |
* Load the plugin textdomain for translations. |
| 58 |
*/ |
| 59 |
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] ); |
| 60 |
add_action( 'plugins_loaded', [ $this, 'setup' ] ); |
| 61 |
add_action( 'plugin_loaded', [ $this, 'setup_ajax_instance' ] ); |
| 62 |
|
| 63 |
/** |
| 64 |
* The code that runs during plugin activation. |
| 65 |
*/ |
| 66 |
register_activation_hook( SUREMAILS_FILE, [ $this, 'activate' ] ); |
| 67 |
|
| 68 |
/** |
| 69 |
* The code that runs during plugin deactivation. |
| 70 |
*/ |
| 71 |
register_deactivation_hook( SUREMAILS_FILE, [ $this, 'deactivate' ] ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Setup the plugin, register hooks, and initialize components. |
| 76 |
* |
| 77 |
* @return void |
| 78 |
*/ |
| 79 |
public function setup(): void { |
| 80 |
$this->load_libraries(); |
| 81 |
|
| 82 |
// Bail if doing AJAX. |
| 83 |
if ( wp_doing_ajax() ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
// Admin loaders. |
| 87 |
if ( is_admin() ) { |
| 88 |
Plugin::instance(); |
| 89 |
} |
| 90 |
Update::instance(); |
| 91 |
ContentGuard::instance(); |
| 92 |
Crons::instance(); |
| 93 |
Api_Init::instance(); |
| 94 |
Analytics::instance(); |
| 95 |
WeeklySummary::instance(); |
| 96 |
|
| 97 |
// Register WordPress Abilities API (WP 6.9+). |
| 98 |
$this->register_abilities(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Load libraries. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function load_libraries() { |
| 107 |
|
| 108 |
if ( ! class_exists( 'BSF_Admin_Notices' ) ) { |
| 109 |
require_once SUREMAILS_DIR . 'inc/lib/astra-notices/class-bsf-admin-notices.php'; |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! class_exists( 'BSF_Analytics_Loader' ) ) { |
| 113 |
require_once SUREMAILS_DIR . 'inc/lib/bsf-analytics/class-bsf-analytics-loader.php'; |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! class_exists( '\BSF_UTM_Analytics' ) ) { |
| 117 |
$utm_path = SUREMAILS_DIR . 'inc/lib/bsf-analytics/modules/utm-analytics.php'; |
| 118 |
if ( file_exists( $utm_path ) ) { |
| 119 |
require_once $utm_path; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$srml_bsf_analytics = \BSF_Analytics_Loader::get_instance(); // @phpstan-ignore class.notFound |
| 124 |
|
| 125 |
$srml_bsf_analytics->set_entity( |
| 126 |
[ |
| 127 |
'suremails' => [ |
| 128 |
'product_name' => 'SureMail', |
| 129 |
'path' => SUREMAILS_DIR . 'inc/lib/bsf-analytics', |
| 130 |
'author' => 'SureMail', |
| 131 |
'time_to_display' => '+24 hours', |
| 132 |
'deactivation_survey' => apply_filters( |
| 133 |
'suremails_deactivation_survey_data', |
| 134 |
[ |
| 135 |
[ |
| 136 |
'id' => 'deactivation-survey-suremails', |
| 137 |
'popup_logo' => SUREMAILS_PLUGIN_URL . 'inc/images/suremails.svg', |
| 138 |
'plugin_slug' => 'suremails', |
| 139 |
'popup_title' => 'Quick Feedback', |
| 140 |
'support_url' => 'https://suremails.com/contact/', |
| 141 |
'popup_description' => 'If you have a moment, please share why you are deactivating SureMail:', |
| 142 |
'show_on_screens' => [ 'plugins' ], |
| 143 |
'plugin_version' => SUREMAILS_VERSION, |
| 144 |
], |
| 145 |
] |
| 146 |
), |
| 147 |
'hide_optin_checkbox' => true, |
| 148 |
], |
| 149 |
] |
| 150 |
); |
| 151 |
|
| 152 |
// load nps survey. |
| 153 |
if ( class_exists( 'SureMails\Inc\Lib\Suremails_Nps_Survey' ) && ! apply_filters( 'suremails_disable_nps_survey', false ) ) { |
| 154 |
Suremails_Nps_Survey::instance(); |
| 155 |
Nps_Notice::instance(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Setup the Ajax instance. |
| 161 |
* |
| 162 |
* @return void |
| 163 |
*/ |
| 164 |
public function setup_ajax_instance() { |
| 165 |
if ( ! wp_doing_ajax() ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
Ajax::instance(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get the singleton instance of the Loader class. |
| 173 |
* |
| 174 |
* @return Loader Singleton instance of the Loader class. |
| 175 |
*/ |
| 176 |
public static function instance(): Loader { |
| 177 |
if ( self::$instance === null ) { |
| 178 |
self::$instance = new self(); |
| 179 |
} |
| 180 |
|
| 181 |
return self::$instance; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Activate the plugin. |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function activate(): void { |
| 190 |
Activator::instance()->activate(); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Deactivate the plugin. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function deactivate(): void { |
| 199 |
// On Deactivation of the plugin. |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Autoload classes based on their namespace and path. |
| 204 |
* |
| 205 |
* @param string $class The fully-qualified class name. |
| 206 |
* @return void |
| 207 |
*/ |
| 208 |
public function autoload( string $class ): void { |
| 209 |
// Define namespace and base directory for your project. |
| 210 |
$namespace = 'SureMails\\Inc\\'; |
| 211 |
$base_directory = SUREMAILS_DIR . 'inc/'; |
| 212 |
|
| 213 |
// Ensure the class belongs to the current namespace. |
| 214 |
if ( strpos( $class, $namespace ) !== 0 ) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
// Strip the namespace prefix from the class. |
| 219 |
$relative_class = substr( $class, strlen( $namespace ) ); |
| 220 |
|
| 221 |
// Convert namespace separators to directory separators. |
| 222 |
// and handle CamelCase to kebab-case conversion for filenames. |
| 223 |
$filename = preg_replace( |
| 224 |
[ '/^' . preg_quote( $namespace, '/' ) . '/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ], |
| 225 |
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ], |
| 226 |
$relative_class |
| 227 |
); |
| 228 |
|
| 229 |
// Ensure the filename is in lowercase. |
| 230 |
if ( is_string( $filename ) ) { |
| 231 |
|
| 232 |
$filename = strtolower( $filename ); |
| 233 |
|
| 234 |
// Construct the full file path. |
| 235 |
$file = $base_directory . $filename . '.php'; |
| 236 |
|
| 237 |
// Include the file if it exists and is readable. |
| 238 |
if ( is_readable( $file ) ) { |
| 239 |
require_once $file; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Load Plugin Text Domain. |
| 246 |
* This will load the translation textdomain depending on the file priorities. |
| 247 |
* 1. Global Languages /wp-content/languages/suremails/ folder |
| 248 |
* 2. Local directory /wp-content/plugins/suremails/languages/ folder |
| 249 |
* |
| 250 |
* @since 0.0.1 |
| 251 |
* @return void |
| 252 |
*/ |
| 253 |
public function load_textdomain(): void { |
| 254 |
// Default languages directory. |
| 255 |
$lang_dir = SUREMAILS_DIR . 'languages/'; |
| 256 |
|
| 257 |
/** |
| 258 |
* Filters the languages directory path to use for plugin. |
| 259 |
* |
| 260 |
* @param string $lang_dir The languages directory path. |
| 261 |
*/ |
| 262 |
$lang_dir = apply_filters( 'suremails_languages_directory', $lang_dir ); |
| 263 |
|
| 264 |
// Traditional WordPress plugin locale filter. |
| 265 |
global $wp_version; |
| 266 |
|
| 267 |
$get_locale = get_locale(); |
| 268 |
|
| 269 |
if ( $wp_version >= 4.7 ) { |
| 270 |
$get_locale = get_user_locale(); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Language Locale for plugin |
| 275 |
* |
| 276 |
* Uses get_user_locale()` in WordPress 4.7 or greater, |
| 277 |
* otherwise uses `get_locale()`. |
| 278 |
*/ |
| 279 |
$locale = apply_filters( 'plugin_locale', $get_locale, 'suremails' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook |
| 280 |
$mofile = sprintf( '%1$s-%2$s.mo', 'suremails', $locale ); |
| 281 |
|
| 282 |
// Setup paths to current locale file. |
| 283 |
$mofile_global = WP_LANG_DIR . '/plugins/' . $mofile; |
| 284 |
$mofile_local = $lang_dir . $mofile; |
| 285 |
|
| 286 |
if ( file_exists( $mofile_global ) ) { |
| 287 |
// Look in global /wp-content/languages/suremails/ folder. |
| 288 |
load_textdomain( 'suremails', $mofile_global ); |
| 289 |
} elseif ( file_exists( $mofile_local ) ) { |
| 290 |
// Look in local /wp-content/plugins/suremails/languages/ folder. |
| 291 |
load_textdomain( 'suremails', $mofile_local ); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Register abilities for the WordPress Abilities API. |
| 297 |
* |
| 298 |
* Graceful degradation: if Abilities API is not available (WordPress < 6.9), |
| 299 |
* the plugin functions normally — abilities simply aren't registered. |
| 300 |
* |
| 301 |
* @since 1.9.4 |
| 302 |
* @return void |
| 303 |
*/ |
| 304 |
private function register_abilities(): void { |
| 305 |
if ( |
| 306 |
! defined( 'SUREMAILS_ABILITY_API' ) || |
| 307 |
! SUREMAILS_ABILITY_API || // @phpstan-ignore booleanNot.alwaysFalse (defensive check — constant may be overridden) |
| 308 |
! function_exists( 'wp_register_ability' ) |
| 309 |
) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
$ability = new Ability(); |
| 314 |
add_action( 'wp_abilities_api_categories_init', [ $ability, 'register_categories' ] ); |
| 315 |
add_action( 'wp_abilities_api_init', [ $ability, 'register' ] ); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Register the autoloader. |
| 320 |
* |
| 321 |
* @return void |
| 322 |
*/ |
| 323 |
private function register_autoload(): void { |
| 324 |
spl_autoload_register( [ $this, 'autoload' ] ); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
// Initialize the loader singleton. |
| 329 |
Loader::instance(); |
| 330 |
|