/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/frontend/schema/class-schema-person.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Frontend\Schema
 */

/**
 * Returns schema Person data.
 *
 * @since 10.2
 */
class WPSEO_Schema_Person implements WPSEO_Graph_Piece {
    
/**
     * A value object with context variables.
     *
     * @var WPSEO_Schema_Context
     */
    
private $context;

    
/**
     * Array of the social profiles we display for a Person.
     *
     * @var string[]
     */
    
private $social_profiles = array(
        
'facebook',
        
'instagram',
        
'linkedin',
        
'pinterest',
        
'twitter',
        
'myspace',
        
'youtube',
        
'soundcloud',
        
'tumblr',
        
'wikipedia',
    );

    
/**
     * The Schema type we use for this class.
     *
     * @var string[]
     */
    
protected $type = array(
        
'Person',
        
'Organization',
    );

    
/**
     * The hash used for images.
     *
     * @var string
     */
    
protected $image_hash;

    
/**
     * WPSEO_Schema_Person constructor.
     *
     * @param WPSEO_Schema_Context $context A value object with context variables.
     */
    
public function __constructWPSEO_Schema_Context $context ) {
        
$this->image_hash WPSEO_Schema_IDs::PERSON_LOGO_HASH;
        
$this->context    $context;
    }

    
/**
     * Determine whether we should return Person schema.
     *
     * @return bool
     */
    
public function is_needed() {
        if ( ( 
$this->context->site_represents === 'person' ) || is_author() ) {
            return 
true;
        }

        return 
false;
    }

    
/**
     * Returns Person Schema data.
     *
     * @return bool|array Person data on success, false on failure.
     */
    
public function generate() {
        
$user_id $this->determine_user_id();
        if ( ! 
$user_id ) {
            return 
false;
        }

        
$data $this->build_person_data$user_id );

        return 
$data;
    }

    
/**
     * Determines a User ID for the Person data.
     *
     * @return bool|int User ID or false upon return.
     */
    
protected function determine_user_id() {
        
$user_id $this->context->site_user_id;

        
/**
         * Filter: 'wpseo_schema_person_user_id' - Allows filtering of user ID used for person output.
         *
         * @api int|bool $user_id The user ID currently determined.
         */
        
return apply_filters'wpseo_schema_person_user_id'$user_id );
    }

    
/**
     * Retrieve a list of social profile URLs for Person.
     *
     * @param int $user_id User ID.
     *
     * @return string[] $output A list of social profiles.
     */
    
protected function get_social_profiles$user_id ) {
        
/**
         * Filter: 'wpseo_schema_person_social_profiles' - Allows filtering of social profiles per user.
         *
         * @param int $user_id The current user we're grabbing social profiles for.
         *
         * @api string[] $social_profiles The array of social profiles to retrieve. Each should be a user meta field
         *                                key. As they are retrieved using the WordPress function `get_the_author_meta`.
         */
        
$social_profiles apply_filters'wpseo_schema_person_social_profiles'$this->social_profiles$user_id );
        
$output          = array();
        foreach ( 
$social_profiles as $profile ) {
            
$social_url $this->url_social_site$profile$user_id );
            if ( 
$social_url ) {
                
$output[] = $social_url;
            }
        }

        return 
$output;
    }

    
/**
     * Builds our array of Schema Person data for a given user ID.
     *
     * @param int $user_id The user ID to use.
     *
     * @return array An array of Schema Person data.
     */
    
protected function build_person_data$user_id ) {
        
$user_data get_userdata$user_id );
        
$data      = array(
            
'@type' => $this->type,
            
'@id'   => WPSEO_Schema_Utils::get_user_schema_id$user_id$this->context ),
            
'name'  => $user_data->display_name,
        );

        
$data $this->add_image$data$user_data );

        if ( ! empty( 
$user_data->description ) ) {
            
$data['description'] = $user_data->description;
        }

        
$social_profiles $this->get_social_profiles$user_id );
        if ( 
is_array$social_profiles ) ) {
            
$data['sameAs'] = $social_profiles;
        }

        return 
$data;
    }

    
/**
     * Returns an ImageObject for the persons avatar.
     *
     * @param array    $data      The Person schema.
     * @param \WP_User $user_data User data.
     *
     * @return array $data The Person schema.
     */
    
protected function add_image$data$user_data ) {
        
$schema_id $this->context->site_url $this->image_hash;

        
$data $this->set_image_from_options$data$schema_id );
        if ( ! isset( 
$data['image'] ) ) {
            
$data $this->set_image_from_avatar$data$user_data$schema_id );
        }

        if ( 
is_array$this->type ) && in_array'Organization'$this->type ) ) {
            
$data['logo'] = array( '@id' => $schema_id );
        }

        return 
$data;
    }

    
/**
     * Generate the person image from our settings.
     *
     * @param array  $data      The Person schema.
     * @param string $schema_id The string used in the `@id` for the schema.
     *
     * @return array    $data      The Person schema.
     */
    
private function set_image_from_options$data$schema_id ) {
        
$person_logo_id WPSEO_Image_Utils::get_attachment_id_from_settings'person_logo' );

        if ( 
$person_logo_id ) {
            
$image         = new WPSEO_Schema_Image$schema_id );
            
$data['image'] = $image->generate_from_attachment_id$person_logo_id$data['name'] );
        }

        return 
$data;
    }

    
/**
     * Generate the person logo from gravatar.
     *
     * @param array    $data      The Person schema.
     * @param \WP_User $user_data User data.
     * @param string   $schema_id The string used in the `@id` for the schema.
     *
     * @return array    $data      The Person schema.
     */
    
private function set_image_from_avatar$data$user_data$schema_id ) {
        
// If we don't have an image in our settings, fall back to an avatar, if we're allowed to.
        
$show_avatars get_option'show_avatars' );
        if ( ! 
$show_avatars ) {
            return 
$data;
        }

        
$url get_avatar_url$user_data->user_email );
        if ( empty( 
$url ) ) {
            return 
$data;
        }

        
$schema_image  = new WPSEO_Schema_Image$schema_id );
        
$data['image'] = $schema_image->simple_image_object$url$user_data->display_name );

        return 
$data;
    }

    
/**
     * Returns an author's social site URL.
     *
     * @param string $social_site The social site to retrieve the URL for.
     * @param mixed  $user_id     The user ID to use function outside of the loop.
     *
     * @return string
     */
    
protected function url_social_site$social_site$user_id false ) {
        
$url get_the_author_meta$social_site$user_id );

        if ( ! empty( 
$url ) ) {
            switch ( 
$social_site ) {
                case 
'twitter':
                    
$url 'https://twitter.com/' $url;
                    break;
            }
        }

        return 
$url;
    }
}