From 72d58f709dabbf427b58f5ed06b5e6d8f80984ee Mon Sep 17 00:00:00 2001 From: Nikita Sinelnikov Date: Thu, 9 Jun 2022 17:17:15 +0300 Subject: [PATCH] - fixed #1008; - fixed #1005; - fixed #1006; - fixed #1010; --- includes/core/class-access.php | 9 +- includes/core/class-member-directory-meta.php | 34 ++++++++ includes/core/class-member-directory.php | 82 ++++++++++++++++++- includes/core/class-query.php | 1 + 4 files changed, 122 insertions(+), 4 deletions(-) diff --git a/includes/core/class-access.php b/includes/core/class-access.php index ec137246..05f3c6a6 100644 --- a/includes/core/class-access.php +++ b/includes/core/class-access.php @@ -49,9 +49,6 @@ if ( ! class_exists( 'um\core\Access' ) ) { // NEW HOOKS - // callbacks for changing terms query - add_action( 'pre_get_terms', array( &$this, 'exclude_hidden_terms_query' ), 99, 1 ); - // Change recent posts widget query add_filter( 'widget_posts_args', array( &$this, 'exclude_restricted_posts_widget' ), 99, 1 ); // Exclude pages displayed by wp_list_pages function @@ -114,6 +111,9 @@ if ( ! class_exists( 'um\core\Access' ) ) { * Rollback function for old business logic to avoid security enhancements with 404 errors */ function disable_restriction_pre_queries() { + // callbacks for changing terms query + add_action( 'pre_get_terms', array( &$this, 'exclude_hidden_terms_query' ), 99, 1 ); + if ( ! UM()->options()->get( 'disable_restriction_pre_queries' ) ) { return; } @@ -728,6 +728,9 @@ if ( ! class_exists( 'um\core\Access' ) ) { } elseif ( '1' == $restriction['_um_restrict_by_custom_message'] ) { $content = ! empty( $restriction['_um_restrict_custom_message'] ) ? stripslashes( $restriction['_um_restrict_custom_message'] ) : ''; } + + // translators: %s: Restricted post message. + $content = sprintf( __( '%s', 'ultimate-member' ), $content ); } return $content; diff --git a/includes/core/class-member-directory-meta.php b/includes/core/class-member-directory-meta.php index 66eb62a3..c2f999f6 100644 --- a/includes/core/class-member-directory-meta.php +++ b/includes/core/class-member-directory-meta.php @@ -663,6 +663,25 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) { } } + $numeric_sorting_keys = array(); + + if ( ! empty( UM()->builtin()->saved_fields ) ) { + foreach ( UM()->builtin()->saved_fields as $key => $data ) { + if ( $key == '_um_last_login' ) { + continue; + } + + if ( isset( $data['type'] ) && 'number' === $data['type'] ) { + if ( array_key_exists( $key . '_desc', $this->sort_fields ) ) { + $numeric_sorting_keys[] = $key . '_desc'; + } + if ( array_key_exists( $key . '_asc', $this->sort_fields ) ) { + $numeric_sorting_keys[] = $key . '_asc'; + } + } + } + } + // handle sorting options // sort members by if ( $sortby == $directory_data['sortby_custom'] || in_array( $sortby, $custom_sort ) ) { @@ -673,6 +692,21 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) { $this->sql_order = " ORDER BY CAST( umm_sort.um_value AS {$custom_sort_type} ) {$order} "; + } elseif ( count( $numeric_sorting_keys ) && in_array( $sortby, $numeric_sorting_keys ) ) { + + if ( strstr( $sortby, '_desc' ) ) { + $sortby = str_replace( '_desc', '', $sortby ); + $order = 'DESC'; + } + + if ( strstr( $sortby, '_asc' ) ) { + $sortby = str_replace( '_asc', '', $sortby ); + $order = 'ASC'; + } + + $this->joins[] = "LEFT JOIN {$wpdb->prefix}um_metadata umm_sort ON ( umm_sort.user_id = u.ID AND umm_sort.um_key = '{$sortby}' )"; + $this->sql_order = " ORDER BY CAST( umm_sort.um_value AS SIGNED ) {$order}, u.user_registered DESC "; + } elseif ( 'username' == $sortby ) { $this->sql_order = " ORDER BY u.user_login {$order} "; diff --git a/includes/core/class-member-directory.php b/includes/core/class-member-directory.php index e2c7290d..b5f128a5 100644 --- a/includes/core/class-member-directory.php +++ b/includes/core/class-member-directory.php @@ -51,6 +51,9 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { var $filter_supported_fields = array(); + var $sorting_supported_fields = array(); + + var $filter_types = array(); @@ -302,9 +305,32 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { 'display_name' => __( 'Display name', 'ultimate-member' ), 'last_first_name' => __( 'Last & First name', 'ultimate-member' ), 'last_login' => __( 'Last login', 'ultimate-member' ), - ) ); + $this->sorting_supported_fields = apply_filters( 'um_members_directory_custom_field_types_supported_sorting', array( 'number' ) ); + + if ( ! empty( UM()->builtin()->saved_fields ) ) { + foreach ( UM()->builtin()->saved_fields as $key => $data ) { + if ( $key == '_um_last_login' ) { + continue; + } + + if ( isset( $data['type'] ) && in_array( $data['type'], $this->sorting_supported_fields ) ) { + if ( isset( $data['title'] ) && array_search( sprintf( __( '%s DESC', 'ultimate-member' ), $data['title'] ), $this->sort_fields ) !== false ) { + $data['title'] = $data['title'] . ' (' . $key . ')'; + } + + $title = isset( $data['title'] ) ? $data['title'] : ( isset( $data['label'] ) ? $data['label'] : '' ); + if ( empty( $title ) ) { + continue; + } + + $this->sort_fields[ $key . '_desc' ] = sprintf( __( '%s DESC', 'ultimate-member' ), $title ); + $this->sort_fields[ $key . '_asc' ] = sprintf( __( '%s ASC', 'ultimate-member' ), $title ); + } + } + } + asort( $this->sort_fields ); $this->default_sorting = apply_filters( 'um_members_directory_default_sort', array_merge( $this->sort_fields, array( @@ -1262,6 +1288,25 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { } } + $numeric_sorting_keys = array(); + + if ( ! empty( UM()->builtin()->saved_fields ) ) { + foreach ( UM()->builtin()->saved_fields as $key => $data ) { + if ( $key == '_um_last_login' ) { + continue; + } + + if ( isset( $data['type'] ) && 'number' === $data['type'] ) { + if ( array_key_exists( $key . '_desc', $this->sort_fields ) ) { + $numeric_sorting_keys[] = $key . '_desc'; + } + if ( array_key_exists( $key . '_asc', $this->sort_fields ) ) { + $numeric_sorting_keys[] = $key . '_asc'; + } + } + } + } + if ( 'username' == $sortby ) { $this->query_args['orderby'] = 'user_login'; @@ -1332,6 +1377,41 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) { $this->query_args['orderby'] = array( 'last_name_c' => 'ASC', 'first_name_c' => 'ASC' ); unset( $this->query_args['order'] ); + } elseif ( count( $numeric_sorting_keys ) && in_array( $sortby, $numeric_sorting_keys ) ) { + + $order = 'DESC'; + if ( strstr( $sortby, '_desc' ) ) { + $sortby = str_replace( '_desc', '', $sortby ); + $order = 'DESC'; + } + + if ( strstr( $sortby, '_asc' ) ) { + $sortby = str_replace( '_asc', '', $sortby ); + $order = 'ASC'; + } + + $this->query_args['meta_query'] = array_merge( + $this->query_args['meta_query'], + array( + array( + 'relation' => 'OR', + array( + 'key' => $sortby, + 'compare' => 'EXISTS', + 'type' => 'NUMERIC', + ), + $sortby . '_ns' => array( + 'key' => $sortby, + 'compare' => 'NOT EXISTS', + 'type' => 'NUMERIC', + ), + ), + ) + ); + + $this->query_args['orderby'] = array( $sortby . '_ns' => $order, 'user_registered' => 'DESC' ); + unset( $this->query_args['order'] ); + } elseif ( ( ! empty( $directory_data['sortby_custom'] ) && $sortby == $directory_data['sortby_custom'] ) || in_array( $sortby, $custom_sort ) ) { $custom_sort_type = apply_filters( 'um_member_directory_custom_sorting_type', 'CHAR', $sortby, $directory_data ); diff --git a/includes/core/class-query.php b/includes/core/class-query.php index d6814f7f..7061be61 100644 --- a/includes/core/class-query.php +++ b/includes/core/class-query.php @@ -229,6 +229,7 @@ if ( ! class_exists( 'um\core\Query' ) ) { foreach ( $users->results as $user ) { update_user_meta( $user, 'account_status', 'approved' ); } + return 0; } else { $args['meta_query'][] = array(array('key' => 'account_status','value' => $status,'compare' => '=')); }