| 1 |
<?php |
| 2 |
/** |
| 3 |
* Link class. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub; |
| 9 |
|
| 10 |
/** |
| 11 |
* ActivityPub Summery Links Class. |
| 12 |
*/ |
| 13 |
class Link { |
| 14 |
|
| 15 |
/** |
| 16 |
* Initialize the class, registering WordPress hooks. |
| 17 |
*/ |
| 18 |
public static function init() { |
| 19 |
\add_filter( 'activitypub_extra_field_content', array( self::class, 'the_content' ) ); |
| 20 |
\add_filter( 'activitypub_activity_object_array', array( self::class, 'filter_activity_object' ), 99 ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Filter only the activity object and replace the summary with URLs. |
| 25 |
* |
| 26 |
* @param array $activity The activity object array. |
| 27 |
* |
| 28 |
* @return array Rhe activity object array. |
| 29 |
*/ |
| 30 |
public static function filter_activity_object( $activity ) { |
| 31 |
if ( ! empty( $activity['summary'] ) && is_actor( $activity ) ) { |
| 32 |
$activity['summary'] = self::the_content( $activity['summary'] ); |
| 33 |
} |
| 34 |
|
| 35 |
if ( ! empty( $activity['content'] ) ) { |
| 36 |
$activity['content'] = self::the_content( $activity['content'] ); |
| 37 |
} |
| 38 |
|
| 39 |
return $activity; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Filter to replace the URLS in the content with links |
| 44 |
* |
| 45 |
* @param string $the_content The post content. |
| 46 |
* |
| 47 |
* @return string the filtered post content. |
| 48 |
*/ |
| 49 |
public static function the_content( $the_content ) { |
| 50 |
return enrich_content_data( $the_content, '/' . ACTIVITYPUB_URL_REGEXP . '/i', array( self::class, 'replace_with_links' ) ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* A callback for preg_replace to build the links. |
| 55 |
* |
| 56 |
* Link shortening https://docs.joinmastodon.org/api/guidelines/#links |
| 57 |
* |
| 58 |
* @param array $result The preg_match results. |
| 59 |
* |
| 60 |
* @return string The final string. |
| 61 |
*/ |
| 62 |
public static function replace_with_links( $result ) { |
| 63 |
if ( 'www.' === substr( $result[0], 0, 4 ) ) { |
| 64 |
$result[0] = 'https://' . $result[0]; |
| 65 |
} |
| 66 |
$parsed_url = \wp_parse_url( html_entity_decode( $result[0] ) ); |
| 67 |
if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { |
| 68 |
return $result[0]; |
| 69 |
} |
| 70 |
|
| 71 |
if ( empty( $parsed_url['scheme'] ) ) { |
| 72 |
$invisible_prefix = 'https://'; |
| 73 |
} else { |
| 74 |
$invisible_prefix = $parsed_url['scheme'] . '://'; |
| 75 |
} |
| 76 |
if ( ! empty( $parsed_url['user'] ) ) { |
| 77 |
$invisible_prefix .= $parsed_url['user']; |
| 78 |
} |
| 79 |
if ( ! empty( $parsed_url['pass'] ) ) { |
| 80 |
$invisible_prefix .= ':' . $parsed_url['pass']; |
| 81 |
} |
| 82 |
if ( ! empty( $parsed_url['user'] ) ) { |
| 83 |
$invisible_prefix .= '@'; |
| 84 |
} |
| 85 |
|
| 86 |
$text_url = $parsed_url['host']; |
| 87 |
if ( 'www.' === substr( $text_url, 0, 4 ) ) { |
| 88 |
$text_url = substr( $text_url, 4 ); |
| 89 |
$invisible_prefix .= 'www.'; |
| 90 |
} |
| 91 |
if ( ! empty( $parsed_url['port'] ) ) { |
| 92 |
$text_url .= ':' . $parsed_url['port']; |
| 93 |
} |
| 94 |
if ( ! empty( $parsed_url['path'] ) ) { |
| 95 |
$text_url .= $parsed_url['path']; |
| 96 |
} |
| 97 |
if ( ! empty( $parsed_url['query'] ) ) { |
| 98 |
$text_url .= '?' . $parsed_url['query']; |
| 99 |
} |
| 100 |
if ( ! empty( $parsed_url['fragment'] ) ) { |
| 101 |
$text_url .= '#' . $parsed_url['fragment']; |
| 102 |
} |
| 103 |
|
| 104 |
$display = \substr( $text_url, 0, 30 ); |
| 105 |
$invisible_suffix = \substr( $text_url, 30 ); |
| 106 |
|
| 107 |
$display_class = ''; |
| 108 |
if ( $invisible_suffix ) { |
| 109 |
$display_class .= 'ellipsis'; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Filters the rel attribute for ActivityPub links. |
| 114 |
* |
| 115 |
* @param string $rel The rel attribute string. Default 'nofollow noopener noreferrer'. |
| 116 |
*/ |
| 117 |
$rel = apply_filters( 'activitypub_link_rel', 'nofollow noopener noreferrer' ); |
| 118 |
|
| 119 |
return \sprintf( |
| 120 |
'<a href="%s" target="_blank" rel="%s" translate="no"><span class="invisible">%s</span><span class="%s">%s</span><span class="invisible">%s</span></a>', |
| 121 |
esc_url( $result[0] ), |
| 122 |
$rel, |
| 123 |
esc_html( $invisible_prefix ), |
| 124 |
$display_class, |
| 125 |
esc_html( $display ), |
| 126 |
esc_html( $invisible_suffix ) |
| 127 |
); |
| 128 |
} |
| 129 |
} |
| 130 |
|