Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Controllers/HTTPTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use Intraxia\Jaxion\Contract\Core\HasActions;
use PressForward\Interfaces\System;
use mattwright\URLResolver;
use PressForward\Libraries\PF_URL_Resolver;

/**
* HTTP utilities.
Expand All @@ -19,7 +19,7 @@ class HTTPTools implements HasActions {
* URLResolver object.
*
* @access public
* @var \mattwright\URLResolver
* @var \PressForward\Libraries\PF_URL_Resolver
*/
public $url_resolver;

Expand All @@ -42,11 +42,11 @@ class HTTPTools implements HasActions {
/**
* Constructor.
*
* @param \mattwright\URLResolver $resolver URLResolver object.
* @param \PressForward\Interfaces\System $system System object.
* @param \PressForward\Controllers\Metas $meta Metas object.
* @param \PressForward\Libraries\PF_URL_Resolver $resolver URLResolver object.
* @param \PressForward\Interfaces\System $system System object.
* @param \PressForward\Controllers\Metas $meta Metas object.
*/
public function __construct( URLResolver $resolver, System $system, Metas $meta ) {
public function __construct( PF_URL_Resolver $resolver, System $system, Metas $meta ) {
$this->url_resolver = $resolver;
$this->system = $system;
$this->meta = $meta;
Expand Down Expand Up @@ -122,7 +122,7 @@ public function resolve_a_url( $url ) {
return $url;
} else {
$check = $this->url_is_aggregation_service( $url );
if ( $check && in_array( 'curl', get_loaded_extensions(), true ) ) {
if ( $check ) {
$url = $this->url_resolver->resolveURL( $url )->getURL();
}
}
Expand Down
19 changes: 4 additions & 15 deletions Core/API/MetaCheckEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PressForward\Core\Admin\PFTemplater;
use PressForward\Controllers\PF_JWT;
use PFOpenGraph;
use mattwright\URLResolver;

use WP_Error;

Expand Down Expand Up @@ -42,29 +41,19 @@ class MetaCheckEndpoint implements \Intraxia\Jaxion\Contract\Core\HasActions {
*/
public $og;

/**
* URL Resolver object.
*
* @access public
* @var \mattwright\URLResolver
*/
public $url_resolver;

/**
* Constructor.
*
* @param array $api_base API base data.
* @param \PressForward\Controllers\PF_JWT $jwt PF_JWT object.
* @param PFOpenGraph $og PFOpenGraph object.
* @param \mattwright\URLResolver $url_resolver URLResolver object.
* @param array $api_base API base data.
* @param \PressForward\Controllers\PF_JWT $jwt PF_JWT object.
* @param PFOpenGraph $og PFOpenGraph object.
*/
public function __construct( $api_base, PF_JWT $jwt, PFOpenGraph $og, URLResolver $url_resolver ) {
public function __construct( $api_base, PF_JWT $jwt, PFOpenGraph $og ) {
$this->api_base = $api_base;
$this->api_base['endpoint'] = 'metachecks';
$this->og = $og;
$namespace = $this->api_base['base_namespace'] . $this->api_base['version'];
$base = $this->api_base['endpoint'];
$this->url_resolver = $url_resolver;
$this->jwt = $jwt;
}

Expand Down
4 changes: 2 additions & 2 deletions Core/Providers/LibrariesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
use Intraxia\Jaxion\Contract\Core\Container;

use PressForward\Libraries\HTMLChecker;
use PressForward\Libraries\PF_URL_Resolver;
use PFOpenGraph;
use AlertBox\The_Alert_Box;
use mattwright\URLResolver;

/**
* LibrariesProvider class.
Expand All @@ -27,7 +27,7 @@ public function register( Container $container ) {
$container->define(
'library.url_resolver',
function () {
return new URLResolver();
return new PF_URL_Resolver();
}
);

Expand Down
112 changes: 112 additions & 0 deletions Libraries/PF_URL_Resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* URL Resolver for PressForward.
*
* A simple URL resolver that follows redirects to find the final URL.
* Replaces the abandoned mattwright/urlresolver library.
*
* @package PressForward
*/

namespace PressForward\Libraries;

/**
* URL Resolver class.
*
* Resolves URLs by following redirects using WordPress HTTP API.
* This class provides a compatible interface with mattwright\URLResolver.
*/
class PF_URL_Resolver {
/**
* The resolved final URL.
*
* @var string
*/
protected $final_url;

/**
* Resolve a URL by following redirects.
*
* @param string $url The URL to resolve.
* @return self Returns this instance for method chaining.
*/
public function resolveURL( $url ) {
$this->final_url = $this->resolve( $url );
return $this;
}

/**
* Get the resolved URL.
*
* @return string The final resolved URL.
*/
public function getURL() {
return $this->final_url;
}

/**
* Actually resolve the URL using wp_remote_head.
*
* Uses WordPress HTTP API to follow redirects and find the final URL.
* This is a simplified replacement for the abandoned mattwright/urlresolver
* library that uses wp_remote_head() instead of raw cURL.
*
* Since WordPress's wp_remote_head() doesn't easily expose the final URL
* after following redirects, we manually follow them by checking Location
* headers.
*
* @param string $url The URL to resolve.
* @return string The final URL after following redirects.
*/
protected function resolve( $url ) {
$max_redirects = 10;
$current_url = $url;

for ( $i = 0; $i < $max_redirects; $i++ ) {
// Make a HEAD request but don't auto-follow redirects.
// We want to manually follow them so we can track the final URL.
$response = wp_remote_head(
$current_url,
array(
'redirection' => 0, // Don't auto-follow redirects.
'timeout' => 30, // 30 second timeout.
)
);

// If there was an error, return the current URL.
if ( is_wp_error( $response ) ) {
return $current_url;
}

$response_code = wp_remote_retrieve_response_code( $response );

// If we got a redirect status code (301, 302, 303, 307, 308),
// follow the Location header.
if ( in_array( $response_code, array( 301, 302, 303, 307, 308 ), true ) ) {
$location = wp_remote_retrieve_header( $response, 'location' );

if ( empty( $location ) ) {
// Redirect without Location header - return current URL.
return $current_url;
}

// Handle relative URLs.
if ( 0 === strpos( $location, '/' ) ) {
$parsed_url = wp_parse_url( $current_url );
$scheme = $parsed_url['scheme'] ?? 'http';
$host = $parsed_url['host'] ?? '';
$location = $scheme . '://' . $host . $location;
}

// Update current URL and continue loop.
$current_url = $location;
} else {
// Not a redirect - we've reached the final URL.
return $current_url;
}
}

// If we've exhausted max redirects, return the current URL.
return $current_url;
}
}
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"firebase/php-jwt": "^6.11",
"paragonie/random_compat": "^9.99",
"davechild/textstatistics": "^1.0",
"mattwright/urlresolver": "^2.0",
"fivefilters/readability.php": "~3.0.0",
"psr/log": "^1.0"
}
Expand Down