forked from menmo/wp-no-category-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-category-base-wpml.php
149 lines (125 loc) · 5.25 KB
/
no-category-base-wpml.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
Plugin Name: No Category Base (WPML)
Version: 1.3
Plugin URI: http://infolific.com/technology/software-worth-using/no-category-base-for-wordpress/
Description: Removes '/category' from your category permalinks. WPML compatible.
Author: Marios Alexandrou
Author URI: http://infolific.com/technology/
License: GPLv2 or later
Text Domain: no-category-base-wpml
*/
/*
Copyright 2015 Marios Alexandrou
Copyright 2011 Mines (email: [email protected])
Copyright 2008 Saurabh Gupta (email: [email protected])
Based on the work by Saurabh Gupta (email : [email protected])
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* hooks */
register_activation_hook(__FILE__, 'no_category_base_refresh_rules');
register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
/* actions */
add_action('created_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('init', 'no_category_base_permastruct');
/* filters */
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
add_filter('query_vars', 'no_category_base_query_vars'); // Adds 'category_redirect' query variable
add_filter('request', 'no_category_base_request'); // Redirects if 'category_redirect' is set
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function no_category_base_deactivate() {
remove_filter( 'category_rewrite_rules', 'no_category_base_rewrite_rules' ); // We don't want to insert our custom rules again
no_category_base_refresh_rules();
}
/**
* Removes category base.
*
* @return void
*/
function no_category_base_permastruct()
{
global $wp_rewrite;
global $wp_version;
if ( $wp_version >= 3.4 ) {
$wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
} else {
$wp_rewrite->extra_permastructs['category'][0] = '%category%';
}
}
/**
* Adds our custom category rewrite rules.
*
* @param array $category_rewrite Category rewrite rules.
*
* @return array
*/
function no_category_base_rewrite_rules($category_rewrite) {
global $wp_rewrite;
$category_rewrite=array();
/* WPML is present: temporary disable terms_clauses filter to get all categories for rewrite */
if ( class_exists( 'Sitepress' ) ) {
global $sitepress;
remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
$categories = get_categories( array( 'hide_empty' => false ) );
//Fix provided by Albin here https://wordpress.org/support/topic/bug-with-wpml-2/#post-8362218
//add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ), 10, 4 );
} else {
$categories = get_categories( array( 'hide_empty' => false, 'parent' => 0 ) );
}
foreach( $categories as $category ) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID ) {
$category->parent = 0;
} elseif ( $category->parent != 0 ) {
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
}
$decoded_category_nicename = urldecode( $category_nicename );
$category_slugs = array_unique( array( $category_nicename, $decoded_category_nicename ) );
foreach( $category_slugs as $category_slug ) {
$category_rewrite["({$category_slug}(?:/.+)?)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$"] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite["({$category_slug}(?:/.+)?)/{$wp_rewrite->pagination_base}/?([0-9]{1,})/?$"] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite["({$category_slug}(?:/.+)?)/?$"] = 'index.php?category_name=$matches[1]';
}
}
// Redirect support from Old Category Base
$old_category_base = get_option( 'category_base' ) ? get_option( 'category_base' ) : 'category';
$old_category_base = trim( $old_category_base, '/' );
$category_rewrite[$old_category_base.'/(.*)$'] = 'index.php?category_redirect=$matches[1]';
return $category_rewrite;
}
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
/**
* Handles category redirects.
*
* @param $query_vars Current query vars.
*
* @return array $query_vars, or void if category_redirect is present.
*/
function no_category_base_request($query_vars) {
if( isset( $query_vars['category_redirect'] ) ) {
$catlink = trailingslashit( get_option( 'home' ) ) . user_trailingslashit( $query_vars['category_redirect'], 'category' );
status_header( 301 );
header( "Location: $catlink" );
exit();
}
return $query_vars;
}