-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
818 lines (656 loc) · 24.4 KB
/
create.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
<?php
/**
* @package Create
* @author Villag
* @license GPL-2.0+
* @link https://github.com/Villag/create-plugin
* @copyright 2014 Villag
*
* @wordpress-plugin
* Plugin Name: Create
* Plugin URI: https://github.com/Villag/create-plugin
* Description: Creative directory
* Version: 1.1
* Author: Villag
* Author URI: https://github.com/Villag
* Text Domain: create
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/Villag/create-plugin
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Register user taxonomies
add_action( 'init', 'create_register_user_taxonomy' );
// Insert 'user_category' CSS into HEAD
add_action( 'wp_head', 'create_user_category_styles' );
// Ajax: get users
add_action( 'wp_ajax_nopriv_create_get_users', 'create_get_users' );
add_action( 'wp_ajax_create_get_users', 'create_get_users' );
// Ajax: email user
add_action( 'wp_ajax_nopriv_create_email_user', 'create_email_user' );
add_action( 'wp_ajax_create_email_user', 'create_email_user' );
// Ajax: save user profile
add_action( 'wp_ajax_nopriv_create_save_user_profile', 'create_save_user_profile' );
add_action( 'wp_ajax_create_save_user_profile', 'create_save_user_profile' );
// After user registration, login user
add_action( 'gform_user_registered', 'create_gravity_registration_autologin', 10, 4 );
add_action( 'user_register', 'create_user_registered', 10, 1 );
add_action( 'init', 'create_on_avatar_save' );
/**
* Via Ajax, sends the given user an email. This avoids exposing the user's
* email address to anyone.
*/
function create_email_user() {
check_ajax_referer( 'create_email_user_ajax_nonce', 'security' );
$subject = $_POST['subject'];
$message = $_POST['message'];
$user_object_to = get_userdata( $_POST['user_id_to'] );
$user_object_from = get_userdata( $_POST['user_id_from'] );
$to = $user_object_to->user_email;
$headers[] = 'From: '. $user_object_from->first_name .' '. $user_object_from->last_name .' <'. $user_object_from->user_email .'>';
$headers[] = 'Reply-To: ' . $user_object_from->user_email;
$result = wp_mail( sanitize_email( $to ), esc_html( $subject ), $message, $headers );
if ( isset( $result ) && ( $result == 1 ) ) {
die(
json_encode(
array(
'success' => true,
'message' => __( 'Your email was successfully sent to '. $user_object_to->first_name .'.', 'create' )
)
)
);
} else {
die(
json_encode(
array(
'success' => false,
'message' => __( 'An error occured. Please refresh the page and try again.', 'create' )
)
)
);
}
}
/**
* Via Ajax, sends the given user an email. This avoids exposing the user's
* email address to anyone.
*/
function create_save_user_profile() {
check_ajax_referer( 'create_save_user_profile_ajax_nonce', 'security' );
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$user_id = $_POST['user_id'];
$blog_id = $_POST['blog_id'];
$twitter = $_POST['twitter'];
$website = $_POST['website'];
$linkedin = $_POST['linkedin'];
$phone = $_POST['phone'];
$zip = $_POST['zip_code'];
$skills = $_POST['skills'];
$user_category = $_POST['user_category'];
$bio = $_POST['bio'];
$allowed_html = array(
'a' => array(
'href' => array(),
'title' => array()
),
'br' => array(),
'em' => array(),
'strong' => array()
);
$meta_value = array(
'twitter' => sanitize_text_field( $twitter ),
'website' => esc_url( $website ),
'linkedin' => esc_url( $linkedin ),
'email' => sanitize_email( $email ),
'phone' => sanitize_text_field( $phone ),
'zip_code' => intval( $zip ),
'skills' => $skills,
'bio' => wp_kses( $bio, $allowed_html )
);
// Global user meta
update_user_meta( $user_id, 'first_name', sanitize_text_field( $first_name ) );
update_user_meta( $user_id, 'last_name', sanitize_text_field( $last_name ) );
// Save user_category to taxonomy
wp_set_object_terms( $user_id, intval( $user_category ), 'user_category' );
$blog_details = get_blog_details( $blog_id );
// Blog-specific user meta
$result = update_user_meta( $user_id, 'user_meta_'. str_replace( '/', '', $blog_details->path ), $meta_value );
if ( isset( $result ) ) {
add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => 'subscriber' ) );
$cache = create_clear_cache( array( 'user_id' => $user_id ) );
$errors = create_user_errors( $user_id );
if( empty( $errors ) ) {
create_clear_cache( 'all' );
}
die(
json_encode(
array(
'success' => true,
'message' => __( 'Your profile has been updated.', 'create' ),
'cache' => $cache
)
)
);
} else {
die(
json_encode(
array(
'success' => false,
'message' => __( 'An error occured. Please refresh the page and try again.', 'create' ),
'cache' => $cache
)
)
);
}
}
/**
* Gets all users for the current site and returns the data as a JSON
* encoded object for use by an ajax call from the theme.
*/
function create_get_users() {
if ( false === ( $users = get_transient( 'users_query' ) ) ) {
$users = get_users( array( 'fields' => 'ID' ) );
foreach ( $users as $user_id ) {
$errors = create_user_errors( $user_id );
if( ! empty( $errors ) ) {
continue;
} else {
$valid_users[] = $user_id;
}
}
shuffle( $valid_users );
set_transient( 'users_query', $valid_users, 12 * HOUR_IN_SECONDS );
} else {
$valid_users = get_transient( 'users_query' );
}
if( ! empty( $valid_users ) ) {
foreach ( $valid_users as $user_id ) {
$user_objects[] = create_get_user( $user_id );
}
$result = array( 'users' => $user_objects );
}
if ( isset( $result ) ) {
die( json_encode( $result ) );
} else {
die(
json_encode(
array(
'success' => false,
'message' => __( 'An error occured. Please refresh the page and try again.', 'nervetask' )
)
)
);
}
}
function create_user_registered( $user_id ) {
create_clear_cache( 'all' );
}
function create_get_user( $user_id ) {
// Get the site-specific user meta
$blog_id = get_current_blog_id();
$blog_details = get_blog_details( $blog_id );
$blog_slug = str_replace( '/', '', $blog_details->path );
if ( false === ( $user = get_transient( 'user_meta_'. $blog_slug .'_'. $user_id ) ) ) {
$user_meta = get_user_meta( $user_id, 'user_meta_'. str_replace( '/', '', $blog_details->path ), true );
// Get the user category
$user_categories = wp_get_object_terms( $user_id, 'user_category' );
if ( $user_categories && ! is_wp_error( $user_categories ) ) :
$user_category_slugs = array();
$user_category_names = array();
foreach ( $user_categories as $user_category ) {
$user_category_slugs[] = $user_category->slug;
$user_category_names[] = $user_category->name;
}
$types = join( ' ', $user_category_slugs );
$primary_jobs = join( ' ', $user_category_names );
endif;
$userdata = get_userdata( $user_id );
$user_object['email'] = $userdata->user_email;
$user_object['ID'] = $user_id;
$user_object['types'] = $types;
$user_object['primary_jobs'] = $primary_jobs;
$user_object['first_name'] = get_user_meta( $user_id, 'first_name', true );
$user_object['last_name'] = get_user_meta( $user_id, 'last_name', true );
$user_object['avatar'] = get_stylesheet_directory_uri() . "/timthumb.php?src=". create_get_wp_user_avatar_src( $user_id, 150 ) ."&w=150&h=150&zc=1&a=c&f=2";
$user = array_merge( $user_object, $user_meta );
set_transient( 'user_meta_'. $blog_slug .'_'. $user_id, $user, 12 * HOUR_IN_SECONDS );
} else {
$user = get_transient( 'user_meta_'. $blog_slug .'_'. $user_id );
}
return $user;
}
function create_get_wp_user_avatar_src( $user_id, $size = 150 ) {
$original = get_wp_user_avatar_src( $user_id, $size );
$home_url = get_bloginfo( 'url' );
$output = str_replace( $home_url, '', $original );
return $output;
}
function create_mail_from( $email ) {
return '[email protected]';
}
add_filter( 'wp_mail_from', 'create_mail_from' );
/**
* Auto login after registration.
*/
function create_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
$user = get_userdata( $user_id );
$user_login = $user->user_login;
$user_password = $password;
wp_signon( array(
'user_login' => $user_login,
'user_password' => $user_password,
'remember' => false
) );
}
/**
* WordPress register with email only, make it possible to register with email
* as username in a multisite installation
* @param Array $result Result array of the wpmu_validate_user_signup-function
* @return Array Altered result array
*/
function custom_register_with_email( $result ) {
if ( $result['user_name'] != '' && is_email( $result['user_name'] ) ) {
unset( $result['errors']->errors['user_name'] );
}
return $result;
}
add_filter( 'wpmu_validate_user_signup','custom_register_with_email' );
/**
* Get the 'user_category' terms and colors and create a <style> block
*/
function create_user_category_styles() {
$terms = get_terms( array( 'user_category' ), array( 'hide_empty' => false ) );
if( $terms ) {
echo "<style id='job_manager_colors'>\n";
$user_category_options = get_option( 'user_category_options' );
foreach ( $terms as $term ) {
if( ! array_key_exists( $term->term_id, $user_category_options) ) {
continue;
}
foreach( $user_category_options[$term->term_id] as $term_meta ) {
$color = $term_meta;
echo '.item.'. $term->slug .', .'. $term->slug .' .modal-header { background: '. $color .'}';
echo '.card-back.'. $term->slug .' a { color: '. $color .'}';
echo '[data-filter=".'. $term->slug .'"] { border-color: '. $color .'}';
echo '[data-filter=".'. $term->slug .'"].selected { background: '. $color .'; color: #fff; }';
}
}
echo "</style>\n";
}
}
/**
* Checks if the user is valid (has all the right info) and returns boolean.
*
*/
function create_is_valid_user( $user_id ) {
$errors = create_user_errors( $user_id );
if( ! empty( $errors ) ) {
return false;
} else {
return true;
}
}
/**
* Displays the errors a user has
* (i.e. missing data required to be a valid user)
*/
function create_user_errors( $user_id ) {
$blog_id = get_current_blog_id();
$blog_details = get_blog_details( $blog_id );
$user_info = get_userdata( $user_id );
$user_meta = get_user_meta( $user_id, 'user_meta_'. str_replace( '/', '', $blog_details->path ), true );
$first_name = get_user_meta( $user_id, 'first_name', true );
$last_name = get_user_meta( $user_id, 'first_name', true );
$email = $user_info->user_email;
$zip = isset( $user_meta['zip_code'] );
$primary_job = isset( $user_meta['primary_jobs'] );
$avatar = get_wp_user_avatar_src( $user_id, 150 );
// Get the user category
$user_categories = wp_get_object_terms( $user_id, 'user_category' );
if ( $user_categories && ! is_wp_error( $user_categories ) ) :
$user_category_slugs = array();
$user_category_names = array();
foreach ( $user_categories as $user_category ) {
$user_category_slugs[] = $user_category->slug;
$user_category_names[] = $user_category->name;
}
$types = join( ' ', $user_category_slugs );
$primary_jobs = join( ' ', $user_category_names );
endif;
$errors = array();
if ( empty( $email ) )
$errors[] = ' email';
if ( empty( $first_name ) )
$errors[] = ' first name';
if ( empty( $last_name ) )
$errors[] = ' last name';
if ( empty( $zip ) )
$errors[] = ' zip code';
if ( empty( $primary_jobs ) )
$errors[] = ' talent';
if ( ! isset( $avatar ) )
$errors[] = ' avatar';
$output = implode( ',', $errors );
return $output;
}
/**
* Get the user's avatar.
*/
function create_get_avatar( $user_id, $size = 150 ) {
$image = get_wp_user_avatar_src( $user_id, $size );
if( strpos( $image, 'wpua') !== false ) {
$image = get_user_meta( $user_id, 'avatar', true );
if( strpos( $image, 'http') !== false ) {
$image = $image;
} else {
$image = get_stylesheet_directory_uri() .'/uploads/avatars/'. $image;
}
}
return $image;
}
/**
* Clears the user's cache when their avatar is updated
*/
function create_on_avatar_save() {
if( isset( $_POST['wp-user-avatar'] ) ) {
create_clear_cache( array( 'user_id' => $_POST['user_id'] ) );
}
}
/**
* Clear the cached user query so this new avatar will show up
*/
function create_clear_cache( $args ) {
if( empty( $args ) ) {
return;
}
if ( array_key_exists( 'user_id', $args ) ) {
if( ! empty( $args['user_id'] ) ) {
// Delete the user's object cache
$blog_id = get_current_blog_id();
$blog_details = get_blog_details( $blog_id );
$blog_slug = str_replace( '/', '', $blog_details->path );
delete_transient( 'user_meta_'. $blog_slug .'_'. $args['user_id'] );
return 'User '. $args['user_id'] . ' cleared';
}
} elseif( $args == 'all' ) {
delete_transient( 'users_query' );
if( class_exists('W3_Plugin_TotalCacheAdmin') ) {
$plugin_totalcacheadmin = & w3_instance('W3_Plugin_TotalCacheAdmin');
$plugin_totalcacheadmin->flush_all();
}
return 'Cleared users_query cache';
}
return 'Did not clear any caches';
}
/**
* Registers the 'user_category' taxonomy for users. This is a taxonomy for the 'user' object type rather than a
* post being the object type.
*/
function create_register_user_taxonomy() {
register_taxonomy(
'user_category',
'user',
array(
'public' => true,
'labels' => array(
'name' => __( 'Users Categories' ),
'singular_name' => __( 'Category' ),
'menu_name' => __( 'Categories' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'separate_items_with_commas' => __( 'Separate categories with commas' ),
'add_or_remove_items' => __( 'Add or remove categories' ),
'choose_from_most_used' => __( 'Choose from the most popular categories' ),
),
'rewrite' => array(
'with_front' => true,
'slug' => 'author/user_category' // Use 'author' (default WP user slug).
),
'capabilities' => array(
'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
'edit_terms' => 'edit_users',
'delete_terms' => 'edit_users',
'assign_terms' => 'read',
),
'update_count_callback' => 'create_update_user_category_count' // Use a custom function to update the count.
)
);
}
/**
* Function for updating the 'user_category' taxonomy count. What this does is update the count of a specific term
* by the number of users that have been given the term. We're not doing any checks for users specifically here.
* We're just updating the count with no specifics for simplicity.
*
* See the _update_post_term_count() function in WordPress for more info.
*
* @param array $terms List of Term taxonomy IDs
* @param object $taxonomy Current taxonomy object of terms
*/
function create_update_user_category_count( $terms, $taxonomy ) {
global $wpdb;
foreach ( (array) $terms as $term ) {
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
do_action( 'edit_term_taxonomy', $term, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
do_action( 'edited_term_taxonomy', $term, $taxonomy );
}
}
/* Adds the taxonomy page in the admin. */
add_action( 'admin_menu', 'create_add_user_category_admin_page' );
/**
* Creates the admin page for the 'user_category' taxonomy under the 'Users' menu. It works the same as any
* other taxonomy page in the admin. However, this is kind of hacky and is meant as a quick solution. When
* clicking on the menu item in the admin, WordPress' menu system thinks you're viewing something under 'Posts'
* instead of 'Users'. We really need WP core support for this.
*/
function create_add_user_category_admin_page() {
$tax = get_taxonomy( 'user_category' );
add_users_page(
esc_attr( $tax->labels->menu_name ),
esc_attr( $tax->labels->menu_name ),
$tax->cap->manage_terms,
'edit-tags.php?taxonomy=' . $tax->name
);
}
/* Create custom columns for the manage user_category page. */
add_filter( 'manage_edit-user_category_columns', 'create_manage_user_category_user_column' );
/**
* Unsets the 'posts' column and adds a 'users' column on the manage user_category admin page.
*
* @param array $columns An array of columns to be shown in the manage terms table.
*/
function create_manage_user_category_user_column( $columns ) {
unset( $columns['posts'] );
$columns['users'] = __( 'Users' );
return $columns;
}
/* Customize the output of the custom column on the manage categories page. */
add_action( 'manage_user_category_custom_column', 'create_manage_user_category_column', 10, 3 );
/**
* Displays content for custom columns on the manage categories page in the admin.
*
* @param string $display WP just passes an empty string here.
* @param string $column The name of the custom column.
* @param int $term_id The ID of the term being displayed in the table.
*/
function create_manage_user_category_column( $display, $column, $term_id ) {
if ( 'users' === $column ) {
$term = get_term( $term_id, 'user_category' );
echo $term->count;
}
}
/* Add section to the edit user page in the admin to select user_category. */
add_action( 'show_user_profile', 'create_edit_user_user_category_section' );
add_action( 'edit_user_profile', 'create_edit_user_user_category_section' );
/**
* Adds an additional settings section on the edit user/profile page in the admin. This section allows users to
* select a user_category from a checkbox of terms from the user_category taxonomy. This is just one example of
* many ways this can be handled.
*
* @param object $user The user object currently being edited.
*/
function create_edit_user_user_category_section( $user ) {
$tax = get_taxonomy( 'user_category' );
/* Make sure the user can assign terms of the user_category taxonomy before proceeding. */
if ( !current_user_can( $tax->cap->assign_terms ) )
return;
/* Get the terms of the 'user_category' taxonomy. */
$terms = get_terms( 'user_category', array( 'hide_empty' => false ) ); ?>
<h3><?php _e( 'Category' ); ?></h3>
<table class="form-table">
<tr>
<th><label for="user_category"><?php _e( 'Select Category' ); ?></label></th>
<td><?php
/* If there are any user_category terms, loop through them and display checkboxes. */
if ( !empty( $terms ) ) {
foreach ( $terms as $term ) { ?>
<input type="radio" name="user_category" id="user_category-<?php echo esc_attr( $term->slug ); ?>" value="<?php echo esc_attr( $term->slug ); ?>" <?php checked( true, is_object_in_term( $user->ID, 'user_category', $term ) ); ?> /> <label for="user_category-<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?></label> <br />
<?php }
}
/* If there are no user_category terms, display a message. */
else {
_e( 'There are no categories available.' );
}
?></td>
</tr>
</table>
<?php }
/* Update the user_category terms when the edit user page is updated. */
add_action( 'personal_options_update', 'create_save_user_user_category_terms' );
add_action( 'edit_user_profile_update', 'create_save_user_user_category_terms' );
/**
* Saves the term selected on the edit user/profile page in the admin. This function is triggered when the page
* is updated. We just grab the posted data and use wp_set_object_terms() to save it.
*
* @param int $user_id The ID of the user to save the terms for.
*/
function create_save_user_user_category_terms( $user_id ) {
$tax = get_taxonomy( 'user_category' );
/* Make sure the current user can edit the user and assign terms before proceeding. */
if ( !current_user_can( 'edit_user', $user_id ) && current_user_can( $tax->cap->assign_terms ) )
return false;
$term = esc_attr( $_POST['user_category'] );
/* Sets the terms (we're just using a single term) for the user. */
wp_set_object_terms( $user_id, array( $term ), 'user_category', false);
clean_object_term_cache( $user_id, 'user_category' );
}
/**
* Registering meta sections for taxonomies
*
* All the definitions of meta sections are listed below with comments, please read them carefully.
* Note that each validation method of the Validation Class MUST return value.
*
* You also should read the changelog to know what has been changed
*
*/
// Hook to 'admin_init' to make sure the class is loaded before
// (in case using the class in another plugin)
add_action( 'admin_init', 'create_register_taxonomy_meta_boxes' );
/**
* Register meta boxes
*
* @return void
*/
function create_register_taxonomy_meta_boxes() {
// Make sure there's no errors when the plugin is deactivated or during upgrade
if ( !class_exists( 'RW_Taxonomy_Meta' ) )
return;
$meta_sections = array();
$meta_sections[] = array(
'title' => 'Standard Fields', // section title
'taxonomies' => array( 'user_category' ), // list of taxonomies. Default is array('category', 'post_tag'). Optional
'id' => 'user_category_options', // ID of each section, will be the option name
'fields' => array(
array(
'name' => 'Color Picker',
'id' => 'color',
'type' => 'color'
)
)
);
foreach ( $meta_sections as $meta_section )
{
new RW_Taxonomy_Meta( $meta_section );
}
}
/**
* Temporary function to migrate users from old data structure
*/
function create_migrate_users() {
$users = get_users( array( 'fields' => 'ID' ) );
foreach ( $users as $user ) {
// Get the user category
$user_categories = wp_get_object_terms( $user, 'user_category' );
if ( $user_categories && ! is_wp_error( $user_categories ) ) :
$user_category_slugs = array();
$user_category_names = array();
foreach ( $user_categories as $user_category ) {
$user_category_slugs[] = $user_category->slug;
$user_category_names[] = $user_category->name;
}
$types = join( ' ', $user_category_slugs );
$primary_jobs = join( ' ', $user_category_names );
endif;
$user_meta['types'] = $types;
$user_meta['primary_jobs'] = $primary_jobs;
$user_meta['website'] = get_user_meta( $user, 'user_website', true );
$user_meta['bio'] = get_the_author_meta( 'description', $user );
$user_meta['phone'] = get_user_meta( $user, 'user_phone', true );
$user_meta['zip_code'] = get_user_meta( $user, 'user_zip', true );
$user_meta['twitter'] = get_user_meta( $user, 'user_twitter', true );
$user_meta['linkedin_url'] = get_user_meta( $user, 'user_linkedin', true );
$user_meta['skills'] = unserialize( get_user_meta( $user, 'user_skills', true ) );
// Get the site-specific user meta
$blog_id = get_current_blog_id();
$blog_details = get_blog_details( $blog_id );
update_user_meta( $user, 'user_meta_'. str_replace( '/', '', $blog_details->path ), $user_meta );
}
}
//add_action( 'init', 'create_migrate_users' );
function create_assign_avatars() {
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
$users = get_users();
foreach( $users as $user ) {
$avatar = create_get_avatar( $user->ID );
if( empty( $avatar ) ) {
continue;
}
$headers = get_headers( $avatar, 1 );
if ( $headers[0] != 'HTTP/1.1 200 OK' ) {
continue;
}
$filename = $wp_upload_dir['path'] .'/'. basename( $avatar );
copy( $avatar, $filename );
// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'post_author' => $user->ID
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_user_meta( $user->ID, 'wp_2_user_avatar', $attach_id );
}
}
//add_action( 'init', 'create_assign_avatars' );