PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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 3.2.4, at includes/class-link.php

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