-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuser-meta-manager.php
2963 lines (2687 loc) · 146 KB
/
user-meta-manager.php
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: User Meta Manager
* Plugin URI: https://github.com/jasonlau/Wordpress-User-Meta-Manager
* Description: Add, edit, or delete user meta data with this handy plugin. Easily restrict access or insert user meta data into posts or pages and more. <strong>Get the Pro extension <a href="http://jasonlau.biz/home/membership-options#umm-pro">here</a>.</strong>
* Version: 3.4.8
* Author: Jason Lau
* Author URI: http://jasonlau.biz
* Text Domain: user-meta-manager
* Disclaimer: Use at your own risk. No warranty expressed or implied.
*
* Always backup your database before making changes.
*
* Copyright 2012+ http://jasonlau.biz
*
* This free version of User Meta Manager is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
* http://www.gnu.org/licenses/gpl.html
*/
if (realpath(__FILE__) == realpath($_SERVER['SCRIPT_FILENAME'])) {
exit('Please don\'t access this file directly.');
}
define('UMM_VERSION', '3.4.8');
define("UMM_PATH", plugin_dir_path(__FILE__) . '/');
define("UMM_SLUG", "user-meta-manager");
define("UMM_AJAX", "admin-ajax.php?action=umm_switch_action&umm_sub_action=");
//error_reporting(E_ALL);
include(UMM_PATH . 'includes/umm-table.php');
include(UMM_PATH . 'includes/umm-contextual-help.php');
function umm_add_custom_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$output = umm_fyi('<p>'.__('Insert a key and default value in the fields below.', UMM_SLUG).'</p>');
$output .= '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Key', UMM_SLUG).':</strong><br />
<input name="umm_meta_key[]" title="'.__('Letters, numbers, and underscores only', UMM_SLUG).'" type="text" value="" placeholder="'.__('Meta Key', UMM_SLUG).'" /><br />
<strong>'.__('Value', UMM_SLUG).':</strong><br />
<textarea rows="3" cols="40" name="umm_meta_value[]" placeholder=""></textarea>
';
$output .= '<br /><strong>' . __('Use This Value For All Current Users', UMM_SLUG) . ':</strong><br /><input type="checkbox" name="all_users" value="1" title="' . __('Check the box to add this value to all current users. Note: Using this option on a checkbox field will cause the checkbox to initially be checked for all current users.', UMM_SLUG) . '" /> ' . __('Yes', UMM_SLUG) . '';
$output .= umm_profile_field_editor();
$output .= '<br />
<input id="umm_add_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Submit', UMM_SLUG).'" />
<input name="mode" type="hidden" value="add" /><input name="umm_user" type="hidden" value="all" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_add_custom_meta&umm_user=0" />
</form>
';
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_add_registration_fields(){
$content = umm_show_profile_fields(false, false, 'register', 'registerform');
echo $content;
}
function umm_add_user_fields(){
$umm_content = umm_show_profile_fields(false, false, 'adduser', 'createuser');
$umm_output = '<div id="umm-add-user-fields" style="display:none;">' . $umm_content . '</div>
<script type="text/javascript">
jQuery(function($){
$("form#createuser p.submit").before(\'<input type="hidden" name="umm_form" value="createuser" />\' + $("div#umm-add-user-fields").html());
var is_duplicate = function(obj, key, value){
var request = $.ajax({
url: \'admin-ajax.php?action=umm_switch_action&umm_sub_action=umm_is_duplicate&echo=true&umm_key=\' + key + \'&umm_value=\' + value,
type: "POST",
dataType: "json"
});
request.done(function(data){
if(data.is_duplicate){
if(!$(".umm-duplicate-warning").html()){
$("input#createusersub").prop("disabled", "disabled");
obj.after(\'<div class="umm-warning umm-duplicate-warning hidden" style="color:red">\' + data.error_message.replace(\'%s\',obj.parent().parent().find(\'th\').html()) + \'</div>\');
obj.css({"background-color": "#FFFF99"});
$(".umm-duplicate-warning").show(\'slow\');
}
return true;
} else {
if($(".umm-duplicate-warning").html()){
$(".umm-duplicate-warning").hide(\'slow\').remove();
}
obj.css({"background-color": "inherit"});
$("input#createusersub").prop("disabled", "");
return false;
}
});
};
$(document).on(\'change\', ".umm-unique", function(event){
is_duplicate($(this), $(this).attr(\'name\'), $(this).val());
});
$(document).on(\'click\', "input#createusersub", function(event){
event.preventDefault();
var has_duplicates = false;
$(".umm-unique").each(function(){
if(is_duplicate($(this), $(this).attr(\'name\'), $(this).val())){
has_duplicates = true;
}
});
if(!has_duplicates){
$("form#createuser").submit();
}
});
});
</script>
';
echo $umm_output;
}
function umm_add_user_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$user_id = sprintf("%d", $_REQUEST['umm_user']);
$output = umm_button('home', null, "umm-back-button") . umm_subpage_title($user_id, __('Adding Meta Data For %s', UMM_SLUG));
$output .= umm_fyi('<p>'.__('Insert a meta key and default value and press <em>Submit</em>.', UMM_SLUG).'</p>');
$output .= '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Key', UMM_SLUG).':</strong><br />
<input name="umm_meta_key[]" title="'.__('Letters, numbers, and underscores only', UMM_SLUG).'" type="text" value="" placeholder="'.__('Meta Key', UMM_SLUG).'" /><br />
<strong>'.__('Value', UMM_SLUG).':</strong><br />
<textarea cols="30" name="umm_meta_value[]" placeholder="'.__('Default Value', UMM_SLUG).'"></textarea>';
$output .= '<br />
<input id="umm_update_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Submit', UMM_SLUG).'" />
<input name="mode" type="hidden" value="add" /><input name="umm_user" type="hidden" value="' . $user_id . '" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_add_user_meta&umm_user=' . $user_id . '" />
</form>
';
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_admin_init(){
if(function_exists('load_plugin_textdomain'))
load_plugin_textdomain( 'user-meta-manager', false, dirname(plugin_basename( __FILE__ )) . '/language/' );
}
function umm_admin_menu(){
add_submenu_page('users.php', 'User Meta Manager', 'User Meta Manager', 'publish_pages', UMM_SLUG, 'umm_ui');
add_action('admin_enqueue_scripts', 'umm_load_scripts');
}
function umm_backup($backup_mode=false, $tofile=false, $print=true){
global $wpdb, $current_user, $table_prefix;
if(current_user_can('export')):
$mode = (!isset($_REQUEST['mode']) || empty($_REQUEST['mode'])) ? '' : sprintf("%s", $_REQUEST['mode']);
$mode = (empty($backup_mode)) ? $mode : $backup_mode;
if(umm_is_pro() && $mode == 'pro'):
if(function_exists('umm_pro_backup')):
umm_pro_backup();
exit;
endif;
endif;
$backup_files = umm_get_option('backup_files');
$to_file = (!isset($_REQUEST['tofile']) || empty($_REQUEST['tofile'])) ? '' : sprintf("%s", $_REQUEST['tofile']);
$tofile = (empty($tofile)) ? $to_file : $tofile;
$backup_files = (!$backup_files || $backup_files == '') ? array() : $backup_files;
$back_button = umm_button("umm_backup_page&umm_user=1", null, "umm-back-button");
switch($mode){
case "sql":
$data = $wpdb->get_results("SELECT * FROM " . $table_prefix . "umm_usermeta_backup");
$budate = umm_get_option('backup_date');
$sql = "DELETE FROM `" . $wpdb->usermeta . "`;\n";
$sql .= "INSERT INTO `" . $wpdb->usermeta . "` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES\n";
foreach($data as $d):
$sql .= "(";
foreach($d as $key => $value):
if($key == 'umeta_id' || $key == 'user_id'):
$sql .= $value . ", ";
elseif($key == 'meta_value'):
$sql .= "'" . addslashes($value) . "', ";
else:
$sql .= "'" . addslashes($value) . "', ";
endif;
endforeach;
$sql = trim($sql,", ");
$sql .= "),\n";
endforeach;
$sql = trim($sql,",\n") . ";";
$output = '<p class="umm-message">' . __("Below is the sql needed to restore the usermeta table.", UMM_SLUG) . "</p><strong>" . __("Backup from", UMM_SLUG) . " " . $budate . "</strong><br />\n<textarea onclick=\"this.focus();this.select();\" cols=\"65\" rows=\"15\">" . $sql . "</textarea>";
break;
case "php":
$data = $wpdb->get_results("SELECT * FROM " . $table_prefix . "umm_usermeta_backup");
$budate = umm_get_option('backup_date');
$output = '<?php
';
$output .= "require('" . ABSPATH . "wp-load.php');\n";
$output .= 'if(!is_user_logged_in() OR !current_user_can(\'update_core\')) wp_die("' . __("Authorization required!", UMM_SLUG) . '");
global $wpdb;
if(isset($_REQUEST[\'umm_confirm_restore\'])):
';
$output .= '$wpdb->query("DELETE FROM $wpdb->usermeta");' . "\n";
foreach($data as $d):
$output .= '$wpdb->query("INSERT INTO $wpdb->usermeta (umeta_id, user_id, meta_key, meta_value) VALUES(';
foreach($d as $key => $value):
if($key == 'umeta_id' || $key == 'user_id'):
$output .= $value . ", ";
elseif($key == 'meta_value'):
$output .= "'" . addslashes($value) . "')";
else:
$output .= "'" . addslashes($value) . "', ";
endif;
endforeach;
$output = trim($output,", ");
$output .= "\");\n";
endforeach;
$output .= "print('" . __("Restore complete.", UMM_SLUG) . "');\nelse:\nprint('<form action=\"#\" method=\"post\"><p>" . __("Are you sure you want to restore all user meta data to the backup version?", UMM_SLUG) . "</p><button type=\"submit\">" . __("Yes", UMM_SLUG) . "</button><input type=\"hidden\" name=\"umm_confirm_restore\" value=\"1\" /></form>');\nendif;\n?>";
if($tofile == "yes"):
$rs = umm_random_str(10);
$temp_file = WP_PLUGIN_DIR . "/user-meta-manager/backups/" . "usermeta-backup-" . date("m.j.Y-") . ".php";
$file = WP_PLUGIN_DIR . "/user-meta-manager/backups/" . "usermeta-backup-" . date("m.j.Y-") . date("g.i.a") . "-" . $current_user->ID . "-" . $_SERVER['REMOTE_ADDR'] . "-" . $rs . ".php";
$link = WP_PLUGIN_URL . "/user-meta-manager/backups/" . "usermeta-backup-" . date("m.j.Y-") . date("g.i.a") . "-" . $current_user->ID . "-" . $_SERVER['REMOTE_ADDR'] . "-" . $rs . ".php";
array_push($backup_files, $file);
umm_update_option('backup_files', $backup_files);
if($fp = @fopen($temp_file, "w+")):
@chmod($temp_file, 0755);
fwrite($fp, trim($output));
fclose($fp);
// Some servers need permissions set
@chmod($temp_file, 0755);
@rename($temp_file, $file);
$output = '<p class="umm-message">' . __("Backup php file was successfully generated at ", UMM_SLUG) . ' <a href="' . $link . '" target="_blank">' . $link . '</a></p><p>' . __("Run the file in your browser to begin the restoration process.", UMM_SLUG) . '</p>' . "\n";
else:
$output = '<p class="umm-warning">' . __("Error: Backup php file could not be generated at ", UMM_SLUG) . ' ' . WP_PLUGIN_DIR . '/user-meta-manager/backups' . '</p><p>' . __("Please be sure the directory exists and is owner-writable.", UMM_SLUG) . '</p>' . "\n";
endif;
else:
$output = '<p class="umm-message">' . __("Below is the php needed to restore the usermeta table. Save this code as a php file to the root WordPress folder, then run it in your browser.", UMM_SLUG) . '</p><strong>' . __("Backup from", UMM_SLUG) . ' ' . $budate . '</strong><br />
<textarea onclick="this.focus();this.select();" cols="65" rows="15">' . $output . '</textarea>' . "\n";
endif;
break;
default:
$wpdb->query("DROP TABLE IF EXISTS " . $table_prefix . "umm_usermeta_backup");
$wpdb->query("CREATE TABLE " . $table_prefix . "umm_usermeta_backup (umeta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, user_id bigint(20) unsigned NOT NULL DEFAULT '0', meta_key varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, meta_value longtext COLLATE utf8_unicode_ci, PRIMARY KEY (umeta_id), KEY user_id (user_id), KEY meta_key (meta_key))");
$wpdb->query("INSERT INTO " . $table_prefix . "umm_usermeta_backup SELECT * FROM " . $wpdb->usermeta);
umm_update_option('backup_date', date("M d, Y") . ' ' . date("g:i A"));
$output = '<p class="umm-message">' . __("User meta data backup was successful.", UMM_SLUG) . "</p>";
break;
}
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
endif;
if($print):
print '<div class="umm-backup-page-container">' . $back_button . $output . '</div>';
exit;
endif;
}
function umm_backup_page(){
global $wpdb;
if(current_user_can('export')):
$budate = umm_get_option('backup_date');
if($budate == "") $budate = __("No backup", UMM_SLUG);
$nonce = wp_create_nonce(md5($_SERVER["REMOTE_ADDR"].$_SERVER["HTTP_USER_AGENT"]));
$fields1 = umm_usermeta_keys_menu(true, true, true, 'csv', 'umm-csv-builder-keys');
$fields2 = umm_usermeta_keys_menu(true, true, true, 'csv', 'umm-csv-builder-keys', false);
$output = umm_fyi('<p>'.__('Use the following links to backup and restore user meta data.', UMM_SLUG).'</p>');
$output .= '<div class="umm-backup-page-container">';
$output .= '<ul><li><a href="#" data-subpage="' . UMM_AJAX . 'umm_backup&umm_user=1" title="'.__('Backup', UMM_SLUG).'" class="umm-subpage">'.__('Backup', UMM_SLUG).'</a> <strong>'.__('Last Backup:', UMM_SLUG). '</strong> ' . $budate . '</li>';
$output .= '<li><a href="#" data-subpage="' . UMM_AJAX . 'umm_restore_confirm&umm_user=1" title="'.__('Restore', UMM_SLUG).'" class="umm-subpage">'.__('Restore', UMM_SLUG).'</a></li>
<li><a href="#" data-subpage="' . UMM_AJAX . 'umm_backup&mode=sql&umm_user=1" title="'.__('Generate SQL', UMM_SLUG).'" class="umm-subpage">'.__('Generate SQL', UMM_SLUG).'</a></li>
<li><a href="#" data-subpage="' . UMM_AJAX . 'umm_backup&mode=php&umm_user=1" title="'.__('Generate PHP', UMM_SLUG).'" class="umm-subpage">'.__('Generate PHP', UMM_SLUG).'</a></li>
<li><a href="#" data-subpage="' . UMM_AJAX . 'umm_backup&mode=php&tofile=yes&umm_user=1" title="'.__('Generate PHP Restoration File', UMM_SLUG).'" class="umm-subpage">'.__('Generate PHP Restoration File', UMM_SLUG).'</a></li>
<li><a href="#" data-subpage="' . UMM_AJAX . 'umm_delete_backup_files" title="'.__('Delete All Backup Files', UMM_SLUG).'" class="umm-subpage">'.__('Delete All Backup Files', UMM_SLUG).'</a></li>
</ul>';
$output .= '<div class="umm-csv-builder"><strong class="umm-csv-builder">'.__('Generate CSV', UMM_SLUG).'</strong>'
. "<table class='umm-csv-builder'><tr>
<td><strong>" . __('Fields', UMM_SLUG) . ":</strong> <span>" . __("Assemble a list of meta keys to display in the CSV file.", UMM_SLUG) . "</span><input type='hidden' data-for='fields' /><div class='umm-csv-builder-fields'>" . $fields1 . " <input type='button' value=' + ' class='umm-csv-builder-fields-add button-secondary' /></div></td>
</tr>
<tr><td><button class='umm-csv-builder-submit button-primary' data-csv_link='" . esc_url(home_url('/')) . "wp-admin/users.php?page=user-meta-manager&umm_output=csv' title='".__('Get CSV', UMM_SLUG)."'>".__('Generate CSV', UMM_SLUG)."</button></td>
</tr>
</table><div id='umm-csv-builder-fields-clone' class='umm-csv-builder-fields-clone umm-hidden'><div class='umm-csv-builder-fields'>" . $fields2 . " <input type='button' value=' + ' class='button-secondary umm-csv-builder-fields-add' /> <input type='button' value=' - ' class='button-secondary umm-csv-builder-remove' /></div></div>";
$output .= '</div>';
if(umm_is_pro()):
if(function_exists('umm_pro_backup_page')):
$output .= umm_pro_backup_page();
endif;
endif;
$output .= '</div>';
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_button($go_to, $label=null, $css_class=null){
$label = (!$label) ? __('<< Back', UMM_SLUG) : $label;
$css_class = (!$css_class) ? 'button-secondary umm-button' : 'button-secondary umm-button ' . $css_class;
switch($go_to){
case 'home':
$umm_button = '<button href="#" data-type="' . $go_to . '" title="' . $label . '" class="umm-homelink ' . $css_class . '">' . $label . '</button>';
break;
default:
$umm_button = '<button href="#" data-type="subpage" data-subpage="' . UMM_AJAX . '' . $go_to . '" title="' . $label . '" class="' . $css_class . '">' . $label . '</button>';
}
return $umm_button;
}
function umm_column_exists($key){
$used_columns = umm_get_columns();
return array_key_exists($key, $used_columns);
}
function umm_get_option($which=false){
if($umm_data = get_option('user_meta_manager_data')):
if($which):
$which_data = (isset($umm_data[$which])) ? $umm_data[$which] : '';
return $which_data;
else:
return $umm_data;
endif;
else:
return false;
endif;
}
function umm_deactivate(){
global $wpdb, $table_prefix;
$umm_data = umm_get_option();
$umm_settings = $umm_data['settings'];
if(empty($umm_settings)) $umm_settings = array('retain_data' => 'yes');
if($umm_settings['retain_data'] == 'no'):
// Delete all saved data
$custom_meta = $umm_data['custom_meta'];
if(empty($custom_meta)) $custom_meta = array();
$umm_singles_data = $umm_data['singles_data'];
if(empty($umm_singles_data)) $umm_singles_data = array();
$data = $wpdb->get_results("SELECT * FROM " . $wpdb->users);
foreach($data as $user):
foreach($umm_data as $meta_key => $value):
delete_user_meta($user->ID, $meta_key);
endforeach;
foreach($umm_singles_data as $meta_key):
delete_user_meta($user->ID, $meta_key);
endforeach;
endforeach;
delete_option('user_meta_manager_data');
$wpdb->query("DROP TABLE IF EXISTS " . $table_prefix . "umm_usermeta_backup");
// Delete depreciated options if they exist
delete_option('umm_singles_data');
delete_option('umm_users_columns');
delete_option('umm_usermeta_columns');
delete_option('umm_backup_date');
delete_option('umm_backup_files');
delete_option('umm_profile_fields');
delete_option('umm_settings');
delete_option('umm_sort_order');
endif;
}
function umm_delete_backup_files(){
if(current_user_can('export')):
$back_button = umm_button("umm_backup_page&umm_user=1", __('<< Back', UMM_SLUG), "umm-back-button");
if(!empty($_REQUEST['umm_confirm_backups_delete'])):
$backups_folder = WP_PLUGIN_DIR . "/user-meta-manager/backups";
chmod($backups_folder, 0755);
$backup_files = umm_get_option('backup_files');
if(is_array($backup_files) && !empty($backup_files)):
foreach($backup_files as $backup_file):
@unlink($backup_file);
endforeach;
endif;
umm_update_option('backup_files', array());
$output = $back_button . '<p class="umm-message">' . __('All backup files successfully deleted.', UMM_SLUG) . '</p>';
else:
$output = $back_button . '<p class="umm-warning"><strong>' . __('Are you sure you want to delete all backup files?', UMM_SLUG) . '</strong><br /><a href="#" data-subpage="' . UMM_AJAX . 'umm_delete_backup_files&umm_confirm_backups_delete=yes" class="umm-subpage">' . __('Yes', UMM_SLUG) . '</a> <a href="#" data-subpage="' . UMM_AJAX . 'umm_backup_page" class="umm-subpage">' . __('Cancel', UMM_SLUG) . '</a></p>';
endif;
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
return;
}
function umm_default_keys($user_id){
// Sets default and posted values for custom meta upon new user registration.
global $wpdb;
if(!isset($user_id) || empty($user_id)):
$data = umm_usermeta_data("ORDER BY user_id DESC LIMIT 1"); // Gets the latest user id
$user_id = $data[0]->user_id;
endif;
$umm_options = umm_get_option();
$umm_data = $umm_options['custom_meta'];
$profile_fields = $umm_options['profile_fields'];
// Set default values for custom meta
if(isset($umm_data)):
foreach($umm_data as $key => $value):
if((isset($profile_fields[$key]) && $profile_fields[$key]['add_to_profile'] != 'yes') || !isset($profile_fields[$key])):
if(umm_is_pro()):
if(isset($profile_fields[$key]['type']) && $profile_fields[$key]['type'] == 'random_string'):
$random_string_length = (isset($profile_fields[$key]['random_string_length']) && $profile_fields[$key]['random_string_length'] > 0) ? $profile_fields[$key]['random_string_length'] : 10;
$random_string_type = (isset($profile_fields[$key]['random_string_type'])) ? $profile_fields[$key]['random_string_type'] : 'mixed';
if($random_string_type == 'numbers'):
$random_string_type = 1;
elseif($random_string_type == 'letters'):
$random_string_type = 2;
elseif($random_string_type == 'all'):
$random_string_type = 4;
else:
$random_string_type = 3;
endif;
$value = umm_random_str($random_string_length, $random_string_type);
endif;
endif;
update_user_meta($user_id, $key, $value, false);
endif;
endforeach;
endif;
// Set posted values for profile fields
umm_update_profile_fields($user_id);
}
function umm_delete_custom_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$data = umm_get_option('custom_meta');
if(!empty($data)):
$delete_key = (!isset($_REQUEST['umm_edit_key']) || empty($_REQUEST['umm_edit_key'])) ? '' : sprintf("%s", $_REQUEST['umm_edit_key']);
$sub_mode = (!isset($_REQUEST['sub_mode']) || empty($_REQUEST['sub_mode'])) ? '' : sprintf("%s", $_REQUEST['sub_mode']);
if($delete_key == "" && $sub_mode != "confirm"):
$output = umm_fyi('<p>'.__('Select from the menu a meta key to delete.').'</p>');
$output .= '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Meta Key', UMM_SLUG).':</strong> <select id="umm_edit_key" name="umm_edit_key" class="umm_meta_key_menu">
<option value="">'.__('Select A Meta Key', UMM_SLUG).'</option>
';
if($data):
foreach($data as $key => $value):
$output .= '<option value="' . $key . '">' . $key . '</option>' . "\n";
endforeach;
endif;
$output .= '</select> <input id="umm_delete_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary button-delete" type="submit" value="'.__('Submit', UMM_SLUG).'" /><input name="all_users" type="hidden" value="true" /><input name="mode" type="hidden" value="" /><input name="umm_user" type="hidden" value="all" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_delete_custom_meta&umm_user=0" />
</form>
';
else:
$output = '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Deleting', UMM_SLUG).':</strong> ' . $delete_key . '
<p class="umm-warning">
'.__('Are you sure you want to delete that item?', UMM_SLUG).'<br />
<input id="umm_delete_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary button-delete" type="submit" value="'.__('Yes', UMM_SLUG).'" /> ';
$output .= umm_button("umm_delete_custom_meta&umm_user=0", __('Cancel', UMM_SLUG));
$output .= '<input name="umm_edit_key" type="hidden" value="' . $delete_key . '" />
<input name="all_users" type="hidden" value="true" /><input name="mode" type="hidden" value="delete" /><input name="umm_user" type="hidden" value="all" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_delete_custom_meta&umm_user=0" /><input name="sub_mode" type="hidden" value="confirm" /></p>
</form>';
endif;
else: // !empty($data)
$output = __('No custom meta to delete.', UMM_SLUG);
endif; // !empty($data)
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_delete_single_key($key){
global $wpdb;
if(current_user_can('edit_users')):
$umm_data = umm_get_option();
$profile_fields = $umm_data['profile_fields'];
$custom_meta = $umm_data['custom_meta'];
unset($profile_fields[$key]);
unset($umm_data[$key]);
umm_update_option('profile_fields', $profile_fields);
umm_update_option('custom_meta', $custom_meta);
$data = $wpdb->get_results("SELECT * FROM " . $wpdb->users);
foreach($data as $user):
update_user_meta($user->ID, $meta_key, $meta_value, false);
endforeach;
$output = "<p>" . __("Meta data successfully deleted.", UMM_SLUG) . "</p>";
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_delete_user_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$user_id = sprintf("%d", $_REQUEST['umm_user']);
$data = umm_usermeta_data("WHERE user_id = $user_id");
$output = umm_button('home', null, "umm-back-button") . umm_subpage_title($user_id, __('Deleting Meta Data For %s', UMM_SLUG));
$all_users = (isset($_REQUEST['all_users']) && !empty($_REQUEST['all_users'])) ? sprintf("%s", $_REQUEST['all_users']) : '';
$delete_key = (isset($_REQUEST['umm_edit_key']) && trim($_REQUEST['umm_edit_key']) != "" && trim($_REQUEST['umm_edit_key']) != "undefined") ? trim(sprintf("%s", $_REQUEST['umm_edit_key'])) : "";
if($delete_key == ""):
$output .= umm_fyi('<p>'.__('Select a <em>Meta Key</em> to delete, then press the <em>Submit</em> button. Select <em>All Users</em> to delete the selected item from all users.').'</p>', UMM_SLUG);
$output .= '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Meta Key', UMM_SLUG).':</strong> <select id="umm_edit_key" name="umm_edit_key" class="umm_meta_key_menu">
<option value="">'.__('Select A Meta Key', UMM_SLUG).'</option>
';
foreach($data as $d):
$output .= '<option value="' . $d->meta_key . '">' . $d->meta_key . '</option>' . "\n";
endforeach;
$output .= '</select><br />
<strong>'.__('All Users', UMM_SLUG).':</strong> <select name="all_users" size="1">
<option value="false">'.__('No', UMM_SLUG).'</option>
<option value="true">'.__('Yes', UMM_SLUG).'</option>
</select><br />
<input id="umm_delete_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary button-delete" type="submit" value="'.__('Submit', UMM_SLUG).'" />
<input name="mode" type="hidden" value="" /><input name="umm_user" type="hidden" value="' . $user_id . '" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_delete_user_meta&umm_user=' . $user_id . '" />
</form>
';
else:
$output = '<form id="umm_update_user_meta_form" method="post">
<strong>'.__('Deleting', UMM_SLUG).':</strong> ' . $delete_key . '
<p class="umm-warning">
'.__('Are you sure you want to delete that item?', UMM_SLUG).'<br />
<input id="umm_delete_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary button-delete" type="submit" value="'.__('Yes', UMM_SLUG).'" /> ';
$output .= umm_button("umm_delete_user_meta&umm_user=" . $user_id, __('Cancel', UMM_SLUG));
$output .= '<input name="umm_edit_key" type="hidden" value="' . $delete_key . '" /><input name="all_users" type="hidden" value="' . $all_users . '" />
<input name="mode" type="hidden" value="delete" /><input name="umm_user" type="hidden" value="' . $user_id . '" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_delete_user_meta&umm_user=' . $user_id . '" /><input name="sub_mode" type="hidden" value="confirm" /></p>
</form>';
endif;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
endif;
print $output;
exit;
}
function umm_edit_columns(){
$columns = umm_get_columns();
$output = umm_fyi('<p>'.__('Use the forms below to edit which table columns are displayed.', UMM_SLUG).'</p>');
$output .= '<form id="umm_manage_columns_form" method="post">
<h3>'.__('Display Columns', UMM_SLUG).'</h3>
<table class="umm_edit_columns_table wp-list-table widefat fixed">
<thead>
<tr>
<th></th>
<th>'.__('Key', UMM_SLUG).'</th>
<th>'.__('Label', UMM_SLUG).'</th>
</tr>
</thead>
';
$x = 1;
foreach($columns as $k => $v){
$c = ($x%2) ? "" : "alternate";
$cb = ($k != 'ID' && $k != 'user_login') ? '<input type="radio" value="'.$k.'|" name="umm_column_key" />' : '<input type="radio" value="'.$k.'|" name="umm_column_key" disabled="disabled" title="Required" />';
$output .= '<tr class="' . $c . '"><td>' . $cb . '</td><td>' . $k . '</td><td>' . $v . '</td></tr>' . "\n";
$x++;
}
$output .= '</table>
<input id="umm_update_columns_submit" data-form="umm_manage_columns_form" data-subpage="umm_update_columns" data-wait="'.__('Wait...', UMM_SLUG).'" data-mode="delete" class="button-primary" type="submit" value="'.__('Remove Selected Column', UMM_SLUG).'" />
<input name="mode" type="hidden" value="remove_columns" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_columns" />
</form>
<form id="umm_add_columns_form" method="post">
<h3>'.__('Add A New Column', UMM_SLUG).'</h3>
<strong>'.__('Key', UMM_SLUG).':</strong> <select name="umm_column_key">
<option value="">'.__('Keys', UMM_SLUG).'</option>';
$output .= umm_users_keys_menu(false, true);
$output .= umm_usermeta_keys_menu(false, true);
$output .= '</select><br>
<strong>'.__('Label', UMM_SLUG).':</strong> <input name="umm_column_label" type="text" value="" placeholder="'.__('Enter a label', UMM_SLUG).'" title="'.__('Enter a label which will appear in the top row of the results table.', UMM_SLUG).'" /><br />';
$output .= '<input id="umm_update_columns_submit" data-form="umm_add_columns_form" data-subpage="umm_update_columns" data-wait="'.__('Wait...', UMM_SLUG).'" data-mode="add" class="button-primary" type="submit" value="'.__('Add Column', UMM_SLUG).'" />
<input name="mode" type="hidden" value="add_columns" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_columns" />
</form>
';
print $output;
exit;
}
function umm_edit_custom_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$data = umm_get_option('custom_meta');
if(!$data):
$output = __('No custom meta to edit.', UMM_SLUG);
else:
$edit_key = (!isset($_REQUEST['umm_edit_key']) || empty($_REQUEST['umm_edit_key'])) ? '' : sprintf("%s", $_REQUEST['umm_edit_key']);
if($edit_key == ""):
$output = umm_fyi('<p>'.__('Select from the list a meta key to edit, or drag and drop to sort.', UMM_SLUG).'</p>');
$output .= '<form id="umm_update_user_meta_form" method="post" data-error_message="' . __('Select a meta key to edit.', UMM_SLUG). '">
<h2>'.__('Edit Key', UMM_SLUG).'</h2>
<ul id="umm_edit_key">
';
$sort_order = umm_get_option('sort_order');
if(empty($sort_order) || !is_array($sort_order)):
$sort_order = array();
$x = 0;
foreach($data as $field_name => $field_settings):
$sort_order[$x] = $field_name;
$x++;
endforeach;
endif;
/* Sort the fields */
if($sort_order):
$new_array = array();
foreach($sort_order as $profile_field_name):
if(isset($data[$profile_field_name]))
$new_array[$profile_field_name] = $data[$profile_field_name];
endforeach;
$data = $new_array;
endif;
$x = 1;
foreach($data as $key => $value):
$class = ($x%2) ? ' class="alternate"' : ' class="umm-draggable"';
$output .= '<li' . $class . ' title="'.__('Drag and drop to sort.', UMM_SLUG).'"><input type="radio" name="umm_edit_key" value="'.$key.'" title="'.__('Select a key to edit.', UMM_SLUG).'" /><input name="umm_item[]" type="hidden" value="' . $key . '" /> '.$key.'</li>
';
$x++;
endforeach;
$output .= '</ul>';
$output .= '<input id="umm_edit_custom_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Submit', UMM_SLUG).'" />
<input name="mode" type="hidden" value="" /><input name="umm_user" type="hidden" value="all" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_custom_meta" />
</form>
';
else:
$profile_fields = umm_get_option('profile_fields');
if(!$profile_fields) $profile_fields = array();
$output = '<strong>' . __('Now Editing', UMM_SLUG) . ':</strong> <span class="umm-highlight">' . sprintf("%s", $_REQUEST['umm_edit_key']) . '</span>';
$output .= umm_fyi('<p>'.__('Editing custom meta data here will edit the value for all new users. The value you set will become the default value for all users. New registrations will receive the custom meta key and default value.', UMM_SLUG).'</p>');
$output .= '<form id="umm_update_user_meta_form" method="post">
';
if(!$data):
$output .= '<tr>
<td colspan="2">' . __('No custom meta to display.', UMM_SLUG) . '</td>
</tr>';
else:
foreach($data as $key => $value):
if($key == $_REQUEST['umm_edit_key']):
$output .= '<strong>' . __('Value', UMM_SLUG) . ':</strong><input name="umm_meta_key[]" type="hidden" value="' . $key . '" /><br /><textarea rows="3" cols="40" name="umm_meta_value[]" placeholder="">' . htmlspecialchars($value) . '</textarea><br />';
endif;
endforeach;
endif;
$output .= '<strong>' . __('Update Value For All Current Users', UMM_SLUG) . ':</strong><br /><input type="checkbox" name="all_users" value="1" title="' . __('Check the box to update the value for all current users. Leave blank to update the value for future registrations only.', UMM_SLUG) . '" /> ' . __('Yes', UMM_SLUG) . '';
$output .= umm_profile_field_editor($edit_key);
$output .= '<input id="umm_update_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Update', UMM_SLUG).'" />
<input name="mode" type="hidden" value="edit" /><input name="umm_user" type="hidden" value="all" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_custom_meta" />
</form>
';
endif; // edit_key
endif; // !$data
print $output;
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print $output;
exit;
endif;
}
function umm_edit_user_meta(){
global $wpdb;
if(current_user_can('edit_users')):
$profile_fields = umm_get_option('profile_fields');
$user_id = sprintf("%d", $_REQUEST['umm_user']);
$data = umm_usermeta_data("WHERE user_id = $user_id");
sort($data);
$umm_settings = umm_get_option('settings');
$shortcut_editing = empty($umm_settings['shortcut_editing']) ? 'no' : $umm_settings['shortcut_editing'];
$output = umm_button('home', null, "umm-back-button") . umm_subpage_title($user_id, __('Editing Meta Data For %s', UMM_SLUG));
$output .= umm_fyi('<p>'.__('Editing an item here will only edit the item for the selected user and not for all users.<br /><a href="#" data-subpage="' . UMM_AJAX . 'umm_edit_custom_meta&umm_user=1" data-nav_button="Edit Custom Meta" title="Edit Custom Meta" class="umm-subpage">Edit Custom Meta Data For All Users</a>', UMM_SLUG).'</p>');
$edit_key = (isset($_REQUEST['umm_edit_key']) && !empty($_REQUEST['umm_edit_key'])) ? sprintf("%s", $_REQUEST['umm_edit_key']) : '';
if($edit_key == "" && $shortcut_editing == 'no'):
$output .= '<form id="umm_update_user_meta_form" method="post">
<strong>Edit Key:</strong> <select id="umm_edit_key" name="umm_edit_key" title="' . __('Select a meta key to edit.', UMM_SLUG) . '">
<option value="">' . __('Select A Key To Edit', UMM_SLUG) . '</option>
';
foreach($data as $d):
$output .= '<option value="'.$d->meta_key.'">'.$d->meta_key.'</option>
';
endforeach;
$output .= '</select>
<input id="umm_edit_custom_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Submit', UMM_SLUG).'" />
<input name="mode" type="hidden" value="edit" /><input name="umm_user" type="hidden" value="' . $user_id . '" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_user_meta&umm_user=' . $user_id . '" />
</form>
';
else:
if($shortcut_editing == 'no'):
$output .= '<strong>' . __('Now Editing', UMM_SLUG) . ':</strong> ' . sprintf("%s", $_REQUEST['umm_edit_key']);
endif;
$output .= '<form id="umm_update_user_meta_form" method="post">
<table class="umm_edit_table wp-list-table widefat">
<thead>
<tr>
<th>'.__('Key', UMM_SLUG).'</th>
<th>'.__('Value', UMM_SLUG).'</th>
</tr>
</thead>
';
$x = 1;
foreach($data as $d):
$class = ($x%2) ? ' class="alternate"' : '';
if($d->meta_key == $edit_key || $shortcut_editing == 'yes'):
$output .= '<tr' . $class . '><td width="25%">' . $d->meta_key . '</td><td><input name="umm_meta_key[]" type="hidden" value="' . $d->meta_key . '" /><textarea name="umm_meta_value[]" value="" cols="40" rows="1">' . stripslashes(htmlspecialchars($d->meta_value)) . '</textarea>';
if(array_key_exists($d->meta_key, $profile_fields) && $profile_fields[$d->meta_key]['unique'] == 'yes'):
$output .= ' <small>*'.__('Unique Value Required', UMM_SLUG).'</small>';
endif;
$output .= '</td></tr>';
$x++;
endif;
endforeach;
$output .= '</table>';
if($shortcut_editing == 'yes'):
$output .= '<input name="umm_edit_key" type="hidden" value="all" />';
endif;
$output .= '<input id="umm_update_user_meta_submit" data-form="umm_update_user_meta_form" data-subpage="umm_update_user_meta" data-wait="'.__('Wait...', UMM_SLUG).'" class="button-primary" type="submit" value="'.__('Update', UMM_SLUG).'" />
<input name="mode" type="hidden" value="edit" /><input name="umm_user" type="hidden" value="' . $user_id . '" /><input name="return_page" type="hidden" value="' . UMM_AJAX . 'umm_edit_user_meta&umm_user=' . $user_id . '" />
</form>
';
endif;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
endif;
print $output;
exit;
}
function umm_first_run(){
$settings['first_run'] = 'no';
umm_update_option('settings', $settings);
}
function umm_format_text($text, $format){
switch ($format):
case 'ucfirst':
$formatted = ucfirst($text);
break;
case 'ucwords':
$formatted = ucwords($text);
break;
case 'uc':
$formatted = strtoupper($text);
break;
case 'lc':
$formatted = strtolower($text);
break;
default:
$formatted = $text;
break;
endswitch;
return $formatted;
}
function umm_fyi($message){
return '<div class="umm-fyi">' . $message . '</div>';
}
function umm_get_columns(){
if(current_user_can('export')):
$umm_options = umm_get_option();
$users_columns = (!$umm_options["users_columns"] ? array('ID' => __('ID', UMM_SLUG), 'user_login' => __('User Login', UMM_SLUG), 'user_registered' => __('Date Registered', UMM_SLUG)) : $umm_options["users_columns"]);
$usermeta_columns = (!$umm_options["usermeta_columns"]) ? array() : $umm_options["usermeta_columns"];
return array_merge($users_columns, $usermeta_columns);
endif;
}
function umm_get_csv(){
if(current_user_can('export')):
$filename = 'user_meta_data_' . date('m-j-y-g-i-a') . '.csv';
$data = umm_usermeta_data();
$output = '"umeta_id","user_id","meta_key","meta_value"' . "\n";
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
foreach($data as $o):
if(!empty($o->umeta_id)):
if(isset($_REQUEST['umm_key']) && !empty($_REQUEST['umm_key'])):
if(in_array($o->meta_key, $_REQUEST['umm_key']) || $_REQUEST['umm_key'][0] == 'all'):
$output .= '"' . $o->umeta_id . '","' . $o->user_id . '","' . $o->meta_key . '","' . addslashes($o->meta_value) . '"' . "\n";
endif;
else:
$output .= '"' . $o->umeta_id . '","' . $o->user_id . '","' . $o->meta_key . '","' . addslashes($o->meta_value) . '"' . "\n";
endif;
endif;
endforeach;
print($output);
exit;
else:
$output = '<p class="umm-message">' . __("You are not authorized to perform this operation.", UMM_SLUG) . '</p>' . "\n";
print($output);
exit;
endif;
}
function umm_get_profile_fields($output_type='array'){
$umm_options = umm_get_option();
$profile_fields = $umm_options['profile_fields'];
if(empty($profile_fields) || !is_array($profile_fields)) $profile_fields = array();
$sort_order = $umm_options['sort_order'];
if(empty($sort_order) || !is_array($sort_order)) $sort_order = false;
if($sort_order):
$new_array = array();
foreach($sort_order as $profile_field_name):
if(isset($profile_fields[$profile_field_name]))
$new_array[$profile_field_name] = $profile_fields[$profile_field_name];
endforeach;
$profile_fields = $new_array;
endif;
switch($output_type){
case "select":
$output = '<select class="umm-profile-fields-select" name="umm_edit_key">' . "\n";
$output .= '<option value="" selected="selected">' . __('Select A Key', UMM_SLUG) . '</option>' . "\n";
foreach($profile_fields as $key => $settings):
$output .= '<option value="' . $key . '">' . $key . '</option>' . "\n";
endforeach;
$output .= '</select>' . "\n";
return $output;
break;
case "radio":
foreach($profile_fields as $key => $settings):
$output .= '<input type="radio" name="umm_edit_key" value="' . $key . '" />' . $key . ' ' . "\n";
endforeach;
return $output;
break;
default:
// Return array
return $profile_fields;
}
}
function umm_get_users($query=false){
global $wpdb;
if(current_user_can('export')):
$umm_settings = umm_get_option('settings');
$m = (isset($umm_settings['max_users']) && !empty($umm_settings['max_users'])) ? $umm_settings['max_users'] : 100;
if(umm_is_pro()):
if(!$query):
$query = "SELECT * FROM " . $wpdb->users;
endif;
else:
if(!$query):
$query = "SELECT * FROM " . $wpdb->users . " LIMIT 0, " . $m;
endif;
endif;
$data = $wpdb->get_results($query);
if(defined('MULTISITE') && MULTISITE):
$blog_id = get_current_blog_id();
$user_data = array();
foreach($data as $d):
if(is_user_member_of_blog($d->ID, $blog_id)):
array_push($user_data, $d);
endif;
endforeach;
$data = $user_data;
endif;
return $data;
endif;
}
function umm_install(){
$default_html_before = '<h3 class="umm-custom-fields">[section-title]</h3>
<table class="form-table umm-custom-fields">
<tbody>';
$default_html_during = '<tr><th>[label]</th><td>[field]</td></tr>';
$default_html_after = '</tbody>
</table>';
$umm_data = array();
$original_data = get_option('user_meta_manager_data');
if(!is_array($original_data)):
if($original_data != ''):
// Backwards compatibility
$new_array = array();
$d = explode(",", $original_data);
foreach($d as $k):
array_push($new_array, trim(stripslashes($k)));
endforeach;
$original_data['custom_meta'] = $new_array;
else:
$original_data = array();
endif;
endif;
if(!array_key_exists('custom_meta', $original_data)):
$umm_data['custom_meta'] = array();
else:
$umm_data['custom_meta'] = $original_data['custom_meta'];
endif;
if(!array_key_exists('singles_data', $original_data)):
$umm_data['singles_data'] = get_option('umm_singles_data');
if(!is_array($umm_data['singles_data'])):
$umm_data['singles_data'] = array();
endif;
else:
$umm_data['singles_data'] = $original_data['singles_data'];
endif;
if(!array_key_exists('users_columns', $original_data)):
$umm_data['users_columns'] = get_option('umm_users_columns');
if(!is_array($umm_data['users_columns'])):
$umm_data['users_columns'] = array('ID' => __('ID', UMM_SLUG), 'user_login' => __('User Login', UMM_SLUG), 'user_registered' => __('Date Registered', UMM_SLUG));
endif;
else:
$umm_data['users_columns'] = $original_data['users_columns'];
endif;
if(!array_key_exists('usermeta_columns', $original_data)):
$umm_data['usermeta_columns'] = get_option('umm_usermeta_columns');
if(!is_array($umm_data['usermeta_columns'])):
$umm_data['usermeta_columns'] = array();
endif;
else:
$umm_data['usermeta_columns'] = $original_data['usermeta_columns'];
endif;
if(!array_key_exists('backup_files', $original_data)):
$umm_data['backup_files'] = get_option('umm_backup_files');
if(!is_array( $umm_data['backup_files'])):
$umm_data['backup_files'] = array();
endif;
else:
$umm_data['backup_files'] = $original_data['backup_files'];
endif;
if(!array_key_exists('profile_fields', $original_data)):
$umm_data['profile_fields'] = get_option('umm_profile_fields');
if(!is_array($umm_data['profile_fields'])):
$umm_data['profile_fields'] = array();
endif;
else:
$umm_data['profile_fields'] = $original_data['profile_fields'];
endif;
if(!array_key_exists('settings', $original_data)):
$umm_data['settings'] = get_option('umm_settings');
if(!is_array($umm_data['settings'])):
$umm_data['settings'] = array('retain_data' => 'yes',
'max_users' => 100,
'first_run' => 'yes',
'shortcut_editing' => 'no',
'section_title' => '',
'duplicate_check_override' => 'no',
'bot_field' => 'umm_forbots',
'version' => UMM_VERSION,
'html_before_adduser' => $default_html_before,
'html_during_adduser' => $default_html_during,
'html_after_adduser' => $default_html_after,
'html_before_register' => $default_html_before,
'html_during_register' => $default_html_during,
'html_after_register' => $default_html_after,
'html_before_shortcode' => $default_html_before,