| | 1 | <?php |
| | 2 | /** |
| | 3 | * HTTP API: Requests hook bridge class |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage HTTP |
| | 7 | * @since 4.6.0 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Bridge to connect Requests internal hooks to WordPress filters. |
| | 12 | * |
| | 13 | * @package WordPress |
| | 14 | * @subpackage HTTP |
| | 15 | * @since 4.6.0 |
| | 16 | */ |
| | 17 | class WP_Requests_Hooks extends Requests_Hooks { |
| | 18 | /** |
| | 19 | * Request URL. |
| | 20 | * |
| | 21 | * @var string Request URL. |
| | 22 | */ |
| | 23 | protected $url; |
| | 24 | |
| | 25 | /** |
| | 26 | * WordPress request data. |
| | 27 | * |
| | 28 | * @var array Request data in WP_Http format. |
| | 29 | */ |
| | 30 | protected $request = array(); |
| | 31 | |
| | 32 | /** |
| | 33 | * Constructor. |
| | 34 | * |
| | 35 | * @param string $url URL to request. |
| | 36 | * @param array $request Request data in WP_Http format. |
| | 37 | */ |
| | 38 | public function __construct( $url, $request ) { |
| | 39 | $this->url = $url; |
| | 40 | $this->request = $request; |
| | 41 | } |
| | 42 | |
| | 43 | /** |
| | 44 | * Dispatch a message. |
| | 45 | * |
| | 46 | * @param string $hook Hook name. |
| | 47 | * @param array $parameters Parameters to pass to callbacks. |
| | 48 | * @return boolean True if hooks were run, false if nothing was hooked. |
| | 49 | */ |
| | 50 | public function dispatch( $hook, $parameters = array() ) { |
| | 51 | $result = parent::$dispatch( $hook, $parameters ); |
| | 52 | |
| | 53 | // Dispatch native WordPress hooks. |
| | 54 | $this->dispatch_native_hook( $hook, $parameters ); |
| | 55 | |
| | 56 | return $result; |
| | 57 | } |
| | 58 | |
| | 59 | /** |
| | 60 | * Dispatch native WordPress hooks. |
| | 61 | * |
| | 62 | * @param string $hook Requests hook name. |
| | 63 | * @param array $parameters Parameters to pass to callbacks. |
| | 64 | */ |
| | 65 | public function dispatch_native_hook( $hook, $parameters ) { |
| | 66 | switch ( $hook ) { |
| | 67 | case 'curl.before_send': |
| | 68 | do_action_ref_array( 'http_api_curl', array( $parameters[0], $this->request, $this->url ) ); |
| | 69 | break; |
| | 70 | } |
| | 71 | |
| | 72 | /** |
| | 73 | * Handle native internal Requests actions. |
| | 74 | * |
| | 75 | * Requests fires actions internally, but those are not usable in |
| | 76 | * regular WordPress actions. This action maps Requests internal hooks |
| | 77 | * to native WordPress hooks. |
| | 78 | * |
| | 79 | * @param array $parameters Parameters from Requests internal hook. |
| | 80 | */ |
| | 81 | do_action_ref_array( "requests-{$hook}", $parameters ); |
| | 82 | } |
| | 83 | } |