| 1 |
<?php |
| 2 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 3 |
namespace Yoast\WP\SEO\Schema_Aggregator\Application\Enhancement; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Yoast\WP\SEO\Models\Indexable; |
| 7 |
use Yoast\WP\SEO\Schema_Aggregator\Domain\Enhancement\Schema_Enhancement_Interface; |
| 8 |
use Yoast\WP\SEO\Schema_Aggregator\Domain\Schema_Piece; |
| 9 |
use Yoast\WP\SEO\Schema_Aggregator\Infrastructure\Enhancement\Person_Config; |
| 10 |
|
| 11 |
/** |
| 12 |
* The Person schema pieces enhancer. |
| 13 |
*/ |
| 14 |
class Person_Schema_Enhancer extends Abstract_Schema_Enhancer implements Schema_Enhancement_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The config. |
| 18 |
* |
| 19 |
* @var Person_Config |
| 20 |
*/ |
| 21 |
private $config; |
| 22 |
|
| 23 |
/** |
| 24 |
* Sets the Person_Config instance. |
| 25 |
* |
| 26 |
* @param Person_Config $config The Person_Config instance. |
| 27 |
* @return void |
| 28 |
* @required |
| 29 |
*/ |
| 30 |
public function set_person_config( Person_Config $config ) { |
| 31 |
$this->config = $config; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Enhances specific Article schema pieces. |
| 36 |
* |
| 37 |
* @param Schema_Piece $schema_piece The schema piece to enhance. |
| 38 |
* @param Indexable $indexable The indexable object that is the source of the schema piece. |
| 39 |
* |
| 40 |
* @return Schema_Piece The enhanced schema piece. |
| 41 |
*/ |
| 42 |
public function enhance( Schema_Piece $schema_piece, Indexable $indexable ): Schema_Piece { |
| 43 |
$schema_data = $schema_piece->get_data(); |
| 44 |
if ( isset( $schema_data['@type'] ) && $schema_data['@type'] === 'Person' ) { |
| 45 |
$schema_data = $this->enhance_schema_piece( $schema_data, $indexable ); |
| 46 |
} |
| 47 |
|
| 48 |
return new Schema_Piece( $schema_data, $schema_piece->get_type() ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Enhance a single schema piece |
| 53 |
* |
| 54 |
* @param array<string> $schema_data The schema data to enhance. |
| 55 |
* @param Indexable $indexable The indexable object that is the source of the schema piece. |
| 56 |
* |
| 57 |
* @return array<string> The enhanced schema data. |
| 58 |
*/ |
| 59 |
protected function enhance_schema_piece( array $schema_data, Indexable $indexable ): array { |
| 60 |
try { |
| 61 |
// Add jobTitle if enabled and not already present. |
| 62 |
if ( $this->config->is_enhancement_enabled( 'person_job_title' ) && ! isset( $schema_data['jobTitle'] ) ) { |
| 63 |
$job_title = $this->get_person_job_title( $indexable->author_id ); |
| 64 |
if ( $job_title !== null && $job_title !== '' ) { |
| 65 |
$schema_data['jobTitle'] = $job_title; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return $schema_data; |
| 70 |
} catch ( Exception $e ) { |
| 71 |
return $schema_data; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get person job title |
| 77 |
* |
| 78 |
* Retrieves job title from user meta. |
| 79 |
* |
| 80 |
* @codeCoverageIgnore |
| 81 |
* |
| 82 |
* @param int $user_id User ID. |
| 83 |
* |
| 84 |
* @return string|null Job title or null if unavailable. |
| 85 |
*/ |
| 86 |
private function get_person_job_title( int $user_id ): ?string { |
| 87 |
$job_title = \get_user_meta( $user_id, 'job_title', true ); |
| 88 |
|
| 89 |
if ( empty( $job_title ) ) { |
| 90 |
return null; |
| 91 |
} |
| 92 |
|
| 93 |
return \trim( $job_title ); |
| 94 |
} |
| 95 |
} |
| 96 |
|