From b35cd9a10e7a74ef0bb1fd605781c0bb0ff4099a Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Sat, 27 Jun 2015 10:51:32 +0530 Subject: [PATCH 01/23] Code Formatiing --- UserTags.php | 34 +++++++++++--------- assets/js/user_taxonomy.js | 48 ++++++++++++++-------------- templates/user-taxonomy-template.php | 6 ++-- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/UserTags.php b/UserTags.php index f84981c..2d540c8 100755 --- a/UserTags.php +++ b/UserTags.php @@ -140,7 +140,8 @@ public function ut_user_taxonomies() { } ?>

-

+ +

$taxonomy_name, - 'slug' => !empty( $taxonomy_slug ) ? ut_taxonomy_name( $taxonomy_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 ); @@ -414,7 +415,7 @@ public function set_user_column_values( $display, $column, $term_id ) { $args = array( 'taxonomy' => $tax->name, 'term' => $term->slug ); } - return "".$count.""; + return "" . $count . ""; } /** @@ -543,9 +544,9 @@ function ut_delete_taxonomy_callback() { $updated = update_site_option( 'ut_taxonomies', $ut_taxonomies ); if ( $updated ) { - wp_send_json_success('updated'); + wp_send_json_success( 'updated' ); } else { - wp_send_json_error('failed'); + wp_send_json_error( 'failed' ); } die( 1 ); } @@ -607,21 +608,22 @@ function admin_ajax() { function ut_users_filter_query( $query ) { global $wpdb, $wp_query, $pagenow; - if ( ! is_admin() || $pagenow != 'users.php' ) + 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 ) ) { + 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]; + $taxonomy = get_taxonomy( $taxonomy_slug ); + if ( $taxonomy && isset( $_GET[ $taxonomy_slug ] ) && ! empty( $_GET[ $taxonomy_slug ] ) ) { + $term_slug = $_GET[ $taxonomy_slug ]; continue; } } @@ -650,15 +652,17 @@ function ut_users_filter() { 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); + $taxonomy = get_taxonomy( $taxonomy_slug ); if ( $taxonomy ) { ?> - + Reset Filters -

Taxonomy deleted.

'; jQuery('.user-taxonomies-page h2:first').after($message); $this.parents().eq(3).remove(); @@ -269,27 +269,27 @@ jQuery(document).ready(function ($) { return false; }); - jQuery('#' + id).after(r); - }); - }); - //Remove notices - setInterval(function () { - jQuery('#message.below-h2').hide('slow', function () { - jQuery('.user-taxonomies-page #message.below-h2').remove(); - }); - }, 3000); - // User Taxonomy Filters - jQuery('.users-php select.ut-taxonomy-filter').each( function() { - if ($(this).val() != '') { - $('select.ut-taxonomy-filter').not(this).prop('disabled', true); - } - }); + jQuery('#' + id).after(r); + }); + }); + //Remove notices + setInterval(function () { + jQuery('#message.below-h2').hide('slow', function () { + jQuery('.user-taxonomies-page #message.below-h2').remove(); + }); + }, 3000); + // User Taxonomy Filters + jQuery('.users-php select.ut-taxonomy-filter').each(function () { + if ($(this).val() != '') { + $('select.ut-taxonomy-filter').not(this).prop('disabled', true); + } + }); - jQuery('.users-php').on('change', 'select.ut-taxonomy-filter', function() { - if ($(this).val() == '') { - $('select.ut-taxonomy-filter').prop('disabled', false); - } else { - $('select.ut-taxonomy-filter').not(this).prop('disabled', true); - } - }); + jQuery('.users-php').on('change', 'select.ut-taxonomy-filter', function () { + if ($(this).val() == '') { + $('select.ut-taxonomy-filter').prop('disabled', false); + } else { + $('select.ut-taxonomy-filter').not(this).prop('disabled', true); + } + }); }); diff --git a/templates/user-taxonomy-template.php b/templates/user-taxonomy-template.php index dc1eb41..8aec0f0 100755 --- a/templates/user-taxonomy-template.php +++ b/templates/user-taxonomy-template.php @@ -10,7 +10,7 @@ No Users found.

"; - echo apply_filters( 'ut_template_content_empty', __( $content ) ); + echo apply_filters( 'ut_template_content_empty', esc_html__( $content ) ); } ?>
From d8522443697cba3e80b3b3ef71a0a5a02fd3dcb8 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Sun, 23 Aug 2020 12:02:29 +0530 Subject: [PATCH 11/23] Move UserTags class to a separate file; Follow WordPress codinng standards --- .../class-shortcode.php | 0 .../class-tags-list.php | 14 +- UserTags.php => inc/class-usertags.php | 406 ++++++++---------- inc/functions.php | 176 ++++++++ lib/functions.php | 163 ------- templates/user-taxonomy-template.php | 2 +- user-tags.php | 68 +++ 7 files changed, 437 insertions(+), 392 deletions(-) rename lib/class-ut-user-tags-shortcode.php => inc/class-shortcode.php (100%) rename lib/class-ut-user-tags-list.php => inc/class-tags-list.php (81%) rename UserTags.php => inc/class-usertags.php (64%) mode change 100755 => 100644 create mode 100755 inc/functions.php delete mode 100755 lib/functions.php create mode 100755 user-tags.php diff --git a/lib/class-ut-user-tags-shortcode.php b/inc/class-shortcode.php similarity index 100% rename from lib/class-ut-user-tags-shortcode.php rename to inc/class-shortcode.php diff --git a/lib/class-ut-user-tags-list.php b/inc/class-tags-list.php similarity index 81% rename from lib/class-ut-user-tags-list.php rename to inc/class-tags-list.php index 7b657a9..156dde8 100755 --- a/lib/class-ut-user-tags-list.php +++ b/inc/class-tags-list.php @@ -64,12 +64,12 @@ function ut_list_taxonomies() { } function no_items() { - esc_html_e( 'No Taxonomies found.', WP_UT_TRANSLATION_DOMAIN ); + esc_html_e( 'No Taxonomies found.', 'user_taxonomy' ); } function get_bulk_actions() { $actions = array(); - $actions['delete'] = esc_html__( 'Delete', WP_UT_TRANSLATION_DOMAIN ) . ''; + $actions['delete'] = esc_html__( 'Delete', 'user_taxonomy' ) . ''; return $actions; } @@ -77,13 +77,13 @@ function get_bulk_actions() { function get_columns() { return array( 'cb' => '', - 'name' => esc_html__( 'Display Name', WP_UT_TRANSLATION_DOMAIN ), - 'taxonomy' => esc_html__( 'Taxonomy', WP_UT_TRANSLATION_DOMAIN ), + 'name' => esc_html__( 'Display Name', 'user_taxonomy' ), + 'taxonomy' => esc_html__( 'Taxonomy', 'user_taxonomy' ), ); } function column_cb( $item ) { - printf( '', $this->_args['plural'], $item['name'] ); + printf( '', $this->_args['plural'], $item['name'] ); } function column_taxonomy( $item ) { @@ -94,9 +94,9 @@ function column_taxonomy( $item ) { function column_name( $item ) { $taxonomy_slug = ! empty( $item['slug'] ) ? $item['slug'] : ut_taxonomy_name( $item['name'] ); - echo ' ' . $item['name'] . ' '; } function process_bulk_action() { diff --git a/UserTags.php b/inc/class-usertags.php old mode 100755 new mode 100644 similarity index 64% rename from UserTags.php rename to inc/class-usertags.php index f8c6eb3..11eb46f --- a/UserTags.php +++ b/inc/class-usertags.php @@ -1,32 +1,8 @@ - * Author URI:http://codechutney.com - * Description: Adds User Taxonomy functionality, It allows you to categorize users on tags and taxonomy basis. - * Version: 1.2.8 - * Reference : http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress - * Text Domain : user_taxonomy - */ -define( 'WP_UT_TRANSLATION_DOMAIN', 'user_taxonomy' ); -define( 'WP_UT_URL', plugins_url( '', __FILE__ ) ); -define( 'WP_UT_PLUGIN_FOLDER', dirname( __FILE__ ) ); -define( 'WP_UT_TEMPLATES', trailingslashit( WP_UT_PLUGIN_FOLDER ) . trailingslashit( 'templates' ) ); - -/* Define all necessary variables first */ -define( 'WP_UT_CSS', WP_UT_URL . "/assets/css/" ); -define( 'WP_UT_JS', WP_UT_URL . "/assets/js/" ); -if ( ! class_exists( 'WP_List_Table' ) ) { - require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); -} -// Includes PHP files located in 'lib' folder -foreach ( glob( dirname( __FILE__ ) . "/lib/*.php" ) as $lib_filename ) { - require_once( $lib_filename ); -} class UserTags { private static $taxonomies = array(); - private $version = "1.2.7"; + private $version = '1.2.7'; /** * Register all the hooks and filters we can in advance @@ -44,9 +20,7 @@ public function __construct() { // Taxonomies add_action( 'admin_enqueue_scripts', array( $this, 'ut_enqueue_scripts' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'ut_enqueue_scripts' ) ); - if ( ! empty( $_POST['taxonomy_name'] ) ) { - $this->ut_update_taxonomy_list(); - } + add_action( 'admin_init', array( $this, 'ut_update_taxonomy_list' ) ); add_action( 'registered_taxonomy', array( $this, 'ut_registered_taxonomy' ), 10, 3 ); /** @@ -92,7 +66,7 @@ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { global $wp_taxonomies; // Only modify user taxonomies, everything else can stay as is - if ( $object != 'user' ) { + if ( 'user' !== $object ) { return; } @@ -114,9 +88,9 @@ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { public function admin_menu() { global $users_taxonomy; if ( is_super_admin() ) { - $users_taxonomy = add_users_page( esc_html__( 'User Taxonomies', WP_UT_TRANSLATION_DOMAIN ), esc_html__( 'Taxonomies', WP_UT_TRANSLATION_DOMAIN ), 'read', 'user-taxonomies', array( + $users_taxonomy = add_users_page( esc_html__( 'User Taxonomies', 'user_taxonomy' ), esc_html__( 'Taxonomies', 'user_taxonomy' ), 'read', 'user-taxonomies', array( $this, - "ut_user_taxonomies" + 'ut_user_taxonomies', ) ); } } @@ -126,10 +100,11 @@ public function admin_menu() { * the name and other values for taxonomy */ public function ut_user_taxonomies() { - $page_title = 'Add New Taxonomy'; - $taxonomy_name = $taxonomy_description = ''; + $page_title = esc_html__( 'Add New Taxonomy', 'user_taxonomy' ); + $taxonomy_name = ''; + $taxonomy_description = ''; if ( ! empty( $_GET['taxonomy'] ) ) { - $slug = $_GET['taxonomy']; + $slug = sanitize_text_field( $_GET['taxonomy'] ); $page_title = 'Edit Taxonomy: ' . $slug; $taxonomy = get_taxonomy( $slug ); $taxonomy_name = ! empty( $taxonomy ) ? $taxonomy->labels->name : ''; @@ -143,7 +118,7 @@ public function ut_user_taxonomies() { } } ?>
-

+

@@ -151,82 +126,94 @@ public function ut_user_taxonomies() {
-

+

-
+
- +

The name is how it appears on your site.

- - + +

- +

-
+ ' : ''; ?> + echo ! empty( $slug ) ? '' : ''; + ?>

-

+ - + class="ut-back-link"> +
-
+ prepare_items(); ?> -
prepare_items(); + ?> + + display(); ?> + $uttaxonomylisttable->display(); + ?>
-
+ $ut_taxonomy ) { - if ( empty( $taxonomy_slug ) && ( $ut_taxonomy['name'] == $taxonomy_name || $ut_taxonomy['slug'] == ut_taxonomy_name( $taxonomy_name ) ) ) { + if ( empty( $taxonomy_slug ) && ( $taxonomy_name === $ut_taxonomy['name'] || ut_taxonomy_name( $taxonomy_name ) === $ut_taxonomy['slug'] ) ) { $taxonomy_exists = true; break; } elseif ( ! empty( $taxonomy_slug ) && $taxonomy_slug == $ut_taxonomy['slug'] ) { @@ -239,7 +226,7 @@ function ut_update_taxonomy_list() { $ut_taxonomies[] = array( 'name' => $taxonomy_name, 'slug' => ! empty( $taxonomy_slug ) ? ut_taxonomy_name( $taxonomy_slug ) : ut_taxonomy_name( $taxonomy_name ), - 'description' => $taxonomy_description + 'description' => $taxonomy_description, ); update_site_option( 'ut_taxonomies', $ut_taxonomies ); //a new taxonomy added, so flush rules required @@ -259,7 +246,7 @@ function ut_update_taxonomy_list() { } function taxonomy_exists_notice() { - echo '
' . esc_html__( 'Taxonomy already exists', WP_UT_TRANSLATION_DOMAIN ) . '
'; + echo '
' . esc_html__( 'Taxonomy already exists', 'user_taxonomy' ) . '
'; } /** @@ -299,7 +286,7 @@ function ut_register_taxonomies() { 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; + $taxonomy_slug = 32 < strlen( $taxonomy_slug ) ? 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( @@ -309,24 +296,24 @@ function ut_register_taxonomies() { 'public' => true, 'hierarchical' => false, 'labels' => array( - 'name' => esc_html__( $name ), - 'singular_name' => esc_html__( $name ), - 'menu_name' => esc_html__( $name ), - 'search_items' => esc_html__( 'Search ' . $name ), - 'popular_items' => esc_html__( 'Popular ' . $name ), - 'all_items' => esc_html__( 'All ' . $name ), - 'edit_item' => esc_html__( 'Edit ' . $name ), - 'update_item' => esc_html__( 'Update ' . $name ), - 'add_new_item' => esc_html__( 'Add New ' . $name ), - 'new_item_name' => esc_html__( 'New ' . $name ), - 'separate_items_with_commas' => esc_html__( 'Separate ' . $name . ' with commas' ), - 'add_or_remove_items' => esc_html__( 'Add or remove ' . $name ), - 'choose_from_most_used' => esc_html__( 'Choose from the most popular ' . $name ), - 'topic_count_text' => esc_html__( 'Choose from the most popular ' . $name ), + 'name' => $name, + 'singular_name' => $name, + 'menu_name' => $name, + 'search_items' => 'Search ' . $name, + 'popular_items' => 'Popular ' . $name, + 'all_items' => 'All ' . $name, + 'edit_item' => 'Edit ' . $name, + 'update_item' => 'Update ' . $name, + 'add_new_item' => 'Add New ' . $name, + 'new_item_name' => 'New ' . $name, + 'separate_items_with_commas' => 'Separate ' . $name . ' with commas', + 'add_or_remove_items' => 'Add or remove ' . $name, + 'choose_from_most_used' => 'Choose from the most popular ' . $name, + 'topic_count_text' => 'Choose from the most popular ' . $name, ), 'rewrite' => array( 'with_front' => true, - 'slug' => $url_prefix . $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. @@ -334,7 +321,7 @@ function ut_register_taxonomies() { 'delete_terms' => 'edit_users', 'assign_terms' => 'read', ), - 'update_count_callback' => array( 'UserTags', 'update_users_count' ) + 'update_count_callback' => array( 'UserTags', 'update_users_count' ), ) ); if ( is_wp_error( $registered ) ) { @@ -356,7 +343,7 @@ function parent_menu( $parent = '' ) { // If we're editing one of the user taxonomies // We must be within the users menu, so highlight that - if ( ! empty( $_GET['taxonomy'] ) && $pagenow == 'edit-tags.php' && isset( self::$taxonomies[ $_GET['taxonomy'] ] ) ) { + if ( ! empty( $_GET['taxonomy'] ) && 'edit-tags.php' === $pagenow && isset( self::$taxonomies[ sanitize_key( $_GET['taxonomy'] ) ] ) ) { $parent = 'users.php'; } @@ -393,7 +380,7 @@ public function set_user_column_values( $display, $column, $term_id ) { return; } if ( 'users' === $column && ! empty( $_GET['taxonomy'] ) ) { - $term = get_term( $term_id, $_GET['taxonomy'] ); + $term = get_term( $term_id, sanitize_text_field( $_GET['taxonomy'] ) ); $count = $term->count; } else { @@ -401,15 +388,18 @@ public function set_user_column_values( $display, $column, $term_id ) { } $count = number_format_i18n( $count ); - $tax = get_taxonomy( $_GET['taxonomy'] ); + $tax = get_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) ); if ( $tax->query_var ) { $args = array( $tax->query_var => $term->slug ); } else { - $args = array( 'taxonomy' => $tax->name, 'term' => $term->slug ); + $args = array( + 'taxonomy' => $tax->name, + 'term' => $term->slug, + ); } - return "" . $count . ""; + return sprintf( '%2$d', esc_url( add_query_arg( $args, 'users.php' ) ), $count ); } /** @@ -417,62 +407,66 @@ public function set_user_column_values( $display, $column, $term_id ) { * * @param Object $user - The user of the view/edit screen */ - public function user_profile( $user ) { ?> + public function user_profile( $user ) { + ?>

User Tags

-
+ $taxonomy ): // Check the current user can assign terms for this taxonomy - { - if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) { - continue; - } + foreach ( self::$taxonomies as $key => $taxonomy ) { // Check the current user can assign terms for this taxonomy + if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) { + continue; + } - // Get all the terms in this taxonomy - - $terms = wp_get_object_terms( $user->ID, $taxonomy->name ); - $num = 0; - $html = ''; - $user_tags = array(); - $choose_from_text = apply_filters( 'ut_tag_cloud_heading', $taxonomy->labels->choose_from_most_used, $taxonomy ); - if ( ! empty( $terms ) ) { - foreach ( $terms as $term ) { - $user_tags[] = $term->name; - $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; - $html .= "
"; - $html .= ' ' . $term->name . ''; - $html .= "
"; - $num ++; - } - $user_tags = implode( ',', $user_tags ); - } ?> - - - - - - ID, $taxonomy->name ); + $num = 0; + $html = ''; + $user_tags = array(); + $choose_from_text = apply_filters( 'ut_tag_cloud_heading', $taxonomy->labels->choose_from_most_used, $taxonomy ); + if ( ! empty( $terms ) ) { + foreach ( $terms as $term ) { + $user_tags[] = $term->name; + $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; + $html .= '
'; + $html .= ' ' . $term->name . ''; + $html .= '
'; + $num ++; + } + $user_tags = implode( ',', $user_tags ); } - endforeach; // Taxonomies + ?> + + + + + + + -
+ $taxonomy_terms ) { + $input_tags = wp_unslash( $_POST['user-tags'] ); + foreach ( $input_tags as $taxonomy => $taxonomy_terms ) { // Check the current user can edit this user and assign terms for this taxonomy if ( ! current_user_can( 'edit_user', $user_id ) && current_user_can( $taxonomy->cap->assign_terms ) ) { return false; @@ -526,17 +521,19 @@ public function restrict_username( $username ) { */ function ut_delete_taxonomy_callback() { + if ( empty( $_POST ) || empty( $_POST['nonce'] ) || empty( $_POST['delete_taxonomy'] ) ) { return false; } - $delete_taxonomy = ''; - extract( $_POST ); - if ( ! wp_verify_nonce( $nonce, 'delete-taxonomy-' . $delete_taxonomy ) ) { + + $nonce = sanitize_key( $_POST['nonce'] ); + $taxnomy = sanitize_key( $_POST['delete_taxonomy'] ); + if ( ! wp_verify_nonce( $nonce, 'delete-taxonomy-' . $taxnomy ) ) { 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['slug'] ) == ut_stripallslashes( $delete_taxonomy ) ) { + if ( ut_stripallslashes( $ut_taxonomy_array['slug'] ) == ut_stripallslashes( $taxnomy ) ) { unset( $ut_taxonomies[ $ut_taxonomy_key ] ); } } @@ -547,7 +544,6 @@ function ut_delete_taxonomy_callback() { } else { wp_send_json_error( 'failed' ); } - die( 1 ); } /** @@ -558,14 +554,20 @@ function ut_load_tag_suggestions_callback() { if ( empty( $_POST ) || empty( $_POST['nonce'] ) || empty( $_POST['q'] ) || empty( $_POST['taxonomy'] ) ) { return false; } - extract( $_POST ); + + $nonce = sanitize_text_field( $_POST['nonce'] ); + $taxonomy = sanitize_text_field( $_POST['taxonomy'] ); + $q = sanitize_text_field( $_POST['q'] ); + if ( ! wp_verify_nonce( $nonce, 'user-tags' ) ) { return false; } - $tags = get_terms( $taxonomy, array( - 'orderby' => 'count', - 'hide_empty' => 0 - ) ); + $tags = get_terms( $taxonomy, + array( + 'orderby' => 'count', + 'hide_empty' => 0, + ) + ); if ( empty( $tags ) || ! is_array( $tags ) ) { return false; } @@ -580,15 +582,17 @@ function ut_load_tag_suggestions_callback() { if ( empty( $result ) ) { return; } - $output = ''; - if ( ! empty( $output ) ) { - echo $output; - } - die( 1 ); + ?> + + "; + + 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 ]; + $term_slug = sanitize_text_field( $_GET[ $taxonomy_slug ] ); continue; } } @@ -649,32 +654,35 @@ function ut_users_filter() { $ut_taxonomies = get_site_option( 'ut_taxonomies' ); if ( empty( $ut_taxonomies ) || ! is_array( $ut_taxonomies ) ) { return; - } ?> + } + ?> + ?> + + + + Reset Filters flush_rules( false ); - update_site_option( 'ut_new_taxonomy', 'FALSE' ); - } -} - -/** - * Show admin message for taxonomy creation - */ -function ut_taxonomy_created() { - echo '
' . esc_html__( 'Taxonomy created', WP_UT_TRANSLATION_DOMAIN ) . '
'; -} - -/** - * Updating a taxonomy - */ -function ut_taxonomy_updated() { - echo '
' . esc_html__( 'Taxonomy updated', WP_UT_TRANSLATION_DOMAIN ) . '
'; -} - -/** - * Class object - */ -function ut_user_tags() { - global $user_tags; - $user_tags = new UserTags(); -} - -/** - * If a new taxonomy was created, Flush rules for template - */ -add_action( 'init', 'wp_ut_flush_rules', 10 ); -?> \ No newline at end of file diff --git a/inc/functions.php b/inc/functions.php new file mode 100755 index 0000000..21f4f0f --- /dev/null +++ b/inc/functions.php @@ -0,0 +1,176 @@ + 32 ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; + + return esc_html( ut_stripallslashes( $taxonomy_slug ) ); +} + +/** + * + */ +add_filter( 'taxonomy_template', 'get_custom_taxonomy_template' ); +/** + * @param string $template + * + * @return string + */ +function get_custom_taxonomy_template( $template = '' ) { + + $taxonomy = get_query_var( 'taxonomy' ); + + //check if taxonomy is for user or not + $user_taxonomies = get_object_taxonomies( 'user', 'object' ); + + if ( ! array( $user_taxonomies ) || empty( $user_taxonomies[ $taxonomy ] ) ) { + return $template; + } + + //Check if theme is overriding the template + $overridden_template = locate_template( 'user-taxonomy-template.php', false, false ); + if ( ! empty( $overridden_template ) ) { + $taxonomy_template = $overridden_template; + } else { + $taxonomy_template = WP_UT_TEMPLATES . 'user-taxonomy-template.php'; + } + $file_headers = @get_headers( $taxonomy_template ); + if ( 'HTTP/1.0 404 Not Found' !== $file_headers[0] ) { + return $taxonomy_template; + } + + return $template; +} + +/** + * Shortcode for Tags UI in frontend + */ +function wp_ut_tag_box() { + $user_id = get_current_user_id(); + $taxonomies = get_object_taxonomies( 'user', 'object' ); + wp_nonce_field( 'user-tags', 'user-tags' ); + wp_enqueue_script( 'user_taxonomy_js' ); + if ( empty( $taxonomies ) ) { + ?> +

+ + +
+ + + +
+ + + $taxonomy_terms ) { + // Check the current user can edit this user and assign terms for this taxonomy + if ( ! current_user_can( 'edit_user', $user_id ) && current_user_can( $taxonomy->cap->assign_terms ) ) { + return false; + } + + // Save the data + if ( ! empty( $taxonomy_terms ) ) { + $taxonomy_terms = array_map( 'trim', explode( ',', $taxonomy_terms ) ); + } + wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false ); + } +} diff --git a/lib/functions.php b/lib/functions.php deleted file mode 100755 index 6accef8..0000000 --- a/lib/functions.php +++ /dev/null @@ -1,163 +0,0 @@ - 32 ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; - - return esc_html( ut_stripallslashes( $taxonomy_slug ) ); -} - -/** - * - */ -add_filter( 'taxonomy_template', 'get_custom_taxonomy_template' ); -/** - * @param string $template - * - * @return string - */ -function get_custom_taxonomy_template( $template = '' ) { - - $taxonomy = get_query_var( 'taxonomy' ); - - //check if taxonomy is for user or not - $user_taxonomies = get_object_taxonomies( 'user', 'object' ); - - if ( ! array( $user_taxonomies ) || empty( $user_taxonomies[ $taxonomy ] ) ) { - return $template; - } - - //Check if theme is overriding the template - $overridden_template = locate_template( 'user-taxonomy-template.php', false, false ); - if ( ! empty( $overridden_template ) ) { - $taxonomy_template = $overridden_template; - } else { - $taxonomy_template = WP_UT_TEMPLATES . "user-taxonomy-template.php"; - } - $file_headers = @get_headers( $taxonomy_template ); - if ( $file_headers[0] != 'HTTP/1.0 404 Not Found' ) { - return $taxonomy_template; - } - - return $template; -} - -/** - * Shortcode for Tags UI in frontend - */ -function wp_ut_tag_box() { - $user_id = get_current_user_id(); - $taxonomies = get_object_taxonomies( 'user', 'object' ); - wp_nonce_field( 'user-tags', 'user-tags' ); - wp_enqueue_script( 'user_taxonomy_js' ); - if ( empty ( $taxonomies ) ) { - ?> -

- -
- - - -
- $taxonomy_terms ) { - // Check the current user can edit this user and assign terms for this taxonomy - if ( ! current_user_can( 'edit_user', $user_id ) && current_user_can( $taxonomy->cap->assign_terms ) ) { - return false; - } - - // Save the data - if ( ! empty( $taxonomy_terms ) ) { - $taxonomy_terms = array_map( 'trim', explode( ',', $taxonomy_terms ) ); - } - wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false ); - } - } -} \ No newline at end of file diff --git a/templates/user-taxonomy-template.php b/templates/user-taxonomy-template.php index 2cfb915..2499580 100755 --- a/templates/user-taxonomy-template.php +++ b/templates/user-taxonomy-template.php @@ -12,7 +12,7 @@ $taxonomy = get_taxonomy( get_query_var( 'taxonomy' ) ); $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>

labels->name, WP_UT_TRANSLATION_DOMAIN ), esc_html__( $term->name, WP_UT_TRANSLATION_DOMAIN ) ) ); + echo apply_filters( 'ut_template_heading', sprintf( '%s: %s', $taxonomy->labels->name, $term->name ) ); ?>

+ * Author URI:http://codechutney.com + * Description: Adds User Taxonomy functionality, It allows you to categorize users on tags and taxonomy basis. + * Version: 1.2.8 + * Reference : http://justintadlock.com/archives/2011/10/20/custom-user-taxonomies-in-wordpress + * Text Domain : user_taxonomy + */ + + +define( 'WP_UT_URL', plugins_url( '', __FILE__ ) ); +define( 'WP_UT_PLUGIN_FOLDER', dirname( __FILE__ ) ); +define( 'WP_UT_TEMPLATES', trailingslashit( WP_UT_PLUGIN_FOLDER ) . trailingslashit( 'templates' ) ); + +/* Define all necessary variables first */ +define( 'WP_UT_CSS', WP_UT_URL . "/assets/css/" ); +define( 'WP_UT_JS', WP_UT_URL . "/assets/js/" ); + +// Includes PHP files located in 'lib' folder +require_once 'inc/class-user-tags.php'; +require_once 'inc/class-tags-list.php'; +require_once 'inc/class-shortcode.php'; +require_once 'inc/functions.php'; + +/** + * Class object + */ +add_action( 'init', 'ut_user_tags' ); + +//Flush rewrite rules +function wp_ut_flush_rules() { + //Check if there is new taxonomy, if there flush rules + $ut_new_taxonomy = get_site_option( 'ut_new_taxonomy', '', false ); + if ( $ut_new_taxonomy !== 'FALSE' ) { + global $wp_rewrite; + $wp_rewrite->flush_rules( false ); + update_site_option( 'ut_new_taxonomy', 'FALSE' ); + } +} + +/** + * Show admin message for taxonomy creation + */ +function ut_taxonomy_created() { + echo '
' . esc_html__( 'Taxonomy created', 'user_taxonomy' ) . '
'; +} + +/** + * Updating a taxonomy + */ +function ut_taxonomy_updated() { + echo '
' . esc_html__( 'Taxonomy updated', 'user_taxonomy' ) . '
'; +} + +/** + * Class object + */ +function ut_user_tags() { + global $user_tags; + $user_tags = new UserTags(); +} + +/** + * If a new taxonomy was created, Flush rules for template + */ +add_action( 'init', 'wp_ut_flush_rules', 10 ); From b9b450eaf639cee2c337c6abd4d2a4355209a602 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Sun, 23 Aug 2020 13:23:35 +0530 Subject: [PATCH 12/23] Code standard --- inc/class-shortcode.php | 23 ------------ ...tags-list.php => class-user-tags-list.php} | 31 +++++++++++----- inc/class-ut-user-tag-cloud.php | 23 ++++++++++++ templates/user-taxonomy-template.php | 35 ++++++++++++------- user-tags.php | 10 +++--- 5 files changed, 72 insertions(+), 50 deletions(-) delete mode 100755 inc/class-shortcode.php rename inc/{class-tags-list.php => class-user-tags-list.php} (65%) create mode 100755 inc/class-ut-user-tag-cloud.php diff --git a/inc/class-shortcode.php b/inc/class-shortcode.php deleted file mode 100755 index 0ec2842..0000000 --- a/inc/class-shortcode.php +++ /dev/null @@ -1,23 +0,0 @@ - - * - */ -class Ut_User_Tag_Cloud { - function __construct() { - add_shortcode('user-tags-cloud', array( $this, 'tag_cloud') ); - } - function tag_cloud( $atts ) { - - extract(shortcode_atts(array( - 'term' => '', - 'limit' => 25 - ), $atts)); - echo "
Variables";
-		print_r( $term);
-		print_r( $limit );
-		echo "
"; - } -} -new Ut_User_Tag_Cloud(); \ No newline at end of file diff --git a/inc/class-tags-list.php b/inc/class-user-tags-list.php similarity index 65% rename from inc/class-tags-list.php rename to inc/class-user-tags-list.php index 156dde8..91f2a6b 100755 --- a/inc/class-tags-list.php +++ b/inc/class-user-tags-list.php @@ -4,9 +4,8 @@ * @author Umesh Kumar (.1) * */ -require_once( dirname( __FILE__ ) . "/functions.php" ); -//If WP List table isn't included +// If WP List table isn't included. if ( ! class_exists( 'WP_List_Table' ) ) { require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); } @@ -83,20 +82,34 @@ function get_columns() { } function column_cb( $item ) { - printf( '', $this->_args['plural'], $item['name'] ); + ?> + + + ' . $item['name'] . '
' . esc_html__( 'Edit', 'user_taxonomy' ) . ' |'; - wp_nonce_field( 'delete-taxonomy-' . $taxonomy_slug, 'delete-taxonomy-' . $taxonomy_slug ); - echo ' ' . esc_html__( 'Trash', 'user_taxonomy' ) . '
'; + $edit_tags_url = "edit-tags.php?taxonomy='" . esc_attr( $taxonomy_slug ) . '"'; + $user_tax_url = "users.php?page=user-taxonomies&taxonomy='" . esc_attr( $taxonomy_slug ) . '"'; + ?> + + + +
+ | + + + + +
+ + * + */ + +class Ut_User_Tag_Cloud { + function __construct() { + add_shortcode( 'user-tags-cloud', array( $this, 'tag_cloud' ) ); + } + + function tag_cloud( $atts ) { + + extract( shortcode_atts( array( + 'term' => '', + 'limit' => 25, + ), $atts ) ); + } +} + +new Ut_User_Tag_Cloud(); diff --git a/templates/user-taxonomy-template.php b/templates/user-taxonomy-template.php index 2499580..495c055 100755 --- a/templates/user-taxonomy-template.php +++ b/templates/user-taxonomy-template.php @@ -4,17 +4,22 @@ * @author Umesh Kumar * @subpackage Custom User Taxonomy Plugin */ -get_header(); ?> +get_header(); +?>
+ + +
-
    + taxonomy ); @@ -47,17 +53,20 @@ '; $template_content .= apply_filters( 'ut_template_content', $c, $user_id ); } - echo $template_content; ?> + echo $template_content; + ?>
- No Users found.

"; - echo apply_filters( 'ut_template_content_empty', esc_html__( $content ) ); - } ?> + $content = apply_filters( 'ut_template_content_empty', '

No Users found.

' ); + echo esc_html( $content ); + } + ?>
-
+flush_rules( false ); update_site_option( 'ut_new_taxonomy', 'FALSE' ); From 2874eec92d315e11f7ad5935f904b9f48368b9b9 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Fri, 18 Sep 2020 17:57:25 +0530 Subject: [PATCH 13/23] Deprecate old filter; phpcs auto fixes --- inc/class-user-tags-list.php | 18 +++++---- inc/class-usertags.php | 58 +++++++++++++++------------- inc/class-ut-user-tag-cloud.php | 12 ++++-- inc/functions.php | 12 +++--- readme.txt | 6 +-- templates/user-taxonomy-template.php | 10 +++-- user-tags.php | 4 +- 7 files changed, 66 insertions(+), 54 deletions(-) diff --git a/inc/class-user-tags-list.php b/inc/class-user-tags-list.php index 91f2a6b..02c2e34 100755 --- a/inc/class-user-tags-list.php +++ b/inc/class-user-tags-list.php @@ -7,18 +7,20 @@ // If WP List table isn't included. if ( ! class_exists( 'WP_List_Table' ) ) { - require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); + require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; } class User_Tags_List extends WP_List_Table { public function __construct() { // Define singular and plural labels, as well as whether we support AJAX. - parent::__construct( array( - 'ajax' => false, - 'plural' => 'taxonomies', - 'singular' => 'taxonomy', - ) ); + parent::__construct( + array( + 'ajax' => false, + 'plural' => 'taxonomies', + 'singular' => 'taxonomy', + ) + ); $this->count_context = null; } @@ -37,7 +39,7 @@ function column_default( $item, $column_name ) { case 'taxonomy': return $item[ $column_name ]; default: - return print_r( $item, true ); //Show the whole array for troubleshooting purposes + return print_r( $item, true ); // Show the whole array for troubleshooting purposes } } @@ -90,7 +92,7 @@ function column_cb( $item ) { function column_taxonomy( $item ) { $taxonomy_slug = ! empty( $item['slug'] ) ? $item['slug'] : ut_taxonomy_name( $item['name'] ); - //var_dump($user_info); + // var_dump($user_info); echo esc_html( $taxonomy_slug ); } diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 11eb46f..3aaf8ec 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -2,7 +2,7 @@ class UserTags { private static $taxonomies = array(); - private $version = '1.2.7'; + private $version = '1.2.7'; /** * Register all the hooks and filters we can in advance @@ -44,7 +44,7 @@ public function __construct() { add_filter( 'pre_user_query', array( $this, 'ut_users_filter_query' ) ); add_action( 'restrict_manage_users', array( $this, 'ut_users_filter' ) ); - //Clear up related tags and taxonomies, when a user is deleted + // Clear up related tags and taxonomies, when a user is deleted add_action( 'deleted_user', array( $this, 'update_user_list' ) ); } @@ -60,7 +60,7 @@ function ut_enqueue_scripts() { * * @param String $taxonomy - The name of the taxonomy being registered * @param String $object - The object type the taxonomy is for; We only care if this is "user" - * @param Array $args - The user supplied + default arguments for registering the taxonomy + * @param Array $args - The user supplied + default arguments for registering the taxonomy */ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { global $wp_taxonomies; @@ -83,15 +83,17 @@ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { } /** - *Adds a Taxonomy Sub page to Users menu + * Adds a Taxonomy Sub page to Users menu */ public function admin_menu() { global $users_taxonomy; if ( is_super_admin() ) { - $users_taxonomy = add_users_page( esc_html__( 'User Taxonomies', 'user_taxonomy' ), esc_html__( 'Taxonomies', 'user_taxonomy' ), 'read', 'user-taxonomies', array( - $this, - 'ut_user_taxonomies', - ) ); + $users_taxonomy = add_users_page( + esc_html__( 'User Taxonomies', 'user_taxonomy' ), esc_html__( 'Taxonomies', 'user_taxonomy' ), 'read', 'user-taxonomies', array( + $this, + 'ut_user_taxonomies', + ) + ); } } @@ -229,18 +231,18 @@ function ut_update_taxonomy_list() { 'description' => $taxonomy_description, ); update_site_option( 'ut_taxonomies', $ut_taxonomies ); - //a new taxonomy added, so flush rules required + // a new taxonomy added, so flush rules required update_site_option( 'ut_new_taxonomy', true ); add_action( 'admin_notices', 'ut_taxonomy_created' ); } elseif ( $taxonomy_exists && ! empty( $taxonomy_slug ) ) { - //Update Taxonomy + // Update Taxonomy $ut_taxonomies[ $taxonomy_key ]['name'] = $taxonomy_name; $ut_taxonomies[ $taxonomy_key ]['description'] = $taxonomy_description; update_site_option( 'ut_taxonomies', $ut_taxonomies ); add_action( 'admin_notices', 'ut_taxonomy_updated' ); } else { - //Warning + // Warning add_action( 'admin_notices', array( $this, 'taxonomy_exists_notice' ) ); } } @@ -256,7 +258,7 @@ function taxonomy_exists_notice() { * * See the _update_post_term_count() function in WordPress for more info. * - * @param array $terms List of Term taxonomy IDs + * @param array $terms List of Term taxonomy IDs * @param object $taxonomy Current taxonomy object of terms */ public static function update_users_count( $terms, $taxonomy ) { @@ -274,7 +276,6 @@ public static function update_users_count( $terms, $taxonomy ) { /** * Get all the Taxonomies from site option 'ut_taxonomies' and register the taxonomies - * */ function ut_register_taxonomies() { $ut_taxonomies = get_site_option( 'ut_taxonomies' ); @@ -285,7 +286,7 @@ function ut_register_taxonomies() { foreach ( $ut_taxonomies as $ut_taxonomy ) { extract( $ut_taxonomy ); $taxonomy_slug = ! empty( $slug ) ? $slug : ut_taxonomy_name( $name ); - //make sure taxonomy name is less than 32 + // make sure taxonomy name is less than 32 $taxonomy_slug = 32 < strlen( $taxonomy_slug ) ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; $url_prefix = apply_filters( 'ut_tag_url_prefix', 'tag' ); $url_prefix = ! empty( $url_prefix ) ? trailingslashit( $url_prefix ) : ''; @@ -328,7 +329,7 @@ function ut_register_taxonomies() { $errors[] = $registered; } } - //End of foreach + // End of foreach } /** @@ -419,7 +420,6 @@ public function user_profile( $user ) { } // Get all the terms in this taxonomy - $terms = wp_get_object_terms( $user->ID, $taxonomy->name ); $num = 0; $html = ''; @@ -429,9 +429,9 @@ public function user_profile( $user ) { foreach ( $terms as $term ) { $user_tags[] = $term->name; $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; - $html .= '
'; - $html .= ' ' . $term->name . ''; - $html .= '
'; + $html .= '
'; + $html .= ' ' . $term->name . ''; + $html .= '
'; $num ++; } $user_tags = implode( ',', $user_tags ); @@ -446,14 +446,14 @@ public function user_profile( $user ) { + class="newtag form-input-tip float-left hide-on-blur" size="16" autocomplete="off" value="">

+ id="user-tags-name ); ?>" value=""/>

'count', 'hide_empty' => 0, @@ -576,7 +579,7 @@ function ut_load_tag_suggestions_callback() { $tag_list[] = $tag->name; } - //Matching Tags + // Matching Tags $input = preg_quote( trim( $q ), '~' ); $result = preg_grep( '~' . $input . '~i', $tag_list ); if ( empty( $result ) ) { @@ -608,6 +611,7 @@ function admin_ajax() { /** * Filters the user query to show list of users for a particular tag or taxonomy + * * @author Garrett Eclipse */ function ut_users_filter_query( $query ) { @@ -650,7 +654,7 @@ function ut_users_filter_query( $query ) { * Thank you Garrett Eclipse for the filter idea */ function ut_users_filter() { - //Show All the taxonomies in single drop down + // Show All the taxonomies in single drop down $ut_taxonomies = get_site_option( 'ut_taxonomies' ); if ( empty( $ut_taxonomies ) || ! is_array( $ut_taxonomies ) ) { return; diff --git a/inc/class-ut-user-tag-cloud.php b/inc/class-ut-user-tag-cloud.php index d66b5c9..d7ed15d 100755 --- a/inc/class-ut-user-tag-cloud.php +++ b/inc/class-ut-user-tag-cloud.php @@ -13,10 +13,14 @@ function __construct() { function tag_cloud( $atts ) { - extract( shortcode_atts( array( - 'term' => '', - 'limit' => 25, - ), $atts ) ); + extract( + shortcode_atts( + array( + 'term' => '', + 'limit' => 25, + ), $atts + ) + ); } } diff --git a/inc/functions.php b/inc/functions.php index 21f4f0f..c6f76ae 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -30,14 +30,14 @@ function get_custom_taxonomy_template( $template = '' ) { $taxonomy = get_query_var( 'taxonomy' ); - //check if taxonomy is for user or not + // check if taxonomy is for user or not $user_taxonomies = get_object_taxonomies( 'user', 'object' ); if ( ! array( $user_taxonomies ) || empty( $user_taxonomies[ $taxonomy ] ) ) { return $template; } - //Check if theme is overriding the template + // Check if theme is overriding the template $overridden_template = locate_template( 'user-taxonomy-template.php', false, false ); if ( ! empty( $overridden_template ) ) { $taxonomy_template = $overridden_template; @@ -89,9 +89,9 @@ function wp_ut_tag_box() { foreach ( $terms as $term ) { $user_tags[] = $term->name; $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; - $html .= '

'; + $html .= '
'; + $html .= 'x ' . $term->name . ''; + $html .= '
'; $num ++; } $user_tags = implode( ',', $user_tags ); @@ -126,7 +126,7 @@ function wp_ut_tag_box() { taxonomy ); continue; } - $c = ' + $c = '
  • ' . - get_avatar( $user_id, '96' ) . ' + get_avatar( $user_id, '96' ) . '

    ' . get_the_author_meta( 'display_name', $user_id ) . '

    ' . - wpautop( get_the_author_meta( 'description', $user_id ) ) . ' + wpautop( get_the_author_meta( 'description', $user_id ) ) . '
  • '; + $template_content .= apply_filters_deprecated( 'ut_tepmplate_content', array( $c, $user_id ), '1.2.8', 'ut_template_content' ); $template_content .= apply_filters( 'ut_template_content', $c, $user_id ); } echo $template_content; diff --git a/user-tags.php b/user-tags.php index 8e676e7..8e621d6 100755 --- a/user-tags.php +++ b/user-tags.php @@ -29,9 +29,9 @@ */ add_action( 'init', 'ut_user_tags' ); -//Flush rewrite rules +// Flush rewrite rules function wp_ut_flush_rules() { - //Check if there is new taxonomy, if there flush rules + // Check if there is new taxonomy, if there flush rules $ut_new_taxonomy = get_site_option( 'ut_new_taxonomy', '', false ); if ( 'FALSE' !== $ut_new_taxonomy ) { global $wp_rewrite; From 9a85cdd51e4da9ba8e39d654a4ee938d915a056d Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Fri, 18 Sep 2020 21:08:07 +0530 Subject: [PATCH 14/23] Code standard fixes --- inc/class-usertags.php | 168 ++++++++++++++++++-------------- inc/class-ut-user-tag-cloud.php | 20 ++-- readme.txt | 2 +- 3 files changed, 109 insertions(+), 81 deletions(-) diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 3aaf8ec..1e40244 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -2,7 +2,7 @@ class UserTags { private static $taxonomies = array(); - private $version = '1.2.7'; + private $version = '1.2.7'; /** * Register all the hooks and filters we can in advance @@ -60,7 +60,7 @@ function ut_enqueue_scripts() { * * @param String $taxonomy - The name of the taxonomy being registered * @param String $object - The object type the taxonomy is for; We only care if this is "user" - * @param Array $args - The user supplied + default arguments for registering the taxonomy + * @param Array $args - The user supplied + default arguments for registering the taxonomy */ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { global $wp_taxonomies; @@ -105,12 +105,16 @@ public function ut_user_taxonomies() { $page_title = esc_html__( 'Add New Taxonomy', 'user_taxonomy' ); $taxonomy_name = ''; $taxonomy_description = ''; + if ( ! empty( $_GET['taxonomy'] ) ) { - $slug = sanitize_text_field( $_GET['taxonomy'] ); - $page_title = 'Edit Taxonomy: ' . $slug; - $taxonomy = get_taxonomy( $slug ); + $slug = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); + + $page_title = 'Edit Taxonomy: ' . $slug; + $taxonomy = get_taxonomy( $slug ); + $taxonomy_name = ! empty( $taxonomy ) ? $taxonomy->labels->name : ''; $ut_taxonomies = get_site_option( 'ut_taxonomies' ); + if ( ! empty( $ut_taxonomies ) ) { foreach ( $ut_taxonomies as $ut_taxonomy ) { if ( $ut_taxonomy['slug'] == $slug ) { @@ -198,10 +202,10 @@ function ut_update_taxonomy_list() { wp_die( 'Invalid request' ); } - $taxonomy_name = sanitize_key( $_POST['taxonomy_name'] ); + $name = sanitize_text_field( wp_unslash( $_POST['taxonomy_name'] ) ); - $taxonomy_description = sanitize_text_field( $_POST['taxonomy_description'] ); - $taxonomy_slug = sanitize_key( $_POST['taxonomy_slug'] ); + $description = sanitize_text_field( wp_unslash( $_POST['taxonomy_description'] ) ); + $slug = sanitize_key( wp_unslash( $_POST['taxonomy_slug'] ) ); // Get all the existing taxonomies. $ut_taxonomies = get_site_option( 'ut_taxonomies' ); @@ -215,10 +219,10 @@ function ut_update_taxonomy_list() { // Check if taxonomy already created by user. $taxonomy_exists = false; foreach ( $ut_taxonomies as $ut_taxonomy_key => $ut_taxonomy ) { - if ( empty( $taxonomy_slug ) && ( $taxonomy_name === $ut_taxonomy['name'] || ut_taxonomy_name( $taxonomy_name ) === $ut_taxonomy['slug'] ) ) { + if ( empty( $slug ) && ( $name === $ut_taxonomy['name'] || ut_taxonomy_name( $name ) === $ut_taxonomy['slug'] ) ) { $taxonomy_exists = true; break; - } elseif ( ! empty( $taxonomy_slug ) && $taxonomy_slug == $ut_taxonomy['slug'] ) { + } elseif ( ! empty( $slug ) && $slug == $ut_taxonomy['slug'] ) { $taxonomy_exists = true; $taxonomy_key = $ut_taxonomy_key; break; @@ -226,19 +230,19 @@ function ut_update_taxonomy_list() { } if ( ! $taxonomy_exists ) { $ut_taxonomies[] = array( - 'name' => $taxonomy_name, - 'slug' => ! empty( $taxonomy_slug ) ? ut_taxonomy_name( $taxonomy_slug ) : ut_taxonomy_name( $taxonomy_name ), - 'description' => $taxonomy_description, + 'name' => $name, + 'slug' => ! empty( $slug ) ? ut_taxonomy_name( $slug ) : ut_taxonomy_name( $name ), + 'description' => $description, ); update_site_option( 'ut_taxonomies', $ut_taxonomies ); // a new taxonomy added, so flush rules required update_site_option( 'ut_new_taxonomy', true ); add_action( 'admin_notices', 'ut_taxonomy_created' ); - } elseif ( $taxonomy_exists && ! empty( $taxonomy_slug ) ) { + } elseif ( $taxonomy_exists && ! empty( $slug ) ) { // Update Taxonomy - $ut_taxonomies[ $taxonomy_key ]['name'] = $taxonomy_name; - $ut_taxonomies[ $taxonomy_key ]['description'] = $taxonomy_description; + $ut_taxonomies[ $taxonomy_key ]['name'] = $name; + $ut_taxonomies[ $taxonomy_key ]['description'] = $description; update_site_option( 'ut_taxonomies', $ut_taxonomies ); add_action( 'admin_notices', 'ut_taxonomy_updated' ); } else { @@ -258,7 +262,7 @@ function taxonomy_exists_notice() { * * See the _update_post_term_count() function in WordPress for more info. * - * @param array $terms List of Term taxonomy IDs + * @param array $terms List of Term taxonomy IDs * @param object $taxonomy Current taxonomy object of terms */ public static function update_users_count( $terms, $taxonomy ) { @@ -284,12 +288,22 @@ function ut_register_taxonomies() { return; } foreach ( $ut_taxonomies as $ut_taxonomy ) { + + //@todo: Test and remove extract, based on data stored in DB. extract( $ut_taxonomy ); + + $name = $ut_taxonomy['name']; + $slug = $ut_taxonomy['slug']; + $taxonomy_slug = ! empty( $slug ) ? $slug : ut_taxonomy_name( $name ); + // make sure taxonomy name is less than 32 $taxonomy_slug = 32 < strlen( $taxonomy_slug ) ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; + + // @todo: Move it to a function. $url_prefix = apply_filters( 'ut_tag_url_prefix', 'tag' ); $url_prefix = ! empty( $url_prefix ) ? trailingslashit( $url_prefix ) : ''; + $registered = register_taxonomy( $taxonomy_slug, 'user', @@ -380,8 +394,15 @@ public function set_user_column_values( $display, $column, $term_id ) { if ( empty( $column ) ) { return; } + + $input_taxonomy = ''; + + if ( ! empty( $_GET['taxonomy'] ) ) : + $input_taxonomy = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); + endif; + if ( 'users' === $column && ! empty( $_GET['taxonomy'] ) ) { - $term = get_term( $term_id, sanitize_text_field( $_GET['taxonomy'] ) ); + $term = get_term( $term_id, $input_taxonomy ); $count = $term->count; } else { @@ -389,7 +410,7 @@ public function set_user_column_values( $display, $column, $term_id ) { } $count = number_format_i18n( $count ); - $tax = get_taxonomy( sanitize_text_field( $_GET['taxonomy'] ) ); + $tax = get_taxonomy( $input_taxonomy ); if ( $tax->query_var ) { $args = array( $tax->query_var => $term->slug ); @@ -412,59 +433,61 @@ public function user_profile( $user ) { ?>

    User Tags

    - $taxonomy ) { // Check the current user can assign terms for this taxonomy - if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) { - continue; - } + $taxonomy ) { // Check the current user can assign terms for this taxonomy + if ( ! current_user_can( $taxonomy->cap->assign_terms ) ) { + continue; + } - // Get all the terms in this taxonomy - $terms = wp_get_object_terms( $user->ID, $taxonomy->name ); - $num = 0; - $html = ''; - $user_tags = array(); - $choose_from_text = apply_filters( 'ut_tag_cloud_heading', $taxonomy->labels->choose_from_most_used, $taxonomy ); - if ( ! empty( $terms ) ) { - foreach ( $terms as $term ) { - $user_tags[] = $term->name; - $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; - $html .= '
    '; - $html .= ' ' . $term->name . ''; - $html .= '
    '; - $num ++; + // Get all the terms in this taxonomy + $terms = wp_get_object_terms( $user->ID, $taxonomy->name ); + $num = 0; + $html = ''; + $user_tags = array(); + + $choose_from_text = apply_filters( 'ut_tag_cloud_heading', $taxonomy->labels->choose_from_most_used, $taxonomy ); + if ( ! empty( $terms ) ) { + foreach ( $terms as $term ) { + $user_tags[] = $term->name; + $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; + $html .= '
    '; + $html .= ' ' . $term->name . ''; + $html .= '
    '; + $num ++; + } + $user_tags = implode( ',', $user_tags ); } - $user_tags = implode( ',', $user_tags ); - } + + ?> + + + + + + + - - - - - - -
    '', - 'limit' => 25, - ), $atts - ) + $atts = shortcode_atts( + array( + 'term' => '', + 'limit' => 25, + ), + $attr, + 'user-tags-cloud' ); + + $term = $atts['term']; + $limit = $atts['limit']; + } } diff --git a/readme.txt b/readme.txt index aac9c5b..7e48433 100755 --- a/readme.txt +++ b/readme.txt @@ -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: 5.4 -Tested up to: 5.4 +Tested up to: 5.5 Stable tag: trunk Adds an admin option to allow creating User Taxonomies and create tags for different taxonomies. From 4f92487fcd3a2625f49e95ac52f6de1fea63114d Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Sat, 19 Sep 2020 08:16:49 +0530 Subject: [PATCH 15/23] Move URL prefix to a function --- inc/class-usertags.php | 6 +----- inc/functions.php | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 1e40244..136dc6a 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -300,10 +300,6 @@ function ut_register_taxonomies() { // make sure taxonomy name is less than 32 $taxonomy_slug = 32 < strlen( $taxonomy_slug ) ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; - // @todo: Move it to a function. - $url_prefix = apply_filters( 'ut_tag_url_prefix', 'tag' ); - $url_prefix = ! empty( $url_prefix ) ? trailingslashit( $url_prefix ) : ''; - $registered = register_taxonomy( $taxonomy_slug, 'user', @@ -328,7 +324,7 @@ function ut_register_taxonomies() { ), 'rewrite' => array( 'with_front' => true, - 'slug' => $url_prefix . $taxonomy_slug, // Use 'author' (default WP user slug). + 'slug' => get_url_prefix() . $taxonomy_slug, // Use 'author' (default WP user slug). ), 'capabilities' => array( 'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple. diff --git a/inc/functions.php b/inc/functions.php index c6f76ae..0c4c07c 100755 --- a/inc/functions.php +++ b/inc/functions.php @@ -174,3 +174,9 @@ function rce_ut_process_form() { wp_set_object_terms( $user_id, $taxonomy_terms, $taxonomy, false ); } } + +function get_url_prefix() { + $url_prefix = apply_filters( 'ut_tag_url_prefix', 'tag' ); + + return trailingslashit( $url_prefix ); +} From 37b9070064134b9153a0f25c852387b1cdff2782 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Mon, 21 Sep 2020 18:15:51 +0530 Subject: [PATCH 16/23] Store plugin version in DB; Rename class; Fixed file inclusion path --- ...tag-cloud.php => class-user-tag-cloud.php} | 4 +- inc/class-user-tags-list.php | 4 +- inc/class-usertags.php | 92 ++++++++++--------- readme.txt | 2 +- user-tags.php | 23 ++++- 5 files changed, 70 insertions(+), 55 deletions(-) rename inc/{class-ut-user-tag-cloud.php => class-user-tag-cloud.php} (86%) diff --git a/inc/class-ut-user-tag-cloud.php b/inc/class-user-tag-cloud.php similarity index 86% rename from inc/class-ut-user-tag-cloud.php rename to inc/class-user-tag-cloud.php index 276003c..9c9d288 100755 --- a/inc/class-ut-user-tag-cloud.php +++ b/inc/class-user-tag-cloud.php @@ -6,7 +6,7 @@ * */ -class Ut_User_Tag_Cloud { +class User_Tag_Cloud { function __construct() { add_shortcode( 'user-tags-cloud', array( $this, 'tag_cloud' ) ); } @@ -28,4 +28,4 @@ function tag_cloud( $attr ) { } } -new Ut_User_Tag_Cloud(); +$user_tag_cloud = new User_Tag_Cloud(); diff --git a/inc/class-user-tags-list.php b/inc/class-user-tags-list.php index 02c2e34..53d40ca 100755 --- a/inc/class-user-tags-list.php +++ b/inc/class-user-tags-list.php @@ -98,8 +98,8 @@ function column_taxonomy( $item ) { function column_name( $item ) { $taxonomy_slug = ! empty( $item['slug'] ) ? $item['slug'] : ut_taxonomy_name( $item['name'] ); - $edit_tags_url = "edit-tags.php?taxonomy='" . esc_attr( $taxonomy_slug ) . '"'; - $user_tax_url = "users.php?page=user-taxonomies&taxonomy='" . esc_attr( $taxonomy_slug ) . '"'; + $edit_tags_url = "edit-tags.php?taxonomy=" . esc_attr( $taxonomy_slug ); + $user_tax_url = "users.php?page=user-taxonomies&taxonomy=" . esc_attr( $taxonomy_slug ); ?> diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 136dc6a..cade83b 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -2,7 +2,9 @@ class UserTags { private static $taxonomies = array(); - private $version = '1.2.7'; + private $version = '1.2.8'; + + public $settings_page = ''; /** * Register all the hooks and filters we can in advance @@ -29,7 +31,7 @@ public function __construct() { $this->ut_register_taxonomies(); // Menus - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); + add_action( 'admin_menu', array( $this, 'register_page' ) ); add_filter( 'parent_file', array( $this, 'parent_menu' ) ); // User Profiles @@ -85,10 +87,9 @@ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { /** * Adds a Taxonomy Sub page to Users menu */ - public function admin_menu() { - global $users_taxonomy; - if ( is_super_admin() ) { - $users_taxonomy = add_users_page( + public function register_page() { + if ( apply_filters( 'ut_is_admin', is_super_admin() ) ) { + $this->settings_page = add_users_page( esc_html__( 'User Taxonomies', 'user_taxonomy' ), esc_html__( 'Taxonomies', 'user_taxonomy' ), 'read', 'user-taxonomies', array( $this, 'ut_user_taxonomies', @@ -126,7 +127,7 @@ public function ut_user_taxonomies() {

    -

    +

    @@ -204,7 +205,7 @@ function ut_update_taxonomy_list() { $name = sanitize_text_field( wp_unslash( $_POST['taxonomy_name'] ) ); - $description = sanitize_text_field( wp_unslash( $_POST['taxonomy_description'] ) ); + $description = !empty( $_POST['taxonomy_description'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy_description'] ) ) : ''; $slug = sanitize_key( wp_unslash( $_POST['taxonomy_slug'] ) ); // Get all the existing taxonomies. @@ -256,9 +257,7 @@ function taxonomy_exists_notice() { } /** - * Function for updating the 'profession' 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. + * Keep a track of users count for each taxonomy. * * See the _update_post_term_count() function in WordPress for more info. * @@ -300,41 +299,44 @@ function ut_register_taxonomies() { // make sure taxonomy name is less than 32 $taxonomy_slug = 32 < strlen( $taxonomy_slug ) ? substr( $taxonomy_slug, 0, 32 ) : $taxonomy_slug; - $registered = register_taxonomy( + $args = array( + 'public' => true, + 'hierarchical' => false, + 'labels' => array( + 'name' => $name, + 'singular_name' => $name, + 'menu_name' => $name, + 'search_items' => 'Search ' . $name, + 'popular_items' => 'Popular ' . $name, + 'all_items' => 'All ' . $name, + 'edit_item' => 'Edit ' . $name, + 'update_item' => 'Update ' . $name, + 'add_new_item' => 'Add New ' . $name, + 'new_item_name' => 'New ' . $name, + 'separate_items_with_commas' => 'Separate ' . $name . ' with commas', + 'add_or_remove_items' => 'Add or remove ' . $name, + 'choose_from_most_used' => 'Choose from the most popular ' . $name, + 'topic_count_text' => 'Choose from the most popular ' . $name, + ), + 'rewrite' => array( + 'with_front' => true, + 'slug' => get_url_prefix() . $taxonomy_slug, // 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' => array( $this, 'update_users_count' ), + ); + + $registered = register_taxonomy( $taxonomy_slug, 'user', - array( - 'public' => true, - 'hierarchical' => false, - 'labels' => array( - 'name' => $name, - 'singular_name' => $name, - 'menu_name' => $name, - 'search_items' => 'Search ' . $name, - 'popular_items' => 'Popular ' . $name, - 'all_items' => 'All ' . $name, - 'edit_item' => 'Edit ' . $name, - 'update_item' => 'Update ' . $name, - 'add_new_item' => 'Add New ' . $name, - 'new_item_name' => 'New ' . $name, - 'separate_items_with_commas' => 'Separate ' . $name . ' with commas', - 'add_or_remove_items' => 'Add or remove ' . $name, - 'choose_from_most_used' => 'Choose from the most popular ' . $name, - 'topic_count_text' => 'Choose from the most popular ' . $name, - ), - 'rewrite' => array( - 'with_front' => true, - 'slug' => get_url_prefix() . $taxonomy_slug, // 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' => array( 'UserTags', 'update_users_count' ), - ) + $args ); + if ( is_wp_error( $registered ) ) { $errors[] = $registered; } @@ -343,7 +345,7 @@ function ut_register_taxonomies() { } /** - * Highlight User Menu item + * Set Users menu as parent for User Taxonomy edit page. * * @param string $parent * @@ -671,7 +673,7 @@ function ut_users_filter_query( $query ) { /** * Adds a dropdown for each taxonomy and used tags to allow filtering of users list * - * Thank you Garrett Eclipse for the filter idea + * @author Garrett Eclipse */ function ut_users_filter() { // Show All the taxonomies in single drop down diff --git a/readme.txt b/readme.txt index 7e48433..40a39c7 100755 --- a/readme.txt +++ b/readme.txt @@ -65,7 +65,7 @@ Users can add new tags. * Fixed - Bubbling up of multiple list on repeated click over most used tags link * Fixed - Tag being saved for admin too on editing other user profile -( Thank you @Tempera for reporting all the issues ) +( Credits: @Tempera ) = 1.2.3 = * Fixed - 'ut_template_content' filter args diff --git a/user-tags.php b/user-tags.php index 8e621d6..0edf0f6 100755 --- a/user-tags.php +++ b/user-tags.php @@ -11,18 +11,20 @@ define( 'WP_UT_URL', plugins_url( '', __FILE__ ) ); -define( 'WP_UT_PLUGIN_FOLDER', dirname( __FILE__ ) ); +define( 'WP_UT_PLUGIN_FOLDER', trailingslashit( dirname( __FILE__ ) ) ); define( 'WP_UT_TEMPLATES', trailingslashit( WP_UT_PLUGIN_FOLDER ) . trailingslashit( 'templates' ) ); +define( 'UT_VERSION', '1.2.8' ); + /* Define all necessary variables first */ define( 'WP_UT_CSS', WP_UT_URL . '/assets/css/' ); define( 'WP_UT_JS', WP_UT_URL . '/assets/js/' ); // Includes PHP files located in 'inc' folder -require_once 'inc/functions.php'; -require_once 'inc/class-user-tags.php'; -require_once 'inc/class-tags-list.php'; -require_once 'inc/class-shortcode.php'; +require_once WP_UT_PLUGIN_FOLDER . 'inc/functions.php'; +require_once WP_UT_PLUGIN_FOLDER . 'inc/class-usertags.php'; +require_once WP_UT_PLUGIN_FOLDER . 'inc/class-user-tags-list.php'; +require_once WP_UT_PLUGIN_FOLDER . 'inc/class-user-tag-cloud.php'; /** * Class object @@ -66,3 +68,14 @@ function ut_user_tags() { * If a new taxonomy was created, Flush rules for template */ add_action( 'init', 'wp_ut_flush_rules', 10 ); + +// Register plugin activation hook, Set/update plugin version. +register_activation_hook( __FILE__, 'ut_activated' ); + +function ut_activated() { + + $version = get_site_option( 'ut_version' ); + if ( ! $version || UT_VERSION !== $version ) { + update_site_option( 'ut_version', UT_VERSION ); + } +} From a21a2713584a61b7c719445f63f58add3b266eca Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Mon, 21 Sep 2020 18:44:48 +0530 Subject: [PATCH 17/23] Copy changes --- README.rst | 6 +++--- assets/js/user_taxonomy.js | 2 +- inc/class-user-tags-list.php | 2 +- inc/class-usertags.php | 6 +++--- readme.txt | 12 ++++++------ templates/user-taxonomy-template.php | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/README.rst b/README.rst index 4fe3800..8b54bde 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ User Tags ====== -Allows creating and managing User Taxonomies from WordPress admin. +Allows creating and managing User Taxonomy from WordPress admin. Description ====== @@ -14,9 +14,9 @@ Ref: [Justin Tadlock](http://justintadlock.com/archives/2011/10/20/custom-user-t Installation ====== -1. Upload the `wp-user-taxonomies` folder to the `/wp-content/plugins/` directory +1. Upload the `user-tags` folder to the `/wp-content/plugins/` directory 2. Activate the plugin through the 'Plugins' menu in WordPress -3. Go to Taxonomies under Users option to create taxonomies for User +3. Go to Taxonomy under Users option to create taxonomy for User Filters Available diff --git a/assets/js/user_taxonomy.js b/assets/js/user_taxonomy.js index 8256f94..6bbaec0 100755 --- a/assets/js/user_taxonomy.js +++ b/assets/js/user_taxonomy.js @@ -100,7 +100,7 @@ jQuery(document).ready(function ($) { }); }, 3000); if (!$('#the-taxonomy-list tr').length) { - $no_taxonomies = 'No Taxonomies found.'; + $no_taxonomies = 'No Taxonomy found.'; $('#the-taxonomy-list').append($no_taxonomies); } } else { diff --git a/inc/class-user-tags-list.php b/inc/class-user-tags-list.php index 53d40ca..eb02f33 100755 --- a/inc/class-user-tags-list.php +++ b/inc/class-user-tags-list.php @@ -65,7 +65,7 @@ function ut_list_taxonomies() { } function no_items() { - esc_html_e( 'No Taxonomies found.', 'user_taxonomy' ); + esc_html_e( 'No Taxonomy found.', 'user_taxonomy' ); } function get_bulk_actions() { diff --git a/inc/class-usertags.php b/inc/class-usertags.php index cade83b..82a601b 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -90,7 +90,7 @@ public function ut_registered_taxonomy( $taxonomy, $object, $args ) { public function register_page() { if ( apply_filters( 'ut_is_admin', is_super_admin() ) ) { $this->settings_page = add_users_page( - esc_html__( 'User Taxonomies', 'user_taxonomy' ), esc_html__( 'Taxonomies', 'user_taxonomy' ), 'read', 'user-taxonomies', array( + esc_html__( 'User Taxonomy', 'user_taxonomy' ), esc_html__( 'Taxonomy', 'user_taxonomy' ), 'read', 'user-taxonomies', array( $this, 'ut_user_taxonomies', ) @@ -103,7 +103,7 @@ public function register_page() { * the name and other values for taxonomy */ public function ut_user_taxonomies() { - $page_title = esc_html__( 'Add New Taxonomy', 'user_taxonomy' ); + $page_title = esc_html__( 'Add new Taxonomy', 'user_taxonomy' ); $taxonomy_name = ''; $taxonomy_description = ''; @@ -125,7 +125,7 @@ public function ut_user_taxonomies() { } } ?>
    -

    +

    diff --git a/readme.txt b/readme.txt index 40a39c7..a585a33 100755 --- a/readme.txt +++ b/readme.txt @@ -1,6 +1,6 @@ === User Tags === Contributors: UmeshSingla -Donate link: https://www.paypal.com/ +Donate link: https://paypal.me/SinglaUmesh Tags: Tags, taxonomies, user taxonomy, user tags License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -8,11 +8,11 @@ Requires at least: 5.4 Tested up to: 5.5 Stable tag: trunk -Adds an admin option to allow creating User Taxonomies and create tags for different taxonomies. +Adds an admin option to allow creating User Taxonomy and create tags for different taxonomies. == Description == -Adds a **Taxonomies** option under **User** to create custom user taxonomy. +Adds a **Taxonomy** option under **User** to create custom user taxonomy. All taxonomies are listed in Profile page for all users which allows users to add tags for the taxonomy. Each Tag is associated with a template, listing all users who added that tag in their profile. @@ -24,9 +24,9 @@ Users can add new tags. == Installation == -1. Upload the `wp-user-taxonomies` folder to the `/wp-content/plugins/` directory -2. Activate the plugin through the 'Plugins' menu in WordPress -3. Go to Taxonomies under Users option to create taxonomies for User +1. Upload the `user-tags` folder to the `/wp-content/plugins/` directory. +2. Activate the plugin through the 'Plugins' menu in WordPress. +3. To create new Taxonomy Go to User -> Taxonomy screen. == Changelog == diff --git a/templates/user-taxonomy-template.php b/templates/user-taxonomy-template.php index f60f435..b4e77df 100755 --- a/templates/user-taxonomy-template.php +++ b/templates/user-taxonomy-template.php @@ -1,6 +1,6 @@ Date: Tue, 22 Sep 2020 08:28:38 +0530 Subject: [PATCH 18/23] Fix: Taxonomy description was nopt being saved; Fixed tag holder styling in back-end --- assets/css/style.css | 35 ++++++++++++++++++++--------------- inc/class-usertags.php | 28 +++++++++++++++++----------- user-tags.php | 18 +++++++++--------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/assets/css/style.css b/assets/css/style.css index 63a6e7d..5db1bc5 100755 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -15,10 +15,11 @@ } .user-taxonomy-wrapper .tagchecklist { - font-size: inherit; - margin-left: inherit; + display: flex; + flex-wrap: wrap; + max-width: 600px; + margin-left: 0; padding: 10px 10px 10px 0; - width: 300px; overflow: hidden; } @@ -50,9 +51,9 @@ td.ajaxtag { .term-link { float: left; - margin-right: 20px; line-height: 1.7; - margin-left: 10px; + margin: 0; + padding: 0 8px; } #message.updated { @@ -81,10 +82,11 @@ td.ajaxtag { } .tag-hldr { + align-items: center; background: #444; - display: inline-block; + display: flex; margin: 2px 3px; - padding: 3px; + padding: 5px; } .tag-hldr:hover { @@ -92,15 +94,14 @@ td.ajaxtag { } .tag-hldr span { - background: rgb(185, 185, 185); + align-items: center; + background: rgb(155, 140, 140); border-radius: 50%; - display: inline-block; - float: none; - margin: 4px 0 0 0; + display: flex; + margin-right: 5px; + max-width: 19px; position: relative; - vertical-align: middle; text-align: center; - width: 18px; } .tag-hldr span:hover { @@ -110,6 +111,7 @@ td.ajaxtag { .tag-hldr a { color: white; + font-size: 1.3em; text-decoration: none; } @@ -138,10 +140,13 @@ ul.user-taxonomy-wrapper .tagchecklist span a { margin: -1px 0 0 0; } .profile-php .user-taxonomy-wrapper .tagchecklist span a { + font-size: 1em; + font-weight: bold; + height: auto; + line-height: 1.6; margin: 0 auto; - font-size: 9px; - display: block; position: relative; + text-shadow: 1px 1px #08090917; } diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 82a601b..15baadf 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -2,7 +2,6 @@ class UserTags { private static $taxonomies = array(); - private $version = '1.2.8'; public $settings_page = ''; @@ -51,8 +50,16 @@ public function __construct() { } 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' ), $this->version, true ); + + $js_mtime = filemtime( UT_DIR . '/assets/js/user_taxonomy.js'); + $version = UT_VERSION . $js_mtime; + wp_register_script( 'user_taxonomy_js', UT_JS_URL . 'user_taxonomy.js', array( 'jquery' ), $version, true ); + + $css_mtime = filemtime( UT_DIR . '/assets/css/style.css'); + $version = UT_VERSION . $css_mtime; + wp_enqueue_style( 'ut-style', UT_CSS_URL . 'style.css', '', $version ); + + wp_enqueue_script( 'user_taxonomy_js' ); } @@ -104,8 +111,9 @@ public function register_page() { */ public function ut_user_taxonomies() { $page_title = esc_html__( 'Add new Taxonomy', 'user_taxonomy' ); - $taxonomy_name = ''; $taxonomy_description = ''; + $slug = ''; + $taxonomy_name = ''; if ( ! empty( $_GET['taxonomy'] ) ) { $slug = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); @@ -127,8 +135,6 @@ public function ut_user_taxonomies() {

    -

    -
    @@ -138,13 +144,13 @@ public function ut_user_taxonomies() {
    - +

    The name is how it appears on your site.

    - +

    @@ -205,7 +211,7 @@ function ut_update_taxonomy_list() { $name = sanitize_text_field( wp_unslash( $_POST['taxonomy_name'] ) ); - $description = !empty( $_POST['taxonomy_description'] ) ? sanitize_text_field( wp_unslash( $_POST['taxonomy_description'] ) ) : ''; + $description = !empty( $_POST['description'] ) ? sanitize_text_field( wp_unslash( $_POST['description'] ) ) : ''; $slug = sanitize_key( wp_unslash( $_POST['taxonomy_slug'] ) ); // Get all the existing taxonomies. @@ -450,7 +456,7 @@ public function user_profile( $user ) { $user_tags[] = $term->name; $term_url = site_url() . '/' . $taxonomy->rewrite['slug'] . '/' . $term->slug; $html .= '
    '; - $html .= ' ' . $term->name . ''; + $html .= sprintf( '%s', esc_url( $term_url ), $term->name ); $html .= '
    '; $num ++; } @@ -472,7 +478,7 @@ class="newtag form-input-tip float-left hide-on-blur" size="16" autocomplete="of

    -
    +
    diff --git a/user-tags.php b/user-tags.php index 0edf0f6..21fb487 100755 --- a/user-tags.php +++ b/user-tags.php @@ -10,21 +10,21 @@ */ -define( 'WP_UT_URL', plugins_url( '', __FILE__ ) ); -define( 'WP_UT_PLUGIN_FOLDER', trailingslashit( dirname( __FILE__ ) ) ); -define( 'WP_UT_TEMPLATES', trailingslashit( WP_UT_PLUGIN_FOLDER ) . trailingslashit( 'templates' ) ); +define( 'UT_URL', plugins_url( '', __FILE__ ) ); +define( 'UT_DIR', trailingslashit( dirname( __FILE__ ) ) ); +define( 'WP_UT_TEMPLATES', trailingslashit( UT_DIR ) . trailingslashit( 'templates' ) ); define( 'UT_VERSION', '1.2.8' ); /* Define all necessary variables first */ -define( 'WP_UT_CSS', WP_UT_URL . '/assets/css/' ); -define( 'WP_UT_JS', WP_UT_URL . '/assets/js/' ); +define( 'UT_CSS_URL', UT_URL . '/assets/css/' ); +define( 'UT_JS_URL', UT_URL . '/assets/js/' ); // Includes PHP files located in 'inc' folder -require_once WP_UT_PLUGIN_FOLDER . 'inc/functions.php'; -require_once WP_UT_PLUGIN_FOLDER . 'inc/class-usertags.php'; -require_once WP_UT_PLUGIN_FOLDER . 'inc/class-user-tags-list.php'; -require_once WP_UT_PLUGIN_FOLDER . 'inc/class-user-tag-cloud.php'; +require_once UT_DIR . 'inc/functions.php'; +require_once UT_DIR . 'inc/class-usertags.php'; +require_once UT_DIR . 'inc/class-user-tags-list.php'; +require_once UT_DIR . 'inc/class-user-tag-cloud.php'; /** * Class object From e35145e9eb5ea4eb958aa35862b40882d46df980 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Tue, 22 Sep 2020 17:38:41 +0530 Subject: [PATCH 19/23] Fixed: Delete tags doesn't work in user profile page. --- assets/js/user_taxonomy.js | 105 ++++++++++++++++++++++++------------- inc/class-usertags.php | 2 + inc/functions.php | 9 ---- 3 files changed, 71 insertions(+), 45 deletions(-) diff --git a/assets/js/user_taxonomy.js b/assets/js/user_taxonomy.js index 6bbaec0..e1175e5 100755 --- a/assets/js/user_taxonomy.js +++ b/assets/js/user_taxonomy.js @@ -2,8 +2,8 @@ * Check for empty fields */ function validate_form(parameters) { - $empty_fields = new Array(); - $i = 0; + let $empty_fields = new Array(); + let $i = 0; jQuery('#editusertaxonomy input, #editusertaxonomy textarea').each(function () { if (!jQuery(this).is('textarea')) { $input_value = jQuery(this).val(); @@ -25,28 +25,30 @@ function validate_form(parameters) { */ function insert_tags($tag_input, $taxonomy_name, $term, $tag_html) { //Fetch current values and split from comma to array - $user_tag_input = jQuery('#user-tags-' + $taxonomy_name); - $user_tag_input_val = $user_tag_input.val(); - if ($user_tag_input_val) { - $user_tag_input_val_array = $user_tag_input_val.split(','); - $insert = true; - for ($i = 0; $i < $user_tag_input_val_array.length; $i++) { - if (jQuery.trim($user_tag_input_val_array[$i]) == jQuery.trim($term)) { + let $input = jQuery('#user-tags-' + $taxonomy_name); + let $input_val = $user_tag_input.val(); + if ($input_val) { + let $input_val_array = $input_val.split(','); + let $insert = true; + for ( let $i = 0; $i < $input_val_array.length; $i++) { + let val = $input_val_array[$i]; + if (jQuery.trim( val ) == jQuery.trim( $term ) ) { $insert = false; break; } } if ($insert) { - $user_tag_input.val($user_tag_input_val + ', ' + $term); + $input.val(input_val + ', ' + $term); $tag_checklist.append($tag_html); } } else { - $user_tag_input.val($term); + $input.val($term); $tag_checklist.append($tag_html); } $tag_input.val(''); jQuery('body .tag-suggestion').remove(); } + jQuery(document).ready(function ($) { /** * Checks for Empty fields on Edit Taxonomy form submission @@ -70,17 +72,21 @@ jQuery(document).ready(function ($) { //Delete Taxonomy $('body').on('click', '.delete-taxonomy a', function (e) { e.preventDefault(); + if (!confirm("Are you sure, you want to delete the taxonomy?")) { return false; } - $this = $(this); - $taxonomy_id = $this.attr('id'); + let $this = $(this); + let $taxonomy_id = $this.attr('id'); + if ($taxonomy_id) { $taxonomy_id = $taxonomy_id.split('-'); $taxonomy_id = $taxonomy_id[1]; } - $taxonomy_name = $this.attr('data-name'); - $nonce = $('#delete-taxonomy-' + $taxonomy_id).val(); + + let $taxonomy_name = $this.attr('data-name'); + let $nonce = $('#delete-taxonomy-' + $taxonomy_id).val(); + $.ajax({ 'type': 'POST', 'url': ajaxurl, @@ -91,26 +97,33 @@ jQuery(document).ready(function ($) { }, success: function (resp_data) { if (typeof resp_data.success !== 'undefined' && resp_data.success) { - $message = '

    Taxonomy deleted.

    '; + + const $message = '

    Taxonomy deleted.

    '; $('.user-taxonomies-page h2:first').after($message); + $this.parents().eq(3).remove(); + setInterval(function () { $('.user-taxonomies-page #message.below-h2').hide('slow', function () { $('.user-taxonomies-page #message.below-h2').remove(); }); }, 3000); + if (!$('#the-taxonomy-list tr').length) { - $no_taxonomies = 'No Taxonomy found.'; + const $no_taxonomies = 'No Taxonomy found.'; $('#the-taxonomy-list').append($no_taxonomies); } + } else { - $error_div = '

    Taxonomy not deleted.

    '; + const $error_div = '

    Taxonomy not deleted.

    '; $('.user-taxonomies-page h2:first').after($error_div); + setInterval(function () { $('.user-taxonomies-page #message.below-h2').hide('slow', function () { $('.user-taxonomies-page #message.below-h2').remove(); }); }, 3000); + } }, error: function (resp_error) { @@ -119,13 +132,15 @@ jQuery(document).ready(function ($) { }); }); - var delay = (function () { - var timer = 0; + + let delay = (function () { + let timer = 0; return function (callback, ms) { clearTimeout(timer); timer = setTimeout(callback, ms); }; })(); + /** * Fetches the tag suggestion based on user input */ @@ -165,6 +180,7 @@ jQuery(document).ready(function ($) { jQuery('.tag-suggestion').remove(); } }); + //Tags UI $('body').on('click', '.tag-suggestion li', function () { $this = $(this); @@ -178,10 +194,11 @@ jQuery(document).ready(function ($) { $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); $taxonomy_name = $taxonomy_id[1]; } - $tag_html = ''; + $tag_html = ''; insert_tags($sibling, $taxonomy_name, $newtag_val[$i], $tag_html); } $('.tag-suggestion').remove(); }); - //Delete Tag + + // Handle tag delete click. $('body').on('click', '.ntdelbutton', function () { - $this = $(this); - $term = $this.parent().next('.term-link').html(); - $tags_input = $this.parents().eq(2).siblings('input[type="hidden"]').val(); - $tags_input = $tags_input.split(','); + let $this = $(this); + let parent = $this.parents().eq(1); + let $term = parent.find('.term-link').html(); + let $tags_list = $this.parents().eq(2).siblings('input[type="hidden"]'); - $tags_input = $.grep($tags_input, function (value) { + let $current_tags = $tags_list.val(); + $current_tags = $current_tags.split(','); + + // Delete the tag from the list. + let $updated_tags = $.grep($current_tags, function (value) { return value != $term; }); - $this.parents().eq(2).siblings('input[type="hidden"]').val($tags_input.join(',')); - $this.parent().next('.term-link').remove(); - $this.parent().parent().remove(); + // Store the updated list. + $tags_list.val($updated_tags.join(',')); + + // Remove tag holder. + parent.remove(); + }); + $('body').on('click', '.term-link', function (e) { if ($(this).attr('href') != '#') return true; else { @@ -232,22 +258,23 @@ jQuery(document).ready(function ($) { return false; } }); - var doing_ajax = false; + + let doing_ajax = false; + //Most Popular tag list $('body').on('click', '.tagcloud-link.user-taxonomy', function (e) { e.preventDefault(); if (doing_ajax) { return false; } + doing_ajax = true; if ($(this).parent().find('.the-tagcloud').length) { $(this).parent().find('.the-tagcloud').remove(); return true; } - doing_ajax = true; var id = $(this).attr('id'); var tax = id.substr(id.indexOf("-") + 1); $.post(ajaxurl, {'action': 'get-tagcloud', 'tax': tax}, function (r, stat) { - doing_ajax = false; if (0 === r || 'success' != stat) r = wpAjax.broken; @@ -271,21 +298,25 @@ jQuery(document).ready(function ($) { $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); $taxonomy_name = $taxonomy_id[1]; } - $tag_html = '
    x ' + $term + ' - - Date: Fri, 2 Oct 2020 14:26:16 +0530 Subject: [PATCH 20/23] User profile page: Fixed Ajax suggestion not working; fixed tag not added from most popular list on click --- assets/js/user_taxonomy.js | 670 +++++++++++++++++++------------------ inc/class-usertags.php | 15 +- inc/functions.php | 8 +- user-tags.php | 2 +- 4 files changed, 357 insertions(+), 338 deletions(-) diff --git a/assets/js/user_taxonomy.js b/assets/js/user_taxonomy.js index e1175e5..5c2bbfd 100755 --- a/assets/js/user_taxonomy.js +++ b/assets/js/user_taxonomy.js @@ -2,344 +2,356 @@ * Check for empty fields */ function validate_form(parameters) { - let $empty_fields = new Array(); - let $i = 0; - jQuery('#editusertaxonomy input, #editusertaxonomy textarea').each(function () { - if (!jQuery(this).is('textarea')) { - $input_value = jQuery(this).val(); - } - if (!$input_value && jQuery(this).attr('data-required')) { - jQuery(this).parents().eq(1).addClass('form-invalid'); - $empty_fields[$i] = jQuery(this).attr('name'); - $i++; - } - }); - return $empty_fields; + let $empty_fields = new Array(); + let $i = 0; + jQuery('#editusertaxonomy input, #editusertaxonomy textarea').each(function () { + if (!jQuery(this).is('textarea')) { + $input_value = jQuery(this).val(); + } + if (!$input_value && jQuery(this).attr('data-required')) { + jQuery(this).parents().eq(1).addClass('form-invalid'); + $empty_fields[$i] = jQuery(this).attr('name'); + $i++; + } + }); + return $empty_fields; } + /** * Creates Tag from input value in the form - * @param $tag_input + * @param $tag_input Input field * @param $taxonomy_name * @param $term - * @param $tag_html + * @param $tag_html Tag markup to be displayed. */ function insert_tags($tag_input, $taxonomy_name, $term, $tag_html) { - //Fetch current values and split from comma to array - let $input = jQuery('#user-tags-' + $taxonomy_name); - let $input_val = $user_tag_input.val(); - if ($input_val) { - let $input_val_array = $input_val.split(','); - let $insert = true; - for ( let $i = 0; $i < $input_val_array.length; $i++) { - let val = $input_val_array[$i]; - if (jQuery.trim( val ) == jQuery.trim( $term ) ) { - $insert = false; - break; - } - } - if ($insert) { - $input.val(input_val + ', ' + $term); - $tag_checklist.append($tag_html); - } - } else { - $input.val($term); - $tag_checklist.append($tag_html); - } - $tag_input.val(''); - jQuery('body .tag-suggestion').remove(); + //Fetch current values and split from comma to array + let $input = jQuery('#user-tags-' + $taxonomy_name); + let $input_val = $input.val(); + let $tag_checklist = $input.siblings('.tagchecklist'); + + // Append to the existing values. + if ($input_val) { + let $input_val_array = $input_val.split(','); + let $insert = true; + for (let $i = 0; $i < $input_val_array.length; $i++) { + let val = $input_val_array[$i]; + if (jQuery.trim(val) == jQuery.trim($term)) { + $insert = false; + break; + } + } + if ($insert) { + $input.val($input_val + ', ' + $term); + $tag_checklist.append($tag_html); + } + } else { + // Add a value. + $input.val($term); + $tag_checklist.append($tag_html); + } + + $tag_input.val(''); + // Remove the particular suggestion. + jQuery('body .tag-suggestion').remove(); } jQuery(document).ready(function ($) { - /** - * Checks for Empty fields on Edit Taxonomy form submission - */ - $('body').on('submit', '#editusertaxonomy', function (e) { - $empty_fields = validate_form(); - if (!$empty_fields.length) { - return true; - } else { - return false; - } - }); - $('#editusertaxonomy input').on('keyup', function () { - if (jQuery(this).parents().eq(1).hasClass('form-invalid')) { - $input_value = $(this).val(); - if ($input_value) { - $(this).parents().eq(1).removeClass('form-invalid'); - } - } - }); - //Delete Taxonomy - $('body').on('click', '.delete-taxonomy a', function (e) { - e.preventDefault(); - - if (!confirm("Are you sure, you want to delete the taxonomy?")) { - return false; - } - let $this = $(this); - let $taxonomy_id = $this.attr('id'); - - if ($taxonomy_id) { - $taxonomy_id = $taxonomy_id.split('-'); - $taxonomy_id = $taxonomy_id[1]; - } - - let $taxonomy_name = $this.attr('data-name'); - let $nonce = $('#delete-taxonomy-' + $taxonomy_id).val(); - - $.ajax({ - 'type': 'POST', - 'url': ajaxurl, - 'data': { - action: 'ut_delete_taxonomy', - delete_taxonomy: $taxonomy_name, - nonce: $nonce - }, - success: function (resp_data) { - if (typeof resp_data.success !== 'undefined' && resp_data.success) { - - const $message = '

    Taxonomy deleted.

    '; - $('.user-taxonomies-page h2:first').after($message); - - $this.parents().eq(3).remove(); - - setInterval(function () { - $('.user-taxonomies-page #message.below-h2').hide('slow', function () { - $('.user-taxonomies-page #message.below-h2').remove(); - }); - }, 3000); - - if (!$('#the-taxonomy-list tr').length) { - const $no_taxonomies = 'No Taxonomy found.'; - $('#the-taxonomy-list').append($no_taxonomies); - } - - } else { - const $error_div = '

    Taxonomy not deleted.

    '; - $('.user-taxonomies-page h2:first').after($error_div); - - setInterval(function () { - $('.user-taxonomies-page #message.below-h2').hide('slow', function () { - $('.user-taxonomies-page #message.below-h2').remove(); - }); - }, 3000); - - } - }, - error: function (resp_error) { - console.log(resp_error); - } - - }); - }); - - let delay = (function () { - let timer = 0; - return function (callback, ms) { - clearTimeout(timer); - timer = setTimeout(callback, ms); - }; - })(); - - /** - * Fetches the tag suggestion based on user input - */ - $('.user-profile-taxonomy').on('keyup', '.newtag', function () { - - $this = $(this); - $tag_input_value = $this.val().split(','); - $tag_input_value = $.trim($tag_input_value[$tag_input_value.length - 1]); - - if ($tag_input_value.length >= 2) { - delay(function () { - $tag_id = $this.attr('id'); - $tag_name = $tag_id.split('new-tag-user_tag_'); - $.ajax({ - 'type': 'post', - 'url': wp_ut_ajax_url, - 'data': { - 'action': 'ut_load_tag_suggestions', - 'tag': 'user_tag', - 'q': $tag_input_value, - 'taxonomy': $tag_name[1], - 'nonce': jQuery('#user-tags').val() - }, - 'success': function (res_data) { - $('.tag-suggestion').remove(); - if (res_data != '' && res_data != 0) { - $this.siblings('p.howto').before(res_data); - } - }, - 'error': function (res_error) { - console.log(res_error); - } - }); - }, 200); - } - else { - jQuery('.tag-suggestion').remove(); - } - }); - - //Tags UI - $('body').on('click', '.tag-suggestion li', function () { - $this = $(this); - $taxonomy_name = ''; - $term = $this.html(); - $tag_checklist = $this.parent().siblings('.tagchecklist'); - $num = ( $tag_checklist.length ); - - $taxonomy_id = $this.parent().siblings('.newtag').attr('id'); - if ($taxonomy_id) { - $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); - $taxonomy_name = $taxonomy_id[1]; - } - $tag_html = ''; - insert_tags($sibling, $taxonomy_name, $newtag_val[$i], $tag_html); - } - $('.tag-suggestion').remove(); - }); - - // Handle tag delete click. - $('body').on('click', '.ntdelbutton', function () { - let $this = $(this); - let parent = $this.parents().eq(1); - let $term = parent.find('.term-link').html(); - let $tags_list = $this.parents().eq(2).siblings('input[type="hidden"]'); - - let $current_tags = $tags_list.val(); - $current_tags = $current_tags.split(','); - - // Delete the tag from the list. - let $updated_tags = $.grep($current_tags, function (value) { - return value != $term; - }); - - // Store the updated list. - $tags_list.val($updated_tags.join(',')); - - // Remove tag holder. - parent.remove(); - - }); - - $('body').on('click', '.term-link', function (e) { - if ($(this).attr('href') != '#') return true; - else { - e.preventDefault(); - return false; - } - }); - - let doing_ajax = false; - - //Most Popular tag list - $('body').on('click', '.tagcloud-link.user-taxonomy', function (e) { - e.preventDefault(); - if (doing_ajax) { - return false; - } - doing_ajax = true; - if ($(this).parent().find('.the-tagcloud').length) { - $(this).parent().find('.the-tagcloud').remove(); - return true; - } - var id = $(this).attr('id'); - var tax = id.substr(id.indexOf("-") + 1); - $.post(ajaxurl, {'action': 'get-tagcloud', 'tax': tax}, function (r, stat) { - if (0 === r || 'success' != stat) - r = wpAjax.broken; - - r = jQuery('

    ' + r + '

    '); - $('a', r).click(function () { - $this = $(this); - $taxonomy_name = ''; - $term = $this.html(); - $tag_checklist = $this.parents().eq(1).siblings('.tagchecklist'); - $sibling = $this.parents().eq(1).siblings('.newtag'); - if ($tag_checklist.length === 0) { - $tag_checklist = $this.parents().eq(1).siblings('.taxonomy-wrapper').find('.tagchecklist'); - } - if ($sibling.length === 0) { - $sibling = $this.parents().eq(1).siblings('.taxonomy-wrapper').find('.newtag'); - } - $num = ( $tag_checklist.length ); - - $taxonomy_id = $sibling.attr('id'); - if ($taxonomy_id) { - $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); - $taxonomy_name = $taxonomy_id[1]; - } - $tag_html = '
    ' + $term + '= 2) { + delay(function () { + $tag_id = $this.attr('id'); + $tag_name = $tag_id.split('new-tag-user_tag_'); + $.ajax({ + 'type': 'post', + 'url': wp_ut_ajax_url, + 'data': { + 'action': 'ut_load_tag_suggestions', + 'tag': 'user_tag', + 'q': $tag_input_value, + 'taxonomy': $tag_name[1], + 'nonce': jQuery('#user-tags').val() + }, + 'success': function (res) { + $('.tag-suggestion').remove(); + if ( res.success && 'undefined' !== typeof (res.data)) { + $this.siblings('p.howto').before(res.data); + } + }, + 'error': function (res_error) { + console.log(res_error); + } + }); + }, 200); + } else { + jQuery('.tag-suggestion').remove(); + } + }); + + //Tags UI + $('body').on('click', '.tag-suggestion li', function () { + let $this = $(this); + let $taxonomy_name = ''; + let $term = $this.html(); + let $tag_checklist = $this.parent().siblings('.tagchecklist'); + let $num = ($tag_checklist.length); + + let $taxonomy_id = $this.parent().siblings('.newtag').attr('id'); + if ($taxonomy_id) { + $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); + $taxonomy_name = $taxonomy_id[1]; + } + let $tag_html = ''; + insert_tags($sibling, $taxonomy_name, $newtag_val[$i], $tag_html); + } + $('.tag-suggestion').remove(); + }); + + // Handle tag delete click. + $('body').on('click', '.ntdelbutton', function () { + let $this = $(this); + let parent = $this.parents().eq(1); + let $term = parent.find('.term-link').html(); + let $tags_list = $this.parents().eq(2).siblings('input[type="hidden"]'); + + let $current_tags = $tags_list.val(); + $current_tags = $current_tags.split(','); + + // Delete the tag from the list. + let $updated_tags = $.grep($current_tags, function (value) { + return value != $term; + }); + + // Store the updated list. + $tags_list.val($updated_tags.join(',')); + + // Remove tag holder. + parent.remove(); + + }); + + $('body').on('click', '.term-link', function (e) { + if ($(this).attr('href') != '#') return true; + else { + e.preventDefault(); + return false; + } + }); + + let doing_ajax = false; + + // Load most Popular tag list. + $('body').on('click', '.tagcloud-link.user-taxonomy', function (e) { + e.preventDefault(); + if (doing_ajax) { + return false; + } + doing_ajax = true; + if ($(this).parent().find('.the-tagcloud').length) { + $(this).parent().find('.the-tagcloud').remove(); + return true; + } + var id = $(this).attr('id'); + var tax = id.substr(id.indexOf("-") + 1); + $.post(ajaxurl, {'action': 'get-tagcloud', 'tax': tax}, function (r, stat) { + if (0 === r || 'success' != stat) + r = wpAjax.broken; + + r = jQuery('

    ' + r + '

    '); + $('a', r).click(function () { + let $this = $(this); + let $taxonomy_name = ''; + + let $term_name = $this.html(); + let $tag_checklist = $this.parents().eq(2).siblings('.tagchecklist'); + let $tax_input = $this.parents().eq(2).siblings('.newtag'); + + if ($tag_checklist.length === 0) { + $tag_checklist = $this.parents().eq(3).siblings('.taxonomy-wrapper').find('.tagchecklist'); + } + + if ($tax_input.length === 0) { + $tax_input = $this.parents().eq(3).siblings('.taxonomy-wrapper').find('.newtag'); + } + + let $num = ($tag_checklist.length); + + let $taxonomy_id = $tax_input.attr('id'); + if ($taxonomy_id) { + $taxonomy_id = $taxonomy_id.split('new-tag-user_tag_'); + $taxonomy_name = $taxonomy_id[1]; + } + + let $tag_html = ' + id="user-tags-name ); ?>" value=""/>

    'Invalid request.' ) ); } $nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ) ); @@ -591,7 +591,7 @@ function ut_load_tag_suggestions_callback() { $q = sanitize_text_field( wp_unslash( $_POST['q'] ) ); if ( ! wp_verify_nonce( $nonce, 'user-tags' ) ) { - return false; + wp_send_json_error( array( 'error' => 'Couldn\'t validate the request.' ) ); } $tags = get_terms( @@ -602,7 +602,7 @@ function ut_load_tag_suggestions_callback() { ) ); if ( empty( $tags ) || ! is_array( $tags ) ) { - return false; + wp_send_json_error(); } $tag_list = array(); foreach ( $tags as $tag ) { @@ -613,8 +613,9 @@ function ut_load_tag_suggestions_callback() { $input = preg_quote( trim( $q ), '~' ); $result = preg_grep( '~' . $input . '~i', $tag_list ); if ( empty( $result ) ) { - return; + wp_send_json_error(); } + ob_start(); ?>

    Date: Thu, 26 Nov 2020 18:24:26 +0530 Subject: [PATCH 21/23] Fixed template content: Users appear twice --- inc/class-usertags.php | 10 ++- readme.txt | 1 + templates/user-taxonomy-template.php | 121 ++++++++++++++------------- user-tags.php | 4 +- 4 files changed, 74 insertions(+), 62 deletions(-) diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 4335f8c..afdfb04 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -1,6 +1,7 @@ $description, ); update_site_option( 'ut_taxonomies', $ut_taxonomies ); - // a new taxonomy added, so flush rules required + // a new taxonomy added, so flush rules required. update_site_option( 'ut_new_taxonomy', true ); add_action( 'admin_notices', 'ut_taxonomy_created' ); @@ -326,9 +328,9 @@ function ut_register_taxonomies() { 'choose_from_most_used' => 'Choose from the most popular ' . $name, 'topic_count_text' => 'Choose from the most popular ' . $name, ), + 'show_in_rest' => false, 'rewrite' => array( - 'with_front' => true, - 'slug' => get_url_prefix() . $taxonomy_slug, // Use 'author' (default WP user slug). + 'slug' => get_url_prefix() . $taxonomy_slug, ), 'capabilities' => array( 'manage_terms' => 'edit_users', // Using 'edit_users' cap to keep this simple. diff --git a/readme.txt b/readme.txt index a585a33..fa293e4 100755 --- a/readme.txt +++ b/readme.txt @@ -35,6 +35,7 @@ Users can add new tags. * Fixed: Updated filter name 'ut_tepmplate_content' => ut_template_content : https://github.com/UmeshSingla/user-tags/issues/7 * Fixed: Return $template variable in functions.php https://github.com/UmeshSingla/user-tags/issues/8 * Fixed: string to array conversion. +* Fixed: Compat with WordPress >= 5.5 ( Fixed fatal error ) * Updated: Switch to div instead of table for new User Taxonomy screen = 1.2.7 = diff --git a/templates/user-taxonomy-template.php b/templates/user-taxonomy-template.php index b4e77df..9b33f8e 100755 --- a/templates/user-taxonomy-template.php +++ b/templates/user-taxonomy-template.php @@ -7,67 +7,76 @@ */ get_header(); ?> -
    -
    - - taxonomy ); - - /** - * Allows to filter user list before displaying it in template - * can be used for sorting the users as per username - */ - $users = apply_filters( 'ut_template_users', $users ); - $template_content = ''; - if ( ! empty( $users ) ) { - ?> -
    - -
    + + No Users found.

    ' ); - echo esc_html( $content ); - } - ?> -
    - -
    + $term_id = get_queried_object_id(); + $term = get_queried_object(); + + $users = get_objects_in_term( $term_id, $term->taxonomy ); + + /** + * Allows to filter user list before displaying it in template + * can be used for sorting the users as per username + */ + $users = apply_filters( 'ut_template_users', $users ); + $template_content = ''; + if ( ! empty( $users ) ) { + ?> +
    +
      + taxonomy ); + continue; + } + $c = '
    • ' . get_avatar( $user_id, '96' ) . + '

      ' . get_the_author_meta( 'display_name', $user_id ) . '

      +
      ' . wpautop( get_the_author_meta( 'description', $user_id ) ) . '
      +
    • '; + + $user = apply_filters_deprecated( 'ut_tepmplate_content', + array( + $c, + $user_id + ), + '1.2.8', + 'ut_template_content' + ); + + $user = apply_filters( 'ut_template_content', $c, $user_id ); + + $template_content .= $user; + } + echo $template_content; + ?> +
    +
    + No Users found.

    ' ); + echo esc_html( $content ); + } + ?> +
    + + +
    flush_rules( false ); - update_site_option( 'ut_new_taxonomy', 'FALSE' ); + delete_site_option( 'ut_new_taxonomy' ); } } From 8e24a0382d5d1331eaca3e693b800bab84bf3863 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Thu, 26 Nov 2020 18:34:35 +0530 Subject: [PATCH 22/23] Removed unused global variables --- inc/class-usertags.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/inc/class-usertags.php b/inc/class-usertags.php index afdfb04..62ead48 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -652,7 +652,7 @@ function admin_ajax() { * @author Garrett Eclipse */ function ut_users_filter_query( $query ) { - global $wpdb, $wp_query, $pagenow; + global $wpdb, $pagenow; if ( ! is_admin() || 'users.php' !== $pagenow ) { return $query; @@ -732,7 +732,6 @@ function ut_users_filter() { * @param $user_id */ function update_user_list( $user_id ) { - global $wpdb, $wp_actions; $taxonomies = get_object_taxonomies( 'user', 'object' ); $taxonomy_list = array(); From a4634fa6130859011a6fb83341da99d136fe9899 Mon Sep 17 00:00:00 2001 From: Umesh Kumar Date: Thu, 26 Nov 2020 19:10:01 +0530 Subject: [PATCH 23/23] Remove user filter feature from 1.8 --- inc/class-usertags.php | 4 ++-- readme.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/class-usertags.php b/inc/class-usertags.php index 62ead48..44e7198 100644 --- a/inc/class-usertags.php +++ b/inc/class-usertags.php @@ -45,8 +45,8 @@ public function __construct() { 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' ) ); +// add_filter( 'pre_user_query', array( $this, 'ut_users_filter_query' ) ); +// add_action( 'restrict_manage_users', array( $this, 'ut_users_filter' ) ); // Clear up related tags and taxonomies, when a user is deleted add_action( 'deleted_user', array( $this, 'update_user_list' ) ); diff --git a/readme.txt b/readme.txt index fa293e4..d03103a 100755 --- a/readme.txt +++ b/readme.txt @@ -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: 5.4 -Tested up to: 5.5 +Tested up to: 5.5.3 Stable tag: trunk Adds an admin option to allow creating User Taxonomy and create tags for different taxonomies.