-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53667 "Speaking" URL for address detail pages (#929)
* 53667 speaking url for address detail pages * 53667 refactor code & update unit test * 53667 refactor code * 53667 update unit test * 53667 refactor code * 53667 update ubuntu v * Refactoring Redirector class for improved reusability p#119769 * Fix unit tests p#119769 --------- Co-authored-by: Frederic Alpers <88546396+fredericalpers@users.noreply.github.com> Co-authored-by: Anja Möller <22166320+andernath@users.noreply.github.com> Co-authored-by: andernath <chibineko@gmail.com>
- Loading branch information
1 parent
71ecd87
commit b2e3e2c
Showing
19 changed files
with
1,561 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
/** | ||
* | ||
* Copyright (C) 2024 onOffice GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
declare (strict_types=1); | ||
|
||
namespace onOffice\WPlugin\Controller; | ||
|
||
|
||
class AddressDetailUrl | ||
{ | ||
/** | ||
* @param string $url | ||
* @param int $addressId | ||
* @param string|null $title | ||
* @param string|null $oldUrl | ||
* @param bool $flag | ||
* | ||
* @return string | ||
*/ | ||
|
||
public function createAddressDetailLink( | ||
string $url, | ||
int $addressId, | ||
string $title = null, | ||
string $oldUrl = null, | ||
bool $flag = false): string | ||
{ | ||
$urlLsSwitcher = $url; | ||
$slashChar = ''; | ||
|
||
if ($addressId !== 0) { | ||
$urlElements = parse_url($url); | ||
$getParameters = []; | ||
|
||
if (!empty($urlElements['query'])) { | ||
parse_str($urlElements['query'], $getParameters); | ||
} | ||
|
||
if (!is_null($oldUrl)) { | ||
$oldUrlElements = parse_url($oldUrl); | ||
$oldUrlPathArr = explode('/', $oldUrlElements['path']); | ||
if (empty(end($oldUrlPathArr)) || $flag) { | ||
$slashChar = '/'; | ||
} | ||
} | ||
|
||
$urlTemp = $addressId; | ||
|
||
if (!empty($title) && $this->isOptionShowTitleUrl()) { | ||
$urlTemp .= $this->getSanitizeTitle($title, $flag); | ||
} | ||
|
||
$urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $urlElements['path'] . $urlTemp . $slashChar; | ||
|
||
if (!empty($getParameters)) { | ||
$urlLsSwitcher .= '?' . http_build_query($getParameters); | ||
} | ||
} | ||
|
||
return $urlLsSwitcher; | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @return bool | ||
* | ||
*/ | ||
|
||
public function isOptionShowTitleUrl() | ||
{ | ||
return get_option('onoffice-address-detail-view-showInfoUserUrl', false); | ||
} | ||
|
||
|
||
/** | ||
* @param string $title | ||
* @param bool $flag | ||
* @return string | ||
*/ | ||
|
||
public function getSanitizeTitle(string $title, bool $flag = false): string | ||
{ | ||
$sanitizeTitle = $flag ? sanitize_title(remove_accents($title)) : sanitize_title($title); | ||
return '-' . $sanitizeTitle; | ||
} | ||
|
||
/** | ||
* @param int $addressId | ||
* @param string|null $title | ||
* @param string|null $oldUrl | ||
* @param bool $isUrlHaveTitle | ||
* @param bool $pAddressRedirection | ||
* @return string | ||
*/ | ||
public function getUrlWithAddressTitle(int $addressId, string $title = null, string $oldUrl = null, bool $isUrlHaveTitle = false, bool $pAddressRedirection = false): string | ||
{ | ||
$getParameters = []; | ||
$urlElements = parse_url($oldUrl); | ||
$urlTemp = $addressId; | ||
|
||
if (!empty($title) && $this->isOptionShowTitleUrl()) { | ||
if ($pAddressRedirection === false && !empty($urlElements['query']) && !$isUrlHaveTitle) { | ||
$urlTemp .= ''; | ||
} else { | ||
$urlTemp .= $this->getSanitizeTitle($title); | ||
} | ||
} | ||
|
||
if (!empty($urlElements['query'])) { | ||
parse_str($urlElements['query'], $getParameters); | ||
} | ||
|
||
$oldUrlPathArr = explode('/', $urlElements['path']); | ||
if (empty(end($oldUrlPathArr))) { | ||
array_pop($oldUrlPathArr); | ||
} | ||
array_pop($oldUrlPathArr); | ||
$newPath = implode('/', $oldUrlPathArr); | ||
|
||
$urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $newPath . '/' . $urlTemp; | ||
|
||
if (!empty($getParameters)) { | ||
$urlLsSwitcher = add_query_arg($getParameters, $urlLsSwitcher); | ||
} | ||
|
||
return $urlLsSwitcher; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace onOffice\WPlugin\Controller\Redirector; | ||
|
||
use onOffice\WPlugin\Controller\AddressDetailUrl; | ||
use onOffice\WPlugin\Utility\Redirector; | ||
use onOffice\WPlugin\WP\WPRedirectWrapper; | ||
|
||
class AddressRedirector | ||
{ | ||
/** @var AddressDetailUrl */ | ||
private $_wpAddressDetailUrl; | ||
|
||
/** @var WPRedirectWrapper */ | ||
private $_wpRedirectWrapper; | ||
|
||
/** @var Redirector */ | ||
private $_redirector; | ||
|
||
|
||
/** | ||
* @param AddressDetailUrl $addressDetailUrl | ||
* @param WPRedirectWrapper $redirectWrapper | ||
*/ | ||
|
||
public function __construct(AddressDetailUrl $addressDetailUrl, WPRedirectWrapper $redirectWrapper) | ||
{ | ||
$this->_wpAddressDetailUrl = $addressDetailUrl; | ||
$this->_wpRedirectWrapper = $redirectWrapper; | ||
$this->_redirector = new Redirector(); | ||
} | ||
|
||
|
||
/** | ||
* @param int $addressId | ||
* @param string $addressTitle | ||
* @param bool $pAddressRedirection | ||
* @return bool|void | ||
*/ | ||
|
||
public function redirectDetailView(int $addressId, string $addressTitle, bool $pAddressRedirection) | ||
{ | ||
$matches = $this->_redirector->checkUrlIsMatchRule(); | ||
if (empty($matches[2])) { | ||
return true; | ||
} | ||
|
||
$oldUrl = $this->_redirector->getCurrentLink(); | ||
$sanitizeTitle = $this->_wpAddressDetailUrl->getSanitizeTitle($addressTitle); | ||
$isUrlHaveTitle = strpos($oldUrl, $sanitizeTitle) !== false; | ||
$newUrl = $this->_wpAddressDetailUrl->getUrlWithAddressTitle($addressId, $addressTitle, $oldUrl, $isUrlHaveTitle, $pAddressRedirection); | ||
if ($newUrl !== $oldUrl) { | ||
$isNewUrlValid = $this->_redirector->checkNewUrlIsValid( | ||
array_filter(explode('/', $newUrl)), | ||
array_filter(explode('/', $oldUrl)) | ||
); | ||
|
||
if ($isNewUrlValid) { | ||
$this->_wpRedirectWrapper->redirect($newUrl); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.