-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-setup.php
More file actions
264 lines (195 loc) · 6.81 KB
/
Copy pathclass-setup.php
File metadata and controls
264 lines (195 loc) · 6.81 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
<?php
/**
* Setup Welcome Email Editor plugin.
*
* @package Welcome_Email_Editor
*/
namespace Weed;
use Weed\Helpers\Content_Helper;
defined( 'ABSPATH' ) || die( "Can't access directly" );
/**
* Class to set up Welcome Email Editor plugin.
*/
class Setup {
/**
* The class instance.
*
* @var object
*/
public static $instance;
/**
* Init the class setup.
*/
public static function init() {
$class = self::get_instance();
add_action( 'plugins_loaded', array( $class, 'setup' ) );
}
/**
* Get instance of the class.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Set up the class.
*/
public function setup() {
// Always load latest settings from DB at the start of each request.
$this->set_initial_data();
add_action( 'updated_option_weed_settings', [ $this, 'handle_updated_option' ], 10, 3 );
add_action( 'init', array( $this, 'update_initial_data' ), 1 );
add_action( 'init', array( $this, 'register_action_links' ) );
add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) );
$this->load_modules();
register_deactivation_hook( WEED_PLUGIN_BASENAME, array( $this, 'deactivation' ), 20 );
}
/**
* Provide initial data for the plugin.
* This is optimal strategy to only get_option once across modules.
*
* At this point, we don't use the real default values
* because it contains translation functions calling.
*/
private function set_initial_data() {
$settings = get_option( 'weed_settings', array() );
$defaults = array(
// General settings.
'from_email' => '',
'force_from_email' => false,
'from_name' => '',
'force_from_name' => false,
'content_type' => 'text',
// Test SMTP settings.
'test_smtp_recipient_email' => '',
// SMTP settings.
'smtp_host' => '',
'smtp_port' => 25,
'smtp_encryption' => '',
'smtp_username' => '',
'smtp_password' => '',
// Welcome email settings - for user.
'user_welcome_email_subject' => '',
'user_welcome_email_body' => '',
'user_welcome_email_attachment_url' => '',
'user_welcome_email_reply_to_email' => '',
'user_welcome_email_reply_to_name' => '',
'user_welcome_email_additional_headers' => '',
// Welcome email settings - for admin.
'admin_new_user_notif_email_subject' => '',
'admin_new_user_notif_email_body' => '',
'admin_new_user_notif_email_custom_recipients' => '',
// Reset password email settings.
'reset_password_email_subject' => '',
'reset_password_email_body' => '',
);
$values = wp_parse_args( $settings, $defaults );
Vars::set( 'values', $values );
}
/**
* Update initial data of the settings cache.
*/
public function update_initial_data() {
$values = ( new Content_Helper() )->parse_settings();
Vars::set( 'values', $values );
}
/**
* Update settings cache after 'weed_settings' option is updated.
*
* Fires after the value of a 'weed_settings' option has been changed
* via 'updated_option_weed_settings' hook.
*
* @see https://developer.wordpress.org/reference/hooks/update_option_option/
*
* @param mixed $old_value The old option value.
* @param mixed $value The new option value.
* @param string $option Option name.
*/
public function handle_updated_option( $old_value, $value, $option ) {
$values = ( new Content_Helper() )->parse_settings( is_array( $value ) ? $value : array() );
Vars::set( 'values', $values );
}
/**
* Load modules.
*/
public function load_modules() {
$modules = array();
$modules['Weed\\Settings\\Settings_Module'] = __DIR__ . '/modules/settings/class-settings-module.php';
$modules['Weed\\Smtp\\Smtp_Module'] = __DIR__ . '/modules/smtp/class-smtp-module.php';
$modules['Weed\\Mailjet_Api\\Mailjet_Api_Module'] = __DIR__ . '/modules/mailjet-api/class-mailjet-api-module.php';
$modules['Weed\\Logs\\Logs_Module'] = __DIR__ . '/modules/logs/class-logs-module.php';
$modules = apply_filters( 'weed_modules', $modules );
foreach ( $modules as $class => $file ) {
$splits = explode( '/', $file );
$module_name = $splits[ count( $splits ) - 2 ];
$filter_name = str_ireplace( '-', '_', $module_name );
$filter_name = 'weed_' . $filter_name;
// We have a filter here weed_$module_name to allow us to prevent loading modules under certain circumstances.
if ( apply_filters( $filter_name, true ) ) {
require_once $file;
$module = new $class();
$module->setup();
}
}
}
/**
* Register plugin action links filter after init hook.
* This ensures translations are loaded before the callback is executed.
*/
public function register_action_links() {
add_filter( 'plugin_action_links_' . WEED_PLUGIN_BASENAME, array( $this, 'plugin_action_links' ) );
}
/**
* Add action links displayed in plugins page.
*
* @param array $links The action links array.
*
* @return array The modified action links array.
*/
public function plugin_action_links( $links ) {
$settings = array( '<a href="' . admin_url( 'admin.php?page=weed_settings' ) . '">' . __( 'Settings', 'welcome-email-editor' ) . '</a>' );
return array_merge( $settings, $links );
}
/**
* Admin body class.
*
* @param string $classes The existing body classes.
*
* @return string The body classes.
*/
public function admin_body_class( $classes ) {
$screens = array(
'toplevel_page_weed_settings',
);
$screen = get_current_screen();
if ( ! in_array( $screen->id, $screens, true ) ) {
return $classes;
}
$classes .= ' heatbox-admin has-header';
return $classes;
}
/**
* Plugin deactivation.
*/
public function deactivation() {
$settings = get_option( 'weed_settings' );
$remove_on_uninstall = isset( $settings['remove_on_uninstall'] ) ? true : false;
if ( $remove_on_uninstall ) {
// Delete all settings.
delete_option( 'weed_settings' );
delete_option( 'weed_v5_compatibility' );
// Remove all email logs.
$email_logs = get_posts( array(
'post_type' => 'weed_email_logs',
'numberposts' => -1, // Retrieve all email logs.
'post_status' => 'any', // Includes all statuses.
));
// Loop through each email log and delete it.
foreach ( $email_logs as $email_log ) {
wp_delete_post( $email_log->ID, true ); // 'true' forces deletion without sending to trash.
}
}
}
}