| 1 |
<?php |
| 2 |
namespace YayMail; |
| 3 |
|
| 4 |
use YayMail\Utils\Helpers; |
| 5 |
use YayMail\Emails\EmailsLoader; |
| 6 |
use YayMail\Engine\Backend\SettingsPage; |
| 7 |
use YayMail\Engine\RestAPI; |
| 8 |
use YayMail\Integrations\IntegrationsLoader; |
| 9 |
use YayMail\PostTypes\TemplatePostType; |
| 10 |
use YayMail\Shortcodes\ShortcodesLoader; |
| 11 |
use YayMail\Utils\SingletonTrait; |
| 12 |
use YayMail\TemplatePatterns\PatternsLoader; |
| 13 |
use YayMail\TemplatePatterns\SectionTemplatesLoader; |
| 14 |
use YayMail\PreviewEmail\PreviewEmailsLoader; |
| 15 |
use YayMail\Notices\NoticeMain; |
| 16 |
use YayMail\SocialIcons\SocialIconEndpoint; |
| 17 |
use YayMail\TemplateLibrary\LibraryTemplateSchema; |
| 18 |
use YayMail\TemplateLibrary\TemplateLibraryLoader; |
| 19 |
/** |
| 20 |
* YayMail Plugin Initializer |
| 21 |
* |
| 22 |
* @method static Initialize get_instance() |
| 23 |
*/ |
| 24 |
class Initialize { |
| 25 |
|
| 26 |
use SingletonTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* The Constructor that load the engine classes |
| 30 |
*/ |
| 31 |
protected function __construct() { |
| 32 |
// Procedural helpers (yaymail_version(), yaymail_is_wc_installed(), ...) are used by |
| 33 |
// every hook callback below, so load them here rather than inside init_core(), whose |
| 34 |
// hook is filterable and may run after init_modules(). |
| 35 |
require_once Helpers::get_plugin_path() . 'src/Functions.php'; |
| 36 |
|
| 37 |
I18n::get_instance(); |
| 38 |
|
| 39 |
/** |
| 40 |
* Handle init core |
| 41 |
* Emails, Templates, Elements, Shortcodes, Integrations |
| 42 |
* $hook_name => $priority |
| 43 |
*/ |
| 44 |
$initialization_core_hooks = [ |
| 45 |
apply_filters( 'yaymail_temp_init_hook_name', 'init' ) => 10, |
| 46 |
// Integrate for Mastercard Gateway plugin |
| 47 |
'woocommerce_api_mastercard_gateway' => 10, |
| 48 |
]; |
| 49 |
foreach ( $initialization_core_hooks as $hook => $priority ) { |
| 50 |
add_action( $hook, [ $this, 'init_core' ], $priority ?? 10 ); |
| 51 |
} |
| 52 |
|
| 53 |
add_action( 'init', [ $this, 'init_modules' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
public function init_core() { |
| 57 |
do_action( 'yaymail_init_start' ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Core Integrations |
| 61 |
*/ |
| 62 |
IntegrationsLoader::get_instance(); |
| 63 |
|
| 64 |
EmailsLoader::get_instance(); |
| 65 |
ShortcodesLoader::get_instance(); |
| 66 |
} |
| 67 |
|
| 68 |
public function init_modules() { |
| 69 |
|
| 70 |
$version_current = yaymail_version(); |
| 71 |
$version_old = get_option( 'yaymail_version' ); |
| 72 |
$version_current_backup = get_option( 'yaymail_version_backup' ); |
| 73 |
|
| 74 |
if ( $version_current !== $version_old && yaymail_is_wc_installed() ) { |
| 75 |
if ( $version_current_backup !== $version_current ) { |
| 76 |
\YayMail\Migrations\MainMigration::get_instance()->migrate(); |
| 77 |
|
| 78 |
update_option( 'yaymail_version', $version_current ); |
| 79 |
update_option( 'yaymail_version_backup', $version_current ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( yaymail_is_wc_installed() ) { |
| 84 |
WooHandler::get_instance(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Preview Email loader |
| 89 |
*/ |
| 90 |
|
| 91 |
PreviewEmailsLoader::get_instance(); |
| 92 |
|
| 93 |
/** |
| 94 |
* Core core filters |
| 95 |
*/ |
| 96 |
|
| 97 |
SectionTemplatesLoader::get_instance(); |
| 98 |
PatternsLoader::get_instance(); |
| 99 |
|
| 100 |
// Template Library only ships WooCommerce order-email designs (Completed, |
| 101 |
// RefundedOrder, NewOrder, ...). Skip its directory scan/class instantiation |
| 102 |
// (41 files, ~23k lines) and the per-request "SHOW TABLES" check when |
| 103 |
// WooCommerce isn't active -- it noticeably slows down every admin page load |
| 104 |
// on WP-only (yaymagic-email-builder) sites for a feature they can't use. |
| 105 |
if ( yaymail_is_wc_installed() ) { |
| 106 |
TemplateLibraryLoader::get_instance(); |
| 107 |
LibraryTemplateSchema::maybe_create_table(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Initialize rest api |
| 112 |
*/ |
| 113 |
RestAPI::get_instance(); |
| 114 |
|
| 115 |
/** |
| 116 |
* Initialize pages |
| 117 |
*/ |
| 118 |
SettingsPage::get_instance(); |
| 119 |
|
| 120 |
TemplatePostType::get_instance(); |
| 121 |
Ajax::get_instance(); |
| 122 |
|
| 123 |
/** |
| 124 |
* Notices |
| 125 |
*/ |
| 126 |
NoticeMain::get_instance(); |
| 127 |
|
| 128 |
/** |
| 129 |
* Social icon dynamic tint endpoint |
| 130 |
*/ |
| 131 |
SocialIconEndpoint::get_instance(); |
| 132 |
|
| 133 |
do_action( 'yaymail_loaded' ); |
| 134 |
} |
| 135 |
|
| 136 |
public function current_yaymail_version() { |
| 137 |
$version = 'yaymail/yaymail.php'; |
| 138 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 139 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 140 |
} |
| 141 |
|
| 142 |
$yaymail_files = [ |
| 143 |
'yaymail/yaymail.php', |
| 144 |
'yaymail-pro/yaymail.php', |
| 145 |
'email-customizer-for-woocommerce/yaymail.php', |
| 146 |
]; |
| 147 |
|
| 148 |
foreach ( $yaymail_files as $file ) { |
| 149 |
if ( is_plugin_active( $file ) || is_plugin_active_for_network( $file ) ) { |
| 150 |
$version = $file; |
| 151 |
break; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return $version; |
| 156 |
} |
| 157 |
} |
| 158 |
|