Qtranslate Slug - Version 0.9

Version Description

  • some wordpress qTranslate bug fixes
  • adds a javascript solution for qTranslate Nav Menus
Download this release

Release Info

Developer carlos_a_sanz
Plugin Icon wp plugin Qtranslate Slug
Version 0.9
Comparing to
See all releases

Version 0.9

Files changed (6) hide show
  1. README.txt +83 -0
  2. js/qt-nav-menu-min.js +4 -0
  3. js/qt-nav-menu.js +89 -0
  4. qtranslate-slug.php +1525 -0
  5. readme.txt +83 -0
  6. version.txt +14 -0
README.txt ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Qtranslate Slug ===
2
+ Contributors: Carlos Sanz García
3
+ Donate link: none
4
+ Tags: qtranslate, slug, multilanguage
5
+ Requires at least: 3.0
6
+ Tested up to: 3.3.2
7
+ Version: 0.9
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Allows to define a slug for each language and some qTranslate bug fixes
12
+
13
+ == Description ==
14
+
15
+ This plugin allows translated slugs in Qtranslate.
16
+ You need install and activate previously Qtranslate, if not, Qtranslate Slug will not work.
17
+
18
+ Tested with Qtranslate versions 2.5.8 and 2.5.9
19
+
20
+ == Installation ==
21
+
22
+ This plugins requires qTranslate installed previously, if not, it will not activate.
23
+
24
+ 1. Upload `qtranslate-slug` to the `/wp-content/plugins/` directory
25
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
26
+ 1. That's all!
27
+
28
+ == Frequently Asked Questions ==
29
+
30
+ = It works with posts and pages, but with other content type ? =
31
+
32
+ This plugin allows to translate slugs of: posts, pages, custom post types, categories, tags and custom taxonomies.
33
+
34
+ = How can I insert a language selector in my theme ? =
35
+
36
+ Place `<?php if (function_exists('qTranslateSlug_generateLanguageSelectCode') ) qTranslateSlug_generateLanguageSelectCode('text'); ?>` in your template.
37
+
38
+ == Screenshots ==
39
+
40
+ 1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from
41
+ the directory of the stable readme.txt, so in this case, `/tags/4.3/screenshot-1.png` (or jpg, jpeg, gif)
42
+ 2. This is the second screen shot
43
+
44
+ == Changelog ==
45
+
46
+ = 0.9 =
47
+ * some wordpress qTranslate bug fixes
48
+ * adds a javascript solution for qTranslate Nav Menus
49
+
50
+ = 0.8 =
51
+ * added support por Categories
52
+ * added support por Tags
53
+ * added support por Taxonomies
54
+ * added support por Custom Post Types
55
+
56
+ = 0.7 ( enhanced by Zapo (http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=4&t=1049&start=50#p7499) ) =
57
+ * added suport for qTranslate TLD domain mode (en: domain.com | fr: domain.fr) visit
58
+
59
+ = 0.5 and 0.6 enhanched by Marco Del Percio =
60
+
61
+ == Upgrade Notice ==
62
+
63
+ = 0.9 =
64
+ This version fix some bugs and allow multilanguage in nav-menus.
65
+
66
+ = 0.8 =
67
+ A lot of slugs content allowed
68
+
69
+ = 0.7 =
70
+ This version allows TLD domain option for a different Qtranslate fork maded by Zappo
71
+
72
+
73
+ == Todo ==
74
+
75
+ * admin options page for setup the taxonomies names
76
+ * change the database system ( post meta ), without installing tables
77
+ * generate translated slug automatically from the translated title
78
+ * check if the slug is already used, and add a progressive number in this case
79
+ * force the use of the translated slug if defined
80
+ * force to show a page/post only at the correct URL
81
+ * try to redirect from a wrong URL to a correct URL
82
+ * keep track of renamed slugs, redirecting from old to new slugs
83
+ * translate categories and tags slugs.
js/qt-nav-menu-min.js ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ jQuery(document).ready(function(a){function c(){regexp=RegExp("&lt;!--:"+b+"--&gt;(.*?)&lt;!--:--&gt;","i");a(".item-title").each(function(){void 0==a(this).data("qt-value")&&a(this).data("qt-value",a(this).html());if(matches=a(this).data("qt-value").match(regexp))a(this).html(matches[1]),a(this).closest("li").find(".link-to-original a").text(matches[1])});regexp=RegExp("<\!--:"+b+"--\>(.*?)<\!--:--\>","i");a("input.edit-menu-item-title").each(function(){void 0==a(this).data("qt-value")&&a(this).data("qt-value",
2
+ a(this).val());(matches=a(this).data("qt-value").match(regexp))&&a(this).val(matches[1])});a("label.menu-item-title").each(function(){var d=a(this).contents().get(1);void 0==a(this).data("qt-value")&&a(this).data("qt-value",d.nodeValue);if(matches=a(this).data("qt-value").match(regexp))d.nodeValue=" "+matches[1]})}function e(){a("input.edit-menu-item-title").each(function(){a(this).val(a(this).data("qt-value"))})}var f=wpNavMenu.addMenuItemToBottom;wpNavMenu.addMenuItemToBottom=function(a,b){f(a,
3
+ b);c()};var g=wpNavMenu.addMenuItemToTop;wpNavMenu.addMenuItemToTop=function(a,b){g(a,b);c()};var b=a("#qt-languages :radio:checked").val();c();a("#qt-languages :radio").change(function(){b=a("#qt-languages :radio:checked").val();c()});a(".submit-add-to-menu").click(function(){b=a("#qt-languages :radio:checked").val();c()});a("input.edit-menu-item-title").live("change",function(){regexp=RegExp("(<\!--:"+b+"--\>)(.*?)(<\!--:--\>)","i");regexp.test(a(this).data("qt-value"))?a(this).data("qt-value",
4
+ a(this).data("qt-value").replace(regexp,"$1"+a(this).val()+"$3")):a(this).data("qt-value",a(this).val())});a(".menu-save").click(function(){e()});window.onbeforeunload=function(){e()}});
js/qt-nav-menu.js ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ jQuery(document).ready(function($){
2
+
3
+ // Change titles (and values) when user add new item to the menu:
4
+ var oldAddMenuItemToBottom = wpNavMenu.addMenuItemToBottom;
5
+ wpNavMenu.addMenuItemToBottom = function( menuMarkup, req ) {
6
+ oldAddMenuItemToBottom( menuMarkup, req );
7
+ changeTitles();
8
+ }
9
+ var oldAddMenuItemToTop = wpNavMenu.addMenuItemToTop;
10
+ wpNavMenu.addMenuItemToTop = function( menuMarkup, req ) {
11
+ oldAddMenuItemToTop( menuMarkup, req );
12
+ changeTitles();
13
+ }
14
+
15
+ // Change titles (and values) when document is ready:
16
+ var lang = $('#qt-languages :radio:checked').val();
17
+ changeTitles();
18
+
19
+ // Change titles (and values) when language is changed:
20
+ $('#qt-languages :radio').change( function() {
21
+ lang = $('#qt-languages :radio:checked').val();
22
+ changeTitles();
23
+ });
24
+
25
+ // Change titles (and values) when new menu is added:
26
+ $('.submit-add-to-menu').click( function() {
27
+ lang = $('#qt-languages :radio:checked').val();
28
+ changeTitles();
29
+ });
30
+
31
+ // Update original value when user changed a value:
32
+ $('input.edit-menu-item-title').live('change', function() {
33
+ regexp = new RegExp('(<!--:' + lang + '-->)(.*?)(<!--:-->)', 'i');
34
+ if( regexp.test( $(this).data( 'qt-value' ) ) )
35
+ $(this).data( 'qt-value', $(this).data('qt-value').replace( regexp, '$1' + $(this).val() + '$3' ) );
36
+ else
37
+ $(this).data( 'qt-value', $(this).val() );
38
+ });
39
+
40
+ // Change titles (and values):
41
+ function changeTitles() {
42
+ // Change menu item titles and links (on the right side):
43
+ regexp = new RegExp('&lt;!--:' + lang + '--&gt;(.*?)&lt;!--:--&gt;', 'i');
44
+ $('.item-title').each( function() {
45
+ if ($(this).data('qt-value') == undefined) $(this).data('qt-value', $(this).html());
46
+ if (matches = $(this).data('qt-value').match(regexp)) {
47
+ $(this).html( matches[1] );
48
+ $(this).closest('li').find('.link-to-original a').text( matches[1] );
49
+ }
50
+ });
51
+
52
+ // Change menu item title inputs (on the right side):
53
+ regexp = new RegExp('<!--:' + lang + '-->(.*?)<!--:-->', 'i');
54
+ $('input.edit-menu-item-title').each( function() {
55
+ if ($(this).data('qt-value') == undefined) $(this).data('qt-value', $(this).val());
56
+ if (matches = $(this).data('qt-value').match(regexp)) {
57
+ $(this).val( matches[1] );
58
+ }
59
+ });
60
+
61
+ // Change menu item checkbox labels (on the left side):
62
+ $('label.menu-item-title').each( function() {
63
+ var textNode = $(this).contents().get(1);
64
+ if ($(this).data('qt-value') == undefined) $(this).data('qt-value', textNode.nodeValue);
65
+ if (matches = $(this).data('qt-value').match(regexp)) {
66
+ textNode.nodeValue = ' ' + matches[1];
67
+ }
68
+ });
69
+ }
70
+
71
+ // Restore the original input values:
72
+ function restoreValues(){
73
+ $('input.edit-menu-item-title').each( function() {
74
+ $(this).val( $(this).data( 'qt-value') );
75
+ });
76
+ }
77
+
78
+ // Just before saving restore the original input values:
79
+ $('.menu-save').click(function() {
80
+ restoreValues();
81
+ });
82
+
83
+ // Just before leaving the page (or refresh) restore the original input values:
84
+ window.onbeforeunload = function(){
85
+ restoreValues();
86
+ return
87
+ }
88
+
89
+ });
qtranslate-slug.php ADDED
@@ -0,0 +1,1525 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: qTranslate slug
4
+ Plugin URI: http://wordpress.org/extend/plugins/qtranslate-slug/
5
+ Description: Allows to define a slug for each language and some qTranslate bug fixes
6
+ Version: 0.9
7
+ Author: Carlos Sanz Garcia
8
+ Author URI: http://codingsomethig.wordpress.com
9
+
10
+ this plugin is a complete fork of the original qTranslate slug developed by Cimatti Consulting http://www.cimatti.it
11
+
12
+ --------------------------------------------------------------------------------------------------------
13
+
14
+
15
+ version 0.9
16
+ + some wordpress qTranslate bug fixes
17
+ + adds a javascript solution for qTranslate Nav Menus
18
+
19
+ version 0.8
20
+ + added support por Categories
21
+ + added support por Tags
22
+ + added support por Taxonomies
23
+ + added support por Custom Post Types
24
+
25
+ version 0.7 enhanced by Zapo (http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=4&t=1049&start=50#p7499)
26
+ + added suport for qTranslate TLD domain mode (en: domain.com | fr: domain.fr) visit
27
+
28
+ Version 0.5 and 0.6 enhanched by Marco Del Percio
29
+
30
+ --------------------------------------------------------------------------------------------------------
31
+
32
+ This program is free software; you can redistribute it and/or
33
+ modify it under the terms of the GNU General Public License
34
+ as published by the Free Software Foundation; either version 2
35
+ of the License, or (at your option) any later version.
36
+
37
+ This program is distributed in the hope that it will be useful,
38
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
39
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40
+ GNU General Public License for more details.
41
+
42
+ You should have received a copy of the GNU General Public License
43
+ along with this program; if not, write to the Free Software
44
+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
45
+
46
+
47
+ The full copy of the GNU General Public License is available here: http://www.gnu.org/licenses/gpl.txt
48
+
49
+ */
50
+
51
+ /* Notes
52
+ * fb() is a function defined by the WP-FirePHP plugin <http://wordpress.org/extend/plugins/wp-firephp/>, that allows debug using Firefox, Firebug and FirePHP.
53
+ *
54
+ * TODO:
55
+
56
+ * admin options page for setup the taxonomies names
57
+ * change the database system ( post meta ), without installing tables
58
+ * generate translated slug automatically from the translated title
59
+ * check if the slug is already used, and add a progressive number in this case
60
+ * force the use of the translated slug if defined
61
+ * force to show a page/post only at the correct URL
62
+ * try to redirect from a wrong URL to a correct URL
63
+ * keep track of renamed slugs, redirecting from old to new slugs
64
+ * translate categories and tags slugs.
65
+ */
66
+
67
+ // Codes used in the database to indicate posts, pages, categories and tags.
68
+ $qts_post_types = array(
69
+ 'QTS_POST' => 1,
70
+ 'QTS_PAGE' => 2,
71
+ 'QTS_CAT' => 3,
72
+ 'QTS_TAG' => 4,
73
+ 'QTS_TAX' => 5
74
+ );
75
+ foreach ($qts_post_types as $type => $id) define($type, $id);
76
+
77
+
78
+ /**
79
+ * Variable used to override the language
80
+ */
81
+ $qts_use_language = null;
82
+
83
+ /**
84
+ * Array of translated versions of the current url
85
+ */
86
+ $qts_self_url = array();
87
+
88
+ /**
89
+ * Function invoked during the installation of the module. It creates or updates the tables
90
+ */
91
+ function qTranslateSlug_install(){
92
+ global $wpdb;
93
+
94
+ $table_name = $wpdb->prefix.'qtranslate_slug';
95
+ require_once(ABSPATH.'wp-admin/includes/upgrade.php');
96
+
97
+ $collate = '';
98
+ if($wpdb->supports_collation()) {
99
+ if(!empty($wpdb->charset)) $collate = "DEFAULT CHARACTER SET $wpdb->charset";
100
+ if(!empty($wpdb->collate)) $collate .= " COLLATE $wpdb->collate";
101
+ }
102
+
103
+ $sql = 'CREATE TABLE IF NOT EXISTS '.$table_name.' (
104
+ qts_type TINYINT(3) UNSIGNED NOT NULL,
105
+ qts_id BIGINT(20) UNSIGNED NOT NULL,
106
+ qts_lang VARCHAR(6) NOT NULL,
107
+ qts_slug VARCHAR(200) NOT NULL,
108
+ PRIMARY KEY id_lang (qts_type, qts_id, qts_lang),
109
+ KEY post_name_lang (qts_slug, qts_type, qts_lang)
110
+ );' ;
111
+ $wpdb->query($sql);
112
+ }
113
+ register_activation_hook( __FILE__ , 'qTranslateSlug_install');
114
+
115
+
116
+
117
+
118
+ function qTranslateSlug_get_custom_taxonomy_slug($taxonomy) {
119
+ global $taxonomies_slugs, $q_config, $qts_use_language;
120
+
121
+ if(!function_exists('qTranslateSlug_term_link') || !isset($taxonomies_slugs)) return $taxonomy;
122
+
123
+ $lang = ($qts_use_language === null) ? $q_config['language'] : $qts_use_language;
124
+
125
+ if (is_array($taxonomies_slugs[$taxonomy])) {
126
+ return $taxonomies_slugs[$taxonomy][$lang];
127
+ } else {
128
+ return $taxonomy;
129
+ }
130
+ }
131
+
132
+
133
+
134
+ /**
135
+ * Function called when query parameters are processed by Wordpress.
136
+ *
137
+ * @param $q query parameters
138
+ * @return array() $q processed
139
+ */
140
+ function qTranslateSlug_filter_request($q){
141
+ global $q_config, $wpdb, $qts_use_language, $qts_self_url, $qts_reset_self_url, $taxonomies_slugs ;
142
+
143
+ if ($qts_use_language !== null){
144
+ $lang = (string) $qts_use_language;
145
+ } else {
146
+ if (!isset($q_config['language'])){
147
+ return $q;
148
+ }
149
+ $lang = (string) $q_config['language'];
150
+ }
151
+
152
+ $new_q = $q;
153
+
154
+ // SEARCH TAXONOMIES
155
+ $args=array(
156
+ 'public' => true,
157
+ '_builtin' => false
158
+ );
159
+ $output = 'object';
160
+ $operator = 'and';
161
+ $taxonomies = get_taxonomies($args, $output, $operator);
162
+
163
+ foreach ($taxonomies as $taxonomy ) {
164
+ if (isset($q[$taxonomy->name])) {
165
+ $type = QTS_TAX;
166
+ $slug = $q[$taxonomy->name];
167
+ $param = 'term_id';
168
+ $get_link = 'qTranslateSlug_term_link';
169
+ $taxonomy_ = $taxonomy->name;
170
+ //unset($new_q[$taxonomy->name]);
171
+ }
172
+ }
173
+
174
+ if (isset($type) && $type == QTS_TAX) {
175
+ // is taxonomy
176
+ } else if (isset($q['post_type']) && !is_admin() ) {
177
+ $id = qTranslateSlug_get_post_type_by_path($q['name'], $lang, $q['post_type']);
178
+ if ($id) {
179
+ $q = $new_q;
180
+ $q['page_id']=$id;
181
+ $get_link = 'get_post_permalink';
182
+ //$post_ = get_post();
183
+
184
+ //$q['name'] = qTranslateSlug_get_post_name($id);
185
+ //$q['name'] = 'pinord-do-penedes/la-zona';
186
+ /*
187
+ print_r($post_);
188
+ exit;
189
+ */
190
+ }
191
+ } else if (isset($q['name'])) {
192
+ $type = QTS_POST;
193
+ $slug = $q['name'];
194
+ $param = 'p';
195
+ $get_link = 'get_permalink';
196
+ unset($new_q['name']);
197
+ } else if (isset($q['pagename'])) {
198
+ //$type = QTS_PAGE;
199
+ //$slug = $q['pagename'];
200
+ //$param = 'page_id';
201
+ $id = qTranslateSlug_get_post_type_by_path($q['pagename'], $lang);
202
+ if ($id) {
203
+ unset($new_q['pagename']);
204
+ $q = $new_q;
205
+ $q['page_id']=$id;
206
+ $get_link = 'get_page_link';
207
+ }
208
+ } else if (isset($q['category_name'])) {
209
+ $type = QTS_CAT;
210
+ $taxonomy_ = 'category';
211
+ $slug = $q['category_name'];
212
+ $param = 'cat';
213
+ $get_link = 'get_category_link';
214
+ unset($new_q['category_name']);
215
+ } else if (isset($q['tag'])) {
216
+ $type = QTS_TAG;
217
+ $taxonomy_ = 'post_tag';
218
+ $slug = $q['tag'];
219
+ $param = 'tag_id';
220
+ $get_link = 'get_tag_link';
221
+ unset($new_q['tag']);
222
+ }
223
+
224
+ if (isset($type)){
225
+ $slugs = explode('/',$slug);
226
+ $slug = array_pop($slugs);
227
+
228
+ $query = 'SELECT qts_id FROM '.$wpdb->prefix.'qtranslate_slug WHERE \''.$wpdb->escape($slug).'\' = qts_slug AND '.$type.' = qts_type AND \''.$wpdb->escape($lang).'\' = qts_lang';
229
+ @ $id = $wpdb->get_var($query);
230
+
231
+ if ($id) {
232
+ $q = $new_q;
233
+
234
+ if (($type == QTS_TAX || $type == QTS_CAT || $type == QTS_TAG )) {
235
+ $term_ = &get_term($id, $taxonomy_);
236
+
237
+ switch ($type) {
238
+ case QTS_CAT:
239
+ $q['category_name'] = $term_->slug;
240
+ break;
241
+ case QTS_TAG:
242
+ $q['tag'] = $term_->slug;
243
+ break;
244
+ default:
245
+ $q[$taxonomy_] = $term_->slug;
246
+ break;
247
+ }
248
+ } else {
249
+ $q[$param]=$id;
250
+ }
251
+ }
252
+ }
253
+
254
+ if (isset($get_link) && $id && (!$qts_self_url)){
255
+ $old_qts_use_language = $qts_use_language;
256
+ foreach($q_config['enabled_languages'] as $l) {
257
+ global $qts_self_url;
258
+ $qts_use_language = $l;
259
+ if (isset($type) && $type == QTS_TAX) {
260
+ $link = rtrim(get_option('home'),"/"). '/'. qTranslateSlug_get_custom_taxonomy_slug($term_->taxonomy) . "/" . $term_->slug . "/";
261
+ $qts_self_url[$l] = call_user_func($get_link, $link, $term_, $taxonomy_) . qTranslateSlug_putGetVars();
262
+ } else if ( isset($type) && ( $type == QTS_TAX || $type == QTS_TAG ) ) {
263
+ $qts_self_url[$l] = call_user_func($get_link, $term_) . qTranslateSlug_putGetVars();
264
+ } else {
265
+ $qts_self_url[$l] = call_user_func($get_link, $id) . qTranslateSlug_putGetVars();
266
+ }
267
+ }
268
+ $qts_use_language = $old_qts_use_language;
269
+ }
270
+
271
+ return $q;
272
+ }
273
+ add_filter('request','qTranslateSlug_filter_request');
274
+
275
+
276
+
277
+ /**
278
+ * Returns the link to the current page in the desired language
279
+ *
280
+ * @param $lang the code of the desired language
281
+ * @return the link for the current page in that language
282
+ */
283
+ function qTranslateSlug_getSelfUrl($lang){
284
+ //global $qts_self_url, $wp_query, $wpdb;
285
+ global $q_config, $qts_self_url;
286
+
287
+ if (!isset($qts_self_url[$lang])) {
288
+ $qts_self_url[$lang] = qTranslateSlug_convertURL(esc_url($q_config['url_info']['url']), $lang) . qTranslateSlug_putGetVars();
289
+ }
290
+
291
+ return $qts_self_url[$lang];
292
+ }
293
+
294
+
295
+
296
+
297
+ /**
298
+ * Converts an url in another language.
299
+ *
300
+ * This corresponds to qtrans_convertURL, but for now it doesn't check if the url contains translatable slugs.
301
+ * If you need a translation of the current page, you should use qTranslateSlug_getSelfUrl($lang).
302
+ * If you need the translation for a specific page or post id, you should use get_page_link($id) or get_permalink($id).
303
+ *
304
+ * I extracted qTranslateSlug_urlAddLang() from this function if you just need to add language and home path to a clean relative url.
305
+ *
306
+ * @param $url
307
+ * @param $lang
308
+ * @param $forceadmin
309
+ * @return unknown_type
310
+ */
311
+ function qTranslateSlug_convertURL($url='', $lang='', $forceadmin = false) {
312
+ if ( defined('WP_ADMIN') && !$forceadmin) return $url;
313
+ global $q_config;
314
+
315
+ if($lang=='') $lang = $q_config['language'];
316
+ //if($url=='') $url = qTranslateSlug_getSelfUrl($lang);
317
+ if($url=='') $url = esc_url($q_config['url_info']['url']);
318
+ if(!qtrans_isEnabled($lang)) return "";
319
+
320
+ // & workaround
321
+ $url = str_replace('&amp;','&',$url);
322
+ $url = str_replace('&#038;','&',$url);
323
+
324
+ // check if it's an external link
325
+ $urlinfo = qtrans_parseURL($url);
326
+ $home = rtrim(get_option('home'),"/");
327
+ if($urlinfo['host']!='') {
328
+ // check for already existing pre-domain language information
329
+ if($q_config['url_mode'] == QT_URL_DOMAIN && preg_match("#^([a-z]{2}).#i",$urlinfo['host'],$match)) {
330
+ if(qtrans_isEnabled($match[1])) {
331
+ // found language information, remove it
332
+ $url = preg_replace("/".$match[1]."\./i","",$url, 1);
333
+ // reparse url
334
+ $urlinfo = qtrans_parseURL($url);
335
+ }
336
+ }
337
+
338
+ if($q_config['url_mode'] == QT_URL_TLD && preg_match("/\.([a-z]{2,3})$/i",$urlinfo['host'],$match)) {
339
+
340
+ //if(qtrans_isEnabled($match[1])) {
341
+
342
+ $tld = (!empty($q_config['language_tldcode'][$lang])) ? $q_config['language_tldcode'][$lang] : $lang;
343
+
344
+ // found language information, replace it
345
+ $url = preg_replace("/\.".$match[1]."/i",'.'.$tld, $url, 1);
346
+
347
+ // reparse url
348
+ $urlinfo = qtrans_parseURL($url);
349
+
350
+ //}
351
+ }
352
+
353
+
354
+ if(substr($url,0,strlen($home))!=$home) {
355
+ return $url;
356
+ }
357
+ // strip home path
358
+ $url = substr($url,strlen($home));
359
+ } else {
360
+ // relative url, strip home path
361
+ $homeinfo = qtrans_parseURL($home);
362
+ if($homeinfo['path']==substr($url,0,strlen($homeinfo['path']))) {
363
+ $url = substr($url,strlen($homeinfo['path']));
364
+ }
365
+ }
366
+
367
+ // check for query language information and remove if found
368
+ if(preg_match("#(&|\?)lang=([^&\#]+)#i",$url,$match) && qtrans_isEnabled($match[2])) {
369
+ $url = preg_replace("#(&|\?)lang=".$match[2]."&?#i","$1",$url);
370
+ }
371
+
372
+ // remove any slashes out front
373
+ $url = ltrim($url,"/");
374
+
375
+ // remove any useless trailing characters
376
+ $url = rtrim($url,"?&");
377
+
378
+ // reparse url without home path
379
+ $urlinfo = qtrans_parseURL($url);
380
+
381
+ // check if its a link to an ignored file type
382
+ $ignore_file_types = preg_split('/\s*,\s*'.'/', strtolower($q_config['ignore_file_types']));
383
+ $pathinfo = pathinfo($urlinfo['path']);
384
+ if(isset($pathinfo['extension']) && in_array(strtolower($pathinfo['extension']), $ignore_file_types)) {
385
+ return $home."/".$url;
386
+ }
387
+
388
+ return qTranslateSlug_urlAddLang($url, $lang, $urlinfo, $home);
389
+ }
390
+
391
+
392
+
393
+ /**
394
+ * Adds home path and language to an already cleaned URL.
395
+ * It doesn't reparse the url, and supposes $url is a clean relative url.
396
+ * You may pass $urlinfo and $home if you have already them.
397
+ *
398
+ * @param $url the relative url
399
+ * @param $lang the desired language
400
+ * @param $urlinfo result of qtrans_parseURL($url)
401
+ * @param $home result of rtrim(get_option('home'),"/")
402
+ * @return the absolute url with language informations
403
+ */
404
+ function qTranslateSlug_urlAddLang($url, $lang='', $urlinfo='', $home='') {
405
+ global $q_config;
406
+
407
+ if($lang=='') $lang = $q_config['language'];
408
+ if($urlinfo=='') $urlinfo = qtrans_parseURL($url);
409
+ if($home=='') $home = rtrim(get_option('home'),"/");
410
+
411
+
412
+ switch($q_config['url_mode']) {
413
+ case QT_URL_PATH: // pre url
414
+ // might already have language information
415
+ if(preg_match("#^([a-z]{2})/#i",$url,$match)) {
416
+ if(qtrans_isEnabled($match[1])) {
417
+ // found language information, remove it
418
+ $url = substr($url, 3);
419
+ }
420
+ }
421
+ //if(!$q_config['hide_default_language']||$lang!=$q_config['default_language']) $url = $lang."/".$url;
422
+ //Check for older version of qtranslate without hide_default_language option
423
+ if ( ($lang!=$q_config['default_language']) || (isset($q_config['hide_default_language']) && (!$q_config['hide_default_language']))) $url = $lang."/".$url;
424
+ break;
425
+ case QT_URL_DOMAIN: // pre domain
426
+ //if(!$q_config['hide_default_language']||$lang!=$q_config['default_language']) $home = preg_replace("#//#","//".$lang.".",$home,1);
427
+ //Check for older version of qtranslate without hide_default_language option
428
+ if ( ($lang!=$q_config['default_language']) || (isset($q_config['hide_default_language']) && (!$q_config['hide_default_language']))) $home = preg_replace("#//#","//".$lang.".",$home,1);
429
+ break;
430
+ case QT_URL_TLD: // tld domain
431
+
432
+ if($lang !=$q_config['default_language']) {
433
+
434
+ $tld = (!empty($q_config['language_tldcode'][$lang])) ? $q_config['language_tldcode'][$lang] : $lang;
435
+
436
+ //$home = preg_replace("#^(http://[^/.]+)(\.?)([a-z]{0,3})#", '$1.'.$tld, $home, 1);
437
+
438
+ if (preg_match("/\.([a-z]{2,3})$/i",$home,$match)) {
439
+ $home = str_replace($match[0], ".".$tld, $home);
440
+ }
441
+ }
442
+ break;
443
+
444
+
445
+ default: // query
446
+ //Check for older version of qtranslate without hide_default_language option
447
+ if ( ($lang!=$q_config['default_language']) || (isset($q_config['hide_default_language']) && (!$q_config['hide_default_language']))) {
448
+ if(strpos($url,'?')===false) {
449
+ $url .= '?';
450
+ } else {
451
+ $url .= '&';
452
+ }
453
+ $url .= "lang=".$lang;
454
+ }
455
+ }
456
+
457
+ // see if cookies are activated
458
+ if(!$q_config['cookie_enabled'] && !$q_config['url_info']['internal_referer'] && $urlinfo['path'] == '' && $lang == $q_config['default_language'] && $q_config['language'] != $q_config['default_language'] && (isset($q_config['hide_default_language'])?$q_config['hide_default_language']:true)) {
459
+ // :( now we have to make unpretty URLs
460
+ $url = preg_replace("#(&|\?)lang=".$match[2]."&?#i","$1",$url);
461
+ if(strpos($url,'?')===false) {
462
+ $url .= '?';
463
+ } else {
464
+ $url .= '&';
465
+ }
466
+ $url .= "lang=".$lang;
467
+ }
468
+
469
+ // &amp; workaround
470
+ $complete = str_replace('&','&amp;',$home."/".$url);
471
+
472
+ return $complete;
473
+ }
474
+
475
+
476
+
477
+ /**
478
+ * Hide auttomatically the wordpress slug blog in edit posts page
479
+ */
480
+ function qTranslateSlug_remove_slug_box() {
481
+
482
+ function qTranslateSlug_remove_slug_block() {
483
+ echo '<!-- QTS remove slug box -->
484
+ <style type="text/css" media="screen">
485
+ #edit-slug-box { display: none !important}
486
+ </style>';
487
+ }
488
+
489
+ if (is_admin())
490
+ add_action('admin_head', 'qTranslateSlug_remove_slug_block', 900);
491
+
492
+ }
493
+
494
+
495
+
496
+ /**
497
+ * Activates filters defined by this module
498
+ */
499
+ add_filter('post_type_link', 'qTranslateSlug_post_type_link', 0, 4);
500
+ add_filter('page_link', 'qTranslateSlug_page_link', 0, 2);
501
+ add_filter('post_link', 'qTranslateSlug_post_link', 0, 3);
502
+ add_filter('term_link', 'qTranslateSlug_term_link', 600 , 3);
503
+ add_action('admin_menu', 'qTranslateSlug_remove_slug_box');
504
+
505
+
506
+ /**
507
+ * Disables qtranslate filter for the link managed by this module
508
+ */
509
+ function qTranslateSlug_remove_qtrans_filters() {
510
+ remove_filter('page_link', 'qtrans_convertURL');
511
+ remove_filter('post_link', 'qtrans_convertURL');
512
+ remove_filter('category_link', 'qtrans_convertURL');
513
+ remove_filter('tag_link', 'qtrans_convertURL');
514
+ }
515
+ add_action('plugins_loaded','qTranslateSlug_remove_qtrans_filters');
516
+
517
+ //TODO: Links that may have to be checked
518
+ //add_filter('category_feed_link', 'qtrans_convertURL');
519
+ //add_filter('the_permalink', 'qtrans_convertURL');
520
+ //add_filter('feed_link', 'qtrans_convertURL');
521
+ //add_filter('post_comments_feed_link', 'qtrans_convertURL');
522
+ //add_filter('tag_feed_link', 'qtrans_convertURL');
523
+ //add_filter('esc_url', 'qtrans_convertURL');
524
+
525
+
526
+
527
+ /**
528
+ * Filter that translates the slug parts in a page link
529
+ *
530
+ * @param $link the link for the page generated by Wordpress
531
+ * @param $id the id of the page
532
+ * @return the link translated
533
+ */
534
+ function qTranslateSlug_page_link($link, $id){
535
+ //$fb = function_exists('fb');
536
+ //$fb && fb($link,'start $link') && fb($id, 'start $id');
537
+ global $wpdb,$q_config, $qts_use_language;
538
+ $lang = ($qts_use_language === null)?$q_config['language']:$qts_use_language;
539
+
540
+ $home = rtrim(get_option('home'),"/");
541
+ $link = substr($link,strlen($home));
542
+ $link = ltrim($link,"/");
543
+ $link = rtrim($link,"?&");
544
+
545
+ $urlinfo = qtrans_parseURL($link);
546
+
547
+ if ($urlinfo['query']){
548
+ return qTranslateSlug_urlAddLang($link, $lang, $urlinfo, $home);
549
+ }
550
+
551
+ $linkParts = explode('/', $link);
552
+ $i = count($linkParts) - 1;
553
+
554
+ if ($linkParts[$i] === ''){
555
+ $i--;
556
+ }
557
+
558
+ do {
559
+ $id = (int)$id;
560
+ $query = 'SELECT post_parent, qts_slug
561
+ FROM '.$wpdb->posts.' p
562
+ LEFT JOIN ( SELECT *
563
+ FROM '.$wpdb->prefix.'qtranslate_slug
564
+ WHERE '.QTS_PAGE.' = qts_type
565
+ AND '.$id.' = qts_id
566
+ AND \''.$wpdb->escape($lang).'\' = qts_lang
567
+ ) qts ON p.ID = qts.qts_id
568
+ WHERE '.$id.' = ID';
569
+ @ $res = $wpdb->get_row($query,ARRAY_A);
570
+ //$fb && fb($query, '$query') && fb($res, '$res');
571
+
572
+ if (!$res) {
573
+ break;
574
+ }
575
+
576
+ $parent = $res['post_parent'];
577
+ $slug = $res['qts_slug'];
578
+
579
+ if ($slug) {
580
+ $linkParts[$i] = $slug;
581
+ }
582
+
583
+ if ((!$parent) || ($parent == $id)){
584
+ break;
585
+ }
586
+
587
+ $id = $parent;
588
+ $i--;
589
+ } while ($i >= 0);
590
+
591
+ $link = implode('/',$linkParts);
592
+
593
+ $ret = qTranslateSlug_urlAddLang($link, $lang, $urlinfo, $home);
594
+ //$fb && fb($link, 'end $link') && fb($id, 'end $id') && fb($ret,'return');
595
+
596
+ return $ret;
597
+ }
598
+
599
+
600
+
601
+ /**
602
+ * Filter that translates the slug parts in a page link
603
+ *
604
+ * @param $link the link for the page generated by Wordpress
605
+ * @param $id the id of the page
606
+ * @return the link translated
607
+ */
608
+
609
+ //$post_link, $post, $leavename, $sample
610
+ function qTranslateSlug_post_type_link($link, $post, $leavename, $sample){
611
+ global $wpdb,$q_config, $qts_use_language, $custom_post_types_slugs;
612
+
613
+ $id = $post->ID;
614
+ $lang = ($qts_use_language === null) ? $q_config['language'] : $qts_use_language;
615
+
616
+ $home = rtrim(get_option('home'),"/");
617
+ $link = substr($link,strlen($home));
618
+ $link = ltrim($link,"/");
619
+ $link = rtrim($link,"?&");
620
+ $urlinfo = qtrans_parseURL($link);
621
+
622
+ if ($urlinfo['query']){
623
+ return qTranslateSlug_urlAddLang($link, $lang, $urlinfo, $home);
624
+ }
625
+
626
+ $linkParts = explode('/', $link);
627
+
628
+ if ($custom_post_types_slugs !== null && !is_admin()) {
629
+
630
+ foreach ($linkParts as $key => $part) {
631
+ if (in_array($part, $custom_post_types_slugs[$post->post_type])) {
632
+ $linkParts[$key] = $custom_post_types_slugs[$post->post_type][$lang];
633
+ }
634
+ }
635
+ }
636
+
637
+ $i = count($linkParts) - 1;
638
+
639
+ if ($linkParts[$i] === '') {
640
+ $i--;
641
+ }
642
+
643
+ do {
644
+ $id = (int)$id;
645
+ $query = 'SELECT post_parent, qts_slug
646
+ FROM '.$wpdb->posts.' p
647
+ LEFT JOIN ( SELECT *
648
+ FROM '.$wpdb->prefix.'qtranslate_slug
649
+ WHERE '.QTS_PAGE.' = qts_type
650
+ AND '.$id.' = qts_id
651
+ AND \''.$wpdb->escape($lang).'\' = qts_lang
652
+ ) qts ON p.ID = qts.qts_id
653
+ WHERE '.$id.' = ID';
654
+ @ $res = $wpdb->get_row($query,ARRAY_A);
655
+ //$fb && fb($query, '$query') && fb($res, '$res');
656
+
657
+ if (!$res) {
658
+ break;
659
+ }
660
+
661
+ $parent = $res['post_parent'];
662
+ $slug = $res['qts_slug'];
663
+
664
+ if ($slug) {
665
+ $linkParts[$i] = $slug;
666
+ }
667
+
668
+ if ((!$parent) || ($parent == $id)){
669
+ break;
670
+ }
671
+
672
+ $id = $parent;
673
+ $i--;
674
+ } while ($i >= 0);
675
+
676
+ $link = implode('/',$linkParts);
677
+
678
+ $ret = qTranslateSlug_urlAddLang($link, $lang, $urlinfo, $home);
679
+ //$fb && fb($link, 'end $link') && fb($id, 'end $id') && fb($ret,'return');
680
+
681
+ return $ret;
682
+ }
683
+
684
+
685
+
686
+ /**
687
+ * Filter that translates the slug in a post link
688
+ *
689
+ * @param $link the link generated by wordpress
690
+ * @param $post the post data
691
+ * @param $leavename parameter used by get_permalink. Whether to keep post name or page name.
692
+ * @return the link translated
693
+ */
694
+ function qTranslateSlug_post_link($link, $post, $leavename){
695
+ global $wpdb, $q_config, $qts_use_language;
696
+
697
+ //$fb = function_exists('fb');
698
+ //$fb && fb($link, 'initial $link') && fb($post, '$post');
699
+
700
+ $lang = ($qts_use_language === null)?$q_config['language']:$qts_use_language;
701
+
702
+ $permalink = get_option('permalink_structure');
703
+
704
+ $home = rtrim(get_option('home'),"/");
705
+
706
+ $trans_slug = null;
707
+
708
+ if ((!empty($post->ID)) && ('' != $permalink) && !in_array($post->post_status, array('draft', 'pending')) ) {
709
+ $query = $wpdb->prepare(
710
+ " SELECT qts_slug
711
+ FROM {$wpdb->prefix}qtranslate_slug
712
+ WHERE %d = qts_type
713
+ AND %d = qts_id
714
+ AND %s = qts_lang",
715
+ QTS_POST, $post->ID, $lang);
716
+ @$trans_slug = $wpdb->get_var($query);
717
+ //$fb && fb($query,'$query') && fb($trans_slug,'$trans_slug');
718
+ }
719
+
720
+ if ($trans_slug !== null) {
721
+ $unixtime = strtotime($post->post_date);
722
+
723
+ $rewritecode = array(
724
+ '%year%',
725
+ '%monthnum%',
726
+ '%day%',
727
+ '%hour%',
728
+ '%minute%',
729
+ '%second%',
730
+ $leavename? '' : '%postname%',
731
+ '%post_id%',
732
+ '%category%',
733
+ '%author%',
734
+ $leavename? '' : '%pagename%',
735
+ );
736
+
737
+ //TODO: translate category slug
738
+ $category = '';
739
+ if ( strpos($permalink, '%category%') !== false ) {
740
+ $cats = get_the_category($post->ID);
741
+ if ( $cats ) {
742
+ usort($cats, '_usort_terms_by_ID'); // order by ID
743
+ $category = $cats[0]->slug;
744
+ if ( $parent = $cats[0]->parent )
745
+ $category = get_category_parents($parent, false, '/', true) . $category;
746
+ }
747
+ // show default category in permalinks, without
748
+ // having to assign it explicitly
749
+ if ( empty($category) ) {
750
+ $default_category = get_category( get_option( 'default_category' ) );
751
+ $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
752
+ }
753
+ }
754
+
755
+ $author = '';
756
+ if ( strpos($permalink, '%author%') !== false ) {
757
+ $authordata = get_userdata($post->post_author);
758
+ $author = $authordata->user_nicename;
759
+ }
760
+
761
+ $date = explode(" ",date('Y m d H i s', $unixtime));
762
+ $rewritereplace = array(
763
+ $date[0],
764
+ $date[1],
765
+ $date[2],
766
+ $date[3],
767
+ $date[4],
768
+ $date[5],
769
+ $trans_slug,
770
+ $post->ID,
771
+ $category,
772
+ $author,
773
+ $trans_slug,
774
+ );
775
+ $link = user_trailingslashit(str_replace($rewritecode, $rewritereplace, $permalink), 'single');
776
+ } else {
777
+ $link = substr($link, strlen($home));
778
+ }
779
+ $link = ltrim($link, '/');
780
+ $link = qTranslateSlug_urlAddLang($link, $lang,'', $home);
781
+ //$fb && fb($link,'new $link');
782
+ return $link;
783
+ }
784
+
785
+ function getParentTermsSlugTranslation($link, $term, $lang, $taxonomy) {
786
+ global $wpdb;
787
+
788
+ $cat_id = $term->term_id;
789
+ $mycategory = $term;
790
+
791
+ $term_type = qTranslateSlug_get_term_type($taxonomy);
792
+
793
+ $category_parent_id = $mycategory->parent;
794
+ $category_parent_id = (int)$category_parent_id;
795
+ if($category_parent_id != 0) {
796
+ $parentCategory = &get_term($category_parent_id, $taxonomy);
797
+ $parentCategorySlug = $parentCategory->slug;
798
+ if ($parentCategorySlug != '') {
799
+ $query = $wpdb->prepare(
800
+ "SELECT qts_slug
801
+ FROM {$wpdb->prefix}qtranslate_slug
802
+ WHERE %d = qts_type
803
+ AND %d = qts_id
804
+ AND %s = qts_lang",
805
+ $term_type, $category_parent_id, $lang);
806
+ @$trans_slug = $wpdb->get_var($query);
807
+ if ($trans_slug !== null) {
808
+ $link = str_replace($parentCategorySlug, $trans_slug, $link);
809
+ }
810
+
811
+ $parentParentCatId = (int)$parentCategory->parent;
812
+ if($parentParentCatId != 0) {
813
+ return getParentTermsSlugTranslation($link, $parentCategory, $lang, $taxonomy);
814
+ }
815
+ else {
816
+ return $link;
817
+ }
818
+ }
819
+ else {
820
+ return $link;
821
+ }
822
+ }
823
+ else {
824
+ return $link;
825
+ }
826
+ }
827
+
828
+ function qTranslateSlug_term_link($link, $term, $taxonomy) {
829
+
830
+ global $wpdb, $q_config, $qts_use_language, $wp_query, $taxonomies_slugs;
831
+
832
+ $id = $term->term_id;
833
+
834
+ $lang = ($qts_use_language === null)?$q_config['language']:$qts_use_language;
835
+
836
+ $home = rtrim(get_option('home'),"/");
837
+
838
+ $term_type = qTranslateSlug_get_term_type($taxonomy);
839
+
840
+ //Marco INIZIO
841
+ $trans_slug = null;
842
+ $permalink = get_option('permalink_structure');
843
+
844
+ $category_id = '';
845
+ $category_slug = '';
846
+
847
+ $mycategory = $term;
848
+ $category_slug = $term->slug;
849
+ $category_id = $id;
850
+
851
+ if (($category_id != '') && ('' != $permalink) && ($category_slug != '')) {
852
+ $query = $wpdb->prepare(
853
+ " SELECT qts_slug
854
+ FROM {$wpdb->prefix}qtranslate_slug
855
+ WHERE %d = qts_type
856
+ AND %d = qts_id
857
+ AND %s = qts_lang",
858
+ $term_type, $category_id, $lang);
859
+ @$trans_slug = $wpdb->get_var($query);
860
+ }
861
+
862
+ $link = ltrim(substr($link, strlen($home)), '/');
863
+
864
+ if ($trans_slug !== null) {
865
+ $link = str_replace($category_slug, $trans_slug, $link);
866
+ }
867
+
868
+ $link = getParentTermsSlugTranslation($link, $term, $lang, $taxonomy);
869
+
870
+ //print_r(qTranslateSlug_urlAddLang($link, $lang, '', $home));
871
+
872
+ return qTranslateSlug_urlAddLang($link, $lang, '', $home);
873
+ }
874
+
875
+ function qTranslateSlug_putGetVars() {
876
+ //global $current_user;
877
+ $vars_ = array();
878
+
879
+ if (isset($_GET) && count($_GET) > 0) {
880
+
881
+ foreach ($_GET as $name => $value) {
882
+ if (is_array($value)) {
883
+ foreach ($value as $value_) {
884
+ $vars_[] = $name. '%5B%5D=' . $value_;
885
+ }
886
+ } else {
887
+ if ($name != 'q' && $name != 's') {
888
+ $vars_[] = $name . '=' . $value;
889
+ }
890
+ }
891
+ }
892
+ }
893
+
894
+ $vars_ = apply_filters('qtranslateslug_putgetvars', $vars_);
895
+
896
+ if (!empty($vars_)) {
897
+ $ret = (isset($_GET['s'])) ? '&amp;' : '?';
898
+ return $ret . implode('&amp;', $vars_);
899
+ } else {
900
+ return '';
901
+ }
902
+ }
903
+
904
+ /**
905
+ * Returns the id of the page with the specified path.
906
+ *
907
+ * @param $page_path the path
908
+ * @param $lang optional, the desired language
909
+ * @return id of the page
910
+ */
911
+ function qTranslateSlug_get_post_type_by_path($page_path, $lang = '', $post_type = 'page') {
912
+ global $wpdb, $q_config, $qts_use_language, $custom_post_types_slugs;
913
+ //$fb = function_exists('fb');
914
+ if ($lang == ''){
915
+ $lang = ($qts_use_language === null)?$q_config['language']:$qts_use_language;
916
+ }
917
+ $page_path = rawurlencode(urldecode($page_path));
918
+ $page_path = str_replace('%2F', '/', $page_path);
919
+ $page_path = str_replace('%20', ' ', $page_path);
920
+ $page_paths = '/' . trim($page_path, '/');
921
+ $page_paths = explode('/', $page_paths);
922
+ $spage_paths = array();
923
+ $full_path = '';
924
+ foreach( (array) $page_paths as $pathdir){
925
+ $pathdir = sanitize_title($pathdir);
926
+ if ($pathdir !== ''){
927
+ $spage_paths[] = $pathdir;
928
+ $full_path .= '/'.$pathdir;
929
+ }
930
+ }
931
+ $leaf_path = array_pop($spage_paths);
932
+
933
+ /* This makes invalid the default slug, if it was defined a slug in the desired language.
934
+ * However, without other modifications, the default slug is found anyway by wordpress, so we can find it here.
935
+ $query = $wpdb->prepare(
936
+ " (SELECT ID, qts_slug AS post_name, post_parent
937
+ FROM {$wpdb->posts}, {$wpdb->prefix}qtranslate_slug
938
+ WHERE %s = qts_slug
939
+ AND %d = qts_type
940
+ AND %s = qts_lang
941
+ AND qts_id = ID)
942
+ UNION (SELECT ID, post_name, post_parent
943
+ FROM $wpdb->posts
944
+ WHERE post_name = %s
945
+ AND (post_type = 'page' OR post_type = 'attachment')
946
+ AND (SELECT qts_id
947
+ FROM {$wpdb->prefix}qtranslate_slug
948
+ WHERE %d = qts_type
949
+ AND ID = qts_id
950
+ AND %s = qts_lang
951
+ LIMIT 1) IS NULL)",
952
+ $leaf_path, QTS_PAGE, $lang, $leaf_path, QTS_PAGE, $lang ); */
953
+
954
+ $query = $wpdb->prepare(
955
+ " (SELECT ID, qts_slug AS post_name, post_parent
956
+ FROM {$wpdb->posts}, {$wpdb->prefix}qtranslate_slug
957
+ WHERE %s = qts_slug
958
+ AND %d = qts_type
959
+ AND %s = qts_lang
960
+ AND qts_id = ID)
961
+ UNION (SELECT ID, post_name, post_parent
962
+ FROM $wpdb->posts
963
+ WHERE post_name = %s AND (post_type = '$post_type' OR post_type = 'attachment'))",
964
+ $leaf_path, QTS_PAGE, $lang, $leaf_path );
965
+ $pages = $wpdb->get_results($query);
966
+
967
+ //$fb && fb($query, 'pages query') && fb($pages, '$pages');
968
+
969
+ foreach ($pages as $page) {
970
+ $path = '/' . $leaf_path;
971
+ $level = count($spage_paths);
972
+ $curpage = $page;
973
+ while ($curpage->post_parent != 0) {
974
+ $level--;
975
+ if ($level < 0) continue 2;
976
+ $curpage = $wpdb->get_row( $wpdb->prepare(
977
+ "SELECT ID, post_name, post_parent, qts_slug
978
+ FROM $wpdb->posts p
979
+ LEFT JOIN ( SELECT *
980
+ FROM {$wpdb->prefix}qtranslate_slug
981
+ WHERE %d = qts_type
982
+ AND %d = qts_id
983
+ AND %s = qts_lang
984
+ ) qts on p.ID = qts.qts_id
985
+ WHERE ID = %d
986
+ and post_type='$post_type'",
987
+ QTS_PAGE, $curpage->post_parent, $lang, $curpage->post_parent ));
988
+ /* This makes invalid the default slug, if it was defined a slug in the desired language.
989
+ * However, without other modifications, the default slug is found anyway by wordpress, so we can find it here.
990
+ if ($curpage->qts_slug){
991
+ if ($curpage->qts_slug === $spage_paths[$level]){
992
+ $path = '/' . $curpage->qts_slug . $path;
993
+ } else {
994
+ continue 2;
995
+ }
996
+ }*/
997
+
998
+
999
+ if ( isset($curpage) && $curpage->qts_slug === $spage_paths[$level]){
1000
+ $path = '/' . $curpage->qts_slug . $path;
1001
+ } else if (isset($curpage) && $curpage->post_name === $spage_paths[$level]) {
1002
+ $path = '/' . $curpage->post_name . $path;
1003
+ } else {
1004
+ continue 2;
1005
+ }
1006
+ }
1007
+
1008
+ if ( $path === $full_path ) {
1009
+ //$fb && fb($level, 'final $level') && fb ($page->ID, '$page->ID');
1010
+ return $page->ID;
1011
+ }
1012
+ }
1013
+
1014
+ return null;
1015
+ }
1016
+
1017
+ /**
1018
+ * Actions used to insert and edit the slug translations
1019
+ */
1020
+ // post / pages
1021
+ add_action('admin_menu', 'qTranslateSlug_add_custom_box');
1022
+ add_action('save_post', 'qTranslateSlug_save_postdata', 605, 2);
1023
+
1024
+ // Categories / Tags /Taxonomies
1025
+ add_action ('edit_category_form_fields', 'qTranslateSlug_tag_fields');
1026
+ add_action ('edit_tag_form_fields', 'qTranslateSlug_tag_fields');
1027
+ add_action ('edited_term', 'save_qTranslateSlug_term_fields', 605, 3);
1028
+
1029
+ function qTranslateSlug_add_custom_box() {
1030
+ if ( function_exists( 'add_meta_box' ) ) {
1031
+ add_meta_box( 'qts_sectionid', 'Slug', 'qTranslateSlug_custom_box', 'post', 'side', 'high');
1032
+ add_meta_box( 'qts_sectionid', 'Slug', 'qTranslateSlug_custom_box', 'page', 'side', 'high' );
1033
+ foreach ( get_post_types( array('_builtin' => false ) ) as $ptype ) {
1034
+ add_meta_box( 'qts_sectionid', 'Slug', 'qTranslateSlug_custom_box', $ptype, 'side', 'high' );
1035
+ }
1036
+ }
1037
+ }
1038
+
1039
+ /**
1040
+ * Shows the fields where insert the translated slugs in the post and page edit form.
1041
+ */
1042
+ function qTranslateSlug_custom_box() {
1043
+ global $post, $wpdb, $q_config;
1044
+
1045
+ $post_types = get_post_types(array('_builtin' => false ));
1046
+
1047
+ if ( 'page' == $post->post_type || in_array($post->post_type, $post_types) ) {
1048
+ $post_type = QTS_PAGE;
1049
+ } else if ($post->post_type == 'post') {
1050
+ $post_type = QTS_POST;
1051
+ } else {
1052
+ return;
1053
+ }
1054
+
1055
+ $query = $wpdb->prepare(
1056
+ "SELECT qts_lang, qts_slug
1057
+ FROM {$wpdb->prefix}qtranslate_slug
1058
+ WHERE %d = qts_type
1059
+ AND %d = qts_id"
1060
+ , $post_type, $post->ID);
1061
+ $results = $wpdb->get_results($query);
1062
+ $slugs = array();
1063
+ foreach ($results as $res) {
1064
+ $slugs[$res->qts_lang] = $res->qts_slug;
1065
+ }
1066
+
1067
+ // Use nonce for verification
1068
+ echo '<table style="width:100%">';
1069
+ echo '<input type="hidden" name="qts_nonce" id="qts_nonce" value="' . wp_create_nonce( 'qts_nonce' ) . '" />';
1070
+
1071
+ foreach($q_config['enabled_languages'] as $lang) {
1072
+ echo '<tr>';
1073
+ echo "<th style=\"text-align:left; width:10%; color:#555\"><label for='qts_{$lang}_slug'>".__($q_config['language_name'][$lang], 'qtranslate')."</label></th>";
1074
+ $value = isset($slugs[$lang])?htmlspecialchars($slugs[$lang],ENT_QUOTES):'';
1075
+ echo "<td><input type='text' id='qts_{$lang}_slug' name='qts_{$lang}_slug' value='$value' style='width:90%; margin-left:10%; color:#777' /></td>\n";
1076
+ echo '</tr>';
1077
+ }
1078
+ echo '</table>';
1079
+ }
1080
+
1081
+
1082
+ function qTranslateSlug_get_term_type($str) {
1083
+ $tag_type = QTS_TAX;
1084
+ if ($str == 'category') {
1085
+ $tag_type = QTS_CAT;
1086
+ } elseif ($str == 'post_tag') {
1087
+ $tag_type = QTS_TAG;
1088
+ }
1089
+ return $tag_type;
1090
+ }
1091
+
1092
+
1093
+ function qTranslateSlug_tag_fields( $tag ) { //check for existing featured ID
1094
+ $t_id = $tag->term_id;
1095
+ //$cat_meta = get_option( "category_$t_id");
1096
+
1097
+ if ($_GET['taxonomy'] == 'newspaper') return $t_id;
1098
+
1099
+ $tag_type = qTranslateSlug_get_term_type($tag->taxonomy);
1100
+
1101
+ global $post, $wpdb, $q_config;
1102
+ $query = $wpdb->prepare(
1103
+ "SELECT qts_lang, qts_slug
1104
+ FROM {$wpdb->prefix}qtranslate_slug
1105
+ WHERE %d = qts_type
1106
+ AND %d = qts_id"
1107
+ , $tag_type, $t_id);
1108
+ $results = $wpdb->get_results($query);
1109
+ $slugs = array();
1110
+ foreach ($results as $res) {
1111
+ $slugs[$res->qts_lang] = $res->qts_slug;
1112
+ }
1113
+
1114
+ echo '<table class="form-table">';
1115
+ echo '<input type="hidden" name="qts_nonce" id="qts_nonce" value="' . wp_create_nonce( 'qts_nonce' ) . '" />';
1116
+
1117
+ foreach($q_config['enabled_languages'] as $lang) {
1118
+ echo "<tr class='form-field form-required'><th scope='row' valig='top'><label for='qts_{$lang}_slug'>Slug (".__($q_config['language_name'][$lang], 'qtranslate').")</label></th>";
1119
+ $value = isset($slugs[$lang])?htmlspecialchars($slugs[$lang],ENT_QUOTES):'';
1120
+ echo "<td><input type='text' name='qts_{$lang}_slug' value='$value' /></td></tr>\n";
1121
+ }
1122
+ echo '</table>';
1123
+ }
1124
+
1125
+
1126
+ function save_qTranslateSlug_term_fields( $term_id, $tt_id, $taxonomy ) {
1127
+ global $wpdb, $q_config, $post_type_object;
1128
+
1129
+
1130
+ if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
1131
+ || (!isset($_POST['tag_ID']) || $term_id != $_POST['tag_ID'])
1132
+ || (!current_user_can('edit_posts'))) {
1133
+ return $term_id;
1134
+ }
1135
+
1136
+ if ( !wp_verify_nonce( $_POST['qts_nonce'], 'qts_nonce' )) {
1137
+ return;
1138
+ }
1139
+
1140
+ $tag_type = qTranslateSlug_get_term_type($taxonomy);
1141
+
1142
+
1143
+ foreach($q_config['enabled_languages'] as $lang) {
1144
+ if (isset($_POST["qts_{$lang}_slug"])){
1145
+ $slug = sanitize_title($_POST["qts_{$lang}_slug"]);
1146
+ if ($slug === ''){
1147
+ $remove[] = $wpdb->prepare('%s',$lang);
1148
+ } else {
1149
+ $update[] = $wpdb->prepare("(%d,%d,%s,%s)",$tag_type,$term_id,$lang,$slug);
1150
+ }
1151
+ }
1152
+ }
1153
+
1154
+ if (isset($remove)){
1155
+ $sql = $wpdb->prepare("DELETE FROM {$wpdb->prefix}qtranslate_slug
1156
+ WHERE qts_type = %d
1157
+ AND qts_id = %d
1158
+ AND qts_lang in (",
1159
+ $tag_type, $term_id) . implode(',',$remove) . ')';
1160
+ $res = $wpdb->query($sql);
1161
+ //$fb && fb($sql, 'remove query') && fb($res, 'remove results');
1162
+ }
1163
+ if (isset($update)){
1164
+ $sql = "INSERT INTO {$wpdb->prefix}qtranslate_slug (qts_type,qts_id,qts_lang,qts_slug) VALUES "
1165
+ . implode(',',$update)
1166
+ . " ON DUPLICATE KEY UPDATE qts_slug=VALUES(qts_slug)";
1167
+ $res = $wpdb->query($sql);
1168
+ //$fb && fb($sql, 'update query') && fb($res, 'update results');
1169
+ }
1170
+ }
1171
+
1172
+
1173
+
1174
+ /**
1175
+ * Saves the translated slug when the page is saved
1176
+ *
1177
+ * @param $post_id the post id
1178
+ * @param $post the post object
1179
+ */
1180
+ function qTranslateSlug_save_postdata( $post_id, $post ) {
1181
+ static $last_done = '';
1182
+
1183
+ global $wpdb, $q_config;
1184
+ //$fb = function_exists('fb');
1185
+ //$fb && fb($_POST,'$_POST') && fb($last_done,'$last_done');
1186
+
1187
+ // verify this came from the our screen and with proper authorization,
1188
+ // because save_post can be triggered at other times
1189
+
1190
+ if (!isset($_POST['qts_nonce'])) return;
1191
+
1192
+ if (!wp_verify_nonce( $_POST['qts_nonce'], 'qts_nonce' )) return;
1193
+
1194
+ $post_types = get_post_types(array('_builtin' => false ));
1195
+
1196
+ if ( 'page' == $post->post_type || in_array($post->post_type, $post_types) ) {
1197
+ $type = QTS_PAGE;
1198
+ if ( !current_user_can( 'edit_page', $post_id ))
1199
+ return;
1200
+ } else if ( 'post' == $post->post_type ) {
1201
+ $type = QTS_POST;
1202
+ if ( !current_user_can( 'edit_post', $post_id ))
1203
+ return;
1204
+ } else {
1205
+ return;
1206
+ }
1207
+
1208
+ // OK, we're authenticated: we need to find and save the data
1209
+ if ($last_done === $post_id) {
1210
+ return;
1211
+ } else {
1212
+ $last_done = $post_id;
1213
+ }
1214
+
1215
+ $update = array();
1216
+ $remove = array();
1217
+
1218
+ ### DO AUTO-TITLE SLUG HERE!!!! $_POST['post_name']
1219
+
1220
+ foreach($q_config['enabled_languages'] as $lang) {
1221
+ if (isset($_POST["qts_{$lang}_slug"])){
1222
+ $slug = sanitize_title($_POST["qts_{$lang}_slug"]);
1223
+ if ($slug === ''){
1224
+ $remove[] = $wpdb->prepare('%s',$lang);
1225
+ } else {
1226
+ $update[] = $wpdb->prepare("(%d,%d,%s,%s)",$type,$post_id,$lang,$slug);
1227
+ }
1228
+ }
1229
+ }
1230
+
1231
+ if ($remove){
1232
+ $sql = $wpdb->prepare("DELETE FROM {$wpdb->prefix}qtranslate_slug
1233
+ WHERE qts_type = %d
1234
+ AND qts_id = %d
1235
+ AND qts_lang in (",
1236
+ $type, $post_id) . implode(',',$remove) . ')';
1237
+ $res = $wpdb->query($sql);
1238
+ //$fb && fb($sql, 'remove query') && fb($res, 'remove results');
1239
+ }
1240
+ if ($update){
1241
+ $sql = "INSERT INTO {$wpdb->prefix}qtranslate_slug (qts_type,qts_id,qts_lang,qts_slug) VALUES "
1242
+ . implode(',',$update)
1243
+ . " ON DUPLICATE KEY UPDATE qts_slug=VALUES(qts_slug)";
1244
+ $res = $wpdb->query($sql);
1245
+ //$fb && fb($sql, 'update query') && fb($res, 'update results');
1246
+ }
1247
+ }
1248
+
1249
+ // Language Select Code for non-Widget users
1250
+ function qTranslateSlug_generateLanguageSelectCode($style='', $id='qtrans_language_chooser') {
1251
+
1252
+ global $q_config;
1253
+
1254
+ if($style=='') $style='text';
1255
+ if(is_bool($style)&&$style) $style='image';
1256
+ switch($style) {
1257
+ case 'image':
1258
+ case 'text':
1259
+ case 'dropdown':
1260
+ echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
1261
+ $sorted_languages = qtrans_getSortedLanguages();
1262
+ $counter_ = 0;
1263
+ $counter_total_ = count($sorted_languages);
1264
+ foreach($sorted_languages as $language) {
1265
+ $counter_ ++;
1266
+ $last_class = ' ';
1267
+ if($counter_ == $counter_total_) $last_class = ' last-child';
1268
+ echo '<li';
1269
+ if((string)$language == (string)$q_config['language']):
1270
+ echo ' class="active'.$last_class.'"';
1271
+ else:
1272
+ echo ' class="'.$last_class.'"';
1273
+ endif;
1274
+ echo '><a href="'.qTranslateSlug_getSelfUrl($language).'"';
1275
+ // set hreflang
1276
+ echo ' hreflang="'.$language.'"';
1277
+ if($style=='image')
1278
+ echo ' class="qtrans_flag qtrans_flag_'.$language.'"';
1279
+ echo '><span';
1280
+ if($style=='image')
1281
+ echo ' style="display:none"';
1282
+ echo '>'.$language.'</span></a></li>';
1283
+ }
1284
+ echo "</ul><div class=\"qtrans_widget_end\"></div>";
1285
+ if($style=='dropdown') {
1286
+ echo "<script type=\"text/javascript\">\n// <![CDATA[\r\n";
1287
+ echo "var lc = document.getElementById('".$id."');\n";
1288
+ echo "var s = document.createElement('select');\n";
1289
+ echo "s.id = 'qtrans_select_".$id."';\n";
1290
+ echo "lc.parentNode.insertBefore(s,lc);";
1291
+ // create dropdown fields for each language
1292
+ foreach(qtrans_getSortedLanguages() as $language) {
1293
+ echo qtrans_insertDropDownElement($language, qTranslateSlug_getSelfUrl($language), $id);
1294
+ }
1295
+ // hide html language chooser text
1296
+ echo "s.onchange = function() { document.location.href = this.value;}\n";
1297
+ echo "lc.style.display='none';\n";
1298
+ echo "// ]]>\n</script>\n";
1299
+ }
1300
+ break;
1301
+ case 'both':
1302
+ echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
1303
+ $sorted_languages = qtrans_getSortedLanguages();
1304
+ $counter_ = 0;
1305
+ $counter_total_ = count($sorted_languages);
1306
+ foreach($sorted_languages as $language) {
1307
+ $counter_ ++;
1308
+ $last_class = ' ';
1309
+ if($counter_ == $counter_total_) $last_class = ' last-child';
1310
+ echo '<li';
1311
+ if($language == $q_config['language']):
1312
+ echo ' class="active'.$last_class.'"';
1313
+ else:
1314
+ echo ' class="'.$last_class.'"';
1315
+ endif;
1316
+ echo '><a href="'.qTranslateSlug_getSelfUrl($language).'"';
1317
+ echo ' class="qtrans_flag_'.$language.' qtrans_flag_and_text"';
1318
+ echo '><span>'.$q_config['language_name'][$language].'</span></a></li>';
1319
+ }
1320
+ echo "</ul><div class=\"qtrans_widget_end\"></div>";
1321
+ break;
1322
+ }
1323
+ }
1324
+
1325
+ class qTranslateslugWidget extends WP_Widget {
1326
+ function qTranslateslugWidget() {
1327
+ $widget_ops = array('classname' => 'widget_qtranslateslug', 'description' => __('Allows your visitors to choose a Language.','qtranslate') );
1328
+ $this->WP_Widget('qtranslateslug', 'qTranslate slug widget', $widget_ops);
1329
+ }
1330
+
1331
+ function widget($args, $instance) {
1332
+ extract($args);
1333
+
1334
+ echo $before_widget;
1335
+ $title = empty($instance['title']) ? __('Language', 'qtranslate') : apply_filters('widget_title', $instance['title']);
1336
+ $hide_title = empty($instance['hide-title']) ? false : 'on';
1337
+ $type = $instance['type'];
1338
+ if($type!='text'&&$type!='image'&&$type!='both'&&$type!='dropdown') $type='text';
1339
+
1340
+ if($hide_title!='on') { echo $before_title . $title . $after_title; };
1341
+ qTranslateSlug_generateLanguageSelectCode($type, $this->id);
1342
+ echo $after_widget;
1343
+ }
1344
+
1345
+ function update($new_instance, $old_instance) {
1346
+ $instance = $old_instance;
1347
+ $instance['title'] = $new_instance['title'];
1348
+ $instance['hide-title'] = $new_instance['hide-title'];
1349
+ $instance['type'] = $new_instance['type'];
1350
+
1351
+ return $instance;
1352
+ }
1353
+
1354
+ function form($instance) {
1355
+ $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'hide-title' => false, 'type' => 'text' ) );
1356
+ $title = $instance['title'];
1357
+ $hide_title = $instance['hide-title'];
1358
+ $type = $instance['type'];
1359
+ ?>
1360
+ <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'qtranslate'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
1361
+ <p><label for="<?php echo $this->get_field_id('hide-title'); ?>"><?php _e('Hide Title:', 'qtranslate'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('hide-title'); ?>" name="<?php echo $this->get_field_name('hide-title'); ?>" <?php echo ($hide_title=='on')?'checked="checked"':''; ?>/></label></p>
1362
+ <p><?php _e('Display:', 'qtranslate'); ?></p>
1363
+ <p><label for="<?php echo $this->get_field_id('type'); ?>1"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>1" value="text"<?php echo ($type=='text')?' checked="checked"':'' ?>/> <?php _e('Text only', 'qtranslate'); ?></label></p>
1364
+ <p><label for="<?php echo $this->get_field_id('type'); ?>2"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>2" value="image"<?php echo ($type=='image')?' checked="checked"':'' ?>/> <?php _e('Image only', 'qtranslate'); ?></label></p>
1365
+ <p><label for="<?php echo $this->get_field_id('type'); ?>3"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>3" value="both"<?php echo ($type=='both')?' checked="checked"':'' ?>/> <?php _e('Text and Image', 'qtranslate'); ?></label></p>
1366
+ <p><label for="<?php echo $this->get_field_id('type'); ?>4"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>4" value="dropdown"<?php echo ($type=='dropdown')?' checked="checked"':'' ?>/> <?php _e('Dropdown Box', 'qtranslate'); ?></label></p>
1367
+ <?php
1368
+ }
1369
+ }
1370
+
1371
+
1372
+
1373
+ /**
1374
+ * adds support for qtranslate nav menus
1375
+ * @package Qtranslate Slug
1376
+ * @version 0.9
1377
+ **/
1378
+ function wa_admin_init() {
1379
+ global $pagenow;
1380
+ if( $pagenow != 'nav-menus.php' ) return;
1381
+
1382
+ wp_enqueue_script('nav-menu-query', plugins_url( 'js/qt-nav-menu-min.js' , __FILE__ ) , 'nav-menu', '1.0');
1383
+ add_meta_box( 'qt-languages', __('Languages'), 'qt_languages_meta_box', 'nav-menus', 'side', 'default' );
1384
+ }
1385
+
1386
+ function qt_languages_meta_box() {
1387
+ global $q_config;
1388
+ echo '<p>';
1389
+ foreach($q_config['enabled_languages'] as $id => $language) {
1390
+ $checked = ($language == $q_config['language']) ? ' checked="checked"' : '';
1391
+ echo '<p style="margin:0 0 5px 0"><input type="radio" style="margin-right:5px" name="wa_qt_lang" value="' . $language . '" id="wa_gt_lang_' . $id . '" ' . $checked . '/>';
1392
+ echo '<label for="wa_gt_lang_' . $id . '">';
1393
+ echo '<img src="' . trailingslashit(WP_CONTENT_URL).$q_config['flag_location'].$q_config['flag'][$language] . '"/>&nbsp;';
1394
+ echo __($q_config['language_name'][$language], 'qtranslate');
1395
+ echo '</label></p>';
1396
+ }
1397
+ echo '</p>';
1398
+ }
1399
+ add_action('admin_init', 'wa_admin_init');
1400
+
1401
+
1402
+ /**
1403
+ * adds support for qtranslate in taxonomies
1404
+ * @package Qtranslate Slug
1405
+ * @version 0.8
1406
+ **/
1407
+ function qtranslate_edit_taxonomies(){
1408
+
1409
+ $has_titlte = array('category', 'post_tag');
1410
+ //$has_slug = array( 'families' );
1411
+
1412
+ $args = array( 'public' => true, 'show_ui' => true, '_builtin' => true );
1413
+ $categories = get_taxonomies($args, 'object');
1414
+ $args = array( 'public' => true, '_builtin' => false );
1415
+ $taxonomies = get_taxonomies($args, 'object' );
1416
+
1417
+ $taxonomies = array_merge( $categories, $taxonomies );
1418
+
1419
+ if ($taxonomies) {
1420
+ foreach ($taxonomies as $taxonomy ) {
1421
+ if (in_array($taxonomy->name, $has_titlte) ) {
1422
+ add_action( $taxonomy->name.'_add_form', 'qtrans_modifyTermFormFor');
1423
+ add_action( $taxonomy->name.'_edit_form', 'qtrans_modifyTermFormFor');
1424
+ }
1425
+ }
1426
+ }
1427
+
1428
+ }
1429
+ add_action('admin_menu', 'qtranslate_edit_taxonomies', 805);
1430
+ add_filter('single_term_title', 'qtrans_useTermLib', 805);
1431
+
1432
+
1433
+
1434
+ /**
1435
+ * adds support for qtranslateSlug in taxonomies
1436
+ * @package Qtranslate Slug
1437
+ * @version 0.8
1438
+ **/
1439
+ /*
1440
+ function qtranslateSlug_filter_taxonomies(){
1441
+
1442
+ if ( !isset($_GET['action']) || $_GET['action'] != 'edit' || !isset($_GET['taxonomy']) || !isset($_GET['tag_ID']) ) return false;
1443
+
1444
+ $has_slug = array( 'category', 'post_tag');
1445
+
1446
+ if (!in_array($_GET['taxonomy'], $has_slug) ) {
1447
+ remove_action('edit_category_form_fields', 'qTranslateSlug_tag_fields');
1448
+ remove_action('edit_tag_form_fields', 'qTranslateSlug_tag_fields');
1449
+ remove_action('edited_term', 'save_qTranslateSlug_term_fields', 10, 3);
1450
+ }
1451
+ }
1452
+ add_action('admin_init', 'qtranslateSlug_filter_taxonomies', 800);
1453
+ */
1454
+
1455
+ /**
1456
+ * hide quickedit button (functionality not supported by qTranslate)
1457
+ * @package Qtranslate Slug
1458
+ * @version 0.8
1459
+ **/
1460
+ function hide_quick_edit_script() {
1461
+ global $pagenow;
1462
+
1463
+ if (is_admin())
1464
+ echo"
1465
+ <script type=\"text/javascript\">
1466
+ jQuery(document).ready(function($) {
1467
+ // Removing Quick Edit button
1468
+ var editinline = $('a.editinline');
1469
+ if(editinline.length > 0) editinline.parent().remove();
1470
+ });
1471
+ </script>";
1472
+ }
1473
+ add_action('admin_footer','hide_quick_edit_script', 600);
1474
+
1475
+
1476
+
1477
+ /**
1478
+ * get traduction slug for post type
1479
+ * @package Qtranslate Slug
1480
+ * @version 0.8
1481
+ **/
1482
+ function get_custom_post_type_slug($post_type) {
1483
+ global $custom_post_types_slugs, $q_config, $qts_use_language;
1484
+
1485
+ if(!function_exists('qTranslateSlug_post_type_link') || !isset($custom_post_types_slugs) ) return $post_type;
1486
+
1487
+ $lang = ($qts_use_language === null) ? $q_config['language'] : $qts_use_language;
1488
+
1489
+ if (is_array($custom_post_types_slugs[$post_type])) {
1490
+ return $custom_post_types_slugs[$post_type][$lang];
1491
+ } else {
1492
+ return $post_type;
1493
+ }
1494
+ }
1495
+
1496
+ function get_custom_taxonomy_slug($taxonomy) {
1497
+ global $taxonomies_slugs, $q_config, $qts_use_language;
1498
+
1499
+ if(!function_exists('qTranslateSlug_term_link') || !isset($taxonomies_slugs) ) return $taxonomy;
1500
+
1501
+ $lang = ($qts_use_language === null) ? $q_config['language'] : $qts_use_language;
1502
+
1503
+ if (is_array($taxonomies_slugs[$taxonomy])) {
1504
+ return $taxonomies_slugs[$taxonomy][$lang];
1505
+ } else {
1506
+ return $taxonomy;
1507
+ }
1508
+ }
1509
+
1510
+
1511
+ function qtranslateSlug_blog_names($blogs) {
1512
+
1513
+ foreach ($blogs as $blog) {
1514
+ $blog->blogname = __($blog->blogname);
1515
+ }
1516
+ return $blogs;
1517
+ }
1518
+
1519
+ add_filter( 'get_blogs_of_user', 'qtranslateSlug_blog_names', 1 );
1520
+
1521
+
1522
+ function qtranslug_widget_init() {
1523
+ register_widget('qTranslateslugWidget');
1524
+ }
1525
+ add_action('widgets_init', 'qtranslug_widget_init');
readme.txt ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Qtranslate Slug ===
2
+ Contributors: Carlos Sanz García
3
+ Donate link: none
4
+ Tags: qtranslate, slug, multilanguage
5
+ Requires at least: 3.0
6
+ Tested up to: 3.3.2
7
+ Version: 0.9
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Allows to define a slug for each language and some qTranslate bug fixes
12
+
13
+ == Description ==
14
+
15
+ This plugin allows translated slugs in Qtranslate.
16
+ You need install and activate previously Qtranslate, if not, Qtranslate Slug will not work.
17
+
18
+ Tested with Qtranslate versions 2.5.8 and 2.5.9
19
+
20
+ == Installation ==
21
+
22
+ This plugins requires qTranslate installed previously, if not, it will not activate.
23
+
24
+ 1. Upload `qtranslate-slug` to the `/wp-content/plugins/` directory
25
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
26
+ 1. That's all!
27
+
28
+ == Frequently Asked Questions ==
29
+
30
+ = It works with posts and pages, but with other content type ? =
31
+
32
+ This plugin allows to translate slugs of: posts, pages, custom post types, categories, tags and custom taxonomies.
33
+
34
+ = How can I insert a language selector in my theme ? =
35
+
36
+ Place `<?php if (function_exists('qTranslateSlug_generateLanguageSelectCode') ) qTranslateSlug_generateLanguageSelectCode('text'); ?>` in your template.
37
+
38
+ == Screenshots ==
39
+
40
+ 1. This screen shot description corresponds to screenshot-1.(png|jpg|jpeg|gif). Note that the screenshot is taken from
41
+ the directory of the stable readme.txt, so in this case, `/tags/4.3/screenshot-1.png` (or jpg, jpeg, gif)
42
+ 2. This is the second screen shot
43
+
44
+ == Changelog ==
45
+
46
+ = 0.9 =
47
+ * some wordpress qTranslate bug fixes
48
+ * adds a javascript solution for qTranslate Nav Menus
49
+
50
+ = 0.8 =
51
+ * added support por Categories
52
+ * added support por Tags
53
+ * added support por Taxonomies
54
+ * added support por Custom Post Types
55
+
56
+ = 0.7 ( enhanced by Zapo (http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=4&t=1049&start=50#p7499) ) =
57
+ * added suport for qTranslate TLD domain mode (en: domain.com | fr: domain.fr) visit
58
+
59
+ = 0.5 and 0.6 enhanched by Marco Del Percio =
60
+
61
+ == Upgrade Notice ==
62
+
63
+ = 0.9 =
64
+ This version fix some bugs and allow multilanguage in nav-menus.
65
+
66
+ = 0.8 =
67
+ A lot of slugs content allowed
68
+
69
+ = 0.7 =
70
+ This version allows TLD domain option for a different Qtranslate fork maded by Zappo
71
+
72
+
73
+ == Todo ==
74
+
75
+ * admin options page for setup the taxonomies names
76
+ * change the database system ( post meta ), without installing tables
77
+ * generate translated slug automatically from the translated title
78
+ * check if the slug is already used, and add a progressive number in this case
79
+ * force the use of the translated slug if defined
80
+ * force to show a page/post only at the correct URL
81
+ * try to redirect from a wrong URL to a correct URL
82
+ * keep track of renamed slugs, redirecting from old to new slugs
83
+ * translate categories and tags slugs.
version.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ = 0.9 =
2
+ * some wordpress qTranslate bug fixes
3
+ * adds a javascript solution for qTranslate Nav Menus
4
+
5
+ = 0.8 =
6
+ * added support por Categories
7
+ * added support por Tags
8
+ * added support por Taxonomies
9
+ * added support por Custom Post Types
10
+
11
+ = 0.7 ( enhanced by Zapo (http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=4&t=1049&start=50#p7499) ) =
12
+ * added suport for qTranslate TLD domain mode (en: domain.com | fr: domain.fr) visit
13
+
14
+ = 0.5 and 0.6 enhanched by Marco Del Percio =