-
Notifications
You must be signed in to change notification settings - Fork 4
/
top-level-cats.php
477 lines (365 loc) · 15.2 KB
/
top-level-cats.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
/*
Plugin Name: FV Top Level Categories
Plugin URI: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
Description: Removes the prefix from the URL for a category. For instance, if your old category link was <code>/category/catname</code> it will now be <code>/catname</code>
Version: 1.9.1
Author: Foliovision
Author URI: http://foliovision.com/
Text Domain: fv_tlc
Domain Path: /languages/
*/
register_activation_hook(__FILE__,'fv_top_level_categories_refresh_rules');
add_action('created_category','fv_top_level_categories_refresh_rules');
add_action('edited_category','fv_top_level_categories_refresh_rules');
add_action('delete_category','fv_top_level_categories_refresh_rules');
function fv_top_level_categories_refresh_rules() {
add_option('fv_top_level_categories_rewrite_rules_flush', 'true');
}
register_deactivation_hook(__FILE__,'fv_top_level_categories_deactivate');
function fv_top_level_categories_deactivate() {
remove_filter('category_rewrite_rules', 'fv_top_level_categories_refresh_rules'); // We don't want to insert our custom rules again
delete_option('fv_top_level_categories_rewrite_rules_flush');
}
// Remove category base
add_action('init', 'fv_top_level_categories_permastruct',999999);
function fv_top_level_categories_permastruct() {
global $wp_rewrite;
$wp_rewrite->extra_permastructs['category'][0] = '%category%';
$bFound = false;
$aRules = get_option('rewrite_rules');
if( $aRules && count($aRules) > 0 ) {
foreach( $aRules AS $key => $value ) {
if( $key == 'fv-top-level-cat-tweaks-detector-235hnguh9hq46j0909iasn0zzdfsAJ' ) {
$bFound = true;
break;
}
}
}
if( !$bFound || get_option('fv_top_level_categories_rewrite_rules_flush') == 'true') {
flush_rewrite_rules();
delete_option('fv_top_level_categories_rewrite_rules_flush');
}
}
// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'fv_top_level_categories_rewrite_rules');
function fv_top_level_categories_rewrite_rules($category_rewrite) {
//var_dump($category_rewrite); // For Debugging
$bAMP = false;
foreach( $category_rewrite AS $k => $v ) {
if( stripos($k, '/amp') !== false ) {
$bAMP = true;
}
}
// First we need to get full URLs of our pages
$pages = get_pages( 'number=0' );
$pages_urls = array();
foreach( $pages AS $pages_item ) {
$pages_urls[] = trim( str_replace( get_bloginfo( 'url' ), '', get_permalink( $pages_item->ID ) ), '/' );
}
global $wp_rewrite;
$category_rewrite=array();
$categories=get_categories(array('hide_empty'=>false));
foreach($categories as $category) {
$category_nicename = $category->slug;
if ( $category->parent == $category->cat_ID ) // recursive recursion
$category->parent = 0;
elseif ($category->parent != 0 )
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
// Let's check if any of the category full URLs matches any of the pages
if( in_array( $category_nicename, $pages_urls ) ) {
continue;
}
$category_rewrite['('.$category_nicename.')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['('.$category_nicename.')/'. $wp_rewrite->pagination_base .'/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
if( $bAMP ) {
$category_rewrite['('.$category_nicename.')/amp/'. $wp_rewrite->pagination_base .'/?([0-9]{1,})/?$'] = 'index.php?amp&category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['('.$category_nicename.')/amp/?$'] = 'index.php?amp&category_name=$matches[1]';
}
$category_rewrite['('.$category_nicename.')/?$'] = '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]';
$category_rewrite['fv-top-level-cat-tweaks-detector-235hnguh9hq46j0909iasn0zzdfsAJ'] = 'index.php?fv-top-level-cat-tweaks-detector-235hnguh9hq46j0909iasn0zzdfsAJ=1';
//var_dump($category_rewrite); // For Debugging
return $category_rewrite;
}
//Redirect to TL parent categ, if "Only use top-level categories in URLs." is on
add_filter('template_redirect', 'fv_top_level_categories_tlc_redirect', 999, 2);
function fv_top_level_categories_tlc_redirect( $link ) {
if( FV_Top_Level_Cats::is_top_level_only() && is_single() ) {
global $wp_query;
$requested_url = is_ssl() ? 'https://' : 'http://';
$requested_url .= $_SERVER['HTTP_HOST'];
$requested_url .= $_SERVER['REQUEST_URI'];
$real_permalink = get_permalink($wp_query->queried_object_id);
if( $real_permalink && FALSE === stripos($requested_url, $real_permalink) ) {
$bMached = preg_match('~/([^/:]+/?)$~',$real_permalink, $end_of_permalink);
if( $bMached && preg_match('~'.$end_of_permalink[1].'(.+)$~', $requested_url, $end_of_url) )
wp_redirect( $real_permalink . $end_of_url[1], 301 );
else
wp_redirect( $real_permalink, 301 );
die();
}
}
return $link;
}
// Add 'category_redirect' query variable
add_filter('query_vars', 'fv_top_level_categories_query_vars');
function fv_top_level_categories_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
// Redirect if 'category_redirect' is set
add_filter('request', 'fv_top_level_categories_request');
function fv_top_level_categories_request($query_vars) {
//print_r($query_vars); // For Debugging
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;
}
add_filter('category_link', 'top_level_cats_remove_cat_base');
function top_level_cats_remove_cat_base($link) {
$category_base = get_option('category_base');
// WP uses "category/" as the default
if ($category_base == '')
$category_base = 'category';
// Remove initial slash, if there is one (we remove the trailing slash in the regex replacement and don't want to end up short a slash)
if (substr($category_base, 0, 1) == '/')
$category_base = substr($category_base, 1);
$category_base .= '/';
return preg_replace('|' . $category_base . '|', '', $link, 1);
}
function fv_top_level_cats_post_link_category_top_level_only( $cat ) {
if( !FV_Top_Level_Cats::is_category_permalinks() ) {
return $cat;
}
while( FV_Top_Level_Cats::is_top_level_only() && $cat->parent != 0 ) {
$cat = get_term_by( 'id', $cat->parent, 'category' );
}
return $cat;
}
add_filter( 'post_link_category', 'fv_top_level_cats_post_link_category_top_level_only', 201, 3 );
function fv_top_level_cats_post_link_category_restrict( $cat ) {
if( !FV_Top_Level_Cats::is_category_permalinks() || !FV_Top_Level_Cats::is_category_restriction() ) {
return $cat;
}
$aArgs = func_get_args();
$aAllowedCats = FV_Top_Level_Cats::get_allowed_cats();
if( !count($aAllowedCats) ) {
return $cat;
}
// check if the main category is allowed
if( in_array( $cat->term_id, $aAllowedCats ) ) {
return $cat;
}
// check if any of the other categories is allowed!
$isOk = false;
foreach( $aArgs[1] AS $objCat ) {
if( in_array( $objCat->term_id, $aAllowedCats ) ) {
$isOk = true;
$cat = $objCat;
}
}
// check if any of the parent categories is allowed
if( !$isOk ) {
foreach( $aArgs[1] AS $objCat ) {
while( $objCat->parent != 0 ) {
$objCat = get_term_by( 'id', $objCat->parent, 'category' );
}
if( in_array( $objCat->term_id, $aAllowedCats ) ) {
$isOk = true;
$cat = $objCat;
}
}
}
return $cat;
}
add_filter( 'post_link_category', 'fv_top_level_cats_post_link_category_restrict', 200, 3 );
function fv_top_level_category_filter( $aCategories ) {
if( class_exists('FV_Top_Level_Cats') && method_exists('FV_Top_Level_Cats','get_allowed_cats') ) {
$aAllowedCats = FV_Top_Level_Cats::get_allowed_cats();
if( !count($aAllowedCats) ) {
return $aCategories;
}
// check if the main category is allowed
foreach( $aCategories AS $objCat ) {
if( in_array( $objCat->term_id, $aAllowedCats ) ) {
return array($objCat);
}
}
foreach( $aCategories AS $objCat ) {
while( $objCat->parent != 0 ) {
$objCat = get_term_by( 'id', $objCat->parent, 'category' );
}
if( in_array( $objCat->term_id, $aAllowedCats ) ) {
return array($objCat);
}
}
}
return $aCategories;
}
function fv_top_level_category( $separator = '', $parents = '', $post_id = false ) {
add_filter( 'get_the_categories', 'fv_top_level_category_filter' );
the_category( $separator, $parents, $post_id );
remove_filter( 'get_the_categories', 'fv_top_level_category_filter' );
}
add_action( 'fv_top_level_category', 'fv_top_level_category', 10, 3 );
class FV_Top_Level_Cats {
var $enabled;
var $default_form_code;
var $default_form_css;
public function __construct() {
add_action( 'admin_menu', array($this, 'admin_menu') );
add_action('init',array($this,'load_languages'));
}
function admin_menu() {
add_options_page( __('FV Top Level Categories','fv_tlc'), __('FV Top Level Categories','fv_tlc'), 'manage_options', 'fv_top_level_cats', array($this, 'options_panel') );
}
public static function get_allowed_cats() {
$options = get_option( 'fv_top_level_cats' );
if( isset($options['category-allow']) ) {
return $options['category-allow'];
} else {
return false;
}
}
public static function is_category_permalinks() {
$sPermalinks = get_option( 'permalink_structure' );
if( stripos($sPermalinks, '%category%/') !== false ) {
return true;
} else {
return false;
}
}
function load_languages(){
// Localization
load_plugin_textdomain('fv_tlc', false, dirname(plugin_basename(__FILE__)) . "/languages");
}
public static function is_top_level_only() {
$options = get_option( 'fv_top_level_cats' );
if( isset($options['top-level-only']) && $options['top-level-only'] ) {
return true;
} else {
return false;
}
}
public static function is_category_restriction() {
$options = get_option( 'fv_top_level_cats' );
if( isset($options['category-allow-enabled']) && $options['category-allow-enabled'] ) {
return true;
} else {
return false;
}
}
function options_panel() {
if (!empty($_POST)) :
check_admin_referer('fv_top_level_cats');
if( isset($_POST['fv_top_level_cats_submit'] ) ) :
if(isset($_POST['fv_top_level_cats'])) {
$options = get_option( 'fv_top_level_cats', array() );
}
if(isset($_POST['post_category'])) {
$options['category-allow'] = $_POST['post_category'];
}
if(isset($_POST['top-level-only'])) {
$options['top-level-only'] = ( $_POST['top-level-only'] ) ? true : false;
}
if(isset($_POST['category-allow-enabled'])){
$options['category-allow-enabled'] = ( $_POST['category-allow-enabled'] ) ? true : false;
}
if(isset($options)) {
update_option( 'fv_top_level_cats', $options );
}
?>
<div id="message" class="updated fade">
<p>
<strong>
Settings saved
</strong>
</p>
</div>
<?php
endif; // fv_top_level_cats_submit
endif;
$options = get_option( 'fv_top_level_cats' );
?>
<div class="wrap">
<div style="position: absolute; right: 20px; margin-top: 5px">
<a href="http://foliovision.com/wordpress/plugins/fv-top-level-categories" target="_blank" title="Documentation"><img alt="visit foliovision" src="http://foliovision.com/shared/fv-logo.png" /></a>
</div>
<div>
<div id="icon-options-general" class="icon32"><br /></div>
<h2><?php _e('FV Top Level Categories','fv_tlc'); ?></h2>
</div>
<?php if( $this->is_category_permalinks() ) : ?>
<style>
#category-allow ul.children { margin-left: 20px; }
</style>
<form method="post" action="">
<?php wp_nonce_field('fv_top_level_cats') ?>
<div id="poststuff" class="ui-sortable">
<div class="postbox">
<h3>
<?php _e('Adjust categories in your post URLs','fv_tlc') ?>
</h3>
<div class="inside">
<table class="form-table">
<tr>
<td>
<label for="top-level-only">
<input type="checkbox" name="top-level-only" id="top-level-only" value="1" <?php if( isset($options['top-level-only'] )) { if( $options['top-level-only'] ) echo 'checked="checked"'; }?> />
<?php _e('Only use top-level categories in URLs.','fv_tlc') ; ?>
</label>
</td>
</tr>
<tr>
<td>
<label for="category-allow-enabled">
<input type="checkbox" name="category-allow-enabled" id="category-allow-enabled" value="1" <?php if(isset($options['category-allow-enabled'])) { if( $options['category-allow-enabled'] ) echo 'checked="checked"'; }?> />
<?php _e('Only allow following categories in URLs:','fv_tlc' );?>
</label>
<blockquote>
<ul id="category-allow"> <?php
if( isset($options['category-allow']) ) {
$descendants_and_self = $options['category-allow'];
}else{
$descendants_and_self = 0; //wp default value
}
wp_category_checklist( 0, 0, $descendants_and_self, false, null, false );
?>
</ul>
</blockquote>
</td>
</tr>
</table>
<p>
<input type="submit" name="fv_top_level_cats_submit" class="button-primary" value="<?php _e('Save Changes','fv_tlc'); ?>" />
</p>
</div>
</div>
<p><?php _e('Are you having any problems or questions? Use our <a target="_blank" href="http://foliovision.com/support/fv-top-level-categories/">support forums</a>.','fv_tlc'); ?></p>
</div>
</form>
<?php else : ?>
<p><?php _e('Since you are not using %category% in your post permalinks, there is nothing to adjust.','fv_tlc'); ?></p>
<?php endif; ?>
</div>
<?php
}
}
$FV_Top_Level_Cats = new FV_Top_Level_Cats;
// Add settings link on plugin page
function fv_top_level_categories_settings_link($links) {
$settings_link = '<a href="options-general.php?page=fv_top_level_cats">' . __('Settings','fv_tlc') . '</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'fv_top_level_categories_settings_link' );