1<?php
2/**
3 * HTTP API: Requests hook bridge class
4 *
5 * @package WordPress
6 * @subpackage HTTP
7 * @since 4.7.0
8 */
9
10/**
11 * Bridge to connect Requests internal hooks to WordPress actions.
12 *
13 * @since 4.7.0
14 *
15 * @see WpOrg\Requests\Hooks
16 */
17#[AllowDynamicProperties]
18class WP_HTTP_Requests_Hooks extends WpOrg\Requests\Hooks {
19 /**
20 * Requested URL.
21 *
22 * @var string Requested URL.
23 */
24 protected $url;
25
26 /**
27 * WordPress WP_HTTP request data.
28 *
29 * @var array Request data in WP_Http format.
30 */
31 protected $request = array();
32
33 /**
34 * Constructor.
35 *
36 * @param string $url URL to request.
37 * @param array $request Request data in WP_Http format.
38 */
39 public function __construct( $url, $request ) {
40 $this->url = $url;
41 $this->request = $request;
42 }
43
44 /**
45 * Dispatch a Requests hook to a native WordPress action.
46 *
47 * @param string $hook Hook name.
48 * @param array $parameters Parameters to pass to callbacks.
49 * @return bool True if hooks were run, false if nothing was hooked.
50 */
51 public function dispatch( $hook, $parameters = array() ) {
52 $result = parent::dispatch( $hook, $parameters );
53
54 // Handle back-compat actions.
55 switch ( $hook ) {
56 case 'curl.before_send':
57 /** This action is documented in wp-includes/class-wp-http-curl.php */
58 do_action_ref_array( 'http_api_curl', array( &$parameters[0], $this->request, $this->url ) );
59 break;
60 }
61
62 /**
63 * Transforms a native Request hook to a WordPress action.
64 *
65 * This action maps Requests internal hook to a native WordPress action.
66 *
67 * @see https://github.com/WordPress/Requests/blob/master/docs/hooks.md
68 *
69 * @since 4.7.0
70 *
71 * @param array $parameters Parameters from Requests internal hook.
72 * @param array $request Request data in WP_Http format.
73 * @param string $url URL to request.
74 */
75 do_action_ref_array( "requests-{$hook}", $parameters, $this->request, $this->url ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
76
77 return $result;
78 }
79}
80