Skip to content
This repository has been archived by the owner on Feb 8, 2022. It is now read-only.

Allow redirects with _GET variables and add option to disable per redirect #180

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
27 changes: 25 additions & 2 deletions assets/components/stercseo/js/mgr/widgets/redirects.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ StercSEO.grid.Redirects = function(config) {
},
save_action: 'mgr/redirect/updatefromgrid',
autosave: true,
fields: ['id', 'resource', 'target', 'url', 'context_key', 'context_name'],
fields: ['id', 'resource', 'target', 'url', 'context_key', 'context_name','uri_ignore_params'],
autoHeight: true,
paging: true,
remoteSort: true,
Expand All @@ -36,6 +36,19 @@ StercSEO.grid.Redirects = function(config) {
dataIndex: 'context_name',
width: 80,
menuDisabled: true
},{
header: _('stercseo.uri_ignore_params_label'),
dataIndex: 'uri_ignore_params',
width: 80,
menuDisabled: true,
renderer: function(value) {
if (value === 1) {
return "Yes";
} else {
return "No";
}

}
}],
tbar: [{
text: _('stercseo.uri_add'),
Expand Down Expand Up @@ -148,7 +161,7 @@ Ext.extend(StercSEO.grid.Redirects, MODx.grid.Grid, {
},
removeRedirect: function(btn, e) {
if (!this.menu.record) return false;

MODx.msg.confirm({
title: _('stercseo.uri_remove')
,text: _('stercseo.uri_remove_confirm')
Expand Down Expand Up @@ -254,6 +267,16 @@ StercSEO.window.Redirect = function(config) {
typeAhead: true,
editable: true,
forceSelection: true
},{
xtype: 'xcheckbox'
,boxLabel: _('stercseo.uri_ignore_params_label')
,hideLabel: true
,name: 'uri_ignore_params'
,value: 1
},{
xtype: 'label',
text: _('stercseo.uri_ignore_params_label_desc'),
cls: 'desc-under'
}]
});
StercSEO.window.Redirect.superclass.constructor.call(this, config);
Expand Down
23 changes: 19 additions & 4 deletions assets/components/stercseo/js/mgr/widgets/resource.grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ Ext.extend(StercSEO.grid.Items, MODx.grid.Grid, {
var myRecord = Ext.data.Record.create([{
name: 'url',
type: 'string'
},
{
name: 'uri_ignore_params',
type: 'int'
}]);

var newRecord = new myRecord({
url: r.a.result.object.url
url: r.a.result.object.url,
uri_ignore_params: r.a.result.object.uri_ignore_params,
});

var store = Ext.getCmp(id).getStore();
Expand All @@ -82,7 +87,7 @@ Ext.extend(StercSEO.grid.Items, MODx.grid.Grid, {
} else {
store.insert(store.getCount(), newRecord);
var JsonData = Ext.encode(Ext.pluck(store.data.items, 'data'));

console.log(JsonData);
Ext.getCmp('sterceseo-urls').setValue(JsonData);
MODx.fireResourceFormChange();
}
Expand Down Expand Up @@ -139,7 +144,7 @@ StercSEO.window.CreateItem = function(config) {
title: _('stercseo.item_create'),
id: this.ident,
modal:true,
height: 200,
height: 300,
width: 475,
url: StercSEO.config.connector_url,
baseParams: {
Expand Down Expand Up @@ -167,7 +172,17 @@ StercSEO.window.CreateItem = function(config) {
}
},{
xtype: 'label',
text: _('stercseo.uri_label_desc'),
text: _('stercseo.uri_ignore_params_label'),
cls: 'desc-under'
},{
xtype: 'xcheckbox'
,boxLabel: _('stercseo.uri_ignore_params_label')
,hideLabel: true
,name: 'uri_ignore_params'
,value: 1
},{
xtype: 'label',
text: _('stercseo.uri_ignore_params_label_desc'),
cls: 'desc-under'
}]
});
Expand Down
28 changes: 19 additions & 9 deletions core/components/stercseo/elements/plugins/stercseo.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
} elseif ($resource) {
$properties['searchable'] = $resource->get('searchable');
}

$properties['urls'] = '';
// Fetch urls from seoUrl collection
if ($urls && is_object($urls)) {
Expand Down Expand Up @@ -125,6 +125,7 @@
'url' => urlencode($url['url']),
'resource' => $oldResource->get('id'),
'context_key' => $oldResource->get('context_key'),
'uri_ignore_params' => $url['uri_ignore_params'],
);
$redirect->fromArray($data);
$redirect->save();
Expand Down Expand Up @@ -210,11 +211,11 @@
if (!$stercseo->isAllowed($resource->context_key)) {
return;
}

/* Tmp overwrite the cache_alias_map setting to prevent that a redirect is removed again. */
$cacheAliasMap = $modx->getOption('cache_alias_map', true);
$modx->setOption('cache_alias_map', false);

$url = urlencode($modx->makeUrl($resource->id, $resource->context_key, '', 'full'));
$urlExists = $modx->getObject('seoUrl', array(
'url' => $url,
Expand All @@ -227,7 +228,7 @@
'context_key' => $resource->context_key
));
}

$modx->setOption('cache_alias_map', $cacheAliasMap);
break;

Expand Down Expand Up @@ -258,8 +259,8 @@
case 'OnPageNotFound':
$options = array();
$query = $modx->newQuery('seoUrl');
$url = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

$url = $_SERVER['HTTP_HOST'] . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$url_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
$query->where(array(
array(
'url' => urlencode('http://' . $url)
Expand Down Expand Up @@ -294,8 +295,17 @@

if ($alreadyExists) {
$url = $modx->makeUrl($alreadyExists->get('resource'), $alreadyExists->get('context_key'), '', 'full', $options);
if($url_query) {
if($alreadyExists->get('uri_ignore_params') === 1) {
$modx->sendRedirect($url."?".$url_query, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
} else {
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
}

} else {
$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
}

$modx->sendRedirect($url, 0, 'REDIRECT_HEADER', 'HTTP/1.1 301 Moved Permanently');
}
break;

Expand Down Expand Up @@ -353,7 +363,7 @@
$stercseo->setWorkingContext($resource->get('context_key'));

$site_url = $stercseo->getOption('site_url', '', $modx->getOption('site_url'));

$childResources = $modx->getIterator('modResource', $cond);
foreach ($childResources as $childResource) {
$url = urlencode($site_url . $childResource->get('uri'));
Expand Down Expand Up @@ -401,4 +411,4 @@
}
}
}
return;
return;
8 changes: 5 additions & 3 deletions core/components/stercseo/lexicon/en/default.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@
$_lang['stercseo.uri_label'] = 'Old URL';
$_lang['stercseo.uri_label_desc'] = 'Enter the full URL, including your domain. Example: "https://www.site.tld/old-pages/about-us".';
$_lang['stercseo.url_missing_protocol'] = 'Incorrect URL. Please add: http:// or https://';
$_lang['stercseo.uri_ignore_params_label'] = 'Ignore URL Parameters in the redirect';
$_lang['stercseo.uri_ignore_params_label_desc'] = 'For example, UTM parameters such as ?utm_campaign={item}&utm_source=website';

//Tab Freeze URL
$_lang['stercseo.freeze_uri'] = 'Freeze URL';
$_lang['stercseo.uri_override'] = 'Set a Freeze URL for this page.';
$_lang['stercseo.uri_after'] = 'URL after [[+site_url]]';
$_lang['stercseo.uri_after_desc'] = 'Freeze URLs can be used to create user-friendly URLs.
$_lang['stercseo.uri_after_desc'] = 'Freeze URLs can be used to create user-friendly URLs.
For example, to set this page URL to [[+site_url]]user-friendly-url", enter "user-friendly-url" in the field above.';

//Settings
Expand All @@ -92,7 +94,7 @@
$_lang['setting_stercseo.xmlsitemap.dependent_ultimateparent_desc'] = 'If turned on, resources will be hidden from the XML sitemap if their parent or ultimate parent resource is deleted or unpublished.';

// CMP
$_lang['stercseo.redirects.description'] = 'Manage your SEO Tab 301 redirects.
$_lang['stercseo.redirects.description'] = 'Manage your SEO Tab 301 redirects.
Redirects can also be added when editing a resource by clicking the tab called "SEO".';
$_lang['stercseo.redirects.window_title'] = 'Add redirect URL';
$_lang['stercseo.uri'] = 'Old URL (URL to redirect)';
Expand All @@ -113,7 +115,7 @@
$_lang['stercseo.migrate_success_msg'] = 'All your redirects have been successfully migrated.';

/* Resource. */
$_lang['stercseo.resource.freeze_uri.redirect_exists'] = 'Could not save the current resource because a SEO Tab redirect already exists for the provided freeze URI: [[+uri]].
$_lang['stercseo.resource.freeze_uri.redirect_exists'] = 'Could not save the current resource because a SEO Tab redirect already exists for the provided freeze URI: [[+uri]].
Please provide a different freeze URI or remove the redirect in the SEO Tab component and save the resource again.';
$_lang['stercseo.resource.error.redirect_already_exists.title'] = 'Redirect already exists';
$_lang['stercseo.resource.error.redirect_already_exists.msg'] = 'The redirect you we\'re trying to add already exists for this resource and therefore the redirect was not saved.';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<model package="stercseo" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" phpdoc-package="stercseo">
<object class="seoUrl" table="seo_urls" extends="xPDOSimpleObject">
<field key="resource" dbtype="integer" precision="10" phptype="int" null="false" default="" />
<field key="url" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="context_key" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
</object>
</model>
<field key="url" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
<field key="context_key" dbtype="varchar" precision="100" phptype="string" null="false" default="" />
<field key="uri_ignore_params" dbtype="integer" precision="1" phptype="int" null="false" default="" />
</object>
</model>
19 changes: 14 additions & 5 deletions core/components/stercseo/model/stercseo/mysql/seourl.map.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,46 @@
'version' => NULL,
'table' => 'seo_urls',
'extends' => 'xPDOSimpleObject',
'fields' =>
'fields' =>
array (
'resource' => 0,
'url' => '',
'context_key' => '',
'uri_ignore_params' => 0
),
'fieldMeta' =>
'fieldMeta' =>
array (
'resource' =>
'resource' =>
array (
'dbtype' => 'integer',
'precision' => '10',
'phptype' => 'int',
'null' => false,
'default' => 0,
),
'url' =>
'url' =>
array (
'dbtype' => 'varchar',
'precision' => '255',
'phptype' => 'string',
'null' => false,
'default' => '',
),
'context_key' =>
'context_key' =>
array (
'dbtype' => 'varchar',
'precision' => '100',
'phptype' => 'string',
'null' => false,
'default' => '',
),
'uri_ignore_params' =>
array (
'dbtype' => 'integer',
'precision' => '1',
'phptype' => 'int',
'null' => false,
'default' => 0,
)
),
);
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class StercSeoCreateProcessor extends modObjectCreateProcessor

public function beforeSave()
{
$pagetitle = '';
$resource = $this->modx->getObject('modResource', $this->getProperty('resource'));
$encodedUrl = urlencode($this->getProperty('url'));
$url = parse_url($this->getProperty('url'));
$pagetitle = '';
$resource = $this->modx->getObject('modResource', $this->getProperty('resource'));
$encodedUrl = urlencode($this->getProperty('url'));
$url = parse_url($this->getProperty('url'));

if ($url['scheme'] === null) {
$this->addFieldError('url', $this->modx->lexicon('stercseo.url_missing_protocol'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function prepareRow(xPDOObject $object)
}
}
$object->set('url', urldecode($object->get('url')));
$object->set('uri_ignore_params', $object->get('uri_ignore_params'));

/* Get context name from context. Defaults to context_key */
$contextName = $object->get('context_key');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ public function process()
}

// if no matches are found in the block above, this will insert a redirect
$this->modx->exec("INSERT INTO {$this->modx->getTableName('seoUrl')}
SET {$this->modx->escape('url')} = {$this->modx->quote($encoded_url)},
{$this->modx->escape('resource')} = {$this->modx->quote($row['modResource_id'])},
{$this->modx->escape('context_key')} = {$this->modx->quote($context_key)}");
$this->modx->exec("INSERT INTO {$this->modx->getTableName('seoUrl')}
SET {$this->modx->escape('url')} = {$this->modx->quote($encoded_url)},
{$this->modx->escape('resource')} = {$this->modx->quote($row['modResource_id'])},
{$this->modx->escape('context_key')} = {$this->modx->quote($context_key)},
{$this->modx->escape('uri_ignore_params')} = {$this->modx->quote($row['uri_ignore_params'])}");

$count++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class StercSeoUpdateProcessor extends modObjectUpdateProcessor
public function beforeSave()
{
$url = urlencode($this->object->get('url'));
if ($existing = $this->modx->getObject($this->classKey, array('url' => $url))) {
$uri_ignore_params = $this->object->get('uri_ignore_params');
if ($existing = $this->modx->getObject($this->classKey, array('url' => $url, 'uri_ignore_params' => $uri_ignore_params))) {
$this->addFieldError(
'url',
$this->modx->lexicon(
Expand Down