class-admin.php
4.13 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
<?php
class WPF_Ontraport_Admin {
private $slug;
private $name;
private $crm;
/**
* Get things started
*
* @access public
* @since 1.0
*/
public function __construct($slug, $name, $crm) {
$this->slug = $slug;
$this->name = $name;
$this->crm = $crm;
add_filter( 'wpf_configure_settings', array( $this, 'register_connection_settings' ), 15, 2 );
add_action( 'show_field_ontraport_header_begin', array($this, 'show_field_ontraport_header_begin' ), 10, 2);
add_action( 'show_field_op_key_end', array($this, 'show_field_op_key_end' ), 10, 2);
// AJAX
add_action( 'wp_ajax_wpf_test_connection_' . $this->slug, array($this, 'test_connection') );
if(wp_fusion()->settings->get('crm') == $this->slug)
$this->init();
}
/**
* Hooks to run when this CRM is selected as active
*
* @access public
* @since 1.0
*/
public function init() {
add_filter( 'wpf_initialize_options', array( $this, 'add_default_fields' ), 10 );
}
/**
* Loads Ontraport connection information on settings page
*
* @access public
* @since 1.0
*/
public function register_connection_settings( $settings, $options ) {
$new_settings = array();
$new_settings['ontraport_header'] = array(
'title' => __('Ontraport Configuration', 'wp-fusion' ),
'std' => 0,
'type' => 'heading',
'section' => 'setup'
);
$new_settings['op_url'] = array(
'title' => __('App ID', 'wp-fusion' ),
'desc' => __('Enter the App ID for your Ontraport account (find it under Settings >> Administration >> Ontraport API in your account).', 'wp-fusion' ),
'std' => '',
'type' => 'text',
'section' => 'setup'
);
$new_settings['op_key'] = array(
'title' => __('API Key', 'wp-fusion' ),
'desc' => __('The API key will appear next to the App ID on the API Keys page.', 'wp-fusion' ),
'type' => 'api_validate',
'section' => 'setup',
'class' => 'api_key',
'post_fields' => array('op_url', 'op_key')
);
$settings = wp_fusion()->settings->insert_setting_after('crm', $settings, $new_settings);
return $settings;
}
/**
* Loads standard Ontraport field names and attempts to match them up with standard local ones
*
* @access public
* @since 1.0
*/
public function add_default_fields( $options ) {
if($options['connection_configured'] == true) {
require_once dirname(__FILE__) . '/ontraport-fields.php';
foreach($options['contact_fields'] as $field => $data) {
if(isset($ontraport_fields[$field])) {
$options['contact_fields'][$field] = array_merge($options['contact_fields'][$field], $ontraport_fields[$field]);
}
}
}
return $options;
}
/**
* Puts a div around the Ontraport configuration section so it can be toggled
*
* @access public
* @since 1.0
*/
public function show_field_ontraport_header_begin( $id, $field ) {
echo '</table>';
$crm = wp_fusion()->settings->get('crm');
echo '<div id="' . $this->slug . '" class="crm-config ' . ($crm == false || $crm != $this->slug ? 'hidden' : 'crm-active') . '" data-name="' . $this->name .'" data-crm="' . $this->slug .'">';
}
/**
* Close out Active Campaign section
*
* @access public
* @since 1.0
*/
public function show_field_op_key_end($id, $field) {
if($field['desc'] != '') {
echo '<span class="description">'.$field['desc'].'</span>';
}
echo '</td>';
echo '</tr>';
echo '</table><div id="connection-output"></div>';
echo '</div>'; // close #ontraport div
echo '<table class="form-table">';
}
/**
* Verify connection credentials
*
* @access public
* @return bool
*/
public function test_connection() {
$api_url = $_POST['op_url'];
$api_key = $_POST['op_key'];
$connection = $this->crm->connect($api_url, $api_key, true);
if( is_wp_error( $connection ) ) {
wp_send_json_error( $connection->get_error_message() );
} else {
$options = wp_fusion()->settings->get_all();
$options['op_url'] = $api_url;
$options['op_key'] = $api_key;
$options['crm'] = $this->slug;
$options['connection_configured'] = true;
wp_fusion()->settings->set_all( $options );
wp_send_json_success();
}
die();
}
}