PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / helpers / user-helper.php

user-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.3, at src/helpers/user-helper.php

124 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Returns the current users display_name.
70 *
71 * @return string
72 */
73 public function get_current_user_display_name(): string {
74 $user = \wp_get_current_user();
75 if ( $user && $user->display_name ) {
76 return $user->display_name;
77 }
78
79 return '';
80 }
81
82 /**
83 * Updates user meta field for a user.
84 *
85 * Use the $prev_value parameter to differentiate between meta fields with the
86 * same key and user ID.
87 *
88 * If the meta field for the user does not exist, it will be added.
89 *
90 * @param int $user_id User ID.
91 * @param string $meta_key Metadata key.
92 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
93 * @param mixed $prev_value Optional. Previous value to check before updating.
94 * If specified, only update existing metadata entries with
95 * this value. Otherwise, update all entries. Default empty.
96 *
97 * @return int|bool Meta ID if the key didn't exist, true on successful update,
98 * false on failure or if the value passed to the function
99 * is the same as the one that is already in the database.
100 */
101 public function update_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
102 return \update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );
103 }
104
105 /**
106 * Removes metadata matching criteria from a user.
107 *
108 * You can match based on the key, or key and value. Removing based on key and
109 * value, will keep from removing duplicate metadata with the same key. It also
110 * allows removing all metadata matching key, if needed.
111 *
112 * @param int $user_id User ID.
113 * @param string $meta_key Metadata name.
114 * @param mixed $meta_value Optional. Metadata value. If provided,
115 * rows will only be removed that match the value.
116 * Must be serializable if non-scalar. Default empty.
117 *
118 * @return bool True on success, false on failure.
119 */
120 public function delete_meta( $user_id, $meta_key, $meta_value = '' ) {
121 return \delete_user_meta( $user_id, $meta_key, $meta_value );
122 }
123 }
124