-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin_init.php
More file actions
297 lines (250 loc) · 10.7 KB
/
admin_init.php
File metadata and controls
297 lines (250 loc) · 10.7 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
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
<?php
global $novaksolutions_exit_webforms;
/**
* Add links to plugin listing.
*
* @param array $links
* @param string $file
* @return array
*/
function novaksolutions_exit_plugin_action_links( $links, $file ) {
if ( $file == plugin_basename( dirname(__FILE__).'/infusionsoft-exit-optin.php' ) ) {
$links[] = '<a href="' . admin_url( 'admin.php?page=novaksolutions_exit_admin_menu' ) . '">'.__( 'Settings' ).'</a>';
$links[] = '<a href="http://novaksolutions.com/integrations/wordpress/?utm_source=wordpress&utm_medium=link&utm_content=exit-optin&utm_campaign=more-plugins">More Plugins by Novak Solutions</a>';
}
return $links;
}
add_filter('plugin_action_links', 'novaksolutions_exit_plugin_action_links', 10, 2);
/**
* Define settings fields.
*/
function novaksolutions_exit_admin_init(){
add_option('novaksolutions_exit_web_form_snippet', '', null, 'no');
// Add the section to reading settings so we can add our
// fields to it
add_settings_section('novaksolutions_exit_setting_section_popup',
'Pop-up Options',
null,
'novaksolutions-exit-settings');
// Add the field with the names and function to use for our new
// settings, put it in our new section
add_settings_field('novaksolutions_exit_web_form',
'Web Form',
'novaksolutions_exit_callback_function_web_form',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
add_settings_field('novaksolutions_exit_web_form_snippet',
'Hosted URL',
'novaksolutions_exit_callback_function_web_form_snippet',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
add_settings_field('novaksolutions_exit_role',
'Minimum Role',
'novaksolutions_exit_callback_function_role',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
add_settings_field('novaksolutions_exit_width',
'Width',
'novaksolutions_exit_callback_function_width',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
add_settings_field('novaksolutions_exit_height',
'Height',
'novaksolutions_exit_callback_function_height',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
add_settings_field('novaksolutions_exit_max_views',
'Max Views',
'novaksolutions_exit_callback_function_max_views',
'novaksolutions-exit-settings',
'novaksolutions_exit_setting_section_popup');
// Register our setting so that $_POST handling is done for us and
// our callback function just has to echo the <input>
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_web_form');
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_web_form_snippet');
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_role');
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_width', 'novaksolutions_exit_sanitize_absint');
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_height', 'novaksolutions_exit_sanitize_absint');
register_setting('novaksolutions-exit-settings', 'novaksolutions_exit_max_views', 'novaksolutions_exit_sanitize_absint');
}
add_action('admin_init', 'novaksolutions_exit_admin_init');
/**
* Make sure the value is a positive integer.
*
* @param string $value
* @return int
*/
function novaksolutions_exit_sanitize_absint($value) {
$value = absint($value);
return $value === 0 ? '' : $value;
}
/**
* Display web form dropdown.
*/
function novaksolutions_exit_callback_function_web_form() {
global $novaksolutions_exit_webforms;
echo '<select name="novaksolutions_exit_web_form">';
echo "<option></option>";
foreach($novaksolutions_exit_webforms as $id => $name)
{
$value = array(
'id' => $id,
'name' => $name,
);
$value = htmlspecialchars(serialize($value));
$current = htmlspecialchars_decode(get_option('novaksolutions_exit_web_form'));
$current = str_replace("\r\n", "\n", $current);
$current = unserialize($current);
echo "<option value=\"$value\"";
if(isset($current['id']) && $id == $current['id']){
echo ' selected="selected"';
}
echo ">" . htmlspecialchars($name) . "</option>";
}
echo '</select>';
}
/**
* Display web form URL.
*/
function novaksolutions_exit_callback_function_web_form_snippet() {
$snippet = get_option('novaksolutions_exit_web_form_snippet');
if(!empty($snippet)){
echo htmlspecialchars($snippet) . '<br />';
echo '<span class="description">This URL will automatically be updated when you click Save Changes.</span>';
}else{
echo 'Please select a web form and click <em>Save Changes</em>.';
}
}
/**
* Display minimum role field.
*/
function novaksolutions_exit_callback_function_role() {
global $novaksolutions_exit_webforms;
$roles = array(
'Everyone' => 1,
'Subscriber' => 2,
'Contributor' => 3,
'Author' => 4,
'Editor' => 5,
'Administrator' => 6,
);
echo '<select name="novaksolutions_exit_role">';
foreach($roles as $role => $id){
echo "<option value=\"$id\"";
if($id == get_option('novaksolutions_exit_role', '1')) echo " selected=\"selected\"";
echo ">$role</option>";
}
echo '</select><br />';
echo '<span class="description">The Exit Optin will only be shown to visitors that are assigned the minimum role or greater. We recommend setting this to Administrator while testing.</span>';
}
/**
* Display width field.
*/
function novaksolutions_exit_callback_function_width() {
echo '<input type="text" name="novaksolutions_exit_width" value="' . get_option('novaksolutions_exit_width', '500') . '" />px';
}
/**
* Display height field.
*/
function novaksolutions_exit_callback_function_height() {
echo '<input type="text" name="novaksolutions_exit_height" value="' . get_option('novaksolutions_exit_height', '300') . '" />px';
}
/**
* Display max view field.
*/
function novaksolutions_exit_callback_function_max_views() {
echo '<input type="text" name="novaksolutions_exit_max_views" value="' . get_option('novaksolutions_exit_max_views', '1') . '" /><br />';
echo '<span class="description">How many times should each visitor be allowed to see the Exit Optin pop-up.</span>';
}
/**
* Add settings page to menu.
*/
function novaksolutions_exit_add_admin_menu(){
add_menu_page( "Infusionsoft Exit Optin", "Exit Optin", "edit_plugins", "novaksolutions_exit_admin_menu", 'novaksolutions_exit_display_admin_page');
add_submenu_page( "novaksolutions_exit_admin_menu", "Infusionsoft Exit Optin", "Settings", "edit_plugins", "novaksolutions_exit_admin_menu", 'novaksolutions_exit_display_admin_page');
}
add_action('admin_menu', 'novaksolutions_exit_add_admin_menu');
/**
* Get list of web forms from Infusionsoft.
*
* @return array
*/
function novaksolutions_exit_get_webforms(){
if( !is_plugin_active( 'infusionsoft-sdk/infusionsoft-sdk.php' )){
$products = array();
return $products;
}
$enough_to_go = false;
$valid_key = true;
if(get_option('infusionsoft_sdk_app_name') != '' && get_option('infusionsoft_sdk_api_key') != ''){
$enough_to_go = true;
try{
Infusionsoft_AppPool::addApp(new Infusionsoft_App(get_option('infusionsoft_sdk_app_name') . '.infusionsoft.com', get_option('infusionsoft_sdk_api_key')));
$webforms = Infusionsoft_WebFormService::getMap();
} catch(Infusionsoft_Exception $e) {
$enough_to_go = false;
if($e == '[InvalidKey]Invalid Key') {
$valid_key = false;
add_settings_error("infusionsoft_sdk_api_key", "infusionsoft_sdk_api_key", "The API key you entered is invalid.", "error");
}
}
} else {
$webforms = array();
if(!get_option('infusionsoft_sdk_app_name')){
add_settings_error("infusionsoft_sdk_app_name", "infusionsoft_sdk_app_name", "Please enter your Infusionsoft app name.", "error");
}
if(!get_option('infusionsoft_sdk_api_key')){
$valid_key = false;
add_settings_error("infusionsoft_sdk_api_key", "infusionsoft_sdk_api_key", "Please enter your Infusionsoft API key.", "error");
}
}
if($enough_to_go && Infusionsoft_DataService::ping()){
try{
Infusionsoft_DataService::findByField(new Infusionsoft_Contact(), 'Id', -1);
} catch(Exception $e){
add_settings_error("infusionsoft_sdk_api_key", "infusionsoft_sdk_api_key", "The API key you entered is invalid.", "error");
}
} else {
if($valid_key){
add_settings_error("infusionsoft_sdk_app_name", "infusionsoft_sdk_app_name", "The app name you entered is invalid.", "error");
}
}
if(!empty($webforms)){
$webform = htmlspecialchars_decode(get_option('novaksolutions_exit_web_form'));
$webform = str_replace("\r\n", "\n", $webform);
$webform = unserialize($webform);
if(isset($webform['id'])){
try {
$js = Infusionsoft_WebFormService::getHostedURL($webform['id']);
update_option( 'novaksolutions_exit_web_form_snippet', $js );
} catch(Exception $e) {
update_option( 'novaksolutions_exit_web_form', '' );
update_option( 'novaksolutions_exit_web_form_snippet', '' );
}
}
}
return $webforms;
}
/**
* Display links to WP.org and Novak Solutions.
*/
function novaksolutions_exit_display_link_back(){
echo '<h2>Like this plugin?</h2>';
echo '<p>If you found this plugin useful, please <a href="http://wordpress.org/support/view/plugin-reviews/infusionsoft-exit-optin">rate it in the plugin directory</a>.</p>';
echo '<p>Visit <a href="http://novaksolutions.com/?utm_source=wordpress&utm_medium=link&utm_campaign=exit">Novak Solutions</a> to find dozens of free tips, tricks, and tools to help you get the most out of Infusionsoft®.</p>';
}
/**
* Display settings page and link back.
*/
function novaksolutions_exit_display_admin_page(){
global $novaksolutions_exit_webforms;
echo '<h2>Infusionsoft Exit Optin Settings</h2>';
$novaksolutions_exit_webforms = novaksolutions_exit_get_webforms();
settings_errors();
echo '<form method="POST" action="options.php">';
settings_fields('novaksolutions-exit-settings'); //pass slug name of page
do_settings_sections('novaksolutions-exit-settings'); //pass slug name of page
submit_button();
echo '</form>';
novaksolutions_exit_display_link_back();
}