class-wp-http-requests-hooks.php 2.0 KB

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