-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-snappy.php
377 lines (322 loc) · 12.3 KB
/
wp-snappy.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/*
Plugin Name: WP Snappy
Plugin URI: https://github.com/ReworkLabs/wp-snappy
Description: <a href="http://besnappy.com/">Snappy</a> WordPress Plugin
Version: 0.5
Author: Knight
Author URI: http://imknight.net
License: GNU General Public License v2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Domain Path: /languages
Text Domain: wp-snappy
GitHub Plugin URI: imknight/wp-snappy
GitHub Branch: master
*/
class WP_Snappy {
protected $wp_snappy_settings;
public function __construct() {
add_action( 'admin_init', array( $this, 'wp_snappy_admin_init' ) );
add_action( 'admin_menu', array( $this, 'wp_snappy_add_menus' ) );
$this->wp_snappy_settings = get_option('wp_snappy_settings');
$this->wp_snappy_widget();
}
public function wp_snappy_admin_init(){
$wp_snappy_settings = $this->wp_snappy_settings;
//for color picker
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
add_action( 'admin_footer', array( $this , 'wp_snappy_colorpicker') );
register_setting( 'wp_snappy_settings', 'wp_snappy_settings', array( $this, 'wp_snappy_validate_options' ) );
add_settings_section( 'wp_snappy_settings',__return_null(),'__return_false', 'wp_snappy_widget_section' );
$settings = $this->get_snappy_settings();
foreach ($settings as $id => $option){
add_settings_field(
'wp_snappy_settings[' . $option['id'] . ']',
$option['name'],
array( $this,'wp_snappy_settings_'.$option['type'].'_callback'),
'wp_snappy_widget_section',
'wp_snappy_settings',
$option
);
}
}
public function wp_snappy_widget(){
$wp_snappy_settings = $this->wp_snappy_settings;
//setting not set yet so don't set the widget
if ( empty($wp_snappy_settings['script_url']) OR empty($wp_snappy_settings['data_domain']) ) {
return;
}
//if widget only display on frontend or both
if ( $wp_snappy_settings['display'] == 'both' || $wp_snappy_settings['display'] == 'frontend' ) {
add_action( 'wp_footer', array( $this , 'wp_snappy_widget_display') );
}
//if widget only display on admin or both
if ( $wp_snappy_settings['display'] == 'both' || $wp_snappy_settings['display'] == 'admin' ) {
add_action( 'admin_footer', array( $this , 'wp_snappy_widget_display') );
}
if ( $wp_snappy_settings['hidefaq'] == 1 ) {
add_action( 'wp_footer', array( $this , 'wp_snappy_widget_addon') );
add_action( 'admin_footer', array( $this , 'wp_snappy_widget_addon') );
}
}
public function wp_snappy_widget_display(){
$wp_snappy_settings = $this->wp_snappy_settings;
$widget = '<script src="'.$wp_snappy_settings['script_url'].'"';
$widget .= ' data-domain="'.$wp_snappy_settings['data_domain'].'"';
$widget .= ' data-title="'.$wp_snappy_settings['title'].'"';
$widget .= ' data-position="'.$wp_snappy_settings['position'].'"';
$widget .= ' data-contact="'.$wp_snappy_settings['contact'].'"';
if ($wp_snappy_settings['prepoinfo'] == 1 && is_user_logged_in() ){
$current_user = wp_get_current_user();
$widget .= $current_user ? ' data-name="'.$current_user->display_name.'"' : '';
$widget .= $current_user ? ' data-email="'.$current_user->user_email.'"' : '';
}
$widget .= ' data-background="'.$wp_snappy_settings['background'].'"';
$widget .= isset($wp_snappy_settings['debug']) ? ' data-faq="'.$wp_snappy_settings['faq'].'"' : '';
$widget .= ' data-debug="'.$wp_snappy_settings['debug'].'"';
$widget .= ' data-whitelabel="'.$wp_snappy_settings['whitelabel'].'"';
$widget .= '></script>';
echo $widget;
}
public function wp_snappy_widget_addon(){
$wp_snappy_settings = $this->wp_snappy_settings;
$widget = '<script>
(function( $ ) {
$(function() {
$( ".snappy-button" ).click(function(e){
e.preventDefault();
SnappyWidget.open({contact: true});
});
});
})( jQuery );
</script>';
echo $widget;
}
public function wp_snappy_colorpicker(){
$html = '<script>
(function( $ ) {
$(function() {
$( ".wp-snappy-color-picker" ).wpColorPicker();
});
})( jQuery );
</script>';
echo $html;
}
public function wp_snappy_add_menus(){
add_options_page(
__('WP Snappy Options','wp-snappy'),
__('WP Snappy','wp-snappy'),
'manage_options',
'wp_snappy',
array( $this,'wp_snappy_page_display')
);
}
public function wp_snappy_page_display(){
?>
<div class='wrap'>
<h2>WP Snappy Settings</h2>
<form method='post' action='options.php'>
<?php
settings_fields( 'wp_snappy_settings' );
do_settings_sections( 'wp_snappy_widget_section' );
submit_button();
?>
</form>
</div>
<?php
}
//validate the options before saving
public function wp_snappy_validate_options($input){
$output = array();
//highlight to user that both script url and data domain is required
if ( empty( $input['script_url'] ) && empty( $input['data_domain'] ) ) {
add_settings_error( 'wp_snappy_settings', 'wp_snappy_settings_error', __( 'Script URL and Data Domain is required.', 'wp-snappy' ), 'error' );
}
foreach( $input as $key => $value ) {
if( isset( $input[$key] ) ) {
$output[$key] = strip_tags( stripslashes( trim( $input[ $key ] ) ) );
}
}
if ( isset( $input['background'] ) && !preg_match( '/^#[a-f0-9]{6}$/i', $input['background'] ) ) {
add_settings_error( 'wp_snappy_settings', 'wp_snappy_settings_error', __( 'Insert a valid color for Background.', 'wp-snappy' ), 'error' );
$input['background'] = '';
}
return apply_filters( 'wp_snappy_validate_options', $output, $input );
}
//getting all the field settings
public function get_snappy_settings(){
$settings = array(
'script_url' => array(
'id' => 'script_url',
'name' => __( 'Script URL', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'text',
'size' => 'regular'
),
'data_domain' => array(
'id' => 'data_domain',
'name' => __( 'Data Domain', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'text',
'size' => 'regular'
),
'title' => array(
'id' => 'title',
'name' => __( 'Title', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'text',
'size' => 'regular',
'std' => 'Help & Support'
),
'position' => array(
'id' => 'position',
'name' => __( 'Position', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'select',
'options' => array(
'top left' => __( 'Top Left', 'wp-snappy' ),
'top right' => __( 'Top Right', 'wp-snappy' ),
'bottom left'=>__('Bottom Left', 'wp-snappy' ),
'bottom right'=>__('Bottom Right', 'wp-snappy' )
),
'std' => 'bottom right'
),
'display' => array(
'id' => 'display',
'name' => __( 'Display', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'select',
'options' => array(
'both' => __( 'Both', 'wp-snappy' ),
'admin' => __( 'Admin Only', 'wp-snappy' ),
'frontend'=>__('Frontend Only', 'wp-snappy' )
)
),
'contact' => array(
'id' => 'contact',
'name' => __( 'Contact Form', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'radio',
'options' => array(
'1' => __( 'Enable', 'wp-snappy' ),
'0' => __( 'Disable', 'wp-snappy' )
),
'std' => '1'
),
'prepoinfo' => array(
'id' => 'prepoinfo',
'name' => __( 'Pre-Populate User Info', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'radio',
'options' => array(
'1' => __( 'Yes', 'wp-snappy' ),
'0' => __( 'No', 'wp-snappy' )
),
'std' => '1'
),
'background' => array(
'id' => 'background',
'name' => __( 'Background', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'color',
'std' => '#ec5a47'
),
'faq' => array(
'id' => 'faq',
'name' => __( 'Default FAQ', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'text',
'size' => 'small'
),
'debug' => array(
'id' => 'debug',
'name' => __( 'Debug', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'radio',
'options' => array(
'1' => __( 'On', 'wp-snappy' ),
'0' => __( 'Off', 'wp-snappy' )
),
'std' => '0'
),
'hidefaq' => array(
'id' => 'hidefaq',
'name' => __( 'Hide FAQ', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'radio',
'options' => array(
'1' => __( 'Yes', 'wp-snappy' ),
'0' => __( 'No', 'wp-snappy' )
),
'std' => '0'
),
'whitelabel' => array(
'id' => 'whitelabel',
'name' => __( 'Powered by Snappy', 'wp-snappy' ),
'desc' => __return_null(),
'type' => 'radio',
'options' => array(
'0' => __( 'Display', 'wp-snappy' ),
'1' => __( 'Hide', 'wp-snappy' )
),
'std' => '0'
)
);
return $settings;
}
//setup text field
public function wp_snappy_settings_text_callback( $args ) {
$wp_snappy_settings = $this->wp_snappy_settings;
if ( isset( $wp_snappy_settings[ $args['id'] ] ) )
$value = $wp_snappy_settings[ $args['id'] ];
else
$value = isset( $args['std'] ) ? $args['std'] : '';
$size = ( isset( $args['size'] ) && ! is_null( $args['size'] ) ) ? $args['size'] : 'regular';
$html = '<input type="text" class="' . $size . '-text" id="wp_snappy_settings[' . $args['id'] . ']" name="wp_snappy_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '"/>';
$html .= '<label for="wp_snappy_settings[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
echo $html;
}
//setup radio field
function wp_snappy_settings_radio_callback( $args ) {
$wp_snappy_settings = $this->wp_snappy_settings;
foreach ( $args['options'] as $key => $option ) :
$checked = false;
if ( isset( $wp_snappy_settings[ $args['id'] ] ) && $wp_snappy_settings[ $args['id'] ] == $key )
$checked = true;
elseif( isset( $args['std'] ) && $args['std'] == $key && ! isset( $wp_snappy_settings[ $args['id'] ] ) )
$checked = true;
echo '<input name="wp_snappy_settings[' . $args['id'] . ']"" id="wp_snappy_settings[' . $args['id'] . '][' . $key . ']" type="radio" value="' . $key . '" ' . checked(true, $checked, false) . '/> ';
echo '<label for="wp_snappy_settings[' . $args['id'] . '][' . $key . ']">' . $option . '</label> ';
endforeach;
echo '<p class="description">' . $args['desc'] . '</p>';
}
//setup color field
public function wp_snappy_settings_color_callback( $args ) {
$wp_snappy_settings = $this->wp_snappy_settings;
if ( isset( $wp_snappy_settings[ $args['id'] ] ) )
$value = $wp_snappy_settings[ $args['id'] ];
else
$value = isset( $args['std'] ) ? $args['std'] : '';
$html = '<input type="text" id="wp_snappy_settings[' . $args['id'] . ']" name="wp_snappy_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" class="wp-snappy-color-picker" />';
$html .= '<label for="wp_snappy_settings[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
echo $html;
}
//setup select field
public function wp_snappy_settings_select_callback($args) {
$wp_snappy_settings = $this->wp_snappy_settings;
if ( isset( $wp_snappy_settings[ $args['id'] ] ) )
$value = $wp_snappy_settings[ $args['id'] ];
else
$value = isset( $args['std'] ) ? $args['std'] : '';
$html = '<select id="wp_snappy_settings[' . $args['id'] . ']" name="wp_snappy_settings[' . $args['id'] . ']"/>';
foreach ( $args['options'] as $option => $name ) :
$selected = selected( $option, $value, false );
$html .= '<option value="' . $option . '" ' . $selected . '>' . $name . '</option>';
endforeach;
$html .= '</select>';
$html .= '<label for="wp_snappy_settings[' . $args['id'] . ']"> ' . $args['desc'] . '</label>';
echo $html;
}
}
new WP_Snappy;