Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.80% |
36 / 41 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Contact | |
92.31% |
36 / 39 |
|
50.00% |
3 / 6 |
12.07 | |
0.00% |
0 / 1 |
| get_content_type | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_content | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
4.00 | |||
| escape | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| uri | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fold | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
4.01 | |||
| decode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * ContactSpec: /.well-known/contact.vcf |
| 4 | * |
| 5 | * @link https://contactspec.org/ |
| 6 | * |
| 7 | * @package automattic/jetpack-publicize |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Publicize\Socialspec; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * The site as a vCard 3.0 (RFC 2426), without an email address. |
| 18 | */ |
| 19 | class Contact implements Document { |
| 20 | |
| 21 | /** |
| 22 | * The media type. |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | public function get_content_type() { |
| 27 | return 'text/vcard'; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * The vCard: site title, URL, tagline and icon. |
| 32 | * |
| 33 | * @return string |
| 34 | */ |
| 35 | public function get_content() { |
| 36 | $host = (string) wp_parse_url( home_url(), PHP_URL_HOST ); |
| 37 | $name = self::decode( get_bloginfo( 'name' ) ); |
| 38 | |
| 39 | $lines = array( |
| 40 | 'BEGIN:VCARD', |
| 41 | 'VERSION:3.0', |
| 42 | 'FN:' . self::escape( '' !== $name ? $name : $host ), |
| 43 | 'N:;;;;', |
| 44 | 'URL:' . self::uri( home_url( '/' ) ), |
| 45 | ); |
| 46 | |
| 47 | $description = self::decode( get_bloginfo( 'description' ) ); |
| 48 | if ( '' !== $description ) { |
| 49 | $lines[] = 'NOTE:' . self::escape( $description ); |
| 50 | } |
| 51 | |
| 52 | $icon = get_site_icon_url( 512 ); |
| 53 | if ( '' !== $icon ) { |
| 54 | $lines[] = 'PHOTO;VALUE=uri:' . self::uri( $icon ); |
| 55 | } |
| 56 | |
| 57 | $lines[] = 'NICKNAME:' . self::escape( '@' . $host ); |
| 58 | $lines[] = 'SOURCE:' . self::uri( home_url( '/.well-known/contact.vcf' ) ); |
| 59 | $lines[] = 'END:VCARD'; |
| 60 | |
| 61 | return implode( "\r\n", array_map( array( __CLASS__, 'fold' ), $lines ) ) . "\r\n"; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Escapes a text value (RFC 2426, section 4). |
| 66 | * |
| 67 | * @param string $value The value. |
| 68 | * @return string |
| 69 | */ |
| 70 | private static function escape( $value ) { |
| 71 | return str_replace( |
| 72 | array( '\\', ',', ';', "\r\n", "\r", "\n" ), |
| 73 | array( '\\\\', '\\,', '\\;', '\\n', '\\n', '\\n' ), |
| 74 | $value |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * A URI value, which is not escaped as text but must stay on its line. |
| 80 | * |
| 81 | * @param string $value The URI. |
| 82 | * @return string |
| 83 | */ |
| 84 | private static function uri( $value ) { |
| 85 | return str_replace( array( "\r", "\n" ), '', $value ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Folds a content line at 75 octets without splitting multibyte characters. |
| 90 | * |
| 91 | * @param string $line The content line. |
| 92 | * @return string |
| 93 | */ |
| 94 | private static function fold( $line ) { |
| 95 | $folded = ''; |
| 96 | $length = 0; |
| 97 | |
| 98 | // Split into UTF-8 characters, or bytes if the line is not valid UTF-8. |
| 99 | $chars = preg_split( '//u', $line, -1, PREG_SPLIT_NO_EMPTY ); |
| 100 | if ( ! $chars ) { |
| 101 | $chars = str_split( $line ); |
| 102 | } |
| 103 | |
| 104 | foreach ( $chars as $char ) { |
| 105 | if ( $length + strlen( $char ) > 75 ) { |
| 106 | $folded .= "\r\n "; |
| 107 | $length = 1; |
| 108 | } |
| 109 | |
| 110 | $folded .= $char; |
| 111 | $length += strlen( $char ); |
| 112 | } |
| 113 | |
| 114 | return $folded; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Decodes the HTML entities WordPress stores in site options. |
| 119 | * |
| 120 | * @param string $value The value. |
| 121 | * @return string |
| 122 | */ |
| 123 | private static function decode( $value ) { |
| 124 | return html_entity_decode( $value, ENT_QUOTES | ENT_HTML5, get_bloginfo( 'charset' ) ); |
| 125 | } |
| 126 | } |