| 1 |
<?php |
| 2 |
/** |
| 3 |
* OpenStation — `link` file type. |
| 4 |
* |
| 5 |
* A "web link" tile — double-click opens the stored URL in a new |
| 6 |
* browser tab via `window.open( url, '_blank', 'noopener,noreferrer' )`. |
| 7 |
* The URL itself is the entity reference; an optional human-friendly |
| 8 |
* name lives on the placement row's `meta.name` so two tiles |
| 9 |
* pointing at the same URL can carry different labels. |
| 10 |
* |
| 11 |
* @package OpenStation |
| 12 |
*/ |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* The `link` desktop file type. |
| 18 |
*/ |
| 19 |
class OpenStation_Link_File extends OpenStation_File { |
| 20 |
|
| 21 |
/** |
| 22 |
* Get the file type identifier. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public static function type(): string { |
| 27 |
return 'link'; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the link URL is non-empty. |
| 32 |
* |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
public function exists(): bool { |
| 36 |
return '' !== $this->url(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get a label from the link URL's host. |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public function title(): string { |
| 45 |
$url = $this->url(); |
| 46 |
if ( '' === $url ) { |
| 47 |
return __( '(missing link)', 'desktop-mode' ); |
| 48 |
} |
| 49 |
$host = wp_parse_url( $url, PHP_URL_HOST ); |
| 50 |
return is_string( $host ) && '' !== $host ? $host : $url; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the Dashicon class for link tiles. |
| 55 |
* |
| 56 |
* @return string |
| 57 |
*/ |
| 58 |
public function icon(): string { |
| 59 |
return 'dashicons-admin-links'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Augment the base serialized shape with the link URL. |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function serialize(): array { |
| 68 |
$shape = parent::serialize(); |
| 69 |
$shape['url'] = $this->url(); |
| 70 |
return $shape; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Sanitize the stored ref into a valid URL. |
| 75 |
* |
| 76 |
* @return string Empty string when the ref is not a valid URL. |
| 77 |
*/ |
| 78 |
private function url(): string { |
| 79 |
$url = esc_url_raw( $this->ref ); |
| 80 |
return is_string( $url ) ? $url : ''; |
| 81 |
} |
| 82 |
} |
| 83 |
|