PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.0
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-embed.php

class-embed.php in ActivityPub 5.7.0, at includes/class-embed.php

114 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ActivityPub Embed Handler.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub;
9
10 use WP_REST_Response;
11
12 /**
13 * Class to handle embedding ActivityPub content
14 */
15 class Embed {
16
17 /**
18 * Initialize the embed handler
19 */
20 public static function init() {
21 add_filter( 'pre_oembed_result', array( __CLASS__, 'maybe_use_activitypub_embed' ), 10, 3 );
22 add_filter( 'oembed_dataparse', array( __CLASS__, 'handle_filtered_oembed_result' ), 11, 3 );
23 }
24
25 /**
26 * Check if a real oEmbed result exists for the given URL.
27 *
28 * @param string $url The URL to check.
29 * @param array $args Additional arguments passed to wp_oembed_get().
30 * @return bool True if a real oEmbed result exists, false otherwise.
31 */
32 public static function has_real_oembed( $url, $args = array() ) {
33 // Temporarily remove our filter to avoid infinite loops.
34 remove_filter( 'pre_oembed_result', array( __CLASS__, 'maybe_use_activitypub_embed' ), 10, 3 );
35
36 // Try to get a "real" oEmbed result. If found, it'll be cached to avoid unnecessary HTTP requests in `wp_oembed_get`.
37 $oembed_result = wp_oembed_get( $url, $args );
38
39 // Add our filter back.
40 add_filter( 'pre_oembed_result', array( __CLASS__, 'maybe_use_activitypub_embed' ), 10, 3 );
41
42 return false !== $oembed_result;
43 }
44
45 /**
46 * Filter the oembed result to handle ActivityPub content when no oEmbed is found.
47 * Implementation is a bit weird because there's no way to filter on a false result, we have to use `pre_oembed_result`.
48 *
49 * @param null|string $result The UNSANITIZED (and potentially unsafe) HTML that should be used to embed.
50 * @param string $url The URL to the content that should be attempted to be embedded.
51 * @param array $args Additional arguments passed to wp_oembed_get().
52 * @return null|string Return null to allow normal oEmbed processing, or string for ActivityPub embed.
53 */
54 public static function maybe_use_activitypub_embed( $result, $url, $args ) {
55 // If we already have a result, return it.
56 if ( null !== $result ) {
57 return $result;
58 }
59
60 // If we found a real oEmbed, return null to allow normal processing.
61 if ( self::has_real_oembed( $url, $args ) ) {
62 return null;
63 }
64
65 // No oEmbed found, try to get ActivityPub representation.
66 $html = get_embed_html( $url );
67
68 // If we couldn't get an ActivityPub embed either, return null to allow normal processing.
69 if ( ! $html ) {
70 return null;
71 }
72
73 // Return the ActivityPub embed HTML.
74 return $html;
75 }
76
77 /**
78 * Handle cases where WordPress has filtered out the oEmbed result for security reasons,
79 * but we can provide a safe ActivityPub-specific markup.
80 *
81 * This runs after wp_filter_oembed_result has potentially nullified the result.
82 *
83 * @param string|false $html The returned oEmbed HTML.
84 * @param object $data A data object result from an oEmbed provider.
85 * @param string $url The URL of the content to be embedded.
86 * @return string|false The filtered oEmbed HTML or our ActivityPub embed.
87 */
88 public static function handle_filtered_oembed_result( $html, $data, $url ) {
89 // If we already have valid HTML, return it.
90 if ( $html ) {
91 return $html;
92 }
93
94 // If this isn't a rich or video type, we can't help.
95 if ( ! isset( $data->type ) || ! in_array( $data->type, array( 'rich', 'video' ), true ) ) {
96 return $html;
97 }
98
99 // If there's no HTML in the data, we can't help.
100 if ( empty( $data->html ) || ! is_string( $data->html ) ) {
101 return $html;
102 }
103
104 // Try to get ActivityPub representation.
105 $activitypub_html = get_embed_html( $url );
106 if ( ! $activitypub_html ) {
107 return $html;
108 }
109
110 // Return our safer ActivityPub embed HTML.
111 return $activitypub_html;
112 }
113 }
114