-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMailjet.php
More file actions
323 lines (296 loc) · 10.5 KB
/
Mailjet.php
File metadata and controls
323 lines (296 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
namespace MailjetWp\MailjetPlugin\Includes;
use MailjetWp\MailjetPlugin\Admin\MailjetAdmin;
use MailjetWp\MailjetPlugin\Front\MailjetPublic;
use MailjetWp\MailjetPlugin\Includes\SettingsPages\IntegrationsSettings;
use MailjetWp\MailjetPlugin\Includes\SettingsPages\UserAccessSettings;
use MailjetWp\MailjetPlugin\Includes\SettingsPages\WooCommerceSettings;
/**
* The core plugin class.
*
* This is used to define internationalization, admin-specific hooks, and
* public-facing site hooks.
*
* Also maintains the unique identifier of this plugin as well as the current
* version of the plugin.
*
* @since 5.0.0
* @package Mailjet
* @subpackage Mailjet/includes
* @author Your Name <email@example.com>
*/
class Mailjet {
/**
* The loader that's responsible for maintaining and registering all hooks that power
* the plugin.
*
* @since 5.0.0
* @access protected
* @var Mailjet_Loader $loader Maintains and registers all hooks for the plugin.
*/
protected $loader;
/**
* The unique identifier of this plugin.
*
* @since 5.0.0
* @access protected
* @var string $plugin_name The string used to uniquely identify this plugin.
*/
protected $plugin_name;
/**
* The current version of the plugin.
*
* @since 1.0.0
* @access protected
* @var string $version The current version of the plugin.
*/
protected $version;
/**
* Define the core functionality of the plugin.
*
* Set the plugin name and the plugin version that can be used throughout the plugin.
* Load the dependencies, define the locale, and set the hooks for the admin area and
* the public-facing side of the site.
*
* @since 5.0.0
*/
public function __construct()
{
if (\defined('MAILJET_VERSION')) {
$this->version = MAILJET_VERSION;
} else {
$this->version = '6.1.2';
}
$this->plugin_name = 'mailjet';
$this->load_dependencies();
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->addMailjetMenu();
$this->addMailjetSettings();
$this->addMailjetPHPMailer();
$this->registerMailjetWidget();
add_shortcode('mailjet_form_builder', array($this, 'display_mailjet_form_builder_widget'));
}
/**
* @param array $attr
* @param string $tag
* @return false|string
*/
public static function display_mailjet_form_builder_widget(array $attr = array(), string $tag = '')
{
\extract(shortcode_atts(array('widget_id' => null), $attr, $tag));
// GET All Mailjet widgets - to find the one that user actually configured with the shortcode
$instance = self::getOption('mailjet_form_builder_widget_options');
// In case we don't have 'widget_id' attribute in the shortcode defined by user - we use the first widget id from the collection
if (empty($widget_id)) {
$widgetIds = array();
foreach (array_keys($instance) as $key) {
if (is_int($key)) {
$widgetIds[] = $key;
}
}
$widget_id = min($widgetIds);
}
if (isset($instance[(int)$widget_id])) {
ob_start();
the_widget('MailjetWp\\MailjetPlugin\\WidgetFormBuilder\\WP_Mailjet_FormBuilder_Widget', $instance[(int)$widget_id]);
return ob_get_clean();
}
return false;
}
/**
* Load the required dependencies for this plugin.
*
* Include the following files that make up the plugin:
*
* - Mailjet_Loader. Orchestrates the hooks of the plugin.
* - Mailjeti18n. Defines internationalization functionality.
* - Mailjet_Admin. Defines all hooks for the admin area.
* - Mailjet_Public. Defines all hooks for the public side of the site.
*
* Create an instance of the loader which will be used to register the hooks
* with WordPress.
*
* @since 5.0.0
* @access private
*/
private function load_dependencies()
{
$this->loader = new MailjetLoader();
}
/**
* Define the locale for this plugin for internationalization.
*
* Uses the Mailjeti18n class in order to set the domain and to register the hook
* with WordPress.
*
* @since 5.0.0
* @access private
*/
private function set_locale()
{
$plugin_i18n = new Mailjeti18n();
$this->loader->add_action('plugins_loaded', $plugin_i18n, 'load_plugin_textdomain');
}
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks()
{
$plugin_admin = new MailjetAdmin($this->get_plugin_name(), $this->get_version());
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
$this->loader->add_action('admin_post_user_access_settings_custom_hook', new UserAccessSettings(), 'user_access_post_handler');
$this->loader->add_action('admin_post_integrationsSettings_custom_hook', new IntegrationsSettings(), 'integrations_post_handler');
if (self::getOption('activate_mailjet_woo_integration') == '1') {
$this->addWoocommerceActions();
}
}
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 5.0.0
* @access private
*/
private function define_public_hooks()
{
$plugin_public = new MailjetPublic($this->get_plugin_name(), $this->get_version());
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles');
$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts');
}
/**
* @return void
*/
private function addMailjetMenu(): void
{
$plugin_menu = new MailjetMenu();
$this->loader->add_action('admin_menu', $plugin_menu, 'display_menu');
}
/**
* @return void
*/
private function addMailjetSettings(): void
{
$plugin_settings = new MailjetSettings();
$this->loader->add_action('admin_init', $plugin_settings, 'mailjet_settings_admin_init');
$this->loader->add_action('init', $plugin_settings, 'mailjet_settings_init');
}
/**
* @return void
*/
private function addMailjetPHPMailer(): void
{
$plugin_mails = new MailjetMail();
$this->loader->add_action('phpmailer_init', $plugin_mails, 'phpmailer_init_smtp');
$this->loader->add_action('wp_mail_failed', $plugin_mails, 'wp_mail_failed_cb');
}
/**
* @return void
*/
private function registerMailjetWidget(): void
{
$this->loader->add_action('widgets_init', $this, 'wp_mailjet_register_widgets');
}
/**
* @return void
*/
public function wp_mailjet_register_widgets()
{
$widgetFormBuilder = 'MailjetWp\\MailjetPlugin\\WidgetFormBuilder\\WP_Mailjet_FormBuilder_Widget';
register_widget($widgetFormBuilder);
}
/**
* Run the loader to execute all of the hooks with WordPress.
*
* @since 5.0.0
*/
public function run(): void
{
$this->loader->run();
}
/**
* The name of the plugin used to uniquely identify it within the context of
* WordPress and to define internationalization functionality.
*
* @return string The name of the plugin.
* @since 1.0.0
*/
public function get_plugin_name(): string
{
return $this->plugin_name;
}
/**
* The reference to the class that orchestrates the hooks with the plugin.
*
* @return Mailjet_Loader Orchestrates the hooks of the plugin.
* @since 5.0.0
*/
public function get_loader()
{
return $this->loader;
}
/**
* Retrieve the version number of the plugin.
*
* @return string The version number of the plugin.
* @since 5.0.0
*/
public function get_version(): string
{
return $this->version;
}
/**
* @return void
*/
private function addWoocommerceActions(): void
{
$wooCommerceSettings = WooCommerceSettings::getInstance();
if (isset($_POST['action']) && $_POST['action'] === 'order_notification_settings_custom_hook') {
$wooCommerceSettings->orders_automation_settings_post();
}
$this->loader->add_action('admin_post_abandoned_cart_settings_custom_hook', $wooCommerceSettings, 'abandoned_cart_settings_post');
if (self::getOption('mailjet_woo_edata_sync') === '1') {
$this->loader->add_action('woocommerce_order_status_changed', $wooCommerceSettings, 'order_edata_sync', 10, 1);
$this->loader->add_action('woocommerce_cheque_process_payment_order_status', $wooCommerceSettings, 'paid_by_cheque_order_edata_sync', 10, 2);
}
$activeActions = self::getOption('mailjet_wc_active_hooks');
$abandonedCartActiveActions = self::getOption('mailjet_wc_abandoned_cart_active_hooks');
if ($activeActions && !empty($activeActions)) {
foreach ($activeActions as $action) {
$this->loader->add_action($action['hook'], $wooCommerceSettings, $action['callable'], 10, 2);
}
}
if ($abandonedCartActiveActions && !empty($abandonedCartActiveActions)) {
foreach ($abandonedCartActiveActions as $action) {
$this->loader->add_action($action['hook'], $wooCommerceSettings, $action['callable'], 10, 2);
}
}
}
/**
* @param string $key
* @return mixed
*/
public static function getOption(string $key)
{
if (!is_multisite()) {
return get_option($key);
}
$mainSiteId = get_main_network_id();
switch_to_blog($mainSiteId);
// If main site has multisite support enabled, we should use the main site options
if (get_option('mailjet_multisite_support') === 'on') {
$optionValue = get_option($key);
restore_current_blog();
return $optionValue;
}
// If main site has multisite support disabled, we should use the current site options
restore_current_blog();
return get_option($key);
}
}