mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-18 14:13:46 +09:00
Merge pull request #1190 from ultimatemember/feature/sorting_data_type
Sorting data type and order
This commit is contained in:
@@ -393,6 +393,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
|
||||
}
|
||||
update_post_meta( $directory_id, '_um_sortby_custom', '' );
|
||||
update_post_meta( $directory_id, '_um_sortby_custom_label', '' );
|
||||
update_post_meta( $directory_id, '_um_sortby_custom_type', '' );
|
||||
update_post_meta( $directory_id, '_um_sortby_custom_order', '' );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,36 @@
|
||||
<?php
|
||||
namespace um\core;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
|
||||
|
||||
/**
|
||||
* Class Member_Directory_Meta
|
||||
* @package um\core
|
||||
*/
|
||||
class Member_Directory_Meta extends Member_Directory {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $joins = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @var array
|
||||
*/
|
||||
var $joins = array();
|
||||
var $where_clauses = array();
|
||||
public $where_clauses = array();
|
||||
|
||||
var $roles = array();
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $roles = array();
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
var $roles_in_query = false;
|
||||
|
||||
var $general_meta_joined = false;
|
||||
@@ -35,7 +44,7 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
/**
|
||||
* Member_Directory_Meta constructor.
|
||||
*/
|
||||
function __construct() {
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
|
||||
add_action( 'updated_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
|
||||
@@ -144,10 +153,10 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
global $wpdb;
|
||||
|
||||
$result = $wpdb->get_var( $wpdb->prepare(
|
||||
"SELECT umeta_id
|
||||
FROM {$wpdb->prefix}um_metadata
|
||||
WHERE user_id = %d AND
|
||||
um_key = %s
|
||||
"SELECT umeta_id
|
||||
FROM {$wpdb->prefix}um_metadata
|
||||
WHERE user_id = %d AND
|
||||
um_key = %s
|
||||
LIMIT 1",
|
||||
$object_id,
|
||||
$meta_key
|
||||
@@ -501,7 +510,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$profile_photo_where = '';
|
||||
if ( $directory_data['has_profile_photo'] == 1 ) {
|
||||
$profile_photo_where = " AND umm_general.um_value LIKE '%s:13:\"profile_photo\";b:1;%'";
|
||||
@@ -517,7 +525,7 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
$this->joins[] = "LEFT JOIN {$wpdb->prefix}um_metadata umm_general ON umm_general.user_id = u.ID";
|
||||
$this->general_meta_joined = true;
|
||||
}
|
||||
$this->where_clauses[] = "( umm_general.um_key = 'um_member_directory_data' AND
|
||||
$this->where_clauses[] = "( umm_general.um_key = 'um_member_directory_data' AND
|
||||
umm_general.um_value LIKE '%s:14:\"account_status\";s:8:\"approved\";%' AND umm_general.um_value LIKE '%s:15:\"hide_in_members\";b:0;%'{$profile_photo_where}{$cover_photo_where} )";
|
||||
} else {
|
||||
if ( ! empty( $cover_photo_where ) || ! empty( $profile_photo_where ) ) {
|
||||
@@ -578,7 +586,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( ! empty( $_POST['search'] ) ) {
|
||||
$search_line = trim( stripslashes( sanitize_text_field( $_POST['search'] ) ) );
|
||||
|
||||
@@ -600,7 +607,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
$this->is_search = true;
|
||||
}
|
||||
|
||||
|
||||
//filters
|
||||
$filter_query = array();
|
||||
if ( ! empty( $directory_data['search_fields'] ) ) {
|
||||
@@ -638,7 +644,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//unable default filter in case if we select other filters in frontend filters
|
||||
//if ( empty( $this->custom_filters_in_query ) ) {
|
||||
$default_filters = array();
|
||||
@@ -676,7 +681,7 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
|
||||
if ( ! empty( UM()->builtin()->saved_fields ) ) {
|
||||
foreach ( UM()->builtin()->saved_fields as $key => $data ) {
|
||||
if ( $key == '_um_last_login' ) {
|
||||
if ( '_um_last_login' === $key ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -694,12 +699,29 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
// handle sorting options
|
||||
// sort members by
|
||||
if ( $sortby == $directory_data['sortby_custom'] || in_array( $sortby, $custom_sort ) ) {
|
||||
$custom_sort_order = ! empty( $directory_data['sortby_custom_order'] ) ? $directory_data['sortby_custom_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}' )";
|
||||
|
||||
$custom_sort_type = apply_filters( 'um_member_directory_custom_sorting_type', 'CHAR', $sortby, $directory_data );
|
||||
$meta_query = new \WP_Meta_Query();
|
||||
$custom_sort_type = ! empty( $directory_data['sortby_custom_type'] ) ? $meta_query->get_cast_for_type( $directory_data['sortby_custom_type'] ) : 'CHAR';
|
||||
if ( ! empty( $directory_data['sorting_fields'] ) ) {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification -- already verified here
|
||||
$sorting = sanitize_text_field( $_POST['sorting'] );
|
||||
$sorting_fields = maybe_unserialize( $directory_data['sorting_fields'] );
|
||||
|
||||
$this->sql_order = " ORDER BY CAST( umm_sort.um_value AS {$custom_sort_type} ) {$order} ";
|
||||
foreach ( $sorting_fields as $field ) {
|
||||
if ( isset( $field[ $sorting ] ) ) {
|
||||
$custom_sort_type = ! empty( $field['type'] ) ? $meta_query->get_cast_for_type( $field['type'] ) : 'CHAR';
|
||||
$custom_sort_order = $field['order'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** This filter is documented in includes/core/class-member-directory.php */
|
||||
$custom_sort_type = apply_filters( 'um_member_directory_custom_sorting_type', $custom_sort_type, $sortby, $directory_data );
|
||||
|
||||
$this->sql_order = " ORDER BY CAST( umm_sort.um_value AS {$custom_sort_type} ) {$custom_sort_order} ";
|
||||
|
||||
} elseif ( count( $numeric_sorting_keys ) && in_array( $sortby, $numeric_sorting_keys ) ) {
|
||||
|
||||
@@ -802,7 +824,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
|
||||
|
||||
$this->sql_order = apply_filters( 'um_modify_sortby_parameter_meta', $this->sql_order, $sortby );
|
||||
|
||||
|
||||
$profiles_per_page = $directory_data['profiles_per_page'];
|
||||
if ( UM()->mobile()->isMobile() && isset( $directory_data['profiles_per_page_mobile'] ) ) {
|
||||
$profiles_per_page = $directory_data['profiles_per_page_mobile'];
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
<?php
|
||||
namespace um\core;
|
||||
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) exit;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
|
||||
|
||||
/**
|
||||
* Class Member_Directory
|
||||
* @package um\core
|
||||
*/
|
||||
class Member_Directory {
|
||||
|
||||
|
||||
/**
|
||||
* Member Directory Views
|
||||
*
|
||||
@@ -28,6 +26,10 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
*/
|
||||
var $sort_fields = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
var $sort_data_types = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
@@ -210,7 +212,6 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
if ( ! empty( $value ) && in_array( $key, array( '_um_view_types', '_um_roles', '_um_roles_can_search', '_um_roles_can_filter' ), true ) ) {
|
||||
$value = array_keys( $value );
|
||||
} elseif ( '_um_search_filters' === $key ) {
|
||||
|
||||
$temp_value = array();
|
||||
|
||||
if ( ! empty( $value ) ) {
|
||||
@@ -264,8 +265,17 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
if ( ! empty( $other_data[ $k ]['label'] ) ) {
|
||||
$metalabel = wp_strip_all_tags( $other_data[ $k ]['label'] );
|
||||
}
|
||||
if ( ! empty( $other_data[ $k ]['data_type'] ) ) {
|
||||
$data_type = sanitize_text_field( $other_data[ $k ]['data_type'] );
|
||||
}
|
||||
if ( ! empty( $other_data[ $k ]['order'] ) ) {
|
||||
$order = sanitize_text_field( $other_data[ $k ]['order'] );
|
||||
}
|
||||
$row = array(
|
||||
$metakey => ! empty( $metalabel ) ? $metalabel : $metakey,
|
||||
$metakey => $metakey,
|
||||
'label' => ! empty( $metalabel ) ? $metalabel : $metakey,
|
||||
'type' => ! empty( $data_type ) ? $data_type : '',
|
||||
'order' => ! empty( $order ) ? $order : '',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -275,6 +285,10 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
$value = sanitize_text_field( $value );
|
||||
} elseif ( '_um_sortby_custom_label' === $key ) {
|
||||
$value = wp_strip_all_tags( $value );
|
||||
} elseif ( '_um_sortby_custom_type' === $key ) {
|
||||
$value = sanitize_text_field( $value );
|
||||
} elseif ( '_um_sortby_custom_order' === $key ) {
|
||||
$value = sanitize_text_field( $value );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,6 +322,20 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
|
||||
$this->sorting_supported_fields = apply_filters( 'um_members_directory_custom_field_types_supported_sorting', array( 'number' ) );
|
||||
|
||||
$this->sort_data_types = array(
|
||||
'CHAR' => __( 'CHAR', 'ultimate-member' ),
|
||||
'NUMERIC' => __( 'NUMERIC', 'ultimate-member' ),
|
||||
'BINARY' => __( 'BINARY', 'ultimate-member' ),
|
||||
'DATE' => __( 'DATE', 'ultimate-member' ),
|
||||
'DATETIME' => __( 'DATETIME', 'ultimate-member' ),
|
||||
'DECIMAL' => __( 'DECIMAL', 'ultimate-member' ),
|
||||
'SIGNED' => __( 'SIGNED', 'ultimate-member' ),
|
||||
'TIME' => __( 'TIME', 'ultimate-member' ),
|
||||
'UNSIGNED' => __( 'UNSIGNED', 'ultimate-member' ),
|
||||
);
|
||||
|
||||
$this->sort_data_types = apply_filters( 'um_members_directory_sort_data_types', $this->sort_data_types );
|
||||
|
||||
if ( ! empty( UM()->builtin()->saved_fields ) ) {
|
||||
foreach ( UM()->builtin()->saved_fields as $key => $data ) {
|
||||
if ( $key == '_um_last_login' ) {
|
||||
@@ -1422,23 +1450,64 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
|
||||
unset( $this->query_args['order'] );
|
||||
|
||||
} elseif ( ( ! empty( $directory_data['sortby_custom'] ) && $sortby == $directory_data['sortby_custom'] ) || in_array( $sortby, $custom_sort ) ) {
|
||||
$custom_sort_order = ! empty( $directory_data['sortby_custom_order'] ) ? $directory_data['sortby_custom_order'] : 'ASC';
|
||||
|
||||
$custom_sort_type = apply_filters( 'um_member_directory_custom_sorting_type', 'CHAR', $sortby, $directory_data );
|
||||
$meta_query = new \WP_Meta_Query();
|
||||
$custom_sort_type = ! empty( $directory_data['sortby_custom_type'] ) ? $meta_query->get_cast_for_type( $directory_data['sortby_custom_type'] ) : 'CHAR';
|
||||
if ( ! empty( $directory_data['sorting_fields'] ) ) {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification -- already verified here
|
||||
$sorting = sanitize_text_field( $_POST['sorting'] );
|
||||
$sorting_fields = maybe_serialize( $directory_data['sorting_fields'] );
|
||||
|
||||
foreach ( $sorting_fields as $field ) {
|
||||
if ( isset( $field[ $sorting ] ) ) {
|
||||
$custom_sort_type = ! empty( $field['type'] ) ? $meta_query->get_cast_for_type( $field['type'] ) : 'CHAR';
|
||||
$custom_sort_order = $field['order'];
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Filters the sorting MySQL type in member directory custom sorting query.
|
||||
*
|
||||
* Note: Possible MySQL types are BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|DECIMAL
|
||||
*
|
||||
* @since 2.1.3
|
||||
* @hook um_member_directory_custom_sorting_type
|
||||
*
|
||||
* @param {string} $custom_sort_type MySQL type to cast meta_value. 'CHAR' is default.
|
||||
* @param {string} $sortby meta_key used for sorting.
|
||||
* @param {array} $directory_data Member directory data.
|
||||
*
|
||||
* @return {string} MySQL type to cast meta_value.
|
||||
* @example <caption>Change type to DATE by the directory ID and mete_key.</caption>
|
||||
* function my_um_member_directory_custom_sorting_type( $custom_sort_type, $sortby, $directory_data ) {
|
||||
* if ( '{selected member directory ID}' == $directory_data['form_id'] && '{custom_date_key}' === $sortby ) {
|
||||
* $custom_sort_type = 'DATE';
|
||||
* }
|
||||
*
|
||||
* return $custom_sort_type;
|
||||
* }
|
||||
* add_filter( 'um_member_directory_custom_sorting_type', 'my_um_member_directory_custom_sorting_type', 10, 3 );
|
||||
*/
|
||||
$custom_sort_type = apply_filters( 'um_member_directory_custom_sorting_type', $custom_sort_type, $sortby, $directory_data );
|
||||
|
||||
$this->query_args['meta_query'][] = array(
|
||||
'relation' => 'OR',
|
||||
'relation' => 'OR',
|
||||
$sortby . '_cs' => array(
|
||||
'key' => $sortby,
|
||||
'compare' => 'EXISTS',
|
||||
'type' => $custom_sort_type,
|
||||
'key' => $sortby,
|
||||
'compare' => 'EXISTS',
|
||||
'type' => $custom_sort_type,
|
||||
),
|
||||
array(
|
||||
'key' => $sortby,
|
||||
'compare' => 'NOT EXISTS',
|
||||
)
|
||||
'key' => $sortby,
|
||||
'compare' => 'NOT EXISTS',
|
||||
),
|
||||
);
|
||||
|
||||
$this->query_args['orderby'] = array( $sortby . '_cs' => 'ASC', 'user_login' => 'ASC' );
|
||||
$this->query_args['orderby'] = array(
|
||||
$sortby . '_cs' => $custom_sort_order,
|
||||
'user_login' => 'ASC',
|
||||
);
|
||||
|
||||
} else {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user