-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.php
133 lines (124 loc) · 3.34 KB
/
settings.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
<?php
class Liane_Form_Settings {
function __construct() {
add_action( 'admin_menu', array($this, 'admin_menu' ) );
add_action( 'admin_init', array($this, 'register_settings' ) );
}
function admin_menu() {
add_options_page(
'Liane Form Settings',
'Liane Form',
'manage_options',
'liane_form',
array( $this, 'settings_page' )
);
}
function settings_page() {
?>
<div class="wrap">
<h1>Liane Form</h1>
<form action="options.php" method="post">
<?php
settings_fields( 'liane_form_settings' );
do_settings_sections( 'liane_form' );
?>
<input
type="submit"
name="submit"
class="button button-primary"
value="<?php esc_attr_e( 'Save' ); ?>"
/>
</form>
</div>
<?php
}
function register_settings() {
register_setting(
'liane_form_settings',
'liane_form_settings',
array( $this, 'validate_settings' )
);
add_settings_section(
'default',
'',
array( $this, 'render_section' ),
'liane_form'
);
add_settings_field(
'server',
'Server',
array( $this, 'render_server_field' ),
'liane_form',
'default'
);
add_settings_field(
'id',
'Campaign ID',
array( $this, 'render_id_field' ),
'liane_form',
'default'
);
add_settings_field(
'shortcode',
'Form shortcode',
array($this, 'render_shortcode_field'),
'liane_form',
'default'
);
add_settings_field(
'shortcode_compact',
'Compact form shortcode',
array($this, 'render_shortcode_compact_field'),
'liane_form',
'default'
);
}
function render_section() {
echo "<p>Setup your Liane server and campaign ID to embed forms using the shortcode.</p>";
}
function render_text_field($name) {
$options = get_option( 'liane_form_settings' );
printf(
'<input type="text" name="%s" value="%s" />',
esc_attr( 'liane_form_settings[' . $name . ']' ),
esc_attr( $options[$name] )
);
}
function render_server_field() {
return $this->render_text_field('server');
}
function render_id_field() {
return $this->render_text_field('id');
}
function render_shortcode_field() {
echo '<p><code>[liane_form]</code></p>';
echo '<p class="description">Paste this shortcode anywhere on a page or post</p>';
}
function render_shortcode_compact_field() {
echo '<p><code>[liane_form compact="1"]</code></p>';
echo '<p class="description">Compact version for leads</p>';
}
function validate_settings($input) {
$output = array();
// Sanitize text field
$server = sanitize_text_field($input["server"]);
$id = sanitize_text_field($input["id"]);
// Validate server url
$server = filter_var($server, FILTER_VALIDATE_URL);
if(!$server) {
add_settings_error('liane_form', '400', 'You must provide a valid URL for the server', 'error');
} else {
$output['server'] = $server;
}
// Validate ID
if(strlen($id) > 17 || !preg_match('/^[\w\d]{17}$/', $id)) {
add_settings_error('liane_form', '400', 'You must provide a valid Campaign ID', 'error');
} else {
$output['id'] = $id;
}
return $output;
}
}
if(class_exists('Liane_Form_Settings')) {
new Liane_Form_Settings();
}