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

53667 "Speaking" URL for address detail pages #929

Merged
merged 12 commits into from
Oct 25, 2024
45 changes: 39 additions & 6 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
use onOffice\WPlugin\Utility\Redirector;
use onOffice\WPlugin\WP\WPQueryWrapper;
use onOffice\WPlugin\ScriptLoader\IncludeFileModel;
use onOffice\WPlugin\Record\AddressIdRequestGuard;
use onOffice\WPlugin\Utility\AddressRedirector;
use onOffice\WPlugin\Controller\AddressDetailUrl;

const DEFAULT_LIMIT_CHARACTER_TITLE = 60;

Expand Down Expand Up @@ -282,14 +285,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 @@ -397,6 +411,25 @@ function custom_cron_schedules($schedules) {
}
});

$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);
}
});

add_action('parse_request', static function(WP $pWP) use ($pDI) {
if (isset($pWP->query_vars['onoffice_estate_type_json'])) {
$content = wp_json_encode($pDI->get(EstateKindTypeReader::class)->read());
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 @@ -558,17 +563,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;
}
}
10 changes: 10 additions & 0 deletions plugin/Gui/AdminPageApiSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,18 @@ private function addFormModelDetailView(string $pageSlug)
$pInputModelShowTitleUrl->setValue(get_option($pInputModelShowTitleUrl->getIdentifier()) == 1);
$pInputModelShowTitleUrl->setDescriptionTextHTML(__('If this checkbox is selected, the title of the property will be part of the URLs of the detail views. The title is placed after the record number, e.g. <code>/1234-nice-location-with-view</code>. No more than the first five words of the title are used.', 'onoffice-for-wp-websites'));

$groupSlugView = 'onoffice-address-detail-view';
$showTitleInUrl = __('Enable "Speaking" URL in Address Detail', 'onoffice-for-wp-websites');
$pInputModelShowInfoContactUrl = new InputModelOption($groupSlugView, 'showInfoUserUrl',
$showTitleInUrl, InputModelOption::SETTING_TYPE_BOOLEAN);
$pInputModelShowInfoContactUrl->setHtmlType(InputModelOption::HTML_TYPE_CHECKBOX);
$pInputModelShowInfoContactUrl->setValuesAvailable(1);
$pInputModelShowInfoContactUrl->setValue(get_option($pInputModelShowInfoContactUrl->getIdentifier()) == 1);
$pInputModelShowInfoContactUrl->setDescriptionTextHTML(__('If this checkbox is selected, the name and company of the address will be included in the URLs of the address detail views. The name and company will be placed after the record number, eg. <code>/1234-firstname-lastname-company</code>.', 'onoffice-for-wp-websites'));

$pFormModel = new FormModel();
$pFormModel->addInputModel($pInputModelShowTitleUrl);
$pFormModel->addInputModel($pInputModelShowInfoContactUrl);
$pFormModel->setGroupSlug($groupSlugView);
$pFormModel->setPageSlug($pageSlug);
$pFormModel->setLabel(__('Detail View URLs', 'onoffice-for-wp-websites'));
Expand Down
1 change: 1 addition & 0 deletions plugin/Installer/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ static public function deinstall()
delete_option('onoffice-settings-opengraph');
delete_option('onoffice-settings-twittercards');
delete_option('onoffice-settings-duration-cache');
delete_option('onoffice-address-detail-view-showInfoUserUrl');

self::flushRules();
}
Expand Down
Loading
Loading