Skip to content

Commit

Permalink
Site represents Person logo and reference fixes (#12994)
Browse files Browse the repository at this point in the history
Site represents Person logo and reference fixes
  • Loading branch information
IreneStr committed May 22, 2019
2 parents d5ad35f + 4ef6383 commit d366281
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 54 deletions.
35 changes: 27 additions & 8 deletions frontend/schema/class-schema-author.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ class WPSEO_Schema_Author extends WPSEO_Schema_Person implements WPSEO_Graph_Pie
*/
private $context;

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

/**
* WPSEO_Schema_Breadcrumb constructor.
*
* @param WPSEO_Schema_Context $context A value object with context variables.
*/
public function __construct( WPSEO_Schema_Context $context ) {
parent::__construct( $context );
$this->context = $context;
$this->logo_hash = WPSEO_Schema_IDs::AUTHOR_LOGO_HASH;
$this->context = $context;
$this->image_hash = WPSEO_Schema_IDs::AUTHOR_LOGO_HASH;
}

/**
Expand All @@ -55,14 +62,17 @@ public function is_needed() {
}

/**
* Builds our array of Schema Person data for a given user ID.
*
* @param int $user_id The user ID to use.
* Returns Person Schema data.
*
* @return array An array of Schema Person data.
* @return bool|array Person data on success, false on failure.
*/
protected function build_person_data( $user_id ) {
$data = parent::build_person_data( $user_id );
public function generate() {
$user_id = $this->determine_user_id();
if ( ! $user_id ) {
return false;
}

$data = $this->build_person_data( $user_id );

// If this is an author page, the Person object is the main object, so we set it as such here.
if ( is_author() ) {
Expand Down Expand Up @@ -127,4 +137,13 @@ protected function determine_user_id() {
protected function determine_schema_id( $user_id ) {
return get_author_posts_url( $user_id ) . WPSEO_Schema_IDs::AUTHOR_HASH;
}

/**
* Gets the Schema type we use for this class.
*
* @return string[] The schema type.
*/
public static function get_type() {
return self::$type;
}
}
76 changes: 49 additions & 27 deletions frontend/schema/class-schema-context.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ class WPSEO_Schema_Context {
*/
public $site_represents_reference;


/**
* WPSEO_Schema_Context constructor.
*/
Expand All @@ -102,53 +101,76 @@ public function __construct() {
* Builds all the required data for the context object.
*/
private function build_data() {
$this->breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' );
if ( ! $this->breadcrumbs_enabled ) {
$this->breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false );
}

// Page level variables.
$front = WPSEO_Frontend::get_instance();
$this->canonical = $front->canonical( false );
$this->title = $front->title( '' );
$this->description = $front->metadesc( false );
$this->id = get_queried_object_id();

$this->site_name = $this->set_site_name();
$this->site_represents = WPSEO_Options::get( 'company_or_person', '' );
$this->site_url = trailingslashit( WPSEO_Utils::home_url() );
// Site level variables.
$this->site_name = $this->set_site_name();
$this->site_url = trailingslashit( WPSEO_Utils::home_url() );

if ( $this->site_represents === 'company' ) {
$this->company_name = WPSEO_Options::get( 'company_name' );
}
$this->set_breadcrumbs_variables();
$this->set_site_represents_variables();
$this->set_site_represents_reference();
}

if ( $this->site_represents === 'person' ) {
$this->site_user_id = WPSEO_Options::get( 'company_or_person_user_id', false );
// Do not use a non-existing user.
if ( $this->site_user_id !== false && get_user_by( 'id', $this->site_user_id ) === false ) {
$this->site_represents = false;
}
/**
* Retrieves the site's name from settings.
*
* @return string
*/
private function set_site_name() {
if ( '' !== WPSEO_Options::get( 'website_name', '' ) ) {
return WPSEO_Options::get( 'website_name' );
}

return get_bloginfo( 'name' );
}

/**
* Sets our site represents reference for easy use.
*/
private function set_site_represents_reference() {
$this->site_represents_reference = false;

if ( $this->site_represents === 'person' ) {
$this->site_represents_reference = array( '@id' => $this->site_url . WPSEO_Schema_IDs::PERSON_HASH );
}

if ( $this->site_represents === 'company' ) {
$this->site_represents_reference = array( '@id' => $this->site_url . WPSEO_Schema_IDs::ORGANIZATION_HASH );
}

$this->id = get_queried_object_id();
}

/**
* Retrieves the site's name from settings.
*
* @return string
* Determines what our site represents, and grabs their values.
*/
private function set_site_name() {
if ( '' !== WPSEO_Options::get( 'website_name', '' ) ) {
return WPSEO_Options::get( 'website_name' );
private function set_site_represents_variables() {
$this->site_represents = WPSEO_Options::get( 'company_or_person', false );

if ( $this->site_represents === 'company' ) {
$this->company_name = WPSEO_Options::get( 'company_name' );
}

return get_bloginfo( 'name' );
if ( $this->site_represents === 'person' ) {
$this->site_user_id = WPSEO_Options::get( 'company_or_person_user_id', false );
// Do not use a non-existing user.
if ( $this->site_user_id !== false && get_user_by( 'id', $this->site_user_id ) === false ) {
$this->site_represents = false;
}
}
}

/**
* Determines whether the site uses Yoast SEO breadcrumbs.
*/
private function set_breadcrumbs_variables() {
$this->breadcrumbs_enabled = current_theme_supports( 'yoast-seo-breadcrumbs' );
if ( ! $this->breadcrumbs_enabled ) {
$this->breadcrumbs_enabled = WPSEO_Options::get( 'breadcrumbs-enable', false );
}
}
}
73 changes: 54 additions & 19 deletions frontend/schema/class-schema-person.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class WPSEO_Schema_Person implements WPSEO_Graph_Piece {
* @var WPSEO_Schema_Context
*/
private $context;

/**
* Array of the social profiles we display for a Person.
*
Expand All @@ -35,13 +36,31 @@ class WPSEO_Schema_Person implements WPSEO_Graph_Piece {
'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 __construct( WPSEO_Schema_Context $context ) {
$this->context = $context;
$this->image_hash = WPSEO_Schema_IDs::PERSON_LOGO_HASH;
$this->context = $context;
}

/**
Expand Down Expand Up @@ -127,7 +146,7 @@ protected function get_social_profiles( $user_id ) {
protected function build_person_data( $user_id ) {
$user_data = get_userdata( $user_id );
$data = array(
'@type' => array( 'Person', 'Organization' ),
'@type' => $this->type,
'@id' => $this->determine_schema_id( $user_id ),
'name' => $user_data->display_name,
);
Expand Down Expand Up @@ -155,33 +174,22 @@ protected function build_person_data( $user_id ) {
* @return array $data The Person schema.
*/
protected function add_image( $data, $user_data ) {
$schema_id = $this->context->site_url . WPSEO_Schema_IDs::PERSON_LOGO_HASH;
$schema_id = $this->context->site_url . $this->image_hash;

$data = $this->set_image_from_options( $data, $schema_id );
if ( isset( $data['image'] ) ) {
return $data;
if ( ! isset( $data['image'] ) ) {
$data = $this->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;
if ( is_array( $this->type ) && in_array( 'Organization', $this->type ) ) {
$data['logo'] = array( '@id' => $schema_id );
}

$schema_image = new WPSEO_Schema_Image( $schema_id );
$data['image'] = $schema_image->simple_image_object( $url, $user_data->display_name );
$data['logo'] = array( '@id' => $schema_id );

return $data;
}

/**
* Generate the person avatar / logo from our settings.
* 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.
Expand All @@ -199,6 +207,33 @@ private function set_image_from_options( $data, $schema_id ) {
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 the string to use in Schema's `@id`.
*
Expand Down

0 comments on commit d366281

Please sign in to comment.