| @@ -1,51 +1,47 @@ | ||
| 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 | + if ( ! empty( $object_array['summary'] ) ) { | |
| 28 | + $object_array['summary'] = self::the_content( $object_array['summary'] ); | |
| 33 | 29 | } |
| 34 | 30 | |
| 35 | - if ( ! empty( $activity['content'] ) ) { | |
| 36 | - $activity['content'] = self::the_content( $activity['content'] ); | |
| 31 | + if ( ! empty( $object_array['content'] ) ) { | |
| 32 | + $object_array['content'] = self::the_content( $object_array['content'] ); | |
| 37 | 33 | } |
| 38 | 34 | |
| 39 | - return $activity; | |
| 35 | + return $object_array; | |
| 40 | 36 | } |
| 41 | 37 | |
| 42 | 38 | /** |
| 43 | 39 | * Filter to replace the URLS in the content with links |
| 44 | 40 | * |
| 45 | - * @param string $the_content The post content. | |
| 41 | + * @param string $the_content the post-content | |
| 46 | 42 | * |
| 47 | - * @return string the filtered post content. | |
| 43 | + * @return string the filtered post-content | |
| 48 | 44 | */ |
| 49 | 45 | public static function the_content( $the_content ) { |
| 50 | 46 | return enrich_content_data( $the_content, '/' . ACTIVITYPUB_URL_REGEXP . '/i', array( self::class, 'replace_with_links' ) ); |
| 51 | 47 | } |
| @@ -50,22 +46,21 @@ | ||
| 50 | 46 | return enrich_content_data( $the_content, '/' . ACTIVITYPUB_URL_REGEXP . '/i', array( self::class, 'replace_with_links' ) ); |
| 51 | 47 | } |
| 52 | 48 | |
| 53 | 49 | /** |
| 54 | - * A callback for preg_replace to build the links. | |
| 50 | + * A callback for preg_replace to build the links | |
| 55 | 51 | * |
| 56 | 52 | * Link shortening https://docs.joinmastodon.org/api/guidelines/#links |
| 57 | 53 | * |
| 58 | - * @param array $result The preg_match results. | |
| 59 | - * | |
| 60 | - * @return string The final string. | |
| 54 | + * @param array $result the preg_match results | |
| 55 | + * @return string the final string | |
| 61 | 56 | */ |
| 62 | 57 | public static function replace_with_links( $result ) { |
| 63 | - if ( 'www.' === \substr( $result[0], 0, 4 ) ) { | |
| 58 | + if ( 'www.' === substr( $result[0], 0, 4 ) ) { | |
| 64 | 59 | $result[0] = 'https://' . $result[0]; |
| 65 | 60 | } |
| 66 | - $parsed_url = \wp_parse_url( \html_entity_decode( $result[0] ) ); | |
| 67 | - if ( ! $parsed_url || empty( $parsed_url['host'] ) ) { | |
| 61 | + $parsed_url = \wp_parse_url( html_entity_decode( $result[0] ) ); | |
| 62 | + if ( ! $parsed_url ) { | |
| 68 | 63 | return $result[0]; |
| 69 | 64 | } |
| 70 | 65 | |
| 71 | 66 | if ( empty( $parsed_url['scheme'] ) ) { |
| @@ -83,10 +78,10 @@ | ||
| 83 | 78 | $invisible_prefix .= '@'; |
| 84 | 79 | } |
| 85 | 80 | |
| 86 | 81 | $text_url = $parsed_url['host']; |
| 87 | - if ( 'www.' === \substr( $text_url, 0, 4 ) ) { | |
| 88 | - $text_url = \substr( $text_url, 4 ); | |
| 82 | + if ( 'www.' === substr( $text_url, 0, 4 ) ) { | |
| 83 | + $text_url = substr( $text_url, 4 ); | |
| 89 | 84 | $invisible_prefix .= 'www.'; |
| 90 | 85 | } |
| 91 | 86 | if ( ! empty( $parsed_url['port'] ) ) { |
| 92 | 87 | $text_url .= ':' . $parsed_url['port']; |
| @@ -100,9 +95,9 @@ | ||
| 100 | 95 | if ( ! empty( $parsed_url['fragment'] ) ) { |
| 101 | 96 | $text_url .= '#' . $parsed_url['fragment']; |
| 102 | 97 | } |
| 103 | 98 | |
| 104 | - $display = \substr( $text_url, 0, 30 ); | |
| 99 | + $display = \substr( $text_url, 0, 30 ); | |
| 105 | 100 | $invisible_suffix = \substr( $text_url, 30 ); |
| 106 | 101 | |
| 107 | 102 | $display_class = ''; |
| 108 | 103 | if ( $invisible_suffix ) { |
| @@ -108,22 +103,17 @@ | ||
| 108 | 103 | if ( $invisible_suffix ) { |
| 109 | 104 | $display_class .= 'ellipsis'; |
| 110 | 105 | } |
| 111 | 106 | |
| 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' ); | |
| 107 | + $rel = apply_filters( 'activitypub_link_rel', 'nofollow noopener noreferrer' ); | |
| 118 | 108 | |
| 119 | 109 | return \sprintf( |
| 120 | 110 | '<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] ), | |
| 111 | + esc_url( $result[0] ), | |
| 122 | 112 | $rel, |
| 123 | - \esc_html( $invisible_prefix ), | |
| 113 | + esc_html( $invisible_prefix ), | |
| 124 | 114 | $display_class, |
| 125 | - \esc_html( $display ), | |
| 126 | - \esc_html( $invisible_suffix ) | |
| 115 | + esc_html( $display ), | |
| 116 | + esc_html( $invisible_suffix ) | |
| 127 | 117 | ); |
| 128 | 118 | } |
| 129 | 119 | } |