Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

44698 unnecessary redirects for multilingual pages with user-friendly urls #740

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,15 @@ function customFieldCallback( $pDI, $format, $limitEllipsis, $meta_key ) {
}


add_filter('wpml_ls_language_url', function($url) use ($pDI){
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 $pDI->get(EstateDetailUrl::class)->createEstateDetailLink($url, $estateId);

return $pEstateIdGuard->createEstateDetailLinkForSwitchLanguageWPML($url, $estateId, $pEstateDetailUrl, $oldUrl, $data['default_locale']);
}, 10, 2);

register_activation_hook(__FILE__, [Installer::class, 'install']);
Expand Down
13 changes: 8 additions & 5 deletions plugin/Controller/EstateDetailUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class EstateDetailUrl
* @param int $estateId
* @param string|null $title
* @param string|null $oldUrl
* @param bool $flag
*
* @return string
*/
Expand All @@ -42,7 +43,8 @@ public function createEstateDetailLink(
string $url,
int $estateId,
string $title = null,
string $oldUrl = null ): string
string $oldUrl = null,
bool $flag = false): string
{
$urlLsSwitcher = $url;
$slashChar = '';
Expand All @@ -58,15 +60,15 @@ public function createEstateDetailLink(
if ( ! is_null( $oldUrl ) ) {
$oldUrlElements = parse_url( $oldUrl );
$oldUrlPathArr = explode( '/', $oldUrlElements['path'] );
if ( empty( end( $oldUrlPathArr ) ) ) {
if ( empty( end( $oldUrlPathArr ) ) || $flag ) {
$slashChar = '/';
}
}

$urlTemp = $estateId;

if ( ! empty( $title ) && $this->isOptionShowTitleUrl() ) {
$urlTemp .= $this->getSanitizeTitle( $title );
$urlTemp .= $this->getSanitizeTitle( $title, $flag );
}

$urlLsSwitcher = $urlElements['scheme'] . '://' . $urlElements['host'] . $urlElements['path'] . $urlTemp . $slashChar;
Expand Down Expand Up @@ -94,12 +96,13 @@ public function isOptionShowTitleUrl()

/**
* @param string $title
* @param bool $flag
* @return string
*/

public function getSanitizeTitle(string $title): string
public function getSanitizeTitle(string $title, bool $flag = false): string
{
$sanitizeTitle = sanitize_title($title);
$sanitizeTitle = $flag ? sanitize_title(remove_accents($title)) : sanitize_title($title);
$arrSanitizeTitle = explode('-', $sanitizeTitle);
if (count($arrSanitizeTitle) > self::MAXIMUM_WORD_TITLE) {
$sanitizeTitle = implode('-', array_splice($arrSanitizeTitle, 0, self::MAXIMUM_WORD_TITLE));
Expand Down
132 changes: 131 additions & 1 deletion plugin/Record/EstateIdRequestGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@

namespace onOffice\WPlugin\Record;

use DI\Container;
use DI\ContainerBuilder;
use onOffice\SDK\onOfficeSDK;
use onOffice\WPlugin\API\APIClientActionGeneric;
use onOffice\WPlugin\ArrayContainerEscape;
use onOffice\WPlugin\Controller\EstateDetailUrl;
use onOffice\WPlugin\Factory\EstateListFactory;
use onOffice\WPlugin\Language;
use onOffice\WPlugin\SDKWrapper;
use onOffice\WPlugin\ViewFieldModifier\EstateViewFieldModifierTypes;
use onOffice\WPlugin\Utility\Redirector;

Expand All @@ -42,16 +49,28 @@ class EstateIdRequestGuard
/** * @var ArrayContainerEscape */
private $_estateData;

/** @var array */
private $_estateDataWPML = [];

/** @var Container */
private $_pContainer = null;

/** @var array */
private $_activeLanguages = [];

/**
*
* @param EstateListFactory $pEstateDetailFactory
* @param Container|null $pContainer
*
*/

public function __construct(EstateListFactory $pEstateDetailFactory)
public function __construct(EstateListFactory $pEstateDetailFactory, Container $pContainer = null)
{
$this->_pEstateDetailFactory = $pEstateDetailFactory;
$pContainerBuilder = new ContainerBuilder;
$pContainerBuilder->addDefinitions(ONOFFICE_DI_CONFIG_PATH);
$this->_pContainer = $pContainer ?? $pContainerBuilder->build();
}


Expand All @@ -67,6 +86,14 @@ public function isValid(int $estateId): bool
$pEstateDetail = $this->_pEstateDetailFactory->createEstateDetail($estateId);
$pEstateDetail->loadEstates();
$this->_estateData = $pEstateDetail->estateIterator(EstateViewFieldModifierTypes::MODIFIER_TYPE_DEFAULT, true);

if ($this->isActiveWPML()) {
$this->_activeLanguages = apply_filters('wpml_active_languages', null);
if ($estateId > 0 && !empty($this->_estateData) && count($this->_activeLanguages) > 1) {
$this->estateDataForWPML($estateId);
}
}

return $this->_estateData !== false;
}

Expand All @@ -84,4 +111,107 @@ public function estateDetailUrlChecker( int $estateId, Redirector $pRedirector,
$estateTitle = $this->_estateData->getValue( 'objekttitel' );
$pRedirector->redirectDetailView($estateId, $estateTitle, $pEstateRedirection);
}

/**
* @param string $url
* @param int $estateId
* @param EstateDetailUrl $pEstateDetailUrl
* @param string $oldUrl
* @param string $switchLocale
*
* @return string
*/
public function createEstateDetailLinkForSwitchLanguageWPML(string $url, int $estateId, EstateDetailUrl $pEstateDetailUrl, string $oldUrl, string $switchLocale): string
{
$estateDetailTitle = '';
$currentLocale = get_locale();
if ($estateId > 0 && !empty($this->_estateData)) {
if ($switchLocale !== $currentLocale) {
$this->addSwitchFilterToLocaleHook($switchLocale);
$estateDetailTitle = $this->_estateDataWPML[$switchLocale];
} else {
$estateDetailTitle = $this->_estateData->getValue('objekttitel');
}
}
$detailLinkForWPML = $pEstateDetailUrl->createEstateDetailLink($url, $estateId, $estateDetailTitle, $oldUrl, true);
$this->addSwitchFilterToLocaleHook($currentLocale);

return $detailLinkForWPML;
}

/**
* @param array $locales
* @param int $estateId
* @return array
* @throws \DI\DependencyException
* @throws \DI\NotFoundException
*/
private function getEstateTitleByLocales(array $locales, int $estateId): array
{
$pApiClientActionClone = null;
$estateTitleByLocales = [];
$listRequestInQueue = [];

$pSDKWrapper = $this->_pContainer->get(SDKWrapper::class);
$pApiClientAction = new APIClientActionGeneric
($pSDKWrapper, onOfficeSDK::ACTION_ID_READ, 'estate');
foreach ($locales as $locale) {
$isoLanguageCode = Language::LOCALE_MAPPING[$locale] ?? 'DEU';
$estateParameters = [
'data' => ['objekttitel'],
'estatelanguage' => $isoLanguageCode,
'outputlanguage' => $isoLanguageCode,
];
$pApiClientActionClone = clone $pApiClientAction;
$pApiClientActionClone->setResourceId((string)$estateId);
$pApiClientActionClone->setParameters($estateParameters);
$pApiClientActionClone->addRequestToQueue();
$listRequestInQueue[$locale] = $pApiClientActionClone;
}
$pApiClientActionClone->sendRequests();

if (empty($pApiClientActionClone->getResultRecords())) {
return [];
}

foreach($listRequestInQueue as $key => $pApiClientAction) {
$estateTitleByLocales[$key] = $pApiClientAction->getResultRecords()[0]['elements']['objekttitel'];
}

return $estateTitleByLocales;
}

/**
* @param int $estateId
* @return void
*/
private function estateDataForWPML(int $estateId)
{
$defaultLocales = [];
foreach ($this->_activeLanguages as $language) {
if (isset($language['default_locale']) && $language['default_locale'] !== get_locale()) {
$defaultLocales[] = $language['default_locale'];
}
}

$this->_estateDataWPML = $this->getEstateTitleByLocales($defaultLocales, $estateId);
}

/**
* @return bool
*/
private function isActiveWPML(): bool
{
return in_array('sitepress-multilingual-cms/sitepress.php', get_option('active_plugins'));
}

/**
* @param string $locale
*/
private function addSwitchFilterToLocaleHook(string $locale)
{
add_filter('locale', function () use ($locale) {
return $locale;
});
}
}
Loading
Loading