| 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\Admin\Activator; |
| 13 |
use SureMails\Inc\Admin\Crons; |
| 14 |
use SureMails\Inc\Admin\Plugin; |
| 15 |
use SureMails\Inc\Ajax\Ajax; |
| 16 |
use SureMails\Inc\API\Api_Init; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class Loader |
| 20 |
* |
| 21 |
* The Loader class is responsible for loading the SureMails plugin, handling translations, |
| 22 |
* autoloading, and initializing components like email settings, connections, and activation hooks. |
| 23 |
* |
| 24 |
* @package SureMails |
| 25 |
*/ |
| 26 |
class Loader { |
| 27 |
/** |
| 28 |
* Singleton instance of the Loader class. |
| 29 |
* |
| 30 |
* @var Loader|null |
| 31 |
*/ |
| 32 |
private static $instance = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Loader constructor. |
| 36 |
* Private to enforce singleton pattern. |
| 37 |
*/ |
| 38 |
private function __construct() { |
| 39 |
/** |
| 40 |
* Register the autoloader. |
| 41 |
*/ |
| 42 |
$this->register_autoload(); |
| 43 |
|
| 44 |
/** |
| 45 |
* Load the plugin textdomain for translations. |
| 46 |
*/ |
| 47 |
add_action( 'plugins_loaded', [ $this, 'load_textdomain' ] ); |
| 48 |
add_action( 'plugins_loaded', [ $this, 'setup' ] ); |
| 49 |
add_action( 'plugin_loaded', [ $this, 'setup_ajax_instance' ] ); |
| 50 |
/** |
| 51 |
* The code that runs during plugin activation. |
| 52 |
*/ |
| 53 |
register_activation_hook( SUREMAILS_FILE, [ $this, 'activate' ] ); |
| 54 |
|
| 55 |
/** |
| 56 |
* The code that runs during plugin deactivation. |
| 57 |
*/ |
| 58 |
register_deactivation_hook( SUREMAILS_FILE, [ $this, 'deactivate' ] ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Setup the plugin, register hooks, and initialize components. |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function setup(): void { |
| 67 |
// Bail if doing AJAX. |
| 68 |
if ( wp_doing_ajax() ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
// Admin loaders. |
| 72 |
if ( is_admin() ) { |
| 73 |
Plugin::instance(); |
| 74 |
} |
| 75 |
Crons::instance(); |
| 76 |
Api_Init::instance(); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Setup the Ajax instance. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function setup_ajax_instance() { |
| 85 |
if ( ! wp_doing_ajax() ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
Ajax::instance(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the singleton instance of the Loader class. |
| 93 |
* |
| 94 |
* @return Loader Singleton instance of the Loader class. |
| 95 |
*/ |
| 96 |
public static function instance(): Loader { |
| 97 |
if ( self::$instance === null ) { |
| 98 |
self::$instance = new self(); |
| 99 |
} |
| 100 |
|
| 101 |
return self::$instance; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Activate the plugin. |
| 106 |
* |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function activate(): void { |
| 110 |
Activator::instance()->activate(); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Deactivate the plugin. |
| 115 |
* |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public function deactivate(): void { |
| 119 |
// On Deactivation of the plugin. |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Autoload classes based on their namespace and path. |
| 124 |
* |
| 125 |
* @param string $class The fully-qualified class name. |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function autoload( string $class ): void { |
| 129 |
// Define namespace and base directory for your project. |
| 130 |
$namespace = 'SureMails\\Inc\\'; |
| 131 |
$base_directory = SUREMAILS_DIR . 'inc/'; |
| 132 |
|
| 133 |
// Ensure the class belongs to the current namespace. |
| 134 |
if ( strpos( $class, $namespace ) !== 0 ) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// Strip the namespace prefix from the class. |
| 139 |
$relative_class = substr( $class, strlen( $namespace ) ); |
| 140 |
|
| 141 |
// Convert namespace separators to directory separators. |
| 142 |
// and handle CamelCase to kebab-case conversion for filenames. |
| 143 |
$filename = preg_replace( |
| 144 |
[ '/^' . preg_quote( $namespace, '/' ) . '/', '/([a-z])([A-Z])/', '/_/', '/\\\/' ], |
| 145 |
[ '', '$1-$2', '-', DIRECTORY_SEPARATOR ], |
| 146 |
$relative_class |
| 147 |
); |
| 148 |
|
| 149 |
// Ensure the filename is in lowercase. |
| 150 |
if ( is_string( $filename ) ) { |
| 151 |
|
| 152 |
$filename = strtolower( $filename ); |
| 153 |
|
| 154 |
// Construct the full file path. |
| 155 |
$file = $base_directory . $filename . '.php'; |
| 156 |
|
| 157 |
// Include the file if it exists and is readable. |
| 158 |
if ( is_readable( $file ) ) { |
| 159 |
require_once $file; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Load Plugin Text Domain. |
| 166 |
* This will load the translation textdomain depending on the file priorities. |
| 167 |
* 1. Global Languages /wp-content/languages/suremails/ folder |
| 168 |
* 2. Local directory /wp-content/plugins/suremails/languages/ folder |
| 169 |
* |
| 170 |
* @since 0.0.1 |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
public function load_textdomain(): void { |
| 174 |
// Default languages directory. |
| 175 |
$lang_dir = SUREMAILS_DIR . 'languages/'; |
| 176 |
|
| 177 |
/** |
| 178 |
* Filters the languages directory path to use for plugin. |
| 179 |
* |
| 180 |
* @param string $lang_dir The languages directory path. |
| 181 |
*/ |
| 182 |
$lang_dir = apply_filters( 'suremails_languages_directory', $lang_dir ); |
| 183 |
|
| 184 |
// Traditional WordPress plugin locale filter. |
| 185 |
global $wp_version; |
| 186 |
|
| 187 |
$get_locale = get_locale(); |
| 188 |
|
| 189 |
if ( $wp_version >= 4.7 ) { |
| 190 |
$get_locale = get_user_locale(); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Language Locale for plugin |
| 195 |
* |
| 196 |
* Uses get_user_locale()` in WordPress 4.7 or greater, |
| 197 |
* otherwise uses `get_locale()`. |
| 198 |
*/ |
| 199 |
$locale = apply_filters( 'plugin_locale', $get_locale, 'suremails' );//phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- wordpress hook |
| 200 |
$mofile = sprintf( '%1$s-%2$s.mo', 'suremails', $locale ); |
| 201 |
|
| 202 |
// Setup paths to current locale file. |
| 203 |
$mofile_global = WP_LANG_DIR . '/plugins/' . $mofile; |
| 204 |
$mofile_local = $lang_dir . $mofile; |
| 205 |
|
| 206 |
if ( file_exists( $mofile_global ) ) { |
| 207 |
// Look in global /wp-content/languages/suremails/ folder. |
| 208 |
load_textdomain( 'suremails', $mofile_global ); |
| 209 |
} elseif ( file_exists( $mofile_local ) ) { |
| 210 |
// Look in local /wp-content/plugins/suremails/languages/ folder. |
| 211 |
load_textdomain( 'suremails', $mofile_local ); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Register the autoloader. |
| 217 |
* |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
private function register_autoload(): void { |
| 221 |
spl_autoload_register( [ $this, 'autoload' ] ); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// Initialize the loader singleton. |
| 226 |
Loader::instance(); |
| 227 |
|