'string', 'single' => true, 'default' => '', 'sanitize_callback' => 'sanitize_text_field', 'show_in_rest' => true, ) ); register_meta( 'user', self::META_SAME_AS, array( 'type' => 'array', 'single' => true, 'default' => array(), 'sanitize_callback' => array( __CLASS__, 'sanitize_url_list' ), 'show_in_rest' => array( 'schema' => array( 'type' => 'array', 'items' => array( 'type' => 'string', 'format' => 'uri', ), ), ), ) ); add_action( 'show_user_profile', array( __CLASS__, 'render_profile_fields' ) ); add_action( 'edit_user_profile', array( __CLASS__, 'render_profile_fields' ) ); add_action( 'personal_options_update', array( __CLASS__, 'save_profile_fields' ) ); add_action( 'edit_user_profile_update', array( __CLASS__, 'save_profile_fields' ) ); } /** * Build a Person node for a WP user. * * @param WP_User|int|null $user User object or ID. * @return array|null */ public static function build_person( $user ) { $user = self::get_user( $user ); if ( ! $user ) { return null; } $name = self::text( $user->display_name ); if ( '' === $name ) { return null; } $node = array( '@type' => 'Person', '@id' => Schema_Node_Ids::person( $user->ID, $user->user_nicename ), 'name' => $name, ); $image = get_avatar_url( $user->ID, array( 'size' => 512 ) ); if ( is_string( $image ) && '' !== $image ) { $node['image'] = $image; } $description = self::text( get_the_author_meta( 'description', $user->ID ) ); if ( '' !== $description ) { $node['description'] = $description; } // Prefer the user's Website field; fall back to the author archive so the // Person always carries a URL identifying the author. $url = self::url( $user->user_url ); $node['url'] = '' !== $url ? $url : get_author_posts_url( $user->ID, $user->user_nicename ); $given_name = self::text( get_the_author_meta( 'first_name', $user->ID ) ); if ( '' !== $given_name ) { $node['givenName'] = $given_name; } $family_name = self::text( get_the_author_meta( 'last_name', $user->ID ) ); if ( '' !== $family_name ) { $node['familyName'] = $family_name; } $job_title = self::text( get_user_meta( $user->ID, self::META_JOB_TITLE, true ) ); if ( '' !== $job_title ) { $node['jobTitle'] = $job_title; } $same_as = self::sanitize_url_list( get_user_meta( $user->ID, self::META_SAME_AS, true ) ); if ( ! empty( $same_as ) ) { $node['sameAs'] = $same_as; } return $node; } /** * Build a ProfilePage node for an author archive. * * @param WP_User|int|null $user User object or ID. * @return array|null */ public static function build_profile_page( $user ) { $user = self::get_user( $user ); if ( ! $user || '' === self::text( $user->display_name ) ) { return null; } return array( '@type' => 'ProfilePage', '@id' => Schema_Node_Ids::profile_page( $user->ID, $user->user_nicename ), 'url' => get_author_posts_url( $user->ID, $user->user_nicename ), 'mainEntity' => array( '@id' => Schema_Node_Ids::person( $user->ID, $user->user_nicename ) ), ); } /** * Render the Jetpack SEO author profile fields. * * @param WP_User $user User being edited. * @return void */ public static function render_profile_fields( WP_User $user ) { $job_field = self::META_JOB_TITLE; $links_field = self::META_SAME_AS; $job_title = get_user_meta( $user->ID, $job_field, true ); $same_as = self::sanitize_url_list( get_user_meta( $user->ID, $links_field, true ) ); printf( '
|
%8$s |