class-admin.php
3.97 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
<?php
class WPF_Drip_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_drip_header_begin', array($this, 'show_field_drip_header_begin' ), 10, 2);
add_action( 'show_field_drip_token_end', array($this, 'show_field_drip_token_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 Drip connection information on settings page
*
* @access public
* @since 1.0
*/
public function register_connection_settings( $settings, $options ) {
$new_settings = array();
$new_settings['drip_header'] = array(
'title' => __('Drip Configuration', 'wp-fusion' ),
'std' => 0,
'type' => 'heading',
'section' => 'setup'
);
$new_settings['drip_account'] = array(
'title' => __('Account ID', 'wp-fusion' ),
'desc' => __('Enter the Account ID for your Drip account (find it under Settings >> Site Setup >> 3rd Party Integrations).', 'wp-fusion' ),
'std' => '',
'type' => 'text',
'section' => 'setup'
);
$new_settings['drip_token'] = array(
'title' => __('API Token', 'wp-fusion' ),
'desc' => __('Enter your Drip API token. You can find it in your Drip account <a href="https://www.getdrip.com/user/edit" target="_blank">here</a>.', 'wp-fusion' ),
'type' => 'api_validate',
'section' => 'setup',
'class' => 'api_key',
'post_fields' => array('drip_account', 'drip_token')
);
$settings = wp_fusion()->settings->insert_setting_after('crm', $settings, $new_settings);
return $settings;
}
/**
* Loads standard Drip 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)
$options['contact_fields']['user_email']['crm_field'] = 'email';
return $options;
}
/**
* Puts a div around the Drip configuration section so it can be toggled
*
* @access public
* @since 1.0
*/
public function show_field_drip_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_drip_token_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 #drip div
echo '<table class="form-table">';
}
/**
* Verify connection credentials
*
* @access public
* @return bool
*/
public function test_connection() {
$api_token = $_POST['drip_token'];
$account_id = $_POST['drip_account'];
$connection = $this->crm->connect($api_token, $account_id, true);
if( is_wp_error( $connection ) ) {
wp_send_json_error( $connection->get_error_message() );
} else {
$options = wp_fusion()->settings->get_all();
$options['drip_token'] = $api_token;
$options['drip_account'] = $account_id;
$options['crm'] = $this->slug;
$options['connection_configured'] = true;
wp_fusion()->settings->set_all( $options );
wp_send_json_success();
}
die();
}
}