Skip to content

Commit

Permalink
53667 "Speaking" URL for address detail pages (#929)
Browse files Browse the repository at this point in the history
* 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
4 people authored Oct 25, 2024
1 parent 71ecd87 commit b2e3e2c
Show file tree
Hide file tree
Showing 19 changed files with 1,561 additions and 102 deletions.
48 changes: 41 additions & 7 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
use onOffice\WPlugin\Utility\Redirector;
use onOffice\WPlugin\WP\WPQueryWrapper;
use onOffice\WPlugin\ScriptLoader\IncludeFileModel;
use onOffice\WPlugin\Record\AddressIdRequestGuard;
use onOffice\WPlugin\Controller\Redirector\AddressRedirector;
use onOffice\WPlugin\Controller\Redirector\EstateRedirector;
use onOffice\WPlugin\Controller\AddressDetailUrl;

const DEFAULT_LIMIT_CHARACTER_TITLE = 60;

Expand Down Expand Up @@ -282,14 +286,25 @@ function customFieldCallback( $pDI, $format, $limitEllipsis, $meta_key ) {


add_filter('wpml_ls_language_url', function($url, $data) use ($pDI) {
/** @var EstateIdRequestGuard $pEstateIdGuard */
$pEstateIdGuard = $pDI->get(EstateIdRequestGuard::class);
$pEstateDetailUrl = $pDI->get(EstateDetailUrl::class);
$oldUrl = $pDI->get(Redirector::class)->getCurrentLink();
$pWPQueryWrapper = $pDI->get(WPQueryWrapper::class);
$estateId = (int) $pWPQueryWrapper->getWPQuery()->get('estate_id', 0);

return $pEstateIdGuard->createEstateDetailLinkForSwitchLanguageWPML($url, $estateId, $pEstateDetailUrl, $oldUrl, $data['default_locale']);
$addressId = (int) $pWPQueryWrapper->getWPQuery()->get('address_id', 0);

if (!empty($estateId)) {
/** @var EstateIdRequestGuard $pEstateIdGuard */
$pEstateIdGuard = $pDI->get(EstateIdRequestGuard::class);
$pEstateDetailUrl = $pDI->get(EstateDetailUrl::class);
$oldUrl = $pDI->get(Redirector::class)->getCurrentLink();
return $pEstateIdGuard->createEstateDetailLinkForSwitchLanguageWPML($url, $estateId, $pEstateDetailUrl, $oldUrl, $data['default_locale']);
}

if (!empty($addressId)) {
/** @var AddressIdRequestGuard $pAddressIdGuard */
$pAddressIdGuard = $pDI->get(AddressIdRequestGuard::class);
$pEstateDetailUrl = $pDI->get(AddressDetailUrl::class);
$oldUrl = $pDI->get(AddressRedirector::class)->getCurrentLink();
return $pAddressIdGuard->createAddressDetailLinkForSwitchLanguageWPML($url, $addressId, $pEstateDetailUrl, $oldUrl, $data['default_locale']);
}
}, 10, 2);

register_activation_hook(__FILE__, [Installer::class, 'install']);
Expand Down Expand Up @@ -393,7 +408,26 @@ function custom_cron_schedules($schedules) {
include(get_query_template('404'));
die();
}
$pEstateIdGuard->estateDetailUrlChecker( $estateId, $pDI->get( Redirector::class ), $pEstateRedirection);
$pEstateIdGuard->estateDetailUrlChecker( $estateId, $pDI->get( EstateRedirector::class ), $pEstateRedirection);
}
});

$pAddressRedirection = apply_filters('oo_is_address_detail_page_redirection', true);

add_action('parse_request', function(WP $pWP) use ($pDI, $pAddressRedirection) {
$addressId = $pWP->query_vars['address_id'] ?? '';
/** @var AddressIdRequestGuard $pAddressIdGuard */
$pAddressIdGuard = $pDI->get(AddressIdRequestGuard::class);

if ($addressId !== '') {
$addressId = (int)$addressId;

if ($addressId === 0 || !$pAddressIdGuard->isValid($addressId)) {
$pWP->handle_404();
include(get_query_template('404'));
die();
}
$pAddressIdGuard->addressDetailUrlChecker($addressId, $pDI->get(AddressRedirector::class), $pAddressRedirection);
}
});

Expand Down
44 changes: 36 additions & 8 deletions plugin/AddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use function esc_html;
use onOffice\WPlugin\Field\UnknownFieldException;
use onOffice\WPlugin\DataView\DataAddressDetailView;
use onOffice\WPlugin\Controller\AddressDetailUrl;

/**
*
Expand Down Expand Up @@ -145,6 +146,9 @@ class AddressList
/** @var FieldsCollection */
private $_pFieldsCollection = [];

/** @var AddressDetailUrl */
private $_pLanguageSwitcher;

/**
*
* @param DataViewAddress $pDataViewAddress
Expand All @@ -159,6 +163,7 @@ public function __construct(DataViewAddress $pDataViewAddress = null, AddressLis
$pSDKWrapper = $this->_pEnvironment->getSDKWrapper();
$this->_pApiClientAction = new APIClientActionGeneric
($pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'address');
$this->_pLanguageSwitcher = new AddressDetailUrl();
}

/**
Expand Down Expand Up @@ -568,17 +573,40 @@ public function generateImageAlt(int $addressId): string
return $imageAlt;
}

/**
* @param string $addressId
* @return string
*/
public function getAddressLink(string $addressId): string
{
$pageId = $this->_pEnvironment->getDataAddressDetailViewHandler()
->getAddressDetailView()->getPageId();
$pageId = $this->_pEnvironment->getDataAddressDetailViewHandler()
->getAddressDetailView()->getPageId();

$currentAddress = $this->getAddressById($addressId);
$firstName = $currentAddress['Vorname'] ?? '';
$lastName = $currentAddress['Name'] ?? '';
$company = $currentAddress['Zusatz1'] ?? '';
$parts = [];
if (!empty($firstName)) {
$parts[] = strtolower($firstName);
}
if (!empty($lastName)) {
$parts[] = strtolower($lastName);
}
if (!empty($company)) {
$parts[] = strtolower($company);
}
$addressTitle = implode(' ', $parts);

$url = get_page_link( $pageId ) . $addressId;
$fullLinkElements = parse_url( $url );
if ( empty( $fullLinkElements['query'] ) ) {
$url .= '/';
}
return $url;
$url = get_page_link( $pageId );
$fullLink = $this->_pLanguageSwitcher->createAddressDetailLink( $url, $addressId, $addressTitle );

$fullLinkElements = parse_url( $fullLink );
if ( empty( $fullLinkElements['query'] ) ) {
$fullLink .= '/';
}

return $fullLink;
}

/**
Expand Down
147 changes: 147 additions & 0 deletions plugin/Controller/AddressDetailUrl.php
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;
}
}
63 changes: 63 additions & 0 deletions plugin/Controller/Redirector/AddressRedirector.php
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);
}
}
}
}
Loading

0 comments on commit b2e3e2c

Please sign in to comment.