-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadmin.php
More file actions
executable file
·157 lines (137 loc) · 5.25 KB
/
Copy pathadmin.php
File metadata and controls
executable file
·157 lines (137 loc) · 5.25 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
<?php
// Add settings link on plugin page
function emfluence_emailer_settings_link($links) {
$settings_link = '<a href="options-general.php?page=emfluence_emailer">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'emfluence_emailer_settings_link' );
// Register the settings page
function emfluence_emailer_admin_menu() {
add_options_page('emfluence Marketing Platform Global Settings', 'emfluence Marketing Platform', 'manage_options', 'emfluence_emailer', '_emfluence_emailer_options_page');
}
add_action('admin_menu', 'emfluence_emailer_admin_menu');
// Build the settings page
function emfluence_emailer_admin_init(){
register_setting('emfluence_emailer', 'emfluence_global', '_emfluence_emailer_options_validate');
add_settings_section(
'account'
,__('emfluence Settings')
,'_emfluence_emailer_options_account_description'
,'emfluence_emailer'
);
add_settings_field(
'api_key'
,__('Access Token')
,'_emfluence_emailer_options_api_key_element'
,'emfluence_emailer'
,'account'
);
add_settings_field(
'blacklist_domains',
'Blacklist Domains',
'_emfluence_emailer_options_blacklist_domains_element',
'emfluence_emailer',
'account'
);
}
add_action('admin_init', 'emfluence_emailer_admin_init');
// Register the plugin form's ajax callback
function emfluence_emailer_admin_enqueue_scripts($hook) {
// Only applies to widgets
if( !in_array($hook, array('widgets.php', 'customize.php')) ) {
return;
}
wp_enqueue_style(
'emfluence-widget',
plugins_url( '/css/widget-settings.css', __FILE__ ),
array(),
filemtime(__DIR__ . '/css/widget-settings.css')
);
wp_enqueue_script(
'emfluence-emailer-widget'
,plugins_url( '/js/widget-settings.min.js', __FILE__ )
,array('jquery', 'jquery-ui-accordion', 'jquery-ui-datepicker')
);
// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
// wp_localize_script( 'emfluence-emailer-widget', 'ajax_object', array( ));
}
add_action( 'admin_enqueue_scripts', 'emfluence_emailer_admin_enqueue_scripts' );
/**
* Handles settings for the administration page
*/
function _emfluence_emailer_options_page() {
?>
<div class="emfluence wrap">
<h2><?php __( 'emfluence Marketing Platform' ); ?></h2>
<form action="options.php" method="post">
<?php settings_fields('emfluence_emailer'); ?>
<?php do_settings_sections('emfluence_emailer'); ?>
<input name="Submit" type="submit" value="<?php esc_attr_e('Save Changes'); ?>" />
</form>
</div>
<?php
}
function _emfluence_emailer_options_account_description(){
echo '<p>Welcome! Please enter your api credentials below to begin. Once authenticated, you can create as many widgets as you need. Settings are saved per widget.</p>';
}
function _emfluence_emailer_options_api_key_element(){
$options = get_option('emfluence_global');
if(empty($options) || !is_array($options) || !is_string($options['api_key'])) $options = array('api_key' => '');
echo "<input id='api_key' name='emfluence_global[api_key]' size='36' type='text' value='{$options['api_key']}' />";
}
function _emfluence_emailer_options_blacklist_domains_element(){
$options = get_option('emfluence_global');
if(!is_array($options)) $options = array();
if(!array_key_exists('blacklist_domains', $options)) $options['blacklist_domains'] = '';
echo "
<textarea id='blacklist_domains' name='emfluence_global[blacklist_domains]' rows='10'>{$options['blacklist_domains']}</textarea>
<p>Form submissions with email address recipients in these domains will be rejected.</p>
<p>This is useful if you only want B2B leads.</p>
<p>Enter one domain per line, without commas. For example:<br />
hotmail.com<br />
gmail.com<br />
aol.com
</p>
";
}
/**
* Validates posted settings page options
* @param array
* @return array
*/
function _emfluence_emailer_options_validate($data){
if(empty($data['api_key'])) return $data;
try {
$api = emfluence_get_api($data['api_key'], TRUE);
// Ensure it works
$result = $api->ping();
} catch(Exception $e) {
$result = FALSE;
$exception_message = $e->getMessage();
}
if( !$result || !$result->success ){
$message = __('Unable to access the API using the api key provided. Error message: ') .
(empty($exception_message) ? $api->errors->get_last(): $exception_message);
add_settings_error( 'api_key', 'api_key', $message , 'error' );
$data['api_key'] = '';
delete_transient('emfl-access-token-validation');
} else {
set_transient('emfl-access-token-validation', TRUE, 60);
}
return $data;
}
/**
* Display a success message if access token was validated.
*/
function emfl_settings_page_messages() {
if(empty($_GET['page']) || ($_GET['page'] != 'emfluence_emailer')) return;
$transient = get_transient('emfl-access-token-validation');
if(empty($transient)) return;
delete_transient('emfl-access-token-validation');
$message = __('Access token validated.');
$class = 'notice notice-success';
printf( '<div class="%1$s"><p>' . $message . '</p></div>', $class );
}
add_action('admin_notices', 'emfl_settings_page_messages');