class-membermouse.php
8.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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if(class_exists( 'MemberMouse' )) {
class WPF_MemberMouse extends WPF_Integrations_Base {
private $saved;
/**
* Gets things started
*
* @access public
* @return void
*/
public function init() {
// Settings
add_filter( 'wpf_user_register', array( $this, 'member_register' ), 10, 2 );
add_filter( 'wpf_meta_fields', array( $this, 'set_contact_field_names' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
// MemberMouse actions
add_action( 'mm_member_account_update', array( $this, 'account_update' ) );
add_action( 'mm_payment_received', array( $this, 'payment_received' ) );
}
/**
* Creates MM submenu item
*
* @access public
* @return void
*/
public function admin_menu() {
$crm = wp_fusion()->crm->name;
$id = add_submenu_page(
'mmdashboard',
$crm . ' Integration',
$crm,
'manage_options',
'mm-wpf-settings',
array($this, 'render_admin_menu')
);
add_action( 'load-' . $id, array($this, 'enqueue_scripts' ) );
// Save settings
if(!isset($_POST['wpf_mm_settings_nonce']))
return;
if(!wp_verify_nonce($_POST['wpf_mm_settings_nonce'], 'wpf_mm_settings')) {
die('Security check. Invalid nonce.');
}
update_option( 'wpf_mm_settings', $_POST['wpf-settings']);
$this->saved = true;
}
/**
* Enqueues WPF scripts and styles on MM options page
*
* @access public
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_style('bootstrap', WPF_DIR_URL . 'includes/admin/options/css/bootstrap.min.css');
wp_enqueue_style('options-css', WPF_DIR_URL .'includes/admin/options/css/options.css');
wp_enqueue_style('wpf-options', WPF_DIR_URL .'assets/css/wpf-options.css');
}
/**
* Renders MM submenu item
*
* @access public
* @return mixed
*/
public function render_admin_menu() { ?>
<?php if ($this->saved) : ?>
<div id="message" class="updated fade"><p><strong>Settings saved.</strong></p></div>
<?php endif; ?>
<div class="wrap">
<h2><?php echo wp_fusion()->crm->name ?> Integration</h2>
<form id="wpf-mm-settings" action="" method="post">
<?php wp_nonce_field('wpf_mm_settings', 'wpf_mm_settings_nonce'); ?>
<input type="hidden" name="action" value="update">
<h4>Product Tags</h4>
<p class="description">For each product below, specify tags to be applied in <?php echo wp_fusion()->crm->name ?> when purchased.</p>
<br />
<?php $view = new MM_ProductView(); ?>
<?php $dataGrid = new MM_DataGrid($_REQUEST, "id", "desc", 10); ?>
<?php $data = array_reverse($view->getViewData($dataGrid)); ?>
<?php $settings = get_option('wpf_mm_settings'); ?>
<table class="table table-hover" id="wpf-mm-products-table">
<thead>
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Apply Tags</th>
</tr>
</thead>
<tbody>
<?php foreach($data as $product) : ?>
<?php if(!isset($settings['apply_tags'][$product->id])) $settings['apply_tags'][$product->id] = array(); ?>
<tr>
<td><?php echo $product->name; ?></td>
<td>$<?php echo number_format((float)$product->price, 2, '.', ''); ?></td>
<td><?php wpf_render_tag_multiselect( $settings['apply_tags'], 'wpf-settings', 'apply_tags', $product->id ) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p class="submit"><input name="Submit" type="submit" class="button-primary" value="Save Changes" /></p>
</form>
</div>
<?php
}
/**
* Set field labels from MM field labels
*
* @access public
* @return array Meta fields
*/
public function set_contact_field_names( $meta_fields ) {
$label_text = '<label class="label label-primary">MM</label> ';
// Misc.
$meta_fields['membership_level'] = array(
'label' => $label_text . 'Membership Level ID',
'type' => 'text'
);
$meta_fields['membership_level_name'] = array(
'label' => $label_text . 'Membership Level Name',
'type' => 'text'
);
$meta_fields['is_member'] = array(
'label' => $label_text . 'Is Member',
'type' => 'checkbox'
);
$meta_fields['is_free'] = array(
'label' => $label_text . 'Member Is Free',
'type' => 'checkbox'
);
$meta_fields['is_gift'] = array(
'label' => $label_text . 'Member Is Gift',
'type' => 'checkbox'
);
$meta_fields['phone'] = array(
'label' => $label_text . 'Phone',
'type' => 'text'
);
$meta_fields['coupon_code'] = array(
'label' => $label_text . 'Coupon Code',
'type' => 'text'
);
// Billing
$meta_fields['billing_address'] = array(
'label' => $label_text . 'Billing Address',
'type' => 'text'
);
$meta_fields['billing_city'] = array(
'label' => $label_text . 'Billing City',
'type' => 'text'
);
$meta_fields['billing_state'] = array(
'label' => $label_text . 'Billing State',
'type' => 'text'
);
$meta_fields['billing_zip'] = array(
'label' => $label_text . 'Billing ZIP',
'type' => 'text'
);
$meta_fields['billing_country'] = array(
'label' => $label_text . 'Billing Country',
'type' => 'text'
);
// Shipping
$meta_fields['shipping_address'] = array(
'label' => $label_text . 'Shipping Address',
'type' => 'text'
);
$meta_fields['shipping_city'] = array(
'label' => $label_text . 'Shipping City',
'type' => 'text'
);
$meta_fields['shipping_state'] = array(
'label' => $label_text . 'Shipping State',
'type' => 'text'
);
$meta_fields['shipping_zip'] = array(
'label' => $label_text . 'Shipping ZIP',
'type' => 'text'
);
$meta_fields['shipping_country'] = array(
'label' => $label_text . 'Shipping Country',
'type' => 'text'
);
// Custom fields
$view = new MM_CustomFieldView();
$dataGrid = new MM_DataGrid($_REQUEST, "id", "desc", 10);
$data = $view->getViewData($dataGrid);
foreach($data as $field_object) {
if($field_object->type == 'input') {
$type = 'text';
} else {
$type = $field_object->type;
}
$meta_fields['cf_' . $field_object->id] = array(
'label' => $label_text . $field_object->display_name,
'type' => $type
);
}
return $meta_fields;
}
/**
* Triggered when new member is added
*
* @access public
* @return array Post data
*/
public function member_register( $post_data, $user_id ) {
if(!isset($post_data['mm-security']))
return $post_data;
foreach($post_data as $key => $value) {
if(strpos($key, 'mm_field_') !== FALSE) {
unset($post_data[$key]);
$key = str_replace('mm_field_', '', $key);
$post_data[$key] = $value;
} elseif(strpos($key, 'mm_') !== FALSE) {
unset($post_data[$key]);
$key = str_replace('mm_', '', $key);
$post_data[$key] = $value;
}
}
if(isset($post_data['email'])) {
$post_data['user_email'] = $post_data['email'];
unset($post_data['email']);
}
if(isset($post_data['password'])) {
$post_data['user_pass'] = $post_data['password'];
unset($post_data['password']);
}
return $post_data;
}
/**
* Triggered when a member's account is updated
*
* @access public
* @return void
*/
public function account_update( $member_data ) {
wp_fusion()->user->push_user_meta($member_data['member_id'], $member_data);
}
/**
* Triggered when a payment is successfully processed
*
* @access public
* @return void
*/
public function payment_received( $order_data ) {
$user_id = $order_data['member_id'];
wp_fusion()->user->push_user_meta($user_id, $order_data);
$settings = get_option('wpf_mm_settings');
if(empty($settings) || !isset($settings['apply_tags']))
return;
$products = json_decode($order_data['order_products'], true);
foreach($products as $product) {
if(isset($settings['apply_tags'][$product['id']]))
wp_fusion()->user->apply_tags($settings['apply_tags'][$product['id']], $user_id);
}
}
}
new WPF_MemberMouse;
}