Skip to content

Commit c805651

Browse files
committed
add new filter to parse request_uri before request custom table redirection
1 parent 6b43f71 commit c805651

File tree

4 files changed

+77
-51
lines changed

4 files changed

+77
-51
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Url Memory
1+
# URL Memory
22

33
If the URL of a page, post, existing is changed, all the internal links pointing to this page website, post must be updated automatically
44

55
* Contributors: http://profiles.wordpress.org/momo360modena/ - http://profiles.wordpress.org/asadowski10
66
* Donate link: http://www.beapi.fr/donate/
77
* Tags: url, path, url memory,
88
* Requires at least: 3.0
9-
* Tested up to: 3.4.1
9+
* Tested up to: 4.8.2
1010
* Stable tag: 1.0
1111
* License: GPLv2 or later
1212
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -39,3 +39,6 @@ This plugin is developped on WordPress 3.4, with the constant WP_DEBUG to TRUE.
3939

4040
* Version 1.0 :
4141
* First version stable
42+
43+
* Version 1.0.1 :
44+
* Add filter "apply_filters( 'url_memory_redirect_request_uri', $_SERVER['REQUEST_URI'] );" to parse result request_uri before request in redirections table

inc/class.client.php

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
class UrlMemory_Client {
34

45
/**
@@ -8,128 +9,147 @@ class UrlMemory_Client {
89
* @author Amaury Balmer, Alexandre Sadowski
910
*/
1011
public function __construct() {
11-
add_action('init', array(__CLASS__, 'init'), 1);
12-
add_action('delete_post', array(__CLASS__, 'delete_post'), 10, 1);
13-
add_action('save_post', array(__CLASS__, 'save_post'), 10, 2);
12+
add_action( 'init', array( __CLASS__, 'init' ), 1 );
13+
add_action( 'delete_post', array( __CLASS__, 'delete_post' ), 10, 1 );
14+
add_action( 'save_post', array( __CLASS__, 'save_post' ), 10, 2 );
1415
}
1516

1617
/**
1718
* Hook call to redirect posts to right url
1819
*
1920
* @param integer $object_id
21+
*
2022
* @return void
2123
* @author Amaury Balmer, Alexandre Sadowski
2224
*/
2325
public static function init() {
2426
global $wpdb;
2527

26-
if (!isset($_SERVER['REQUEST_URI']) || empty($_SERVER['REQUEST_URI']))
28+
if ( ! isset( $_SERVER['REQUEST_URI'] ) || empty( $_SERVER['REQUEST_URI'] ) ) {
2729
return false;
30+
}
31+
32+
$request_uri = apply_filters( 'url_memory_redirect_request_uri', $_SERVER['REQUEST_URI'] );
2833

29-
$result_id = $wpdb -> get_var($wpdb -> prepare("SELECT post_id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 0, $_SERVER['REQUEST_URI']));
30-
if ($result_id != false && (int)$result_id > 0) {
31-
$result = get_post($result_id);
32-
if ($result -> post_status != 'publish') {
34+
$result_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 0, $request_uri ) );
35+
if ( false != $result_id && (int) $result_id > 0 ) {
36+
$result = get_post( $result_id );
37+
if ( 'publish' != $result->post_status ) {
3338
return false;
3439
}
3540

3641
// Test is valid redirect exist ?
37-
$counter = $wpdb -> get_var($wpdb -> prepare("SELECT COUNT(post_id) FROM $wpdb->url_redirect WHERE status = %d AND post_id = %d", 1, $result_id));
38-
if ( $counter == 1 ) {
39-
wp_redirect(get_permalink($result_id), 301);
42+
$counter = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(post_id) FROM $wpdb->url_redirect WHERE status = %d AND post_id = %d", 1, $result_id ) );
43+
if ( 1 == $counter ) {
44+
wp_redirect( get_permalink( $result_id ), 301 );
4045
exit();
4146
}
4247
}
43-
48+
4449
return false;
4550
}
4651

4752
/**
4853
* Hook call for delete url redirection of deleted post
4954
*
5055
* @param integer $object_id
56+
*
5157
* @return void
5258
* @author Amaury Balmer, Alexandre Sadowski
5359
*/
54-
public static function delete_post($object_id = 0) {
55-
um_delete_redirect_rows(array($object_id));
60+
public static function delete_post( $object_id = 0 ) {
61+
um_delete_redirect_rows( array( $object_id ) );
5662
}
5763

5864
/**
5965
* Hook call for add url redirection of new post
66+
*
6067
* @param integer $object_id
6168
* @param string $object
69+
*
6270
* @return void
6371
* @author Amaury Balmer, Alexandre Sadowski
6472
*/
65-
public static function save_post($object_id = 0, $object = null) {
73+
public static function save_post( $object_id = 0, $object = null ) {
6674
global $wpdb, $post;
6775

6876
// Object ID is valid ?
69-
$object_id = (int)$object_id;
70-
if ($object_id == 0) {
77+
$object_id = (int) $object_id;
78+
if ( 0 === $object_id ) {
7179
return false;
7280
}
7381

7482
// Be sure to have POST data
75-
if (is_null($object)) {
76-
$object = get_post($object_id);
83+
if ( is_null( $object ) ) {
84+
$object = get_post( $object_id );
7785
}
7886

7987
// Slug is empty ?
80-
if (empty($object -> post_name)) {
88+
if ( empty( $object->post_name ) ) {
8189
return false;
8290
}
8391

8492
// Published content ?
85-
if ($object -> post_status != 'publish') {
93+
if ( 'publish' != $object->post_status ) {
8694
return false;
8795
}
8896

8997
// Get permalink and remove HTTP and host
90-
$path = str_replace(home_url(), '', get_permalink($object_id));
98+
$path = str_replace( home_url(), '', get_permalink( $object_id ) );
9199

92100
// Loop for insert on DB or change status and path !
93-
if (isset($path) && !empty($path)) {
94-
$result = $wpdb -> get_var($wpdb -> prepare("SELECT id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 1, $path));
95-
if ($result == false) {
101+
if ( isset( $path ) && ! empty( $path ) ) {
102+
$result = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 1, $path ) );
103+
if ( false === $result ) {
96104
// New URL or previous used URL ?
97-
$result_id = $wpdb -> get_var($wpdb -> prepare("SELECT id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 0, $path));
98-
if ($result_id == false) {
105+
$result_id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM $wpdb->url_redirect WHERE status = %d AND path = %s", 0, $path ) );
106+
if ( false === $result_id ) {
99107
// Add new content URL
100-
$row_changed = (int) $wpdb -> insert($wpdb -> url_redirect, array('status' => 1, 'post_id' => $object_id, 'path' => $path), array('%d', '%d', '%s'));
108+
$row_changed = (int) $wpdb->insert( $wpdb->url_redirect, array(
109+
'status' => 1,
110+
'post_id' => $object_id,
111+
'path' => $path,
112+
), array( '%d', '%d', '%s' ) );
101113
if ( $row_changed > 0 && $wpdb->insert_id > 0 ) {
102114
$save_insert_id = $wpdb->insert_id;
103-
115+
104116
// Deactive active URL for manage redirect !
105-
$wpdb -> update($wpdb -> url_redirect, array('status' => 0), array('post_id' => $object_id));
106-
117+
$wpdb->update( $wpdb->url_redirect, array( 'status' => 0 ), array( 'post_id' => $object_id ) );
118+
107119
// Restore status on active URL
108-
$wpdb -> update($wpdb -> url_redirect, array('status' => 1), array('id' => $save_insert_id));
120+
$wpdb->update( $wpdb->url_redirect, array( 'status' => 1 ), array( 'id' => $save_insert_id ) );
109121
}
110122
} else {
111123
// Deactive active URL for manage redirect !
112-
$wpdb -> update($wpdb -> url_redirect, array('status' => 0), array('post_id' => $object_id));
113-
124+
$wpdb->update( $wpdb->url_redirect, array( 'status' => 0 ), array( 'post_id' => $object_id ) );
125+
114126
// Restore status on active URL
115-
$wpdb -> update($wpdb -> url_redirect, array('status' => 1), array('id' => $result_id));
127+
$wpdb->update( $wpdb->url_redirect, array( 'status' => 1 ), array( 'id' => $result_id ) );
116128
}
117129

118130
}
119131
}
120-
132+
121133
// Loop for change status and path of posts childs !
122-
if (is_post_type_hierarchical($object -> post_type) != false) {
123-
$child_query = new WP_Query( array('post_parent' => $object_id, 'post_type' => $object -> post_type, 'post_status' => 'publish', 'nopaging' => true));
124-
if ($child_query -> have_posts()) {
125-
while ($child_query -> have_posts()) {
126-
$child_query -> the_post();
127-
self::save_post(get_the_ID(), $post);
134+
if ( is_post_type_hierarchical( $object->post_type ) != false ) {
135+
$child_query = new WP_Query( array(
136+
'post_parent' => $object_id,
137+
'post_type' => $object->post_type,
138+
'post_status' => 'publish',
139+
'nopaging' => true,
140+
'no_found_rows' => true,
141+
) );
142+
143+
if ( $child_query->have_posts() ) {
144+
while ( $child_query->have_posts() ) {
145+
$child_query->the_post();
146+
self::save_post( get_the_ID(), $post );
128147
}
129148
}
149+
130150
wp_reset_postdata();
131151
}
132-
152+
133153
return true;
134154
}
135155

readme.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
=== Url Memory ===
1+
=== URL Memory ===
22

33
Contributors: momo360modena,asadowski10
44
Donate link: http://www.beapi.fr/donate/
55
Tags: url, path, url memory,
66
Requires at least: 3.0
7-
Tested up to: 3.4.1
7+
Tested up to: 4.8.2
88
Stable tag: 1.0
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -38,4 +38,7 @@ This plugin is developped on WordPress 3.4, with the constant WP_DEBUG to TRUE.
3838
== Changelog ==
3939

4040
* Version 1.0 :
41-
* First version stable
41+
* First version stable
42+
43+
* Version 1.0.1 :
44+
* Add filter "apply_filters( 'url_memory_redirect_request_uri', $_SERVER['REQUEST_URI'] );" to parse result request_uri before request in redirections table

url-memory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Description: Save history URLs for a post and manages 301 redirects for SEO
66
Author: Amaury Balmer, Alexandre Sadowski
77
Author URI: http://www.beapi.fr
8-
Version: 1.1
8+
Version: 1.0.1
99
Text Domain: url-memory
1010
Domain Path: /languages/
1111
Network: false
@@ -35,7 +35,7 @@
3535
$wpdb -> url_redirect = $wpdb -> prefix . 'url_redirect';
3636

3737
// Folder name
38-
define('UM_VERSION', '1.0');
38+
define('UM_VERSION', '1.0.1');
3939
define('UM_OPTION', 'url_redirect');
4040

4141
define('UM_URL', plugins_url('', __FILE__));
@@ -63,7 +63,7 @@
6363
if (!class_exists('WP_List_Table')) {
6464
require_once (ABSPATH . '/wp-admin/includes/class-wp-list-table.php');
6565
}
66-
66+
6767
// Call Admin Manage class
6868
require (UM_DIR . '/inc/class.admin.manage.php');
6969
}

0 commit comments

Comments
 (0)