forked from nopio/wordpress_user_taxonomy_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-category-taxonomy.php
211 lines (176 loc) · 5.8 KB
/
user-category-taxonomy.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
<?php
define( 'USER_CATEGORY_NAME', 'user_category' );
define( 'USER_CATEGORY_META_KEY', '_user_category' );
add_action( 'init', 'nopio_register_user_category_taxonomy' );
function nopio_register_user_category_taxonomy() {
register_taxonomy(
USER_CATEGORY_NAME, //taxonomy name
'user', //object for which the taxonomy is created
array( //taxonomy details
'public' => true,
'labels' => array(
'name' => 'User Categories',
'singular_name' => 'User Category',
'menu_name' => 'User Categories',
'search_items' => 'Search User Category',
'popular_items' => 'Popular User Categories',
'all_items' => 'All User Categories',
'edit_item' => 'Edit User Category',
'update_item' => 'Update User Category',
'add_new_item' => 'Add New User Category',
'new_item_name' => 'New User Category Name',
),
'update_count_callback' => function() {
return; //important
}
)
);
}
add_action( 'admin_menu', 'nopio_add_user_categories_admin_page' );
function nopio_add_user_categories_admin_page() {
$taxonomy = get_taxonomy( USER_CATEGORY_NAME );
add_users_page(
esc_attr( $taxonomy->labels->menu_name ),//page title
esc_attr( $taxonomy->labels->menu_name ),//menu title
$taxonomy->cap->manage_terms,//capability
'edit-tags.php?taxonomy=' . $taxonomy->name//menu slug
);
}
add_filter( 'submenu_file', 'nopio_set_user_category_submenu_active' );
function nopio_set_user_category_submenu_active( $submenu_file ) {
global $parent_file;
if( 'edit-tags.php?taxonomy=' . USER_CATEGORY_NAME == $submenu_file ) {
$parent_file = 'users.php';
}
return $submenu_file;
}
add_action( 'show_user_profile', 'nopio_admin_user_profile_category_select' );
add_action( 'edit_user_profile', 'nopio_admin_user_profile_category_select' );
function nopio_admin_user_profile_category_select( $user ) {
$taxonomy = get_taxonomy( USER_CATEGORY_NAME );
if ( !user_can( $user, 'author' ) ) {
return;
}
?>
<table class="form-table">
<tr>
<th>
<label for="<?php echo USER_CATEGORY_META_KEY ?>">User Category</label>
</th>
<td>
<?php
$user_category_terms = get_terms( array(
'taxonomy' => USER_CATEGORY_NAME,
'hide_empty' => 0
) );
$select_options = array();
foreach ( $user_category_terms as $term ) {
$select_options[$term->term_id] = $term->name;
}
$meta_values = get_user_meta( $user->ID, USER_CATEGORY_META_KEY, true );
echo nopio_custom_form_select(
USER_CATEGORY_META_KEY,
$meta_values,
$select_options,
'',
array( 'multiple' =>'multiple' )
);
?>
</td>
</tr>
</table>
<?php
}
function nopio_custom_form_select( $name, $value, $options, $default_var ='', $html_params = array() ) {
if( empty( $options ) ) {
$options = array( '' => '---choose---');
}
$html_params_string = '';
if( !empty( $html_params ) ) {
if ( array_key_exists( 'multiple', $html_params ) ) {
$name .= '[]';
}
foreach( $html_params as $html_params_key => $html_params_value ) {
$html_params_string .= " {$html_params_key}='{$html_params_value}'";
}
}
echo "<select name='{$name}'{$html_params_string}>";
foreach( $options as $options_value => $options_label ) {
if( ( is_array( $value ) && in_array( $options_value, $value ) )
|| $options_value == $value ) {
$selected = " selected='selected'";
} else {
$selected = '';
}
if( empty( $value ) && !empty( $default_var ) && $options_value == $default_var ) {
$selected = " selected='selected'";
}
echo "<option value='{$options_value}'{$selected}>{$options_label}</option>";
}
echo "</select>";
}
add_action( 'personal_options_update', 'nopio_admin_save_user_categories' );
add_action( 'edit_user_profile_update', 'nopio_admin_save_user_categories' );
function nopio_admin_save_user_categories( $user_id ) {
$tax = get_taxonomy( USER_CATEGORY_NAME );
$user = get_userdata( $user_id );
if ( !user_can( $user, 'author' ) ) {
return false;
}
$new_categories_ids = $_POST[USER_CATEGORY_META_KEY];
$user_meta = get_user_meta( $user_id, USER_CATEGORY_META_KEY, true );
$previous_categories_ids = array();
if( !empty( $user_meta ) ) {
$previous_categories_ids = (array)$user_meta;
}
if( ( current_user_can( 'administrator' ) && $_POST['role'] != 'author' ) ) {
delete_user_meta( $user_id, USER_CATEGORY_META_KEY );
nopio_update_users_categories_count( $previous_categories_ids, array() );
} else {
update_user_meta( $user_id, USER_CATEGORY_META_KEY, $new_categories_ids );
nopio_update_users_categories_count( $previous_categories_ids, $new_categories_ids );
}
}
function nopio_update_users_categories_count( $previous_terms_ids, $new_terms_ids ) {
global $wpdb;
$terms_ids = array_unique( array_merge( (array)$previous_terms_ids, (array)$new_terms_ids ) );
if( count( $terms_ids ) < 1 ) { return; }
foreach ( $terms_ids as $term_id ) {
$count = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value LIKE %s",
USER_CATEGORY_META_KEY,
'%"' . $term_id . '"%'
)
);
$wpdb->update( $wpdb->term_taxonomy, array( 'count' => $count ), array( 'term_taxonomy_id' => $term_id ) );
}
}
function nopio_show_authors_in_categories() {
$categories = get_terms(array(
'taxonomy' => USER_CATEGORY_NAME,
'hide_empty' => true
));
echo '<ul>';
foreach( $categories as $category ) {
echo '<li>';
echo $category->name;
echo " (#{$category->count})";
$args = array(
'role' => 'Author',
'meta_key' => USER_CATEGORY_META_KEY,
'meta_value' => '"' . $category->term_id . '"',
'meta_compare' => 'LIKE'
);
$authors = new WP_User_Query( $args );
echo '<ul>';
foreach( $authors->results as $author ) {
echo '<li>';
echo $author->display_name;
echo '</li>';
}
echo '</ul>';
echo '</li>';
}
echo '</ul>';
}