Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
WP Plugins
ETH Simple Shortlinks
Commits
b0856e8d
Commit
b0856e8d
authored
Mar 20, 2016
by
Erick Hitter
Browse files
Permalink and redirect
parent
677588e3
Changes
1
Show whitespace changes
Inline
Side-by-side
eth-simple-shortlinks.php
View file @
b0856e8d
...
...
@@ -50,10 +50,57 @@ class ETH_Simple_Shortlinks {
public
function
__wakeup
()
{
_doing_it_wrong
(
__FUNCTION__
,
'Cheatin’ huh?'
,
'0.1'
);
}
public
function
__call
(
$name
=
''
,
$args
=
array
()
)
{
unset
(
$name
,
$args
);
return
null
;
}
/**
* Class properties
*/
private
$qv
=
'eth-shortlink'
;
/**
*
*/
private
function
__construct
()
{}
private
function
__construct
()
{
add_action
(
'init'
,
array
(
$this
,
'add_rewrite_rule'
)
);
add_filter
(
'query_vars'
,
array
(
$this
,
'filter_query_vars'
)
);
add_action
(
'parse_request'
,
array
(
$this
,
'action_parse_request'
)
);
}
/**
* Register rewrite rule
*/
public
function
add_rewrite_rule
()
{
add_rewrite_rule
(
'^p/([\d]+)/?$'
,
'index.php?p=$matches[1]&'
.
$this
->
qv
.
'=1'
,
'top'
);
}
/**
* Add custom query var to those permitted, so it can be detected at `parse_request`
*/
public
function
filter_query_vars
(
$qv
)
{
$qv
[]
=
$this
->
qv
;
return
$qv
;
}
/**
* Catch this plugin's requests and issue redirects, otherwise WP will serve content at duplicate URLs
*/
public
function
action_parse_request
(
$request
)
{
if
(
isset
(
$request
->
query_vars
[
$this
->
qv
]
)
)
{
$home_url
=
user_trailingslashit
(
home_url
()
);
$dest
=
get_permalink
(
$request
->
query_vars
[
'p'
]
);
if
(
$dest
)
{
$dest
=
wp_validate_redirect
(
$dest
,
$home_url
);
$status
=
301
;
}
else
{
$dest
=
$home_url
;
$status
=
302
;
}
wp_redirect
(
$dest
,
$status
);
exit
;
}
}
}
ETH_Simple_Shortlinks
::
get_instance
();
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment