PluginProbe
ActivityPub / 4.1.1
ActivityPub v4.1.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-link.php

class-link.php in ActivityPub 4.1.1, at includes/class-link.php

128 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /* phpcs:ignore Squiz.PHP.CommentedOutCode.Found
32 Only changed it for Person and Group as long is not merged: https://github.com/mastodon/mastodon/pull/28629
33 */
34 if ( ! empty( $activity['summary'] ) && in_array( $activity['type'], array( 'Person', 'Group' ), true ) ) {
35 $activity['summary'] = self::the_content( $activity['summary'] );
36 }
37
38 if ( ! empty( $activity['content'] ) ) {
39 $activity['content'] = self::the_content( $activity['content'] );
40 }
41
42 return $activity;
43 }
44
45 /**
46 * Filter to replace the URLS in the content with links
47 *
48 * @param string $the_content The post content.
49 *
50 * @return string the filtered post content.
51 */
52 public static function the_content( $the_content ) {
53 return enrich_content_data( $the_content, '/' . ACTIVITYPUB_URL_REGEXP . '/i', array( self::class, 'replace_with_links' ) );
54 }
55
56 /**
57 * A callback for preg_replace to build the links.
58 *
59 * Link shortening https://docs.joinmastodon.org/api/guidelines/#links
60 *
61 * @param array $result The preg_match results.
62 *
63 * @return string The final string.
64 */
65 public static function replace_with_links( $result ) {
66 if ( 'www.' === substr( $result[0], 0, 4 ) ) {
67 $result[0] = 'https://' . $result[0];
68 }
69 $parsed_url = \wp_parse_url( html_entity_decode( $result[0] ) );
70 if ( ! $parsed_url || empty( $parsed_url['host'] ) ) {
71 return $result[0];
72 }
73
74 if ( empty( $parsed_url['scheme'] ) ) {
75 $invisible_prefix = 'https://';
76 } else {
77 $invisible_prefix = $parsed_url['scheme'] . '://';
78 }
79 if ( ! empty( $parsed_url['user'] ) ) {
80 $invisible_prefix .= $parsed_url['user'];
81 }
82 if ( ! empty( $parsed_url['pass'] ) ) {
83 $invisible_prefix .= ':' . $parsed_url['pass'];
84 }
85 if ( ! empty( $parsed_url['user'] ) ) {
86 $invisible_prefix .= '@';
87 }
88
89 $text_url = $parsed_url['host'];
90 if ( 'www.' === substr( $text_url, 0, 4 ) ) {
91 $text_url = substr( $text_url, 4 );
92 $invisible_prefix .= 'www.';
93 }
94 if ( ! empty( $parsed_url['port'] ) ) {
95 $text_url .= ':' . $parsed_url['port'];
96 }
97 if ( ! empty( $parsed_url['path'] ) ) {
98 $text_url .= $parsed_url['path'];
99 }
100 if ( ! empty( $parsed_url['query'] ) ) {
101 $text_url .= '?' . $parsed_url['query'];
102 }
103 if ( ! empty( $parsed_url['fragment'] ) ) {
104 $text_url .= '#' . $parsed_url['fragment'];
105 }
106
107 $display = \substr( $text_url, 0, 30 );
108 $invisible_suffix = \substr( $text_url, 30 );
109
110 $display_class = '';
111 if ( $invisible_suffix ) {
112 $display_class .= 'ellipsis';
113 }
114
115 $rel = apply_filters( 'activitypub_link_rel', 'nofollow noopener noreferrer' );
116
117 return \sprintf(
118 '<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>',
119 esc_url( $result[0] ),
120 $rel,
121 esc_html( $invisible_prefix ),
122 $display_class,
123 esc_html( $display ),
124 esc_html( $invisible_suffix )
125 );
126 }
127 }
128