Skip to content

Commit

Permalink
Merge pull request #6 from swissspidy/patch
Browse files Browse the repository at this point in the history
Prevent redirects by filtering the SQL queries
  • Loading branch information
grappler committed May 12, 2015
2 parents 4f404bc + 8803a5c commit 87e1d73
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions polylang-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,49 @@ function polylang_slug_unique_slug_in_language( $slug, $post_ID, $post_status, $
}
add_filter( 'wp_unique_post_slug', 'polylang_slug_unique_slug_in_language', 10, 6 );

/**
* Modify the sql query to include checks for the current language
*
* @since 0.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global StdClass $polylang
*
* @param string $query Database query.
*
* @return string The modified query
*/
function polylang_slug_filter_queries( $query ) {
global $wpdb, $polylang;
// keep a record of the queries
$queries[] = $query;

$is_main_sql = preg_match(
"#SELECT ID, post_name, post_parent, post_type FROM {$wpdb->posts} WHERE post_name IN \(([^)]+)\) AND post_type IN \(([^)]+)\)#",
trim(str_replace(array("\t", "\n"), array( '', ' ' ), $query)),
$matches );

if ( $is_main_sql ) {

$lang = pll_current_language();

// " INNER JOIN $wpdb->term_relationships AS pll_tr ON pll_tr.object_id = " . ('term' == $type ? "t.term_id" : "ID");
$join_clause = $polylang->model->join_clause( 'post' );
// " AND pll_tr.term_taxonomy_id IN (" . implode(',', $languages) . ")"
$where_clause = $polylang->model->where_clause( $lang, 'post' );

$query = "SELECT ID, post_name, post_parent, post_type
FROM {$wpdb->posts}
$join_clause
WHERE post_name IN ({$matches[1]})
AND post_type IN ({$matches[2]})
$where_clause";
}

return $query;
}
add_filter( 'query', 'polylang_slug_filter_queries' );

/**
* Extend the WHERE clause of the query
*
Expand Down

0 comments on commit 87e1d73

Please sign in to comment.