-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-ml-slack-admin.php
335 lines (288 loc) · 8.53 KB
/
class-ml-slack-admin.php
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
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/**
* Class ML_Slack_Admin
*/
class ML_Slack_Admin {
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
static $slug = 'ml_admin';
static $prefix = 'ml_admin_';
static $option = 'ml_slack_options';
/**
* Start up
*/
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page() {
// This page will be under "Settings"
add_options_page(
'ML Slack Settings',
'ML Slack Admin',
'manage_options',
static::$slug,
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page() {
// Set class property
$this->options = get_option( static::$option );
?>
<div class="wrap">
<h1>My Settings</h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( static::$prefix . 'option_group' );
do_settings_sections( static::$slug );
submit_button();
?>
</form>
<script type="text/javascript">
(function( $ ) {
$.toggleShowPassword = function( options ) {
var settings = $.extend( {
field: "#password",
}, options );
var field = $( settings.field );
var control = $( field ).next( '.showhide' );
control.bind( 'click', function() {
if ( control.is( ':checked' ) ) {
field.attr( 'type', 'text' );
} else {
field.attr( 'type', 'password' );
}
} )
};
$.toggleShowPassword( {
field: '#slack_login_pass'
} );
$.toggleShowPassword( {
field: '#slack_mojibot_token'
} );
$.toggleShowPassword( {
field: '#google_api_key'
} );
}( jQuery ));
</script>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init() {
register_setting(
static::$prefix . 'option_group', // Option group
static::$option, // Option name
array( $this, 'sanitize' ) // Sanitize
);
/**
* Slack settings section
*/
add_settings_section(
'slack_instance', // ID
'Slack Instance Details', // Title
array( $this, 'print_section_info' ), // Callback
static::$slug // Page
);
add_settings_field(
'slack_domain', // ID
'Slack Domain', // Title
array( $this, 'slack_domain_callback' ), // Callback
static::$slug, // Page
'slack_instance' // Section
);
add_settings_field(
'slack_login_email', // ID
'Slack Login: Email', // Title
array( $this, 'slack_login_email_callback' ), // Callback
static::$slug, // Page
'slack_instance' // Section
);
add_settings_field(
'slack_login_pass',
'Slack Login: Pass',
array( $this, 'slack_login_pass_callback' ),
static::$slug,
'slack_instance'
);
add_settings_field(
'slack_mojibot_token',
'Slack Moijibot Token',
array( $this, 'slack_mojibot_token_callback' ),
static::$slug,
'slack_instance'
);
/**
* Google Settings
*/
add_settings_section(
'google_details', // ID
'Google Details', // Title
array( $this, 'print_section_info' ), // Callback
static::$slug // Page
);
add_settings_field(
'google_search_cx', // ID
'Google Search CX ID', // Title
array( $this, 'google_search_cx_callback' ), // Callback
static::$slug, // Page
'google_details' // Section
);
add_settings_field(
'google_api_key', // ID
'Google API Key', // Title
array( $this, 'google_api_key_callback' ), // Callback
static::$slug, // Page
'google_details' // Section
);
}
/**
* Sanitize each setting field as needed
*
* @param array $input Contains all settings fields as array keys
*
* @return array
*/
public function sanitize( $input ) {
$new_input = array();
if ( isset( $input['slack_domain'] ) ) {
$new_input['slack_domain'] = untrailingslashit( sanitize_url( $input['slack_domain'] ) );
}
if ( isset( $input['slack_login_email'] ) ) {
$new_input['slack_login_email'] = sanitize_email( $input['slack_login_email'] );
}
if ( isset( $input['slack_login_pass'] ) ) {
$new_input['slack_login_pass'] = sanitize_text_field( $input['slack_login_pass'] );
}
if ( isset( $input['slack_mojibot_token'] ) ) {
$new_input['slack_mojibot_token'] = sanitize_text_field( $input['slack_mojibot_token'] );
}
if ( isset( $input['google_search_cx'] ) ) {
$new_input['google_search_cx'] = sanitize_text_field( $input['google_search_cx'] );
}
if ( isset( $input['google_api_key'] ) ) {
$new_input['google_api_key'] = sanitize_text_field( $input['google_api_key'] );
}
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info() {
echo 'Enter your settings below:';
}
/**
* Return a password field with show/hide checkbox
*
* @param $options
* @param $opt_id
*
* @return string
*/
static function get_password_field( $options, $opt_id ) {
$html = sprintf(
'<input type="password" id="' . $opt_id . '" name="' . static::$option . '[' . $opt_id . ']" value="%s" />',
isset( $options[ $opt_id ] ) ? esc_attr( $options[ $opt_id ] ) : ''
);
$html .= '<input type="checkbox" class="showhide" />';
return $html;
}
/**
* Get the settings option array and print one of its values
*/
public function slack_domain_callback() {
$opt_id = 'slack_domain';
printf(
'<input type="text" placeholder="%1$s" class="widefat" id="%2$s" name="%3$s[%2$s]" value="%4$s" />',
'https://myteam.slack.com',
$opt_id,
static::$option,
isset( $this->options[ $opt_id ] ) ? esc_url( $this->options[ $opt_id ] ) : ''
);
echo '<p class="description">Enter the full URL of the slack domain your bot will be posting to.</p>';
}
public function slack_login_email_callback() {
$opt_id = 'slack_login_email';
printf(
'<input type="email" id="' . $opt_id . '" name="' . static::$option . '[' . $opt_id . ']" value="%s" />',
isset( $this->options[ $opt_id ] ) ? esc_attr( $this->options[ $opt_id ] ) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function slack_login_pass_callback() {
echo static::get_password_field( $this->options, 'slack_login_pass' );
}
public function slack_mojibot_token_callback() {
echo static::get_password_field( $this->options, 'slack_mojibot_token' );
?>
<p class="description">The token from the Mojobot\'s outgoing webhook.
When creating the outgoing webhook, you should set the URL to
<code style="white-space: nowrap">https://{yoursite.com}/wp-json/ml/v1/mojibot</code>*
and the following trigger words are supported: </p>
<pre><?php echo ML_Slack::$mojibot_trigger_words; ?></pre>
<p class="description">* It should be noted, I believe Slack requires the site you
use for the API to be secure (https)</p>
<?php
}
public function google_search_cx_callback() {
$opt_id = 'google_search_cx';
printf(
'<input type="text" id="$1%s" name="%2$s[%1$s]" value="%3$s" />',
$opt_id,
static::$option,
isset( $this->options[ $opt_id ] ) ? esc_attr( $this->options[ $opt_id ] ) : ''
);
echo '<p class="description">The Google Search "cx" value/ID.</p>';
}
public function google_api_key_callback() {
echo static::get_password_field( $this->options, 'google_api_key' );
echo '<p class="description">The google API key with search enabled.</p>';
}
/**
* @param string $url
* @param string $emoji_slug
*
* @return bool|int Attachment ID
*/
static function insert_attachment_from_url( $url, $emoji_slug ) {
$response = wp_safe_remote_get( $url );
if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$upload = wp_upload_bits( basename( $url ), null, $response['body'] );
if ( ! empty( $upload['error'] ) ) {
return false;
}
// make sure emoji file size exists
add_image_size( 'slack_emoji', 128, 128, array( 'center', 'center' ) );
$file_path = $upload['file'];
$extension = pathinfo( $file_path, PATHINFO_EXTENSION );
$file_name = sanitize_key( $emoji_slug ) . '.' . $extension;
$file_type = wp_check_filetype( $file_name, null );
// TODO: modify the uploads directory so we can save all the emoji in one place
$wp_upload_dir = wp_upload_dir();
$post_info = array(
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
'post_mime_type' => $file_type['type'],
'post_title' => $emoji_slug,
'post_content' => $url,
'post_status' => 'inherit',
);
// Create the attachment
$attach_id = wp_insert_attachment( $post_info, $file_path, 0 );
return $attach_id;
}
}