Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
45 / 45 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Person_Schema_Node | |
100.00% |
45 / 45 |
|
100.00% |
3 / 3 |
16 | |
100.00% |
1 / 1 |
| build | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
7 | |||
| image | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 | |||
| text | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Site-level Person Schema.org node builder. |
| 4 | * |
| 5 | * @package automattic/jetpack-seo-package |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\SEO; |
| 9 | |
| 10 | /** |
| 11 | * Builds the site-level Person node. |
| 12 | */ |
| 13 | class Person_Schema_Node { |
| 14 | |
| 15 | /** |
| 16 | * Build the site Person, or return null when neither settings nor site identity supply a name. |
| 17 | * |
| 18 | * @param array $settings Optional schema settings: `name`, `description`, |
| 19 | * `sameAs` (array of URLs). Empty `name` / `description` |
| 20 | * fall back to site identity. |
| 21 | * @return array|null |
| 22 | */ |
| 23 | public static function build( array $settings = array() ) { |
| 24 | $name = self::text( $settings['name'] ?? '' ); |
| 25 | if ( '' === $name ) { |
| 26 | $name = self::text( get_bloginfo( 'name' ) ); |
| 27 | } |
| 28 | if ( '' === $name ) { |
| 29 | return null; |
| 30 | } |
| 31 | |
| 32 | $node = array( |
| 33 | '@type' => 'Person', |
| 34 | '@id' => Schema_Node_Ids::site_person(), |
| 35 | 'name' => $name, |
| 36 | 'url' => home_url( '/' ), |
| 37 | ); |
| 38 | |
| 39 | $description = self::text( $settings['description'] ?? '' ); |
| 40 | if ( '' === $description ) { |
| 41 | $description = self::text( get_bloginfo( 'description' ) ); |
| 42 | } |
| 43 | if ( '' !== $description ) { |
| 44 | $node['description'] = $description; |
| 45 | } |
| 46 | |
| 47 | $image = self::image(); |
| 48 | if ( null !== $image ) { |
| 49 | $node['image'] = $image; |
| 50 | } |
| 51 | |
| 52 | $same_as = Schema_Settings::sanitize_url_list( $settings['sameAs'] ?? array() ); |
| 53 | if ( ! empty( $same_as ) ) { |
| 54 | $node['sameAs'] = $same_as; |
| 55 | } |
| 56 | |
| 57 | return $node; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * The person's image as an ImageObject: the Site Logo (Customizer) when set, otherwise the Site Icon. |
| 62 | * |
| 63 | * @return array|null |
| 64 | */ |
| 65 | private static function image() { |
| 66 | $custom_logo_id = get_theme_mod( 'custom_logo' ); |
| 67 | if ( $custom_logo_id ) { |
| 68 | $src = wp_get_attachment_image_src( $custom_logo_id, 'full' ); |
| 69 | if ( is_array( $src ) && ! empty( $src[0] ) ) { |
| 70 | $image = array( |
| 71 | '@type' => 'ImageObject', |
| 72 | 'url' => $src[0], |
| 73 | ); |
| 74 | if ( ! empty( $src[1] ) && ! empty( $src[2] ) ) { |
| 75 | $image['width'] = (int) $src[1]; |
| 76 | $image['height'] = (int) $src[2]; |
| 77 | } |
| 78 | return $image; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | $icon_url = get_site_icon_url(); |
| 83 | if ( $icon_url ) { |
| 84 | return array( |
| 85 | '@type' => 'ImageObject', |
| 86 | 'url' => $icon_url, |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Normalize a scalar setting/site value to trimmed plain text. |
| 95 | * |
| 96 | * @param mixed $value Raw value. |
| 97 | * @return string |
| 98 | */ |
| 99 | private static function text( $value ) { |
| 100 | if ( ! is_string( $value ) ) { |
| 101 | return ''; |
| 102 | } |
| 103 | return trim( wp_strip_all_tags( $value ) ); |
| 104 | } |
| 105 | } |