| 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 OPcacheManager\System; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define the user functionality. |
| 16 |
* |
| 17 |
* Handles all user operations and detection. |
| 18 |
* |
| 19 |
* @package System |
| 20 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class User { |
| 24 |
|
| 25 |
/** |
| 26 |
* Initializes the class and set its properties. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get a user nice name. |
| 35 |
* |
| 36 |
* @param integer $id Optional. The user id. |
| 37 |
* @param string $default Optional. Default value to return if user is not detected. |
| 38 |
* @return string The user nice name if detected, $default otherwise. |
| 39 |
* @since 1.0.0 |
| 40 |
*/ |
| 41 |
public static function get_user_name( $id = null, $default = 'anonymous' ) { |
| 42 |
if ( $id && is_numeric( $id ) && $id > 0 ) { |
| 43 |
$user_info = get_userdata( $id ); |
| 44 |
return $user_info->display_name; |
| 45 |
|
| 46 |
} else { |
| 47 |
return $default; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the current user id. |
| 53 |
* |
| 54 |
* @param mixed $default Optional. Default value to return if user is not detected. |
| 55 |
* @param bool $force Optional. Try to force authent if user is not detected at first try. |
| 56 |
* @return mixed|integer The user id if detected, null otherwise. |
| 57 |
* @since 1.0.0 |
| 58 |
*/ |
| 59 |
public static function get_current_user_id( $default = null, $force = false ) { |
| 60 |
if ( $force ) { |
| 61 |
$user_id = ( isset( $default ) ? (int) $default : 0 ); |
| 62 |
$id = get_current_user_id(); |
| 63 |
if ( $id && is_numeric( $id ) && $id > 0 ) { |
| 64 |
$user_id = $id; |
| 65 |
} |
| 66 |
return $user_id; |
| 67 |
} |
| 68 |
global $current_user; |
| 69 |
if ( ! empty( $current_user ) ) { |
| 70 |
if ( $current_user instanceof \WP_User ) { |
| 71 |
return ( isset( $current_user->ID ) ? (int) $current_user->ID : ( isset( $default ) ? (int) $default : 0 ) ); |
| 72 |
} |
| 73 |
if ( is_object( $current_user ) && isset( $current_user->ID ) ) { |
| 74 |
return $current_user->ID; |
| 75 |
} |
| 76 |
} |
| 77 |
return ( isset( $default ) ? (int) $default : 0 ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the current user nice name. |
| 82 |
* |
| 83 |
* @param string $default Optional. Default value to return if user is not detected. |
| 84 |
* @return string The current user nice name if detected, "anonymous" otherwise. |
| 85 |
* @since 1.0.0 |
| 86 |
*/ |
| 87 |
public static function get_current_user_name( $default = 'anonymous' ) { |
| 88 |
return self::get_user_name( self::get_current_user_id(), $default ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Delete some usermeta values. |
| 93 |
* |
| 94 |
* @param string $key The end of meta_key field. |
| 95 |
* @param integer $userid Optional. The user id. If not specified, |
| 96 |
* current user id is used. |
| 97 |
* @return int|false The number of rows deleted, or false on error. |
| 98 |
* @since 1.0.0 |
| 99 |
*/ |
| 100 |
public static function delete_meta( $key, $userid = null ) { |
| 101 |
if ( ! isset( $userid ) ) { |
| 102 |
$userid = self::get_current_user_id(); |
| 103 |
} |
| 104 |
global $wpdb; |
| 105 |
$table_name = $wpdb->prefix . 'usermeta'; |
| 106 |
$sql = 'DELETE FROM ' . $table_name . ' WHERE meta_key LIKE "%\_' . $key . '%" AND user_id=' . $userid . ';'; |
| 107 |
// phpcs:ignore |
| 108 |
return $wpdb->query( $sql ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Delete all usermeta values for all users. |
| 113 |
* |
| 114 |
* @return int|false The number of rows deleted, or false on error. |
| 115 |
* @since 1.0.0 |
| 116 |
*/ |
| 117 |
public static function delete_all_meta() { |
| 118 |
global $wpdb; |
| 119 |
$table_name = $wpdb->prefix . 'usermeta'; |
| 120 |
$sql = 'DELETE FROM ' . $table_name . ' WHERE meta_key LIKE "%\_opcm-%";'; |
| 121 |
// phpcs:ignore |
| 122 |
return $wpdb->query( $sql ); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|