|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class file for Twitter_Embeds |
| 4 | + * |
| 5 | + |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + * |
| 10 | + * @package wp-alleyvate |
| 11 | + */ |
| 12 | + |
| 13 | +declare( strict_types=1 ); |
| 14 | + |
| 15 | +namespace Alley\WP\Alleyvate\Features; |
| 16 | + |
| 17 | +use Alley\WP\Types\Feature; |
| 18 | +use WP_Error; |
| 19 | +use WpOrg\Requests\Transport\Fsockopen; |
| 20 | + |
| 21 | +/** |
| 22 | + * Twitter_Embeds feature. |
| 23 | + */ |
| 24 | +final class Twitter_Embeds implements Feature { |
| 25 | + /** |
| 26 | + * Array of attempts to catch 404 responses from Twitter. |
| 27 | + * |
| 28 | + * @var int[] $attempts |
| 29 | + */ |
| 30 | + private array $attempts = []; |
| 31 | + |
| 32 | + /** |
| 33 | + * Boot the feature. |
| 34 | + */ |
| 35 | + public function boot(): void { |
| 36 | + add_filter( 'oembed_providers', [ $this, 'add_twitter_oembed_provider' ] ); |
| 37 | + add_filter( 'http_response', [ $this, 'filter_twitter_oembed_404s' ], 10, 3 ); |
| 38 | + add_filter( 'alleyvate_twitter_embeds_404_backstop', [ $this, 'attempt_404_backstop' ], 10, 3 ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Add Twitter oEmbed provider. |
| 43 | + * |
| 44 | + * @param array{string, boolean}[] $providers Array of oEmbed providers. |
| 45 | + * @return array{string, boolean}[] |
| 46 | + */ |
| 47 | + public function add_twitter_oembed_provider( array $providers ): array { |
| 48 | + /* phpcs:disable WordPress.Arrays.MultipleStatementAlignment */ |
| 49 | + return array_merge( |
| 50 | + $providers, |
| 51 | + [ |
| 52 | + '#https?://(www\.)?x\.com/\w{1,15}/status(es)?/.*#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 53 | + '#https?://(www\.)?x\.com/\w{1,15}$#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 54 | + '#https?://(www\.)?x\.com/\w{1,15}/likes$#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 55 | + '#https?://(www\.)?x\.com/\w{1,15}/lists/.*#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 56 | + '#https?://(www\.)?x\.com/\w{1,15}/timelines/.*#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 57 | + '#https?://(www\.)?x\.com/i/moments/.*#i' => [ 'https://publish.twitter.com/oembed', true ], |
| 58 | + ], |
| 59 | + ); |
| 60 | + /* phpcs:enable WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned */ |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Attempt to catch 404 responses from Twitter. |
| 65 | + * |
| 66 | + * @param array<mixed>|WP_Error $response HTTP response. |
| 67 | + * @param array<mixed> $parsed_args HTTP request arguments. |
| 68 | + * @param string $url URL of the HTTP request. |
| 69 | + * @return array<mixed>|WP_Error |
| 70 | + */ |
| 71 | + public function filter_twitter_oembed_404s( array|WP_Error $response, array $parsed_args, string $url ): array|WP_Error { |
| 72 | + if ( |
| 73 | + strpos( $url, 'publish.twitter.com' ) !== false |
| 74 | + && ! is_wp_error( $response ) |
| 75 | + && 404 === $response['response']['code'] /* @phpstan-ignore offsetAccess.nonOffsetAccessible */ |
| 76 | + ) { |
| 77 | + $this->attempts[ $url ] = ( $this->attempts[ $url ] ?? 0 ) + 1; |
| 78 | + |
| 79 | + /** |
| 80 | + * Filter the response for a 404 from Twitter. |
| 81 | + * |
| 82 | + * @param array $response HTTP response. |
| 83 | + * @param string $url URL of the HTTP request. |
| 84 | + * @param int $attempts Number of times this filter has fired for this URL during this request. |
| 85 | + * @param array $parsed_args HTTP request arguments. |
| 86 | + */ |
| 87 | + return apply_filters( |
| 88 | + 'alleyvate_twitter_embeds_404_backstop', |
| 89 | + $response, |
| 90 | + $url, |
| 91 | + $this->attempts[ $url ], |
| 92 | + $parsed_args |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return $response; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Attempt to catch 404 responses from Twitter. |
| 101 | + * |
| 102 | + * @param array<mixed> $response HTTP response. |
| 103 | + * @param string $url URL of the HTTP request. |
| 104 | + * @param int $attempts Number of times this filter has fired for this URL during this request. |
| 105 | + * @return array<mixed> |
| 106 | + */ |
| 107 | + public function attempt_404_backstop( array $response, string $url, int $attempts ): array|WP_Error { |
| 108 | + if ( 1 === $attempts ) { |
| 109 | + $env_endpoint = \function_exists( 'vip_get_env_var' ) |
| 110 | + ? vip_get_env_var( 'TWITTER_OEMBED_BACKSTOP_ENDPOINT' ) |
| 111 | + : getenv( 'TWITTER_OEMBED_BACKSTOP_ENDPOINT' ); |
| 112 | + if ( $env_endpoint ) { |
| 113 | + // If there's a backstop endpoint defined, use it. |
| 114 | + $url = str_replace( 'https://publish.twitter.com/oembed', $env_endpoint, $url ); |
| 115 | + $response = wp_safe_remote_get( $url ); |
| 116 | + } else { |
| 117 | + // Attempt the request again using the fsockopen transport, which might get a different outcome. |
| 118 | + add_action( 'requests-requests.before_request', [ $this, 'attempt_fsockopen_for_twitter_oembeds' ], 10, 5 ); |
| 119 | + $response = wp_safe_remote_get( $url ); |
| 120 | + remove_action( 'requests-requests.before_request', [ $this, 'attempt_fsockopen_for_twitter_oembeds' ] ); |
| 121 | + } |
| 122 | + } |
| 123 | + return $response; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Attempt to use fsockopen transport for Twitter oEmbeds. |
| 128 | + * |
| 129 | + * @param string $url URL of the HTTP request. |
| 130 | + * @param array<string> $headers HTTP request headers. Ignored. |
| 131 | + * @param array<mixed> $data HTTP request data. Ignored. |
| 132 | + * @param string $type HTTP request type. Ignored. |
| 133 | + * @param array<mixed> $options HTTP request options. Passed by reference. |
| 134 | + */ |
| 135 | + public function attempt_fsockopen_for_twitter_oembeds( &$url, &$headers, &$data, &$type, &$options ): void { |
| 136 | + if ( class_exists( Fsockopen::class ) && str_starts_with( $url, 'https://publish.twitter.com/oembed' ) ) { |
| 137 | + $options['transport'] = Fsockopen::class; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments