class-admin.php
5.21 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
<?php
/**
* Class MC4WP_Integration_Admin
*
* @ignore
* @access private
*/
class MC4WP_Integration_Admin
{
/**
* @var MC4WP_Integration_Manager
*/
protected $integrations;
/**
* @var MC4WP_Admin_Messages
*/
protected $messages;
/**
* @param MC4WP_Integration_Manager $integrations
* @param MC4WP_Admin_Messages $messages
*/
public function __construct(MC4WP_Integration_Manager $integrations, MC4WP_Admin_Messages $messages)
{
$this->integrations = $integrations;
$this->messages = $messages;
}
/**
* Add hooks
*/
public function add_hooks()
{
add_action('admin_init', array( $this, 'register_setting' ));
add_action('mc4wp_admin_enqueue_assets', array( $this, 'enqueue_assets' ), 10, 2);
add_filter('mc4wp_admin_menu_items', array( $this, 'add_menu_item' ));
}
/**
* Register settings
*/
public function register_setting()
{
register_setting('mc4wp_integrations_settings', 'mc4wp_integrations', array( $this, 'save_integration_settings' ));
}
/**
* Enqueue assets
*
* @param string $suffix
* @param string $page
*
* @return void
*/
public function enqueue_assets($suffix, $page)
{
// only load on integrations pages
if ($page !== 'integrations') {
return;
}
wp_register_script('mc4wp-integrations-admin', mc4wp_plugin_url('assets/js/integrations-admin.js'), array( 'mc4wp-admin' ), MC4WP_VERSION, true);
wp_enqueue_script('mc4wp-integrations-admin');
}
/**
* @param array $items
*
* @return array
*/
public function add_menu_item($items)
{
$items[] = array(
'title' => esc_html__('Integrations', 'mailchimp-for-wp'),
'text' => esc_html__('Integrations', 'mailchimp-for-wp'),
'slug' => 'integrations',
'callback' => array( $this, 'show_integrations_page' ),
'position' => 20,
);
return $items;
}
/**
* @param array $new_settings
* @return array
*/
public function save_integration_settings(array $new_settings)
{
$integrations = $this->integrations->get_all();
$current_settings = (array) get_option('mc4wp_integrations', array());
$settings = array();
foreach ($integrations as $slug => $integration) {
$settings[ $slug ] = $this->parse_integration_settings($slug, $current_settings, $new_settings);
}
return $settings;
}
/**
* @since 3.0
* @param string $slug
* @param array $current
* @param array $new
*
* @return array
*/
protected function parse_integration_settings($slug, $current, $new)
{
$settings = array();
// start with current settings
if (! empty($current[ $slug ])) {
$settings = $current[ $slug ];
}
// if no new settings were given, return current settings.
if (empty($new[ $slug ])) {
return $settings;
}
// merge new settings with currents (to allow passing partial setting arrays)
$settings = array_merge($settings, $new[ $slug ]);
// sanitize settings
$settings = $this->sanitize_integration_settings($settings);
return $settings;
}
/**
* @param array $settings
* @return array
*/
protected function sanitize_integration_settings($settings)
{
// filter null values from lists setting
if (! empty($settings['lists'])) {
$settings['lists'] = array_filter($settings['lists']);
} else {
$settings['lists'] = array();
}
$settings['label'] = strip_tags($settings['label'], '<strong><b><br><a><script><u><em><i><span><img>');
if (! current_user_can('unfiltered_html')) {
$settings['label'] = mc4wp_kses($settings['label']);
}
return $settings;
}
/**
* Show the Integration Settings page
*
* @internal
*/
public function show_integrations_page()
{
if (! empty($_GET['integration'])) {
$this->show_integration_settings_page($_GET['integration']);
return;
}
// get all installed & enabled integrations
$enabled_integrations = $this->integrations->get_enabled_integrations();
// get all integrations but remove enabled integrations from the resulting array
$integrations = $this->integrations->get_all();
require __DIR__ . '/views/integrations.php';
}
/**
* @param string $slug
*
* @internal
*/
public function show_integration_settings_page($slug)
{
try {
$integration = $this->integrations->get($slug);
} catch (Exception $e) {
echo sprintf('<h3>Integration not found.</h3><p>No integration with slug <strong>%s</strong> was found.</p>', esc_html($slug));
return;
}
$opts = $integration->options;
$mailchimp = new MC4WP_MailChimp();
$lists = $mailchimp->get_lists();
require __DIR__ . '/views/integration-settings.php';
}
}