- code formatted;

- added handlers when you create UM custom fields;
This commit is contained in:
nikitasinelnikov
2020-01-06 11:05:32 +02:00
parent 1f1c7985d6
commit e965b1b3c1
7 changed files with 389 additions and 184 deletions
+8
View File
@@ -150,6 +150,8 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
unset( $fields[ $id ]['in_group'] );
unset( $fields[ $id ]['position'] );
do_action( 'um_add_new_field', $id );
update_option( 'um_fields', $fields );
}
@@ -248,6 +250,9 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
$fields = UM()->builtin()->saved_fields;
if ( isset( $fields[ $id ] ) ) {
unset( $fields[ $id ] );
do_action( 'um_delete_custom_field', $id );
update_option( 'um_fields', $fields );
}
}
@@ -354,6 +359,9 @@ if ( ! class_exists( 'um\core\Fields' ) ) {
unset( $all_fields[ $new_metakey ]['in_group'] );
unset( $all_fields[ $new_metakey ]['position'] );
do_action( 'um_add_new_field', $new_metakey );
UM()->query()->update_attr( 'custom_fields', $form_id, $fields );
update_option( 'um_fields', $all_fields );
}
+93 -24
View File
@@ -33,9 +33,92 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
add_action( 'updated_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
add_action( 'added_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
add_action( 'deleted_user_meta', array( &$this, 'on_delete_usermeta' ), 10, 4 );
add_action( 'um_add_new_field', array( &$this, 'on_new_field_added' ), 10, 1 );
add_action( 'um_delete_custom_field', array( &$this, 'on_delete_custom_field' ), 10, 1 );
}
/**
* Delete custom field and metakey from UM usermeta table
*
* @param $metakey
*/
function on_delete_custom_field( $metakey ) {
$metakeys = get_option( 'um_usermeta_fields', array() );
if ( in_array( $metakey, $metakeys ) ) {
unset( $metakeys[ array_search( $metakey, $metakeys ) ] );
global $wpdb;
$wpdb->delete(
"{$wpdb->prefix}um_metadata",
array(
'um_key' => $metakey
),
array(
'%s'
)
);
update_option( 'um_usermeta_fields', $metakeys );
}
}
/**
* Add metakey to usermeta fields
*
* @param $metakey
*/
function on_new_field_added( $metakey ) {
$metakeys = get_option( 'um_usermeta_fields', array() );
if ( ! in_array( $metakey, $metakeys ) ) {
$metakeys[] = $metakey;
update_option( 'um_usermeta_fields', $metakeys );
}
}
/**
* When you delete usermeta - remove row from um_metadata
*
* @param int|array $meta_ids
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_delete_usermeta( $meta_ids, $object_id, $meta_key, $_meta_value ) {
$metakeys = get_option( 'um_usermeta_fields', array() );
if ( ! in_array( $meta_key, $metakeys ) ) {
return;
}
global $wpdb;
$wpdb->delete(
"{$wpdb->prefix}um_metadata",
array(
'user_id' => $object_id,
'um_key' => $meta_key
),
array(
'%d',
'%s'
)
);
}
/**
* When you add/update usermeta - add/update row from um_metadata
*
* @param int $meta_id
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_update_usermeta( $meta_id, $object_id, $meta_key, $_meta_value ) {
$metakeys = get_option( 'um_usermeta_fields', array() );
@@ -45,7 +128,15 @@ 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 LIMIT 1", $object_id, $meta_key ) );
$result = $wpdb->get_var( $wpdb->prepare(
"SELECT umeta_id
FROM {$wpdb->prefix}um_metadata
WHERE user_id = %d AND
um_key = %s
LIMIT 1",
$object_id,
$meta_key
) );
if ( empty( $result ) ) {
$wpdb->insert(
@@ -68,8 +159,7 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
'um_value' => $_meta_value,
),
array(
'umeta_id' => $result,
'um_key' => $meta_key,
'umeta_id' => $result,
),
array(
'%s',
@@ -79,22 +169,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
)
);
}
// $wpdb->update(
// "{$wpdb->prefix}um_metadata",
// array(
// $meta_key => $_meta_value,
// ),
// array(
// 'user_id' => $object_id,
// ),
// array(
// '%s'
// ),
// array(
// '%d'
// )
// );
}
@@ -140,11 +214,6 @@ if ( ! class_exists( 'um\core\Member_Directory_Meta' ) ) {
$profile_photo_where = '';
if ( $directory_data['has_profile_photo'] == 1 ) {
// $gravatars_query = '';
// if ( UM()->options()->get( 'use_gravatars' ) ) {
// $gravatars_query = " OR ( umm_general.synced_gravatar_hashed_id != '' AND umm_general.synced_gravatar_hashed_id IS NOT NULL )";
// }
$profile_photo_where = " AND umm_general.um_value LIKE '%s:13:\"profile_photo\";b:1;%'";
}
+16 -92
View File
@@ -95,40 +95,9 @@ if ( ! class_exists( 'um\core\Member_Directory' ) ) {
add_filter( 'init', array( &$this, 'init_filter_types' ), 2 );
add_action( 'template_redirect', array( &$this, 'access_members' ), 555 );
//add_action( 'updated_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
}
function on_update_usermeta( $meta_id, $object_id, $meta_key, $_meta_value ) {
//$object_id //userID
//$meta_key // meta_key
//$_meta_value // value
$metakeys = get_option( 'um_usermeta_fields', array() );
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$wpdb->prefix}um_followers (
user_id int(11) unsigned NOT NULL auto_increment,
time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
user_id1 int(11) unsigned NOT NULL,
user_id2 int(11) unsigned NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
/**
* Getting member directory post ID via hash
* Hash is unique attr, which we use visible at frontend
@@ -948,9 +917,9 @@ PRIMARY KEY (id)
}
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( array(
'key' => 'account_status',
'value' => 'approved',
'compare' => '='
'key' => 'um_member_directory_data',
'value' => 's:14:"account_status";s:8:"approved";',
'compare' => 'LIKE'
) ) );
}
@@ -987,38 +956,11 @@ PRIMARY KEY (id)
return;
}
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'hide_in_members',
'compare' => 'NOT EXISTS'
),
);
if ( __( 'Yes', 'ultimate-member' ) == 'Yes' ) {
$meta_query[] = array(
'key' => 'hide_in_members',
'value' => 'Yes',
'compare' => 'NOT LIKE',
);
} else {
$meta_query[] = array(
'relation' => 'AND',
array(
'key' => 'hide_in_members',
'value' => __( 'Yes', 'ultimate-member' ),
'compare' => 'NOT LIKE'
),
array(
'key' => 'hide_in_members',
'value' => 'Yes',
'compare' => 'NOT LIKE'
),
);
}
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( $meta_query ) );
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( array(
'key' => 'um_member_directory_data',
'value' => 's:15:"hide_in_members";b:0;',
'compare' => 'LIKE'
) ) );
}
@@ -1063,29 +1005,11 @@ PRIMARY KEY (id)
*/
function show_only_with_avatar( $directory_data ) {
if ( $directory_data['has_profile_photo'] == 1 ) {
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'synced_profile_photo', // addons
'value' => '',
'compare' => '!='
),
array(
'key' => 'profile_photo', // from upload form
'value' => '',
'compare' => '!='
)
);
if ( UM()->options()->get( 'use_gravatars' ) ) {
$meta_query[] = array(
'key' => 'synced_gravatar_hashed_id', // gravatar
'value' => '',
'compare' => '!='
);
}
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( $meta_query ) );
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( array(
'key' => 'um_member_directory_data',
'value' => 's:13:"profile_photo";b:1;',
'compare' => 'LIKE'
) ) );
}
}
@@ -1098,9 +1022,9 @@ PRIMARY KEY (id)
function show_only_with_cover( $directory_data ) {
if ( $directory_data['has_cover_photo'] == 1 ) {
$this->query_args['meta_query'] = array_merge( $this->query_args['meta_query'], array( array(
'key' => 'cover_photo',
'value' => '',
'compare' => '!='
'key' => 'um_member_directory_data',
'value' => 's:11:"cover_photo";b:1;',
'compare' => 'LIKE'
) ) );
}
}
+148
View File
@@ -93,6 +93,154 @@ if ( ! class_exists( 'um\core\User' ) ) {
} else {
add_action( 'delete_user', array( &$this, 'delete_user_handler' ), 10, 1 );
}
add_action( 'updated_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
add_action( 'added_user_meta', array( &$this, 'on_update_usermeta' ), 10, 4 );
add_action( 'deleted_user_meta', array( &$this, 'on_delete_usermeta' ), 10, 4 );
}
/**
* When you delete usermeta connected with member directory - reset it to default value
*
* @param int|array $meta_ids
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_delete_usermeta( $meta_ids, $object_id, $meta_key, $_meta_value ) {
$metakeys = array( 'account_status', 'hide_in_members', 'synced_gravatar_hashed_id', 'synced_profile_photo', 'profile_photo', 'cover_photo', '_um_verified' );
if ( ! in_array( $meta_key, $metakeys ) ) {
return;
}
$md_data = get_user_meta( $object_id, 'um_member_directory_data', true );
if ( empty( $md_data ) ) {
$md_data = array(
'account_status' => 'approved',
'hide_in_members' => false,
'profile_photo' => false,
'cover_photo' => false,
'verified' => false,
);
}
switch ( $meta_key ) {
case 'account_status':
$md_data['account_status'] = 'approved';
break;
case 'hide_in_members':
$md_data['hide_in_members'] = false;
break;
case 'synced_gravatar_hashed_id':
if ( UM()->options()->get( 'use_gravatars' ) ) {
$profile_photo = get_user_meta( $object_id, 'profile_photo', true );
$synced_profile_photo = get_user_meta( $object_id, 'synced_profile_photo', true );
$md_data['profile_photo'] = ! empty( $profile_photo ) || ! empty( $synced_profile_photo );
}
break;
case 'synced_profile_photo':
$profile_photo = get_user_meta( $object_id, 'profile_photo', true );
$synced_gravatar_hashed_id = false;
if ( UM()->options()->get( 'use_gravatars' ) ) {
$synced_gravatar_hashed_id = get_user_meta( $object_id, 'synced_gravatar_hashed_id', true );
}
$md_data['profile_photo'] = ! empty( $profile_photo ) || ! empty( $synced_gravatar_hashed_id );
break;
case 'profile_photo':
$synced_profile_photo = get_user_meta( $object_id, 'synced_profile_photo', true );
$synced_gravatar_hashed_id = false;
if ( UM()->options()->get( 'use_gravatars' ) ) {
$synced_gravatar_hashed_id = get_user_meta( $object_id, 'synced_gravatar_hashed_id', true );
}
$md_data['profile_photo'] = ! empty( $synced_profile_photo ) || ! empty( $synced_gravatar_hashed_id );
break;
case 'cover_photo':
$md_data['cover_photo'] = false;
break;
case '_um_verified':
$md_data['verified'] = false;
break;
}
update_user_meta( $object_id, 'um_member_directory_data', $md_data );
}
/**
* When you add/update usermeta connected with member directories - set this data to member directory metakey
*
* @param int $meta_id
* @param int $object_id
* @param string $meta_key
* @param mixed $_meta_value
*/
function on_update_usermeta( $meta_id, $object_id, $meta_key, $_meta_value ) {
$metakeys = array( 'account_status', 'hide_in_members', 'synced_gravatar_hashed_id', 'synced_profile_photo', 'profile_photo', 'cover_photo', '_um_verified' );
if ( ! in_array( $meta_key, $metakeys ) ) {
return;
}
$md_data = get_user_meta( $object_id, 'um_member_directory_data', true );
if ( empty( $md_data ) ) {
$md_data = array(
'account_status' => 'approved',
'hide_in_members' => false,
'profile_photo' => false,
'cover_photo' => false,
'verified' => false,
);
}
switch ( $meta_key ) {
case 'account_status':
$md_data['account_status'] = $_meta_value;
break;
case 'hide_in_members':
$hide_in_members = false;
if ( ! empty( $_meta_value ) ) {
if ( $_meta_value == 'Yes' || $_meta_value == __( 'Yes', 'ultimate-member' ) ||
$_meta_value == serialize( array( 'Yes' ) ) || $_meta_value == serialize( array( __( 'Yes', 'ultimate-member' ) ) ) ) {
$hide_in_members = true;
}
}
$md_data['hide_in_members'] = $hide_in_members;
break;
case 'synced_gravatar_hashed_id':
if ( UM()->options()->get( 'use_gravatars' ) ) {
if ( empty( $md_data['profile_photo'] ) ) {
$md_data['profile_photo'] = ! empty( $_meta_value );
}
}
break;
case 'synced_profile_photo':
case 'profile_photo':
if ( empty( $md_data['profile_photo'] ) ) {
$md_data['profile_photo'] = ! empty( $_meta_value );
}
break;
case 'cover_photo':
$md_data['cover_photo'] = ! empty( $_meta_value );
break;
case '_um_verified':
$md_data['verified'] = $_meta_value == 'verified' ? true : false;
break;
}
update_user_meta( $object_id, 'um_member_directory_data', $md_data );
}