Skip to content
Merged
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
48 changes: 6 additions & 42 deletions features/requests.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,21 @@ Feature: Requests integration with both v1 and v2
When I run `wp eval 'var_dump( \WP_CLI\Utils\http_request( "GET", "https://example.com/" ) );'`
Then STDOUT should contain:
"""
object(WpOrg\Requests\Response)
"""
And STDOUT should contain:
"""
HTTP/1.1 200 OK
"""
And STDERR should be empty

When I run `wp eval 'var_dump( \WpOrg\Requests\Requests::get( "https://example.com/" ) );'`
Then STDOUT should contain:
"""
object(WpOrg\Requests\Response)
object(Requests_Response)
"""
And STDOUT should contain:
"""
HTTP/1.1 200 OK
"""
And STDERR should be empty

When I run `wp eval 'var_dump( \Requests::get( "https://example.com/" ) );'`
When I run `wp plugin install duplicate-post`
Then STDOUT should contain:
"""
object(WpOrg\Requests\Response)
Success: Installed 1 of 1 plugins.
"""
And STDOUT should contain:
"""
HTTP/1.1 200 OK
"""
And STDERR should be empty

Scenario: Current version with WordPress-bundled Requests v2
Scenario: Current version with WordPress-bundled Requests v2
Given a WP installation
And I run `wp core update --version=6.2 --force`

Expand All @@ -107,28 +91,8 @@ Feature: Requests integration with both v1 and v2
"""
And STDERR should be empty

When I run `wp eval 'var_dump( \WpOrg\Requests\Requests::get( "https://example.com/" ) );'`
When I run `wp plugin install duplicate-post`
Then STDOUT should contain:
"""
object(WpOrg\Requests\Response)
"""
And STDOUT should contain:
"""
HTTP/1.1 200 OK
"""
And STDERR should be empty

# Expect a deprecation warning here.
When I try `wp eval 'var_dump( \Requests::get( "https://example.com/" ) );'`
Then STDOUT should contain:
"""
object(WpOrg\Requests\Response)
"""
And STDOUT should contain:
"""
HTTP/1.1 200 OK
"""
And STDERR should contain:
"""
The PSR-0 `Requests_...` class names in the Requests library are deprecated.
Success: Installed 1 of 1 plugins.
"""
36 changes: 0 additions & 36 deletions php/WP_CLI/Bootstrap/ExtractDefaultCaCertificate.php

This file was deleted.

3 changes: 1 addition & 2 deletions php/WP_CLI/Bootstrap/IncludeFrameworkAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class IncludeFrameworkAutoloader.
*
* Loads the framework autoloader through an autolaoder separate from the
* Loads the framework autoloader through an autoloader separate from the
* Composer one, to avoid coupling the loading of the framework with bundled
* commands.
*
Expand All @@ -34,7 +34,6 @@ public function process( BootstrapState $state ) {
$mappings = [
'WP_CLI' => WP_CLI_ROOT . '/php/WP_CLI',
'cli' => WP_CLI_VENDOR_DIR . '/wp-cli/php-cli-tools/lib/cli',
'WpOrg\Requests' => WP_CLI_VENDOR_DIR . '/rmccue/requests/src',
'Symfony\Component\Finder' => WP_CLI_VENDOR_DIR . '/symfony/finder/',
];

Expand Down
117 changes: 117 additions & 0 deletions php/WP_CLI/Bootstrap/IncludeRequestsAutoloader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace WP_CLI\Bootstrap;

use WP_CLI\Autoloader;
use WP_CLI\RequestsLibrary;

/**
* Class IncludeRequestsAutoloader.
*
* Loads the Requests autoloader that best fits the current environment.
*
* If a WordPress installation is found, it autoloads that version of Requests.
* Otherwise, it loads the version of Requests bundled with WP-CLI.
*
* This is done in order to avoid conflicts between Requests versions.
*
* @package WP_CLI\Bootstrap
*/
final class IncludeRequestsAutoloader implements BootstrapStep {

/**
* Requests is being used from the WordPress installation.
*
* @var string
*/
const FROM_WP_CORE = 'wp-core';

/**
* Requests is being used from the WP-CLI dependencies.
*
* @var string
*/
const FROM_WP_CLI = 'wp-cli';

/**
* Process this single bootstrapping step.
*
* @param BootstrapState $state Contextual state to pass into the step.
*
* @return BootstrapState Modified state to pass to the next step.
*/
public function process( BootstrapState $state ) {
// If Requests is already loaded, don't do anything.
if ( class_exists( RequestsLibrary::CLASS_NAME_V2, false ) || class_exists( RequestsLibrary::CLASS_NAME_V1, false ) ) {
return;
}

$runner = new RunnerInstance();

// Make sure we don't deal with an invalid `--path` value.
$config = $runner()->config;
if ( isset( $config['path'] ) &&
( is_bool( $config['path'] ) || empty( $config['path'] ) )
) {
return $state;
}

$wp_root = rtrim( $runner()->find_wp_root(), '/' );

// First try to detect a newer Requests version bundled with WordPress.
if ( file_exists( $wp_root . '/wp-includes/Requests/src/Autoload.php' ) ) {
require_once $wp_root . '/wp-includes/Requests/src/Autoload.php';

\WpOrg\Requests\Autoload::register();

$this->store_requests_meta( RequestsLibrary::CLASS_NAME_V2, self::FROM_WP_CORE );

return $state;
}

// Then see if we can detect the older version bundled with WordPress.
if ( file_exists( $wp_root . '/wp-includes/class-requests.php' ) ) {
require_once $wp_root . '/wp-includes/class-requests.php';

\Requests::register_autoloader();

$this->store_requests_meta( RequestsLibrary::CLASS_NAME_V1, self::FROM_WP_CORE );

return $state;
}

// Finally, fall back to the Requests version bundled with WP-CLI.
$autoloader = new Autoloader();
$autoloader->add_namespace(
'WpOrg\Requests',
WP_CLI_VENDOR_DIR . '/rmccue/requests/src'
);

$autoloader->register();

\WpOrg\Requests\Autoload::register();

$this->store_requests_meta( RequestsLibrary::CLASS_NAME_V2, self::FROM_WP_CLI );

return $state;
}

/**
* Store meta information about the used Requests integration.
*
* This can be used for all the conditional code that needs to work
* across multiple Requests versions.
*
* @param string $class_name The class name of the Requests integration.
* @param string $source The source of the Requests integration.
*/
private function store_requests_meta( $class_name, $source ) {
RequestsLibrary::set_version(
RequestsLibrary::CLASS_NAME_V2 === $class_name
? RequestsLibrary::VERSION_V2
: RequestsLibrary::VERSION_V1
);
RequestsLibrary::set_source( $source );
RequestsLibrary::set_class_name( $class_name );
}
}
Loading