DataInconsistency
2 weeks ago
License
2 months ago
Notices
2 months ago
pQuery
2 months ago
APIPermissionHelper.php
1 year ago
CdnAssetUrl.php
3 years ago
ConflictResolver.php
1 month ago
Cookies.php
2 months ago
DBCollationChecker.php
2 months ago
DOM.php
2 years ago
DateConverter.php
3 years ago
FreeDomains.php
3 years ago
Headers.php
1 year ago
Helpers.php
1 month ago
Installation.php
1 year ago
LegacyDatabase.php
1 year ago
Request.php
2 months ago
SecondLevelDomainNames.php
3 years ago
Security.php
2 months ago
ThirdPartyOutput.php
1 month ago
Url.php
2 months ago
index.php
3 years ago
Url.php
80 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Util; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\WP\Functions as WPFunctions; |
| 9 | |
| 10 | class Url { |
| 11 | /** @var WPFunctions */ |
| 12 | private $wp; |
| 13 | |
| 14 | public function __construct( |
| 15 | WPFunctions $wp |
| 16 | ) { |
| 17 | $this->wp = $wp; |
| 18 | } |
| 19 | |
| 20 | public function getCurrentUrl() { |
| 21 | $homeUrl = parse_url($this->wp->homeUrl()); |
| 22 | $queryArgs = $this->wp->addQueryArg(null, null); |
| 23 | |
| 24 | // Remove $this->wp->homeUrl() path from add_query_arg |
| 25 | if ( |
| 26 | is_array($homeUrl) |
| 27 | && isset($homeUrl['path']) |
| 28 | ) { |
| 29 | $queryArgs = str_replace($homeUrl['path'], '', $queryArgs); |
| 30 | } |
| 31 | |
| 32 | return $this->wp->homeUrl($queryArgs); |
| 33 | } |
| 34 | |
| 35 | public function redirectTo($url = null) { |
| 36 | $this->wp->wpSafeRedirect($url); |
| 37 | exit(); |
| 38 | } |
| 39 | |
| 40 | public function redirectBack($params = []) { |
| 41 | // check mailpoet_redirect parameter |
| 42 | $referer = (isset($_POST['mailpoet_redirect']) && is_string($_POST['mailpoet_redirect']) |
| 43 | ? sanitize_text_field(wp_unslash($_POST['mailpoet_redirect'])) |
| 44 | : $this->wp->wpGetReferer() |
| 45 | ); |
| 46 | |
| 47 | // fallback: home_url |
| 48 | if (!$referer) { |
| 49 | $referer = $this->wp->homeUrl(); |
| 50 | } |
| 51 | |
| 52 | // append extra params to url |
| 53 | if (!empty($params)) { |
| 54 | $referer = $this->wp->addQueryArg($params, $referer); |
| 55 | } |
| 56 | |
| 57 | $this->redirectTo($referer); |
| 58 | exit(); |
| 59 | } |
| 60 | |
| 61 | public function redirectWithReferer($url = null) { |
| 62 | $currentUrl = $this->getCurrentUrl(); |
| 63 | $url = $this->wp->addQueryArg( |
| 64 | [ |
| 65 | 'mailpoet_redirect' => urlencode($currentUrl), |
| 66 | ], |
| 67 | $url |
| 68 | ); |
| 69 | |
| 70 | if ($url !== $currentUrl) { |
| 71 | $this->redirectTo($url); |
| 72 | } |
| 73 | exit(); |
| 74 | } |
| 75 | |
| 76 | public function isUsingHttps(string $url): bool { |
| 77 | return $this->wp->wpParseUrl($url, PHP_URL_SCHEME) === 'https'; |
| 78 | } |
| 79 | } |
| 80 |