- added ability to use custom metakey as profile slug;

- important: use for this case required usermeta which cannot be empty
This commit is contained in:
Mykyta Synelnikov
2023-09-11 15:41:29 +03:00
parent 77d3ad9b56
commit 0149605a36
5 changed files with 115 additions and 15 deletions
+19 -13
View File
@@ -7,41 +7,34 @@ if ( ! defined( 'ABSPATH' ) ) {
if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
/**
* Class Admin_Settings
* @package um\admin\core
*/
class Admin_Settings {
/**
* @var array
*/
public $settings_map;
/**
* @var array
*/
public $settings_structure;
/**
* @var
*/
private $previous_licenses;
/**
* @var
*/
private $need_change_permalinks;
private $gravatar_changed = false;
/**
* Admin_Settings constructor.
*/
@@ -668,6 +661,9 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
'permalink_base' => array(
'sanitize' => 'key',
),
'permalink_base_custom_meta' => array(
'sanitize' => 'text',
),
'display_name' => array(
'sanitize' => 'key',
),
@@ -1014,15 +1010,24 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
// translators: %s: Profile page URL
'tooltip' => sprintf( __( 'Here you can control the permalink structure of the user profile URL globally e.g. %s<strong>username</strong>/', 'ultimate-member' ), trailingslashit( um_get_core_page( 'user' ) ) ),
'options' => array(
'user_login' => __( 'Username', 'ultimate-member' ),
'name' => __( 'First and Last Name with \'.\'', 'ultimate-member' ),
'name_dash' => __( 'First and Last Name with \'-\'', 'ultimate-member' ),
'name_plus' => __( 'First and Last Name with \'+\'', 'ultimate-member' ),
'user_id' => __( 'User ID', 'ultimate-member' ),
'hash' => __( 'Unique hash string', 'ultimate-member' ),
'user_login' => __( 'Username', 'ultimate-member' ),
'name' => __( 'First and Last Name with \'.\'', 'ultimate-member' ),
'name_dash' => __( 'First and Last Name with \'-\'', 'ultimate-member' ),
'name_plus' => __( 'First and Last Name with \'+\'', 'ultimate-member' ),
'user_id' => __( 'User ID', 'ultimate-member' ),
'hash' => __( 'Unique hash string', 'ultimate-member' ),
'custom_meta' => __( 'Custom usermeta', 'ultimate-member' ),
),
'placeholder' => __( 'Select...', 'ultimate-member' ),
),
array(
'id' => 'permalink_base_custom_meta',
'type' => 'text',
'label' => __( 'Profile Permalink Base Custom Meta Key', 'ultimate-member' ),
'tooltip' => __( 'Specify the custom field meta key that you want to use as profile permalink base. Meta value should be unique.', 'ultimate-member' ),
'conditional' => array( 'permalink_base', '=', 'custom_meta' ),
'size' => 'medium',
),
array(
'id' => 'display_name',
'type' => 'select',
@@ -1048,6 +1053,7 @@ if ( ! class_exists( 'um\admin\core\Admin_Settings' ) ) {
'label' => __( 'Display Name Custom Field(s)', 'ultimate-member' ),
'tooltip' => __( 'Specify the custom field meta key or custom fields seperated by comma that you want to use to display users name on the frontend of your site', 'ultimate-member' ),
'conditional' => array( 'display_name', '=', 'field' ),
'size' => 'medium',
),
array(
'id' => 'author_redirect',
+1
View File
@@ -511,6 +511,7 @@ if ( ! class_exists( 'um\Config' ) ) {
'disable_restriction_pre_queries' => 0,
'uninstall_on_delete' => 0,
'permalink_base' => 'user_login',
'permalink_base_custom_meta' => '',
'display_name' => 'full_name',
'display_name_field' => '',
'author_redirect' => 1,
+13 -1
View File
@@ -257,6 +257,18 @@ if ( ! class_exists( 'um\core\Permalinks' ) ) {
global $wpdb;
$permalink_base = UM()->options()->get( 'permalink_base' );
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
} else {
$meta_key = $custom_meta;
}
} else {
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
}
$user_id = $wpdb->get_var(
$wpdb->prepare(
@@ -266,7 +278,7 @@ if ( ! class_exists( 'um\core\Permalinks' ) ) {
meta_value = %s
ORDER BY umeta_id ASC
LIMIT 1",
'um_user_profile_url_slug_' . $permalink_base,
$meta_key,
$slug
)
);
+12
View File
@@ -153,6 +153,14 @@ if ( ! class_exists( 'um\core\Rewrite' ) ) {
*/
public function locate_user_profile() {
$permalink_base = UM()->options()->get( 'permalink_base' );
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
}
}
if ( um_queried_user() && um_is_core_page( 'user' ) ) {
if ( 'user_login' === $permalink_base ) {
$user_id = username_exists( um_queried_user() );
@@ -204,6 +212,10 @@ if ( ! class_exists( 'um\core\Rewrite' ) ) {
$user_id = UM()->user()->user_exists_by_hash( um_queried_user() );
}
if ( 'custom_meta' === $permalink_base ) {
$user_id = UM()->user()->user_exists_by_custom_meta( um_queried_user() );
}
if ( in_array( $permalink_base, array( 'name', 'name_dash', 'name_dot', 'name_plus' ), true ) ) {
$user_id = UM()->user()->user_exists_by_name( um_queried_user() );
}
+70 -1
View File
@@ -706,7 +706,19 @@ if ( ! class_exists( 'um\core\User' ) ) {
public function get_profile_slug( $user_id ) {
// Permalink base
$permalink_base = UM()->options()->get( 'permalink_base' );
$profile_slug = get_user_meta( $user_id, "um_user_profile_url_slug_{$permalink_base}", true );
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
} else {
$meta_key = $custom_meta;
}
} else {
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
}
$profile_slug = get_user_meta( $user_id, $meta_key, true );
//get default username permalink if it's empty then return false
if ( empty( $profile_slug ) ) {
@@ -781,6 +793,17 @@ if ( ! class_exists( 'um\core\User' ) ) {
$user_in_url = $this->generate_user_hash( $user_id );
}
if ( 'custom_meta' === $permalink_base ) {
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
} else {
$user_in_url = rawurlencode( get_user_meta( $user_id, $custom_meta, true ) );
$user_in_url = apply_filters( 'um_custom_meta_permalink_base_generate_user_slug', $user_in_url, $user_id, $custom_meta );
}
}
// Username
if ( 'user_login' === $permalink_base ) {
@@ -2391,6 +2414,52 @@ if ( ! class_exists( 'um\core\User' ) ) {
return false;
}
/**
* @param string $slug
*
* @return bool|int
*/
public function user_exists_by_custom_meta( $slug ) {
$permalink_base = UM()->options()->get( 'permalink_base' );
$custom_meta = UM()->options()->get( 'permalink_base_custom_meta' );
if ( empty( $custom_meta ) ) {
// Set default permalink base if custom meta is empty.
$permalink_base = 'user_login';
$meta_key = 'um_user_profile_url_slug_' . $permalink_base;
} else {
$meta_key = $custom_meta;
}
$raw_value = $slug;
// Search by Profile Slug
$args = array(
'fields' => array( 'ID' ),
'meta_query' => array(
'relation' => 'OR',
array(
'key' => $meta_key,
'value' => strtolower( $raw_value ),
'compare' => '=',
),
array(
'key' => 'um_user_profile_url_slug_' . $permalink_base,
'value' => strtolower( $raw_value ),
'compare' => '=',
),
),
);
$ids = new \WP_User_Query( $args );
if ( $ids->total_users > 0 ) {
$um_user_query = current( $ids->get_results() );
return $um_user_query->ID;
}
return false;
}
/**
* This method checks if a user exists or not in your site based on the user email as username
*