Skip to content

Commit

Permalink
Merge Dev
Browse files Browse the repository at this point in the history
  • Loading branch information
UmeshSingla committed Jun 27, 2015
2 parents 8e16a79 + 21cd41f commit efe788f
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 257 deletions.
130 changes: 118 additions & 12 deletions UserTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function __construct() {
add_action( 'edit_user_profile_update', array( $this, 'ut_save_profile' ) );
add_filter( 'sanitize_user', array( $this, 'restrict_username' ) );
add_action( 'wp_head', array( $this, 'admin_ajax' ) );

// User Query Filter
add_filter( 'pre_user_query', array( $this, 'ut_users_filter_query' ) );
add_action( 'restrict_manage_users', array( $this, 'ut_users_filter' ) );
}

function ut_enqueue_scripts() {
Expand Down Expand Up @@ -136,6 +140,7 @@ public function ut_user_taxonomies() {
} ?>
<div class="wrap nosubsub user-taxonomies-page">
<h2><?php _e( 'User Taxonomies', WP_UT_TRANSLATION_DOMAIN ); ?></h2>
<p><?php _e('This screen allows to create new Taxonomies without registering it in code, do not confuse it with category or tags screen.', 'user_taxonomy'); ?></p>

<div id="col-container">
<div id="col-right"><?php
Expand All @@ -158,17 +163,31 @@ public function ut_user_taxonomies() {
<label for="taxonomy_name"><?php _ex( 'Name', 'Taxonomy Name' ); ?></label>
</th>
<td>
<input name="taxonomy_name" id="taxonomy_name" type="text" value="<?php echo $taxonomy_name; ?>" size="40" data-required="true" maxlength="24"/>
<input name="taxonomy_name" id="taxonomy_name" type="text" value="<?php echo $taxonomy_name; ?>" size="40" data-required="true" maxlength="32"/>

<p class="description"><?php _e( 'The name is how it appears on your site.' ); ?></p>
</td>
</tr>
<?php if ( ! global_terms_enabled() ) : ?>
<tr>
<th><label for="taxonomy-slug"><?php _e( 'Taxonomy Slug' ); ?></label></th>
<td>
<input name="taxonomy_slug" id="taxonomy-slug" type="text" value="" size="40"/>

<p><?php _e( 'The &#8220;slug&#8221; is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.' ); ?></p>
</td>
</tr>
<?php endif; // global_terms_enabled() ?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="description"><?php _ex( 'Description', 'Taxonomy Description' ); ?></label>
<label for="description"><?php _e( 'Description', 'Taxonomy Description' ); ?></label>
</th>
<td>
<textarea name="description" id="description" rows="5" cols="50" class="large-text"><?php echo $taxonomy_description; ?></textarea>

<p><?php _e( 'The description is not prominent by default; however, some themes may show it.' ); ?></p>
</td>
</tr> <?php
</tr><?php
wp_nonce_field( 'ut_register_taxonomy', 'ut_register_taxonomy' );
echo ! empty( $slug ) ? '<input type="hidden" name="taxonomy_slug" value="' . $slug . '"/>' : ''; ?>
</table>
Expand All @@ -193,18 +212,23 @@ function ut_update_taxonomy_list() {
if ( empty( $_POST ) ) {
return;
}
$taxonomy_description = $taxonomy_key = '';
$taxonomy_description = $taxonomy_key = $taxonomy_slug = '';
extract( $_POST );

$nonce_verified = ! empty( $ut_register_taxonomy ) ? wp_verify_nonce( $ut_register_taxonomy, 'ut_register_taxonomy' ) : false;
if ( ! $nonce_verified ) {
wp_die( 'Invalid request' );
}

//Get all the existing taxonomies
$ut_taxonomies = get_site_option( 'ut_taxonomies' );
if ( ! is_array( $ut_taxonomies ) && empty( $ut_taxonomies ) ) {
$ut_taxonomies = array();
} elseif ( ! is_array( $ut_taxonomies ) ) {
$ut_taxonomies = array( $ut_taxonomies );
}

//Check if taxonomy already created by user
$taxonomy_exists = false;
foreach ( $ut_taxonomies as $ut_taxonomy_key => $ut_taxonomy ) {
if ( empty( $taxonomy_slug ) && ( $ut_taxonomy['name'] == $taxonomy_name || $ut_taxonomy['slug'] == ut_taxonomy_name( $taxonomy_name ) ) ) {
Expand All @@ -219,7 +243,7 @@ function ut_update_taxonomy_list() {
if ( ! $taxonomy_exists ) {
$ut_taxonomies[] = array(
'name' => $taxonomy_name,
'slug' => ut_taxonomy_name( $taxonomy_name ),
'slug' => !empty( $taxonomy_slug ) ? ut_taxonomy_name( $taxonomy_slug ) : ut_taxonomy_name( $taxonomy_name ),
'description' => $taxonomy_description
);
update_site_option( 'ut_taxonomies', $ut_taxonomies );
Expand Down Expand Up @@ -288,7 +312,7 @@ function ut_register_taxonomies() {
'user',
array(
'public' => true,
'hierarchical' => true,
'hierarchical' => false,
'labels' => array(
'name' => __( $name ),
'singular_name' => __( $name ),
Expand Down Expand Up @@ -382,7 +406,15 @@ public function set_user_column_values( $display, $column, $term_id ) {
}
$count = number_format_i18n( $count );

return $count;
$tax = get_taxonomy( $_GET['taxonomy'] );

if ( $tax->query_var ) {
$args = array( $tax->query_var => $term->slug );
} else {
$args = array( 'taxonomy' => $tax->name, 'term' => $term->slug );
}

return "<a href='" . esc_url( add_query_arg( $args, 'users.php' ) ) . "'>".$count."</a>";
}

/**
Expand Down Expand Up @@ -497,23 +529,23 @@ function ut_delete_taxonomy_callback() {
if ( empty( $_POST ) || empty( $_POST['nonce'] ) || empty( $_POST['delete_taxonomy'] ) ) {
return false;
}
$delete_taxonomy = '';
extract( $_POST );
$taxonomy_slug = ut_taxonomy_name( $delete_taxonomy );
if ( ! wp_verify_nonce( $nonce, 'delete-taxonomy-' . $taxonomy_slug ) ) {
if ( ! wp_verify_nonce( $nonce, 'delete-taxonomy-' . $delete_taxonomy ) ) {
return false;
}
$ut_taxonomies = get_site_option( 'ut_taxonomies' );
foreach ( $ut_taxonomies as $ut_taxonomy_key => $ut_taxonomy_array ) {
if ( ut_stripallslashes( $ut_taxonomy_array['name'] ) == ut_stripallslashes( $delete_taxonomy ) ) {
if ( ut_stripallslashes( $ut_taxonomy_array['slug'] ) == ut_stripallslashes( $delete_taxonomy ) ) {
unset( $ut_taxonomies[ $ut_taxonomy_key ] );
}
}
$updated = update_site_option( 'ut_taxonomies', $ut_taxonomies );

if ( $updated ) {
echo "deleted";
wp_send_json_success('updated');
} else {
echo "failed";
wp_send_json_error('failed');
}
die( 1 );
}
Expand Down Expand Up @@ -569,6 +601,80 @@ function admin_ajax() {
</script><?php
}

/**
* User Query Filter
*/
function ut_users_filter_query( $query ) {
global $wpdb, $wp_query, $pagenow;

if ( ! is_admin() || $pagenow != 'users.php' )
return $query;

if ( isset( $_GET['taxonomy'] ) && ! empty( $_GET['taxonomy'] ) && isset( $_GET['term'] ) && ! empty( $_GET['term'] ) ) {
$term_slug = $_GET['term'];
} else {
$ut_taxonomies = get_site_option( 'ut_taxonomies' );
if ( !empty( $ut_taxonomies ) && is_array( $ut_taxonomies ) ) {
foreach ( $ut_taxonomies as $ut_taxonomy ) {
extract( $ut_taxonomy );
$taxonomy_slug = ! empty( $slug ) ? $slug : ut_taxonomy_name( $name );
$taxonomy_slug = strlen( $taxonomy_slug ) > 32 ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug;
$taxonomy = get_taxonomy($taxonomy_slug);
if ( $taxonomy && isset( $_GET[$taxonomy_slug] ) && ! empty( $_GET[$taxonomy_slug] ) ) {
$term_slug = $_GET[$taxonomy_slug];
continue;
}
}
}
}

if ( ! empty( $term_slug ) ) {
$query->query_from .= " INNER JOIN {$wpdb->term_relationships} ON {$wpdb->users}.`ID` = {$wpdb->term_relationships}.`object_id` INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_relationships}.`term_taxonomy_id` = {$wpdb->term_taxonomy}.`term_taxonomy_id` INNER JOIN {$wpdb->terms} ON {$wpdb->terms}.`term_id` = {$wpdb->term_taxonomy}.`term_id`";
$query->query_where .= " AND {$wpdb->terms}.`slug` = '{$term_slug}'";
}

return $query;

}

/**
* User Filter
*/
function ut_users_filter() {
$ut_taxonomies = get_site_option( 'ut_taxonomies' );
if ( empty( $ut_taxonomies ) || ! is_array( $ut_taxonomies ) ) {
return;
}

foreach ( $ut_taxonomies as $ut_taxonomy ) {
extract( $ut_taxonomy );
$taxonomy_slug = ! empty( $slug ) ? $slug : ut_taxonomy_name( $name );
$taxonomy_slug = strlen( $taxonomy_slug ) > 32 ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug;
$taxonomy = get_taxonomy($taxonomy_slug);
if ( $taxonomy ) { ?>
<label class="screen-reader-text" for="<?php echo $taxonomy_slug; ?>"><?php esc_html_e( 'Filter by '.$name, WP_UT_TRANSLATION_DOMAIN ); ?></label>
<select name="<?php echo $taxonomy_slug; ?>" id="<?php echo $taxonomy_slug; ?>" class="ut-taxonomy-filter">
<option value=''><?php esc_html_e( 'Filter by '.$name, WP_UT_TRANSLATION_DOMAIN ); ?></option>
<?php
$taxonomy_terms = get_terms( $taxonomy_slug );
foreach ( $taxonomy_terms as $taxonomy_term ) : ?>
<option value="<?php echo esc_attr( $taxonomy_term->slug ); ?>"<?php if ( isset( $_GET[$taxonomy_slug] ) && ! empty( $_GET[$taxonomy_slug] ) && $_GET[$taxonomy_slug] == $taxonomy_term->slug) { echo ' selected="selected"'; } ?>><?php echo $taxonomy_term->name; ?></option>
<?php endforeach; ?>
</select>
<?php
}
}
submit_button( __( 'Filter', WP_UT_TRANSLATION_DOMAIN ), 'secondary', 'ut-filter-users', false );

wp_nonce_field( 'ut-filter-users', 'ut-filter-users-nonce' );

?>
<a class="ut-reset-filters button-primary" href="users.php" title="Reset User Filters">Reset Filters</a>
<?php


}

}

/**
Expand Down
Loading

0 comments on commit efe788f

Please sign in to comment.