| 1 |
<?php |
| 2 |
/** |
| 3 |
* Users handling |
| 4 |
* |
| 5 |
* Handles all user operations and detection. |
| 6 |
* |
| 7 |
* @package System |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\System; |
| 13 |
|
| 14 |
use Decalog\System\Hash; |
| 15 |
|
| 16 |
/** |
| 17 |
* Define the user functionality. |
| 18 |
* |
| 19 |
* Handles all user operations and detection. |
| 20 |
* |
| 21 |
* @package System |
| 22 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
class User { |
| 26 |
|
| 27 |
/** |
| 28 |
* Initializes the class and set its properties. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
*/ |
| 32 |
public function __construct() { |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get a user nice name. |
| 37 |
* |
| 38 |
* @param integer $id Optional. The user id. |
| 39 |
* @param string $default Optional. Default value to return if user is not detected. |
| 40 |
* @return string The user nice name if detected, $default otherwise. |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public static function get_user_name( $id = null, $default = 'anonymous' ) { |
| 44 |
if ( $id && is_numeric( $id ) && $id > 0 ) { |
| 45 |
$user_info = get_userdata( $id ); |
| 46 |
return $user_info->display_name; |
| 47 |
|
| 48 |
} else { |
| 49 |
return $default; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get a user string representation. |
| 55 |
* |
| 56 |
* @param integer $id Optional. The user id. |
| 57 |
* @param boolean $pseudonymize Optional. Has this user to be pseudonymized. |
| 58 |
* @return string The user string representation, ready to be inserted in a log. |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public static function get_user_string( $id = null, $pseudonymize = false ) { |
| 62 |
if ( $id && is_numeric( $id ) && $id > 0 && ! $pseudonymize ) { |
| 63 |
$user_info = get_userdata( $id ); |
| 64 |
$name = $user_info->display_name; |
| 65 |
} else { |
| 66 |
if ( $pseudonymize ) { |
| 67 |
$name = 'pseudonymized user'; |
| 68 |
$id = Hash::simple_hash( (string) $id ); |
| 69 |
} else { |
| 70 |
return 'anonymous user'; |
| 71 |
} |
| 72 |
} |
| 73 |
return sprintf( '%s (user ID %s)', $name, $id ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get the current user id. |
| 78 |
* |
| 79 |
* @param mixed $default Optional. Default value to return if user is not detected. |
| 80 |
* @param bool $force Optional. Try to force authent if user is not detected at first try. |
| 81 |
* @return mixed|integer The user id if detected, null otherwise. |
| 82 |
* @since 1.0.0 |
| 83 |
*/ |
| 84 |
public static function get_current_user_id( $default = null, $force = false ) { |
| 85 |
if ( $force ) { |
| 86 |
$user_id = ( isset( $default ) ? (int) $default : 0 ); |
| 87 |
$id = get_current_user_id(); |
| 88 |
if ( $id && is_numeric( $id ) && $id > 0 ) { |
| 89 |
$user_id = $id; |
| 90 |
} |
| 91 |
return $user_id; |
| 92 |
} |
| 93 |
global $current_user; |
| 94 |
if ( ! empty( $current_user ) ) { |
| 95 |
if ( $current_user instanceof \WP_User ) { |
| 96 |
return ( isset( $current_user->ID ) ? (int) $current_user->ID : ( isset( $default ) ? (int) $default : 0 ) ); |
| 97 |
} |
| 98 |
if ( is_object( $current_user ) && isset( $current_user->ID ) ) { |
| 99 |
return $current_user->ID; |
| 100 |
} |
| 101 |
} |
| 102 |
return ( isset( $default ) ? (int) $default : 0 ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get the current user nice name. |
| 107 |
* |
| 108 |
* @param string $default Optional. Default value to return if user is not detected. |
| 109 |
* @return string The current user nice name if detected, "anonymous" otherwise. |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public static function get_current_user_name( $default = 'anonymous' ) { |
| 113 |
return self::get_user_name( self::get_current_user_id(), $default ); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Delete some usermeta values. |
| 118 |
* |
| 119 |
* @param string $key The end of meta_key field. |
| 120 |
* @param integer $userid Optional. The user id. If not specified, |
| 121 |
* current user id is used. |
| 122 |
* @return int|false The number of rows deleted, or false on error. |
| 123 |
* @since 1.0.0 |
| 124 |
*/ |
| 125 |
public static function delete_meta( $key, $userid = null ) { |
| 126 |
if ( ! isset( $userid ) ) { |
| 127 |
$userid = self::get_current_user_id(); |
| 128 |
} |
| 129 |
global $wpdb; |
| 130 |
$table_name = $wpdb->base_prefix . 'usermeta'; |
| 131 |
$sql = 'DELETE FROM ' . $table_name . ' WHERE meta_key LIKE "%\_' . $key . '%" AND user_id=' . $userid . ';'; |
| 132 |
// phpcs:ignore |
| 133 |
return $wpdb->query( $sql ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Delete all usermeta values for all users. |
| 138 |
* |
| 139 |
* @return int|false The number of rows deleted, or false on error. |
| 140 |
* @since 1.0.0 |
| 141 |
*/ |
| 142 |
public static function delete_all_meta() { |
| 143 |
global $wpdb; |
| 144 |
$table_name = $wpdb->base_prefix . 'usermeta'; |
| 145 |
$sql = 'DELETE FROM ' . $table_name . ' WHERE meta_key LIKE "%\_decalog-%";'; |
| 146 |
// phpcs:ignore |
| 147 |
return $wpdb->query( $sql ); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|