robotstxt-telemetry/includes/class-robotstxt-telemetry-endpoints.php
2026-08-20 14:31:43 +00:00

190 lines
5.3 KiB
PHP

<?php
/**
* Endpoint rewrites and blocks.
*
* Intercepts outbound requests to WordPress.org services at the HTTP
* layer and, depending on the settings (all enabled by default):
*
* - Replaces the WordPress News feed with WordPress Planet by Fair.
* - Replaces the WordPress Events service with WordPress Events by
* The WP World, sending the same request data.
* - Blocks the Browse Happy browser check, answering that the browser
* is compatible.
*
* Also rewrites the locale query parameters when the installation
* language masking is enabled (the rewrite must happen at this layer
* because WordPress does not provide a request-URL filter).
*
* @package RobotstxtTelemetry
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Robotstxt_Telemetry_Endpoints' ) ) {
/**
* Class Robotstxt_Telemetry_Endpoints
*/
class Robotstxt_Telemetry_Endpoints {
/**
* Replacement URL for the WordPress News feed.
*
* @var string
*/
const NEWS_FEED_URL = 'https://planet.fair.pm/atom.xml';
/**
* Replacement URL for the WordPress Events service.
*
* @var string
*/
const EVENTS_API_URL = 'https://api.fair.pm/fair/v1/events';
/**
* Register hooks.
*
* @return void
*/
public function register() {
add_filter( 'pre_http_request', array( $this, 'intercept' ), 10, 3 );
}
/**
* Intercept outbound WordPress.org requests.
*
* @param false|array<string,mixed>|WP_Error $preempt Short-circuit value from other filters.
* @param array{body?:string|array<mixed,mixed>,headers?:string|array<mixed,mixed>} $args HTTP request arguments.
* @param string $url Request URL.
* @return false|array<string,mixed>|WP_Error
*/
public function intercept( $preempt, $args, $url ) {
if ( false !== $preempt ) {
return $preempt;
}
$parsed = wp_parse_url( $url );
if ( empty( $parsed['host'] ) ) {
return $preempt;
}
$host = strtolower( $parsed['host'] );
$path = isset( $parsed['path'] ) ? $parsed['path'] : '';
if ( 'api.wordpress.org' === $host ) {
// Browse Happy: answer that the browser is compatible.
if ( 0 === stripos( $path, '/core/browse-happy' ) && $this->setting( 'robotstxt_telemetry_disable_browse_happy' ) ) {
return $this->mock_browse_happy_response();
}
// Events: proxy to the replacement service, same request data.
if ( 0 === stripos( $path, '/events' ) && $this->setting( 'robotstxt_telemetry_replace_events_api' ) ) {
$query = isset( $parsed['query'] ) ? '?' . $parsed['query'] : '';
return wp_remote_request( self::EVENTS_API_URL . $query, $args );
}
// Locale masking: proxy to the rewritten URL. The WordPress
// version is never masked in query parameters: the update
// and information APIs need the real version to work.
if ( 0 !== stripos( $path, '/translations' ) && $this->mask_locale_enabled() ) {
$masked = $this->mask_locale_url( $url );
if ( $masked !== $url ) {
return wp_remote_request( $masked, $args );
}
}
return $preempt;
}
// News feed: proxy to the replacement feed (also covers Rosetta hosts).
$is_wordpress_host = 'wordpress.org' === $host || '.wordpress.org' === substr( $host, -strlen( '.wordpress.org' ) );
if ( $is_wordpress_host && '/news/feed/' === $path && $this->setting( 'robotstxt_telemetry_replace_news_feed' ) ) {
return wp_remote_request( self::NEWS_FEED_URL, $args );
}
return $preempt;
}
/**
* Build a successful Browse Happy response marking the browser
* as up to date and secure, so no browser nag is shown.
*
* @return array<string,mixed>
*/
private function mock_browse_happy_response() {
$body = wp_json_encode(
array(
'platform' => 'other',
'name' => 'Browser',
'version' => '1.0',
'current_version' => '1.0',
'upgrade' => false,
'insecure' => false,
'update_url' => '',
'img_src' => '',
'img_src_ssl' => '',
)
);
return array(
'headers' => array(),
'body' => $body,
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => null,
);
}
/**
* Whether the installation language should be masked as en_US.
*
* @return bool
*/
private function mask_locale_enabled() {
return (bool) Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_mask_locale', false );
}
/**
* Whether a feature setting is enabled.
*
* @param string $name Option name.
* @return bool
*/
private function setting( $name ) {
return (bool) Robotstxt_Telemetry_Network::get_setting( $name, true );
}
/**
* Replace the locale query parameters of a WordPress.org URL.
*
* Covers standalone `locale` parameters and `request[locale]`
* parameters (also URL-encoded).
*
* @param string $url Request URL.
* @return string
*/
private function mask_locale_url( $url ) {
$masked = preg_replace(
'/([?&](?:request(?:%5[Bb]|\[)locale(?:%5[Dd]|\])|locale)=)[^&]*/',
'${1}en_US',
$url,
-1,
$count
);
if ( $count > 0 && null !== $masked ) {
return $masked;
}
return $url;
}
}
}