暫無描述

Url.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace MailPoet\Util;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\WP\Functions as WPFunctions;
  5. class Url {
  6. /** @var WPFunctions */
  7. private $wp;
  8. public function __construct(
  9. WPFunctions $wp
  10. ) {
  11. $this->wp = $wp;
  12. }
  13. public function getCurrentUrl() {
  14. $homeUrl = parse_url($this->wp->homeUrl());
  15. $queryArgs = $this->wp->addQueryArg(null, null);
  16. // Remove $this->wp->homeUrl() path from add_query_arg
  17. if (
  18. is_array($homeUrl)
  19. && isset($homeUrl['path'])
  20. ) {
  21. $queryArgs = str_replace($homeUrl['path'], '', $queryArgs);
  22. }
  23. return $this->wp->homeUrl($queryArgs);
  24. }
  25. public function redirectTo($url = null) {
  26. $this->wp->wpSafeRedirect($url);
  27. exit();
  28. }
  29. public function redirectBack($params = []) {
  30. // check mailpoet_redirect parameter
  31. $referer = (isset($_POST['mailpoet_redirect'])
  32. ? $_POST['mailpoet_redirect']
  33. : $this->wp->wpGetReferer()
  34. );
  35. // fallback: home_url
  36. if (!$referer) {
  37. $referer = $this->wp->homeUrl();
  38. }
  39. // append extra params to url
  40. if (!empty($params)) {
  41. $referer = $this->wp->addQueryArg($params, $referer);
  42. }
  43. $this->redirectTo($referer);
  44. exit();
  45. }
  46. public function redirectWithReferer($url = null) {
  47. $currentUrl = $this->getCurrentUrl();
  48. $url = $this->wp->addQueryArg(
  49. [
  50. 'mailpoet_redirect' => urlencode($currentUrl),
  51. ],
  52. $url
  53. );
  54. if ($url !== $currentUrl) {
  55. $this->redirectTo($url);
  56. }
  57. exit();
  58. }
  59. }