| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Helpers; |
| 4 |
|
| 5 |
/** |
| 6 |
* A helper object for the user. |
| 7 |
*/ |
| 8 |
class User_Helper { |
| 9 |
|
| 10 |
/** |
| 11 |
* Retrieves user meta field for a user. |
| 12 |
* |
| 13 |
* @param int $user_id User ID. |
| 14 |
* @param string $key Optional. The meta key to retrieve. By default, returns data for all keys. |
| 15 |
* @param bool $single Whether to return a single value. |
| 16 |
* |
| 17 |
* @return mixed Will be an array if $single is false. Will be value of meta data field if $single is true. |
| 18 |
*/ |
| 19 |
public function get_meta( $user_id, $key = '', $single = false ) { |
| 20 |
return \get_user_meta( $user_id, $key, $single ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Counts the number of posts the user has written in this post type. |
| 25 |
* |
| 26 |
* @param int $user_id User ID. |
| 27 |
* @param array|string $post_type Optional. Single post type or array of post types to count the number of posts |
| 28 |
* for. Default 'post'. |
| 29 |
* |
| 30 |
* @return int The number of posts the user has written in this post type. |
| 31 |
*/ |
| 32 |
public function count_posts( $user_id, $post_type = 'post' ) { |
| 33 |
return (int) \count_user_posts( $user_id, $post_type, true ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Retrieves the requested data of the author. |
| 38 |
* |
| 39 |
* @param string $field The user field to retrieve. |
| 40 |
* @param int|false $user_id User ID. |
| 41 |
* |
| 42 |
* @return string The author's field from the current author's DB object. |
| 43 |
*/ |
| 44 |
public function get_the_author_meta( $field, $user_id ) { |
| 45 |
return \get_the_author_meta( $field, $user_id ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Retrieves the archive url of the user. |
| 50 |
* |
| 51 |
* @param int|false $user_id User ID. |
| 52 |
* |
| 53 |
* @return string The author's archive url. |
| 54 |
*/ |
| 55 |
public function get_the_author_posts_url( $user_id ) { |
| 56 |
return \get_author_posts_url( $user_id ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Retrieves the current user ID. |
| 61 |
* |
| 62 |
* @return int The current user's ID, or 0 if no user is logged in. |
| 63 |
*/ |
| 64 |
public function get_current_user_id() { |
| 65 |
return \get_current_user_id(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Updates user meta field for a user. |
| 70 |
* |
| 71 |
* Use the $prev_value parameter to differentiate between meta fields with the |
| 72 |
* same key and user ID. |
| 73 |
* |
| 74 |
* If the meta field for the user does not exist, it will be added. |
| 75 |
* |
| 76 |
* @param int $user_id User ID. |
| 77 |
* @param string $meta_key Metadata key. |
| 78 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
| 79 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
| 80 |
* If specified, only update existing metadata entries with |
| 81 |
* this value. Otherwise, update all entries. Default empty. |
| 82 |
* |
| 83 |
* @return int|bool Meta ID if the key didn't exist, true on successful update, |
| 84 |
* false on failure or if the value passed to the function |
| 85 |
* is the same as the one that is already in the database. |
| 86 |
*/ |
| 87 |
public function update_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 88 |
return \update_user_meta( $user_id, $meta_key, $meta_value, $prev_value ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Removes metadata matching criteria from a user. |
| 93 |
* |
| 94 |
* You can match based on the key, or key and value. Removing based on key and |
| 95 |
* value, will keep from removing duplicate metadata with the same key. It also |
| 96 |
* allows removing all metadata matching key, if needed. |
| 97 |
* |
| 98 |
* @param int $user_id User ID. |
| 99 |
* @param string $meta_key Metadata name. |
| 100 |
* @param mixed $meta_value Optional. Metadata value. If provided, |
| 101 |
* rows will only be removed that match the value. |
| 102 |
* Must be serializable if non-scalar. Default empty. |
| 103 |
* |
| 104 |
* @return bool True on success, false on failure. |
| 105 |
*/ |
| 106 |
public function delete_meta( $user_id, $meta_key, $meta_value = '' ) { |
| 107 |
return \delete_user_meta( $user_id, $meta_key, $meta_value ); |
| 108 |
} |
| 109 |
} |
| 110 |
|