Skip to content

Commit

Permalink
code cleanup and filter url option
Browse files Browse the repository at this point in the history
  • Loading branch information
UmeshSingla committed Feb 1, 2015
1 parent a87de1f commit a61d8a6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 31 deletions.
54 changes: 32 additions & 22 deletions UserTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/**
* Plugin Name: User Tags for Wordpress
* Author: Umesh Kumar<umeshsingla05@gmail.com>
* Author URI: http://codechutney.com
* Description: Adds User Taxonomy functionality
* Version: 1.2.6
* Author URI:http://codechutney.com
* Description: Adds User Taxonomy functionality, that allows you to categorize users on tags and taxonomy basis
* Version: 1.2.7
* Reference : http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress
* Text Domain : user_taxonomy
*/
Expand Down Expand Up @@ -66,7 +66,7 @@ public function __construct() {
add_action( 'wp_head', array( $this, 'admin_ajax' ) );
}

function ut_enqueue_scripts( $hook ) {
function ut_enqueue_scripts() {
wp_enqueue_style( 'ut-style', WP_UT_CSS . 'style.css' );
wp_register_script( 'user_taxonomy_js', WP_UT_JS . 'user_taxonomy.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'user_taxonomy_js' );
Expand Down Expand Up @@ -217,12 +217,12 @@ function ut_update_taxonomy_list() {
}
}
if ( ! $taxonomy_exists ) {
$ut_taxonomies[] = array(
$ut_taxonomies[] = array(
'name' => $taxonomy_name,
'slug' => ut_taxonomy_name( $taxonomy_name ),
'description' => $taxonomy_description
);
$taxonomy_site_option = update_site_option( 'ut_taxonomies', $ut_taxonomies );
update_site_option( 'ut_taxonomies', $ut_taxonomies );
//a new taxonomy added, so flush rules required
update_site_option( 'ut_new_taxonomy', true );

Expand Down Expand Up @@ -277,17 +277,18 @@ function ut_register_taxonomies() {
return;
}
foreach ( $ut_taxonomies as $ut_taxonomy ) {
global $user_tags;
extract( $ut_taxonomy );
$taxonomy_slug = ! empty( $slug ) ? $slug : ut_taxonomy_name( $name );
//make sure taxonomy name is less than 32
$taxonomy_slug = strlen( $taxonomy_slug ) > 32 ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug;
$url_prefix = apply_filters( 'ut_tag_url_prefix', 'tag' );
$url_prefix = ! empty( $url_prefix ) ? trailingslashit( $url_prefix ) : '';
$registered = register_taxonomy(
$taxonomy_slug,
'user',
array(
'public' => true,
'hierarchical' => false,
'hierarchical' => true,
'labels' => array(
'name' => __( $name ),
'singular_name' => __( $name ),
Expand All @@ -306,7 +307,7 @@ function ut_register_taxonomies() {
),
'rewrite' => array(
'with_front' => true,
'slug' => 'tag/' . $taxonomy_slug // Use 'author' (default WP user slug).
'slug' => $url_prefix . $taxonomy_slug // Use 'author' (default WP user slug).
),
'capabilities' => array(
'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple.
Expand All @@ -326,6 +327,10 @@ function ut_register_taxonomies() {

/**
* Highlight User Menu item
*
* @param string $parent
*
* @return string
*/
function parent_menu( $parent = '' ) {
global $pagenow;
Expand All @@ -342,6 +347,8 @@ function parent_menu( $parent = '' ) {
/**
* Correct the column names for user taxonomies
* Need to replace "Posts" with "Users"
*
* @param $columns
*/
public function set_user_column( $columns ) {
if ( empty( $columns ) ) {
Expand All @@ -355,6 +362,12 @@ public function set_user_column( $columns ) {

/**
* Set values for custom columns in user taxonomies
*
* @param $display
* @param $column
* @param $term_id
*
* @return mixed|string|void
*/
public function set_user_column_values( $display, $column, $term_id ) {
if ( empty( $column ) ) {
Expand All @@ -369,16 +382,7 @@ public function set_user_column_values( $display, $column, $term_id ) {
}
$count = number_format_i18n( $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 $count;
// return "<a href='" . esc_url( add_query_arg( $args, 'users.php' ) ) . "'>$count</a>";
}

/**
Expand Down Expand Up @@ -443,6 +447,8 @@ public function user_profile( $user ) {
* Save the custom user taxonomies when saving a users profile
*
* @param Integer $user_id - The ID of the user to update
*
* @return bool|void
*/
public function ut_save_profile( $user_id ) {
if ( empty( $_POST['user-tags'] ) ) {
Expand All @@ -457,10 +463,10 @@ public function ut_save_profile( $user_id ) {
// Save the data
if ( ! empty( $taxonomy_terms ) ) {
$taxonomy_terms = array_map( 'trim', explode( ',', $taxonomy_terms ) );
$updated = wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false );
wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false );
} else {
//No terms left, delete all terms
$updated = wp_set_object_terms( $user_id, array(), $taxonomy, false );
wp_set_object_terms( $user_id, array(), $taxonomy, false );
}
}
}
Expand All @@ -469,6 +475,10 @@ public function ut_save_profile( $user_id ) {
* Usernames can't match any of our user taxonomies
* As otherwise it will cause a URL conflict
* This method prevents that happening
*
* @param $username
*
* @return string
*/
public function restrict_username( $username ) {
if ( isset( self::$taxonomies[ $username ] ) ) {
Expand Down Expand Up @@ -526,7 +536,6 @@ function ut_load_tag_suggestions_callback() {
) );
if ( empty( $tags ) || ! is_array( $tags ) ) {
return false;
die;
}
$tag_list = array();
foreach ( $tags as $tag ) {
Expand Down Expand Up @@ -574,7 +583,7 @@ function wp_ut_flush_rules() {
if ( $ut_new_taxonomy !== 'FALSE' ) {
global $wp_rewrite;
$wp_rewrite->flush_rules( false );
$updated = update_site_option( 'ut_new_taxonomy', 'FALSE' );
update_site_option( 'ut_new_taxonomy', 'FALSE' );
}
}

Expand All @@ -596,6 +605,7 @@ function ut_taxonomy_updated() {
* Class object
*/
function ut_user_tags() {
global $user_tags;
$user_tags = new UserTags();
}

Expand Down
2 changes: 1 addition & 1 deletion assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ a.ntdelbutton {
width: auto;
}
ul.user-taxonomy-wrapper .tagchecklist span a {
margin: -1px 0 0 0px;
margin: -1px 0 0 0;
}
.profile-php .user-taxonomy-wrapper .tagchecklist span a {
margin: -1px 0 0 5px;
Expand Down
5 changes: 2 additions & 3 deletions assets/js/user_taxonomy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Check for empty fields
*/
function validate_form(parameters) {
$empty_fields = new Array();
$empty_fields = [];
$i = 0;
jQuery('#editusertaxonomy input, #editusertaxonomy textarea').each(function () {
if (!jQuery(this).is('textarea')) {
Expand Down Expand Up @@ -51,7 +51,7 @@ function insert_tags($tag_input, $taxonomy_name, $term, $tag_html) {
jQuery(document).ready(function ($) {
jQuery('body').on('submit', '#editusertaxonomy', function (e) {
$empty_fields = validate_form();
if ($empty_fields.length == 0) {
if (!$empty_fields.length) {
return true;
} else {
return false;
Expand Down Expand Up @@ -155,7 +155,6 @@ jQuery(document).ready(function ($) {
}
else {
jQuery('.tag-suggestion').remove();
return;
}
});
//Tags UI
Expand Down
2 changes: 1 addition & 1 deletion lib/class-ut-user-tags-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function process_bulk_action() {
}
}
}
$updated = update_site_option( 'ut_taxonomies', $ut_taxonomies );
update_site_option( 'ut_taxonomies', $ut_taxonomies );
}

}
5 changes: 2 additions & 3 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ function wp_ut_tag_box() {
function wp_ut_ajax_url() {
?>
<script type="text/javascript">
$wp_ut_ajax_url =
<?php echo json_encode(admin_url('admin-ajax.php')); ?>
$wp_ut_ajax_url = '<?php echo json_encode(admin_url('admin-ajax.php')); ?>';
</script><?php
}

Expand Down Expand Up @@ -150,7 +149,7 @@ function rce_ut_process_form() {
if ( ! empty( $taxonomy_terms ) ) {
$taxonomy_terms = array_map( 'trim', explode( ',', $taxonomy_terms ) );
}
$terms_updated = wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false );
wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false );
}
}
}
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: Tags, taxonomies, user taxonomy, user tags
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Requires at least: 3.0
Tested up to: 4.0
Tested up to: 4.1
Stable tag: trunk

Adds an admin option to allow creating User Taxonomies and create tags for different taxonomies.
Expand All @@ -29,6 +29,9 @@ Users can add new tags.

== Changelog ==

= 1.2.7 =
* New - Filter: `ut_tag_url_prefix` to filter the way, tags URLs are formed

= 1.2.6 =
* New - Filter: `ut_template_users` in Taxonomy template to filter the list of users before displaying
* Fixed - handle count callback for register taxonomy (Fixes tag cloud size issue )
Expand Down

0 comments on commit a61d8a6

Please sign in to comment.