FV Top Level Categories - Version 1.5

Version Description

Download this release

Release Info

Developer FolioVision
Plugin Icon 128x128 FV Top Level Categories
Version 1.5
Comparing to
See all releases

Code changes from version 1.4 to 1.5

Files changed (3) hide show
  1. README.md +6 -0
  2. readme.txt +16 -9
  3. top-level-cats.php +297 -2
README.md ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ fv-top-level-cats
2
+ =================
3
+
4
+ This is a fix of Top Level Categories plugin for Wordpress 3.1. and above.
5
+ =======
6
+ 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>
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: FolioVision
3
  Donate link: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
4
  Tags: categories, permalink
5
  Requires at least: 3.2.1
6
- Tested up to: 3.4.1
7
  Stable tag: trunk
8
 
9
  This is a fix of Top Level Categories plugin for Wordpress 3.1. and above.
@@ -45,27 +45,34 @@ Make sure your categories have unique slugs - watch out for pages with the same
45
 
46
  == Changelog ==
47
 
48
- = 1.4 =
 
 
 
 
 
 
 
49
  * fix for Wordpress 3.4.1 - category prefix was part of the generated URLs
50
 
51
- = 1.3 =
52
  * for for flushing of rewrite rules on plugin activation in WP 3.3
53
 
54
- = 1.2 =
55
  * fix for WP 3.3
56
  * fix for /%categor%/%post-name% permalink structure
57
 
58
- = 1.1.3 =
59
  * fix for deeper nested pages
60
 
61
- = 1.1.2 =
62
  * fix for /category/child-category redirecting to /child-category page
63
 
64
- = 1.1.1 =
65
  * fix for deeper nested categories
66
 
67
- = 1.1 =
68
  * fix for WP 3.1
69
 
70
- = 1.0.1 =
71
  * original version
3
  Donate link: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
4
  Tags: categories, permalink
5
  Requires at least: 3.2.1
6
+ Tested up to: 4.0
7
  Stable tag: trunk
8
 
9
  This is a fix of Top Level Categories plugin for Wordpress 3.1. and above.
45
 
46
  == Changelog ==
47
 
48
+ = Version 1.5 - July 22nd, 2014 =
49
+ * Settings screen added!
50
+ * Category restrictions for post permalinks added! If you use post permalink structure with category in it, you can now restrict which categories will be allowed in the URL. Wordpress always picks the category with lowest category ID and that often causes inappropriate categories to show up in URLs - like /featured-content/2014/07/my-post"
51
+ * Or you can simply force only the parent categories to show up in post URLs. So /parent-category/child-category/2014/07/my-post will change to /parent-category/2014/07/my-post
52
+ * If you open the old post URL, proper 301 redirection to the new URL will be used.
53
+
54
+
55
+ = Version 1.4 =
56
  * fix for Wordpress 3.4.1 - category prefix was part of the generated URLs
57
 
58
+ = Version 1.3 =
59
  * for for flushing of rewrite rules on plugin activation in WP 3.3
60
 
61
+ = Version 1.2 =
62
  * fix for WP 3.3
63
  * fix for /%categor%/%post-name% permalink structure
64
 
65
+ = Version 1.1.3 =
66
  * fix for deeper nested pages
67
 
68
+ = Version 1.1.2 =
69
  * fix for /category/child-category redirecting to /child-category page
70
 
71
+ = Version 1.1.1 =
72
  * fix for deeper nested categories
73
 
74
+ = Version 1.1 =
75
  * fix for WP 3.1
76
 
77
+ = Version 1.0.1 =
78
  * original version
top-level-cats.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: FV Top Level Categories
4
  Plugin URI: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
5
  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>
6
- Version: 1.4
7
  Author: Foliovision
8
  Author URI: http://foliovision.com/
9
  */
@@ -79,6 +79,31 @@ function fv_top_level_categories_rewrite_rules($category_rewrite) {
79
  return $category_rewrite;
80
  }
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  // Add 'category_redirect' query variable
83
  add_filter('query_vars', 'fv_top_level_categories_query_vars');
84
  function fv_top_level_categories_query_vars($public_query_vars) {
@@ -116,4 +141,274 @@ function top_level_cats_remove_cat_base($link) {
116
  return preg_replace('|' . $category_base . '|', '', $link, 1);
117
  }
118
 
119
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  Plugin Name: FV Top Level Categories
4
  Plugin URI: http://foliovision.com/seo-tools/wordpress/plugins/fv-top-level-categories
5
  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>
6
+ Version: 1.5
7
  Author: Foliovision
8
  Author URI: http://foliovision.com/
9
  */
79
  return $category_rewrite;
80
  }
81
 
82
+ //Redirect to TL parent categ, if "Only use top-level catogories in URLs." is on
83
+ add_filter('template_redirect', 'fv_top_level_categories_tlc_redirect', 999, 2);
84
+ function fv_top_level_categories_tlc_redirect( $link ) {
85
+ if( FV_Top_Level_Cats::is_top_level_only() && is_single() ) {
86
+ global $wp_query;
87
+ $requested_url = is_ssl() ? 'https://' : 'http://';
88
+ $requested_url .= $_SERVER['HTTP_HOST'];
89
+ $requested_url .= $_SERVER['REQUEST_URI'];
90
+
91
+ $real_permalink = get_permalink($wp_query->queried_object_id);
92
+
93
+ if( FALSE === stripos($requested_url, $real_permalink) ) {
94
+
95
+ $bMached = preg_match('~/([^/:]+/?)$~',$real_permalink, $end_of_permalink);
96
+ if( $bMached && preg_match('~'.$end_of_permalink[1].'(.+)$~', $requested_url, $end_of_url) )
97
+ wp_redirect( $real_permalink . $end_of_url[1], 301 );
98
+ else
99
+ wp_redirect( $real_permalink, 301 );
100
+ die();
101
+ }
102
+ }
103
+
104
+ return $link;
105
+ }
106
+
107
  // Add 'category_redirect' query variable
108
  add_filter('query_vars', 'fv_top_level_categories_query_vars');
109
  function fv_top_level_categories_query_vars($public_query_vars) {
141
  return preg_replace('|' . $category_base . '|', '', $link, 1);
142
  }
143
 
144
+
145
+
146
+
147
+ function fv_top_level_cats_post_link_category_top_level_only( $cat ) {
148
+ if( !FV_Top_Level_Cats::is_category_permalinks() ) {
149
+ return $cat;
150
+ }
151
+
152
+ while( FV_Top_Level_Cats::is_top_level_only() && $cat->parent != 0 ) {
153
+ $cat = get_term_by( 'id', $cat->parent, 'category' );
154
+ }
155
+
156
+ return $cat;
157
+ }
158
+ add_filter( 'post_link_category', 'fv_top_level_cats_post_link_category_top_level_only', 201, 3 );
159
+
160
+
161
+
162
+
163
+ function fv_top_level_cats_post_link_category_restrict( $cat ) {
164
+ if( !FV_Top_Level_Cats::is_category_permalinks() || !FV_Top_Level_Cats::is_category_restriction() ) {
165
+ return $cat;
166
+ }
167
+
168
+ $aArgs = func_get_args();
169
+
170
+ $aAllowedCats = FV_Top_Level_Cats::get_allowed_cats();
171
+ if( !count($aAllowedCats) ) {
172
+ return $cat;
173
+ }
174
+
175
+ // check if the main category is allowed
176
+ if( in_array( $cat->term_id, $aAllowedCats ) ) {
177
+ return $cat;
178
+ }
179
+
180
+ // check if any of the other categories is allowed!
181
+ $isOk = false;
182
+ foreach( $aArgs[1] AS $objCat ) {
183
+ if( in_array( $objCat->term_id, $aAllowedCats ) ) {
184
+ $isOk = true;
185
+ $cat = $objCat;
186
+ }
187
+ }
188
+
189
+ // check if any of the parent categories is allowed
190
+ if( !$isOk ) {
191
+ foreach( $aArgs[1] AS $objCat ) {
192
+ while( $objCat->parent != 0 ) {
193
+ $objCat = get_term_by( 'id', $objCat->parent, 'category' );
194
+ }
195
+ if( in_array( $objCat->term_id, $aAllowedCats ) ) {
196
+ $isOk = true;
197
+ $cat = $objCat;
198
+ }
199
+ }
200
+ }
201
+
202
+ return $cat;
203
+ }
204
+ add_filter( 'post_link_category', 'fv_top_level_cats_post_link_category_restrict', 200, 3 );
205
+
206
+
207
+
208
+
209
+ function fv_top_level_category_filter( $aCategories ) {
210
+ if( class_exists('FV_Top_Level_Cats') && method_exists('FV_Top_Level_Cats','get_allowed_cats') ) {
211
+
212
+ $aAllowedCats = FV_Top_Level_Cats::get_allowed_cats();
213
+ if( !count($aAllowedCats) ) {
214
+ return $aCategories;
215
+ }
216
+
217
+ // check if the main category is allowed
218
+ foreach( $aCategories AS $objCat ) {
219
+ if( in_array( $objCat->term_id, $aAllowedCats ) ) {
220
+ return array($objCat);
221
+ }
222
+ }
223
+
224
+ foreach( $aCategories AS $objCat ) {
225
+ while( $objCat->parent != 0 ) {
226
+ $objCat = get_term_by( 'id', $objCat->parent, 'category' );
227
+ }
228
+ if( in_array( $objCat->term_id, $aAllowedCats ) ) {
229
+ return array($objCat);
230
+ }
231
+ }
232
+ }
233
+ return $aCategories;
234
+ }
235
+
236
+
237
+
238
+
239
+
240
+ function fv_top_level_category( $separator = '', $parents = '', $post_id = false ) {
241
+ add_filter( 'get_the_categories', 'fv_top_level_category_filter' );
242
+ the_category( $separator, $parents, $post_id );
243
+ remove_filter( 'get_the_categories', 'fv_top_level_category_filter' );
244
+ }
245
+ add_action( 'fv_top_level_category', 'fv_top_level_category', 10, 3 );
246
+
247
+
248
+
249
+
250
+ class FV_Top_Level_Cats {
251
+
252
+ var $enabled;
253
+ var $default_form_code;
254
+ var $default_form_css;
255
+
256
+
257
+
258
+
259
+ public function __construct() {
260
+ add_action( 'admin_menu', array($this, 'admin_menu') );
261
+ }
262
+
263
+
264
+
265
+
266
+ function admin_menu() {
267
+ add_options_page( 'FV Top Level Categories', 'FV Top Level Categories', 'manage_options', 'fv_top_level_cats', array($this, 'options_panel') );
268
+ }
269
+
270
+
271
+
272
+
273
+ public static function get_allowed_cats() {
274
+ $options = get_option( 'fv_top_level_cats' );
275
+ if( isset($options['category-allow']) ) {
276
+ return $options['category-allow'];
277
+ } else {
278
+ return false;
279
+ }
280
+ }
281
+
282
+
283
+
284
+
285
+ public static function is_category_permalinks() {
286
+ $sPermalinks = get_option( 'permalink_structure' );
287
+ if( stripos($sPermalinks, '%category%/') !== false ) {
288
+ return true;
289
+ } else {
290
+ return false;
291
+ }
292
+ }
293
+
294
+
295
+
296
+
297
+ public static function is_top_level_only() {
298
+ $options = get_option( 'fv_top_level_cats' );
299
+ if( isset($options['top-level-only']) && $options['top-level-only'] ) {
300
+ return true;
301
+ } else {
302
+ return false;
303
+ }
304
+ }
305
+
306
+
307
+
308
+
309
+ public static function is_category_restriction() {
310
+ $options = get_option( 'fv_top_level_cats' );
311
+ if( isset($options['category-allow-enabled']) && $options['category-allow-enabled'] ) {
312
+ return true;
313
+ } else {
314
+ return false;
315
+ }
316
+ }
317
+
318
+
319
+
320
+
321
+ function options_panel() {
322
+
323
+ if (!empty($_POST)) :
324
+
325
+ check_admin_referer('fv_top_level_cats');
326
+
327
+ if( isset($_POST['fv_top_level_cats_submit'] ) ) :
328
+ $options = get_option( 'fv_top_level_cats', array() );
329
+
330
+ $options['category-allow'] = $_POST['post_category'];
331
+ $options['top-level-only'] = ( $_POST['top-level-only'] ) ? true : false;
332
+ $options['category-allow-enabled'] = ( $_POST['category-allow-enabled'] ) ? true : false;
333
+
334
+ update_option( 'fv_top_level_cats', $options );
335
+ ?>
336
+ <div id="message" class="updated fade">
337
+ <p>
338
+ <strong>
339
+ Settings saved
340
+ </strong>
341
+ </p>
342
+ </div>
343
+ <?php
344
+ endif; // fv_top_level_cats_submit
345
+
346
+ endif;
347
+
348
+ $options = get_option( 'fv_top_level_cats' );
349
+ ?>
350
+
351
+ <div class="wrap">
352
+ <div style="position: absolute; right: 20px; margin-top: 5px">
353
+ <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>
354
+ </div>
355
+ <div>
356
+ <div id="icon-options-general" class="icon32"><br /></div>
357
+ <h2>FV Top Level Categories</h2>
358
+ </div>
359
+
360
+ <?php if( $this->is_category_permalinks() ) : ?>
361
+ <style>
362
+ #category-allow ul.children { margin-left: 20px; }
363
+ </style>
364
+ <form method="post" action="">
365
+ <?php wp_nonce_field('fv_top_level_cats') ?>
366
+ <div id="poststuff" class="ui-sortable">
367
+ <div class="postbox">
368
+ <h3>
369
+ <?php _e('Adjust categories in your post URLs') ?>
370
+ </h3>
371
+ <div class="inside">
372
+ <table class="form-table">
373
+ <tr>
374
+ <td>
375
+ <label for="top-level-only">
376
+ <input type="checkbox" name="top-level-only" id="top-level-only" value="1" <?php if( $options['top-level-only'] ) echo 'checked="checked"'; ?> />
377
+ Only use top-level catogories in URLs.
378
+ </label>
379
+ </td>
380
+ </tr>
381
+ <tr>
382
+ <td>
383
+ <label for="category-allow-enabled">
384
+ <input type="checkbox" name="category-allow-enabled" id="category-allow-enabled" value="1" <?php if( $options['category-allow-enabled'] ) echo 'checked="checked"'; ?> />
385
+ Only allow following categories in URLs:
386
+ </label>
387
+ <blockquote><ul id="category-allow"><?php wp_category_checklist( 0, 0, $options['category-allow'], false, null, false ); ?></ul></blockquote>
388
+ </td>
389
+ </tr>
390
+ </table>
391
+ <p>
392
+ <input type="submit" name="fv_top_level_cats_submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
393
+ </p>
394
+ </div>
395
+ </div>
396
+ <p><?php echo __('Are you having any problems or questions? Use our <a target="_blank" href="http://foliovision.com/support/fv-top-level-categories/">support forums</a>.'); ?></p>
397
+ </div>
398
+
399
+ </form>
400
+ <?php else : ?>
401
+ <p>Since you are not using %category% in your post permalinks, there is nothing to adjust.</p>
402
+ <?php endif; ?>
403
+
404
+ </div>
405
+
406
+
407
+ <?php
408
+ }
409
+
410
+
411
+ }
412
+
413
+
414
+ $FV_Top_Level_Cats = new FV_Top_Level_Cats;