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