Skip to content

Commit 014795e

Browse files
committed
Redirects: Allow setting redirects from custom RPC method
1 parent 1ea3f28 commit 014795e

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

mu-plugins/redirects.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2+
/**
3+
* Plugin Name: jquery-wp-content Redirection extensions
4+
* Description: Adds custom XML-RPC methods to control redirection.
5+
*/
26

3-
$jquery_redirects = array(
4-
'qunitjs.com' => array(
5-
'/addons/' => '/plugins/'
6-
)
7-
);
7+
$jquery_redirects = 'jquery_redirects';
88

99
add_filter( 'template_redirect', function() {
1010
global $jquery_redirects;
@@ -14,14 +14,34 @@
1414
return;
1515
}
1616

17-
// Check if this site has any redirects
18-
if ( empty( $jquery_redirects[ JQUERY_LIVE_SITE ] ) ) {
19-
return;
17+
$url = trailingslashit( $_SERVER[ 'REQUEST_URI' ] );
18+
19+
// Check for redirects stored in the transients
20+
$transient = get_site_transient( $jquery_redirects );
21+
22+
if ( $transient && !empty( $transient[ $url ] ) ) {
23+
wp_redirect( $transient[ $url ], 301 );
2024
}
25+
} );
2126

22-
// See if any redirects match the current URL
23-
$url = trailingslashit( $_SERVER[ 'REQUEST_URI' ] );
24-
if ( !empty( $jquery_redirects[ JQUERY_LIVE_SITE ][ $url ] ) ) {
25-
wp_redirect( $jquery_redirects[ JQUERY_LIVE_SITE ][ $url ], 301 );
27+
add_filter( 'xmlrpc_methods', function( $methods ) {
28+
$methods[ 'jq.setRedirects' ] = 'jq_set_redirects';
29+
return $methods;
30+
} );
31+
32+
function jq_set_redirects( $args ) {
33+
global $wp_xmlrpc_server;
34+
global $jquery_redirects;
35+
36+
// Authenticate
37+
$blog_id = $args[ 0 ];
38+
$username = $args[ 1 ];
39+
$password = $args[ 2 ];
40+
41+
if ( ! $user = $wp_xmlrpc_server->login( $username, $password ) ) {
42+
return $wp_xmlrpc_server->error;
2643
}
27-
});
44+
45+
// Store redirects
46+
return set_site_transient( $jquery_redirects, json_decode( $args[ 3 ], true ), 0 );
47+
}

0 commit comments

Comments
 (0)