web/wp-content/plugins/xili-language/xili-language.php
changeset 170 8e3a5a6fc63e
child 180 d8780eeb2607
equal deleted inserted replaced
169:2d410dc8c1f3 170:8e3a5a6fc63e
       
     1 <?php
       
     2 /*
       
     3 Plugin Name: xili-language
       
     4 Plugin URI: http://dev.xiligroup.com/xili-language/
       
     5 Description: This plugin modify on the fly the translation of the theme depending the language of the post or other blog elements - a way to create a real multilanguage site (cms or blog). It introduce a new taxonomy - here language - to describe posts and pages. To complete with tags, use also xili-tidy-tags plugin. 
       
     6 Author: dev.xiligroup.com - MS
       
     7 Version: 1.4.1
       
     8 Author URI: http://dev.xiligroup.com
       
     9 */
       
    10 # updated 100220 - 1.4.1 - wp_title translation for categories, () suppressed in cats list display, auto-search linked posts option
       
    11 # updated 100216 - 1.3.2 - Option to modify home query according rules by chief editor. fixes gold functions. New Recent Posts Widget.
       
    12 # updated 100216 - 1.3.1 - Just to correct a minor omission - Add New link works now for linked pages.
       
    13 # updated 100215 - 1.3.0 - new functions to change and restore loop's language query-tag. Better post UI to create linked post - fixes lost lang's link when trash or untrash.
       
    14 # updated 100207 - 1.2.1 - fixes some directories issues in (rare) xamp servers - Some improvements in post edit UI.
       
    15 # updated 100109 - 1.2.0 - tested with WP 2.9.1 - more localization for admin UI (RU)
       
    16 # updated 091104 - 1.1.9.1 - fixes special functions
       
    17 # updated 091103 - 1.1.9 - optional improve hooking ways to be compatible with l10n cache of Johan see line 2200 - fix title of wp_get_archive links with current permalinks.
       
    18 # updated 091019 - 1.1.8 - gold functions and shortcode for linked posts - first tests with WP 2.9
       
    19 # updated 091007 - 1.1.es - tests - gold functions active - update undefined posts functions in library
       
    20 # updated 090918 - 1.1 - xiliml_the_other_posts function improved and upgraded for CMS webmasters
       
    21 # updated 090719 - 1.0.2 - fix unexpected like tags metabox added by WP 28 tracs #10437
       
    22 # updated 090626 - 1.0.1 - fix filter unique id for category link hooks
       
    23 # updated 090615 - 1.0 - Via admin UI, new ways to choose default language of front-page (page, home,...)
       
    24 # updated 090606 - 0.9.9.6 - ready for 2.8 hooks - ready for multiple languages list widget
       
    25 # see readme text for these intermediate versions.
       
    26 # updated 090228 - Class and OOP - see 0.9.7 in comments of functions below - only for WP 2.7.x
       
    27 
       
    28 # This plugin is free software; you can redistribute it and/or
       
    29 # modify it under the terms of the GNU Lesser General Public
       
    30 # License as published by the Free Software Foundation; either
       
    31 # version 2.1 of the License, or (at your option) any later version.
       
    32 #
       
    33 # This plugin is distributed in the hope that it will be useful,
       
    34 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    35 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       
    36 # Lesser General Public License for more details.
       
    37 #
       
    38 # You should have received a copy of the GNU Lesser General Public
       
    39 # License along with this plugin; if not, write to the Free Software
       
    40 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
       
    41 
       
    42 define('XILILANGUAGE_VER','1.4.1'); /* used in admin UI*/
       
    43 
       
    44 class xili_language {
       
    45 	
       
    46 	var $default_lang; /* language of config.php*/
       
    47 	var $curlang;
       
    48 	var $langstate; /* undefined or not */
       
    49 	var $browseroption = '';
       
    50 	var $authorbrowseroption = '';
       
    51 	var $functions_enable = '';
       
    52 	var $default_dir = ''; /* undefined or not in WP config '' or rtl or ltr */
       
    53 	var $curlang_dir = ''; /* undefined or not according array */
       
    54 	var $rtllanglist = 'ar-he-fa-ur'; /*default-list - can be set after class instantiation*/
       
    55 	var $post_ajax = false; /* ajax used in meta box in post edit UI unstable yet */
       
    56 	var $is_metabox = false; /* meta box in post edit UI - if used don't use custom fields that are not refreshed */
       
    57 	var $xili_settings; /* saved in options */
       
    58 	var $langs_group_id; /* group ID and Term Taxo ID */
       
    59 	var $langs_group_tt_id; 
       
    60 	var $get_archives_called = array(); /* if != '' - insert lang in link */
       
    61 	var $idx = array(); /* used to identify filter or action set from this class - since 0.9.9.6 */
       
    62 	var $theme_locale = false; /* to control locale hook */
       
    63 	var $ossep = "/"; /* for recursive file search in xamp */
       
    64 	var $current_lang_query_tag = ""; /* since 1.3.0 */
       
    65 	var $temp_lang_query_tag = "";
       
    66 	
       
    67 	function xili_language($metabox = false, $post_ajax = false, $locale_method = false) {
       
    68 		$this->is_metabox = $metabox;
       
    69 		$this->post_ajax = $post_ajax;
       
    70 		$this->locale_method = $locale_method; /* added for compatibility with cache plugin from johan */
       
    71 		/*activated when first activation of plug*/
       
    72 		register_activation_hook(__FILE__,array(&$this,'xili_language_activate'));
       
    73 		$this->ossep = strtoupper(substr(PHP_OS,0,3)=='WIN')?'\\':'/';
       
    74 		/*get current settings - name of taxonomy - name of query-tag - 0.9.8 new taxonomy taxolangsgroup */
       
    75 		$this->xili_settings = get_option('xili_language_settings');
       
    76 		if(empty($this->xili_settings)) {
       
    77 			$submitted_settings = array(
       
    78 			    'taxonomy'		=> 'language',
       
    79 			    'version' 		=> '0.4',
       
    80 			    'reqtag'		=> 'lang',
       
    81 			    'browseroption' => '',
       
    82 			    'authorbrowseroption' => '',
       
    83 			    'taxolangsgroup' => 'languages_group',
       
    84 			    'functions_enable' => '',
       
    85 			    'langs_folder' => '',
       
    86 			    'theme_domain' => '',
       
    87 			    'homelang' => '' 
       
    88 		    );
       
    89 			define('TAXONAME','language');
       
    90 			define('QUETAG','lang');
       
    91 			define('TAXOLANGSGROUP','languages_group');
       
    92 			update_option('xili_language_settings', $submitted_settings);	
       
    93 			$this->xili_settings = get_option('xili_language_settings');		
       
    94 		} else {
       
    95 			define('TAXONAME',$this->xili_settings['taxonomy']);
       
    96 			define('QUETAG',$this->xili_settings['reqtag']);
       
    97 			$this->browseroption = $this->xili_settings['browseroption'];
       
    98 			$this->authorbrowseroption = $this->xili_settings['authorbrowseroption'];
       
    99 			$this->functions_enable = $this->xili_settings['functions_enable'];
       
   100 			if ($this->xili_settings['version'] == '0.2' || $this->xili_settings['version'] == '0.3') { /* 1.3.2 */
       
   101 				$this->xili_settings['taxolangsgroup'] = 'languages_group';
       
   102 				$this->xili_settings['homelang'] = '';
       
   103 				$this->xili_settings['version'] = '0.4';
       
   104 				update_option('xili_language_settings', $this->xili_settings);
       
   105 			}	
       
   106 			define('TAXOLANGSGROUP',$this->xili_settings['taxolangsgroup']);
       
   107 		}
       
   108 		define('XILIFUNCTIONSPATH',WP_PLUGIN_DIR.'/xilidev-libraries'); /* since 1.0 to add xili-libraries */
       
   109 		
       
   110 		/** add new taxonomy in available taxonomies 
       
   111 		 * 1.0.2 - add label false as http://core.trac.wordpress.org/ticket/10437 
       
   112 		 * 			to avoid metabox as tag displayed
       
   113 		 */
       
   114 		register_taxonomy( TAXONAME, 'post',array('hierarchical' => false, 'label'=>false, 'rewrite' => false, 'update_count_callback' => array(&$this,'_update_post_lang_count')));
       
   115 		register_taxonomy( TAXOLANGSGROUP, 'term',array('hierarchical' => false, 'update_count_callback' => ''));
       
   116 		$thegroup = get_terms(TAXOLANGSGROUP, array('hide_empty' => false,'slug' => 'the-langs-group'));
       
   117 		if (!$thegroup) { /* update langs group 0.9.8 */
       
   118 			$args = array( 'alias_of' => '', 'description' => 'the group of languages', 'parent' => 0, 'slug' =>'the-langs-group');
       
   119 			wp_insert_term( 'the-langs-group', TAXOLANGSGROUP, $args); /* create and link to existing langs */
       
   120 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   121 			foreach($listlanguages as $language) {
       
   122 				wp_set_object_terms($language->term_id, 'the-langs-group', TAXOLANGSGROUP);
       
   123 			}
       
   124 			$thegroup = get_terms(TAXOLANGSGROUP, array('hide_empty' => false,'slug' => 'the-langs-group'));
       
   125 		}
       
   126 		$this->langs_group_id = $thegroup[0]->term_id;
       
   127 		$this->langs_group_tt_id = $thegroup[0]->term_taxonomy_id;
       
   128 		
       
   129 		/* default values */
       
   130 		if (''!= WPLANG && strlen(WPLANG)==5) :
       
   131 			$this->default_lang = WPLANG;
       
   132 		else:
       
   133 			$this->default_lang = 'en_US';
       
   134 		endif;
       
   135 		define('DEFAULTSLUG', $this->get_default_slug());
       
   136 		if ( $dir = get_bloginfo('text_direction') ) /* if present in blog options @since 0.9.9 */
       
   137 			$this->default_dir = $dir;
       
   138 		
       
   139 		add_filter('query_vars', array(&$this,'keywords_addQueryVar'));
       
   140 		add_filter('posts_join', array(&$this,'with_lang'));
       
   141 		add_filter('posts_where', array(&$this,'where_lang'));
       
   142 		
       
   143 		add_action('wp', array(&$this,'xiliml_language_wp')); 
       
   144 		/* 'wp' = where theme's language is defined just after query */
       
   145 		if ($this->locale_method)
       
   146 			add_filter('locale', array(&$this,'xiliml_setlocale'), 10);
       
   147 		/* to be compatible with l10n cache from Johan since 1.1.9 */
       
   148 		add_filter('language_attributes',  array(&$this,'head_language_attributes'));
       
   149 		add_action('wp_head', array(&$this,'head_insert_language_metas'),10,2);
       
   150  
       
   151  	 	add_filter('widget_title', array(&$this,'widget_texts')); /* added 0.9.8.1 */
       
   152 	 	add_filter('widget_text', array(&$this,'widget_texts'));
       
   153 		add_filter('list_cats', array(&$this,'xiliml_cat_language'),10,2); /* mode 2 : content = name */
       
   154 		
       
   155 		add_filter('category_link', array(&$this,'xiliml_link_append_lang'));
       
   156 		$filter = 'category_link';
       
   157 		$function = 'xiliml_link_append_lang';
       
   158 		$this->idx['xiliml_link_append_lang'] = _wp_filter_build_unique_id($filter, array (&$this, $function == '' ? $filter : $function), 10); /* unique id of this filter from object fixed 1.0.1 */
       
   159 		
       
   160 		add_filter('category_description',array(&$this,'xiliml_link_translate_desc'));
       
   161 		add_filter('single_cat_title',array(&$this,'xiliml_single_cat_title_translate')); /* 1.4.1 wp_title() */
       
   162 		add_filter('tag_link', array(&$this,'xiliml_taglink_append_lang' ));
       
   163 		
       
   164 		add_action('pre_get_posts', array(&$this,'xiliml_modify_querytag'));
       
   165 		/* filters for archives since 0.9.9.4 */
       
   166 		add_filter('getarchives_join', array(&$this,'xiliml_getarchives_join'),10,2);
       
   167 		add_filter('getarchives_where', array(&$this,'xiliml_getarchives_where'),10,2);
       
   168 		add_filter('get_archives_link', array(&$this,'xiliml_get_archives_link'));
       
   169 		/* actions for post and page admin UI */
       
   170 		add_action('save_post', array(&$this,'xili_language_add'));
       
   171 		//add_action('publish_post', array(&$this,'xili_language_add')); /* only set when published !*/
       
   172 		add_action('save_page', array(&$this,'xili_language_add'));
       
   173 		//add_action('publish_page', array(&$this,'xili_language_add'));
       
   174 		if ($this->post_ajax) {
       
   175 			add_action( 'wp_ajax_oklinked', array(&$this,'ok_linked') );
       
   176 			add_action( 'wp_ajax_customrefresh', array(&$this,'custom_refresh') );
       
   177 		}
       
   178 		/* admin settings UI*/
       
   179 		add_action('init', array(&$this, 'init_textdomain'));
       
   180 		add_filter('plugin_action_links',  array(&$this,'xililang_filter_plugin_actions'), 10, 2);
       
   181 		
       
   182 		add_action('admin_menu', array(&$this,'myplugin_add_custom_box'));
       
   183 		add_action('admin_menu', array(&$this,'xili_add_pages'));
       
   184 		/* special to detect theme changing since 1.1.9 */
       
   185 		add_action('switch_theme', array(&$this,'theme_switched'));
       
   186 		/* inspired from custax */
       
   187 		add_action('manage_posts_custom_column', array(&$this,'xili_manage_column'), 10, 2);
       
   188 		add_filter('manage_edit_columns', array(&$this,'xili_manage_column_name'));
       
   189 
       
   190 		add_action('manage_pages_custom_column', array(&$this,'xili_manage_column'), 10, 2);
       
   191 		add_filter('manage_edit-pages_columns', array(&$this,'xili_manage_column_name'));
       
   192 		
       
   193 		/* new actions for xili-language theme's templates tags */
       
   194 		
       
   195 		$this->add_action('xili_language_list','xili_language_list',10,3); /* add third param 0.9.7.4*/
       
   196 		$this->add_action('xili_post_language','xili_post_language',10,2);
       
   197 		
       
   198 		$this->add_action('xiliml_the_other_posts','xiliml_the_other_posts',10,4); /* add a param 1.1 */
       
   199 		$this->add_action('xiliml_the_category','xiliml_the_category',10,3);
       
   200 		$this->add_action('xiliml_langinsearchform','xiliml_langinsearchform',10,2);
       
   201 		
       
   202 	}
       
   203 	
       
   204 	function add_action ($action, $function = '', $priority = 10, $accepted_args = 1)
       
   205 	{
       
   206 		add_action ($action, array (&$this, $function == '' ? $action : $function), $priority, $accepted_args);
       
   207 		$this->idx[$action] = _wp_filter_build_unique_id($action, array (&$this, $function == '' ? $action : $function), $priority); /* unique id of this filter from object */		 
       
   208 	}
       
   209 	
       
   210 	function add_filter ($filter, $function = '', $priority = 10, $accepted_args = 1)
       
   211 	{
       
   212 		add_filter ($filter, array (&$this, $function == '' ? $filter : $function), $priority, $accepted_args);
       
   213 		$this->idx[$filter] = _wp_filter_build_unique_id($filter, array (&$this, $function == '' ? $filter : $function), $priority); /* unique id of this filter from object fixed 1.0.1 */
       
   214 	}
       
   215 			
       
   216 	/**
       
   217 	 * More than one filter for the function. 
       
   218 	 *
       
   219 	 * @since 0.9.7
       
   220 	 * 
       
   221 	 * @param $the_function (string). 
       
   222 	 * @return true if more than one.
       
   223 	 */
       
   224 	function this_has_filter($the_function) {
       
   225 		global $wp_filter;
       
   226 		$has = $wp_filter[$the_function];
       
   227 		//print_r($has);
       
   228 		$keys = array_keys($has);
       
   229 		//echo count($has[$keys[0]]);
       
   230 		if (count($has[$keys[0]]) >= 2) { /*one from class others from functions.php or elsewhere*/
       
   231 			return true;
       
   232 		} else {
       
   233 			return false;
       
   234 		} 	
       
   235 	}	
       
   236 
       
   237 	function myplugin_add_custom_box() {		
       
   238  		add_meta_box('xilil-2', __("Page's language",'xili-language'), array(&$this,'xili_language_checkboxes_n'), 'page', 'side','high');
       
   239  		add_meta_box('xilil-2', __("Post's language",'xili-language'), array(&$this,'xili_language_checkboxes_n'), 'post', 'side','high');
       
   240  		if ($this->is_metabox) {
       
   241  			add_meta_box('xilil-1', __('Linked posts','xili-language'), array(&$this,'xili_language_linked_posts'), 'post', 'side','high');
       
   242  			add_meta_box('xilil-1', __('Linked pages','xili-language'), array(&$this,'xili_language_linked_posts'), 'page', 'side','high');
       
   243  		}
       
   244 	}
       
   245 	
       
   246 	/**
       
   247 	 * Will update term count based on posts AND pages.
       
   248 	 *  
       
   249 	 * @access private from register taxonomy etc...
       
   250 	 * @since 0.9.8.1
       
   251 	 * @uses $wpdb
       
   252 	 *
       
   253 	 * @param array $terms List of Term taxonomy IDs
       
   254 	 */
       
   255 	function _update_post_lang_count( $terms ) {
       
   256 		global $wpdb;
       
   257 		foreach ( (array) $terms as $term ) {
       
   258 			$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND term_taxonomy_id = %d", $term ) );
       
   259 			$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
       
   260 		}
       
   261 	}
       
   262 	
       
   263 	/**
       
   264 	 * set language when post or page is saved or changed 
       
   265 	 *
       
   266 	 * @since 0.9.0
       
   267 	 * @completed 0.9.7.1 to record postmeta of linked posts in other languages
       
   268 	 * @updated 0.9.7.5 to delete relationship when undefined
       
   269 	 * @updated 0.9.9 to avoid delete relationship when in quick_edit
       
   270 	 * @updated 1.3.0 to avoid delete relationship when trashing - 1.4.1 - create post-meta xl-search-linked
       
   271 	 * @param $post_ID
       
   272 	 */
       
   273 	function xili_language_add($post_ID) {
       
   274 		if (!isset($_POST['_inline_edit'])) { /* to avoid delete relationship when in quick_edit (edit.php) */
       
   275 			$sellang = $_POST['xili_language_set'];
       
   276 			if ("" != $sellang) {
       
   277 				wp_set_object_terms($post_ID, $sellang, TAXONAME);
       
   278 			} else {
       
   279 				if ($_GET['action'] != 'trash' && $_GET['action'] != 'untrash')
       
   280 						wp_delete_object_term_relationships( $post_ID, TAXONAME ); 	
       
   281 			}
       
   282 			if ($this->is_metabox) {	
       
   283 				/* the linked posts set by author */	
       
   284 				$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   285 				foreach ($listlanguages as $language) {
       
   286 					$inputid = 'xili_language_'.QUETAG.'-'.$language->slug ;
       
   287 					$recinputid = 'xili_language_rec_'.QUETAG.'-'.$language->slug ;
       
   288 					$linkid = $_POST[$inputid];
       
   289 					$reclinkid = $_POST[$recinputid]; /* hidden previous value */
       
   290 					$langslug = QUETAG.'-'.$language->slug ;
       
   291 					
       
   292 					if ($reclinkid != $linkid) { /* only if changed value or created since 1.3.0 */			
       
   293 						if ((is_numeric($linkid) && $linkid == 0) || '' == $linkid ) {
       
   294 							delete_post_meta($post_ID, $langslug);
       
   295 						} elseif (is_numeric($linkid) && $linkid > 0) {
       
   296 							update_post_meta($post_ID, $langslug, $linkid);
       
   297 							if ($reclinkid == "-1")	update_post_meta($linkid, QUETAG.'-'.$sellang, $post_ID); 
       
   298 						}
       
   299 					}	
       
   300 				}
       
   301 				//if (isset($_POST['xili_language_search_lang'])) {
       
   302 					if ('' != $_POST['xili_language_search_lang']) {
       
   303 						update_post_meta($post_ID, '_xl-search-linked', $_POST['xili_language_search_lang']);
       
   304 					} else {
       
   305 						if ($_GET['action'] != 'trash' && $_GET['action'] != 'untrash')
       
   306 												delete_post_meta($post_ID, '_xl-search-linked');
       
   307 					}
       
   308 				//}	
       
   309 				
       
   310 			}
       
   311 		} /* quick edit */		
       
   312 	}
       
   313 	
       
   314 	/**
       
   315 	 * Return language dir
       
   316 	 *
       
   317 	 * @since 0.9.9
       
   318 	 * @param slug of lang
       
   319 	 */
       
   320 	function get_dir_of_cur_language($lang_slug) {
       
   321 		$rtlarray = explode ('-',$this->rtllanglist);			
       
   322 		$dir = (in_array(substr(strtolower($lang_slug),0,2),$rtlarray)) ? 'rtl' : 'ltr';
       
   323 		return $dir;
       
   324 	}
       
   325 	
       
   326 	/**
       
   327 	 * Return language of post.
       
   328 	 *
       
   329 	 * @since 0.9.0
       
   330 	 * @updated 0.9.7.6, 0.9.9
       
   331 	 *
       
   332 	 * @param $post_ID.
       
   333 	 * @return slug of language of post or false if var langstate is false.
       
   334 	 */
       
   335 	function get_cur_language($post_ID) {
       
   336 		$ress = wp_get_object_terms($post_ID, TAXONAME);
       
   337 		if ($ress) {
       
   338 			if (is_a($ress, 'WP_Error')){
       
   339 				echo "Language table not created ! see plug-in admin";
       
   340 				$this->langstate = false;
       
   341 			} else {
       
   342 				$obj_term = $ress[0];
       
   343 				$this->langstate = true;
       
   344 				$postlang = $obj_term->slug;
       
   345 				$postlangdir = $this->get_dir_of_cur_language($postlang);
       
   346 				return array('lang'=>$postlang,'direction'=>$postlangdir);
       
   347 			}
       
   348 	 	} else {
       
   349 	 		$this->langstate = false; /* can be used in language attributes for header */
       
   350 	  		return false;	/* undefined state */
       
   351 	 	}		
       
   352 	}
       
   353 		
       
   354 	/* first activation of plugin */
       
   355 	function xili_language_activate() {
       
   356 		$this->xili_settings = get_option('xili_language_settings');
       
   357 		if(empty($this->xili_settings)) {
       
   358 			$this->xili_settings = array(
       
   359 			    'taxonomy'		=> 'language',
       
   360 			    'version' 		=> '0.4',
       
   361 			    'reqtag'		=> 'lang',
       
   362 			    'browseroption' => '',
       
   363 			    'authorbrowseroption' => '',
       
   364 			    'taxolangsgroup' => 'languages_group',
       
   365 			    'functions_enable' => '',
       
   366 			    'langs_folder' => '',
       
   367 			    'theme_domain' => '',
       
   368 			    'homelang' => '' 
       
   369 		    );
       
   370 		    update_option('xili_language_settings', $this->xili_settings);
       
   371 		}
       
   372 	}
       
   373 	
       
   374 	/*enable the new query tag associated with new taxonomy*/
       
   375 	function keywords_addQueryVar($vars) {
       
   376 		$vars[] = QUETAG;
       
   377 	return $vars ;
       
   378 	}
       
   379 
       
   380 	function get_default_slug() {
       
   381 		$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   382 		$default_slug = 'en_us';
       
   383 		foreach ($listlanguages as $language) {
       
   384 			if ($language->name == $this->default_lang ) return $language->slug;
       
   385 		}
       
   386 		return $default_slug ;
       
   387 	}
       
   388 	
       
   389 	/** 
       
   390 	 * filters used when querytag is used - 
       
   391 	 * see below and functions.php where rules depend from theme
       
   392 	 */
       
   393 	function with_lang($join) {
       
   394 		global $wp_query, $wpdb;	
       
   395 		if ( '' != $wp_query->query_vars[QUETAG] ) {
       
   396 			$join .= " LEFT JOIN $wpdb->term_relationships as tr ON ($wpdb->posts.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) ";
       
   397 		}
       
   398 	
       
   399 	return $join;
       
   400 	}
       
   401 	
       
   402 	/**
       
   403 	 * Setup global post data.
       
   404 	 *
       
   405 	 * @since 0.9.0
       
   406 	 * @updated 0.9.4 (OR added) lang=xx_xx,yy_yy,..
       
   407 	 *
       
   408 	 * @param object $where.
       
   409 	 * @return $where.
       
   410 	 */
       
   411 	function where_lang($where) {
       
   412 		global $wp_query , $wpdb;
       
   413 		$reqtags = array();
       
   414 		$thereqtags = array();
       
   415 		if ( '' != $wp_query->query_vars[QUETAG] ) {
       
   416 		/* one or more lang - no + because only one lang per post now */
       
   417 			if ( strpos($wp_query->query_vars[QUETAG], ',') !== false ) {
       
   418 				$langs = preg_split('/[,\s]+/', $wp_query->query_vars[QUETAG]);
       
   419 				foreach ( (array) $langs as $lang ) {
       
   420 					$lang = sanitize_term_field('slug', $lang, 0, 'post_tag', 'db');
       
   421 					$reqtags[]= $lang;
       
   422 				}
       
   423 				
       
   424 				foreach ($reqtags as $reqtag){
       
   425 					$reqtagt = is_term( $reqtag, TAXONAME );
       
   426 					if ($reqtagt)
       
   427 						$thereqtags[] = $reqtagt['term_id']; 
       
   428 				}
       
   429 				
       
   430 				$wherereqtag = implode(", ", $thereqtags);
       
   431 				$where .= " AND tt.taxonomy = '".TAXONAME."' ";
       
   432 				$where .= " AND tt.term_id IN ( $wherereqtag )";
       
   433 			
       
   434 			} else {
       
   435 			/* only one lang */
       
   436 				$wp_query->query_vars[QUETAG] = sanitize_term_field('slug', $wp_query->query_vars[QUETAG], 0, 'post_tag', 'db');
       
   437 				$reqtag = $wp_query->query_vars[QUETAG];
       
   438 				$reqtag = is_term( $reqtag, TAXONAME );
       
   439 				if (''!= $reqtag) {
       
   440 					$wherereqtag = $reqtag['term_id'];
       
   441 				} else {
       
   442 					$wherereqtag = 0;	
       
   443 				}
       
   444 				$where .= " AND tt.taxonomy = '".TAXONAME."' ";
       
   445 				$where .= " AND tt.term_id = $wherereqtag ";
       
   446 			}
       
   447 					
       
   448 		}	
       
   449 		return $where;
       
   450 	}
       
   451 
       
   452 	/******** template theme live modifications ********/
       
   453 	
       
   454 	/**
       
   455 	 * wp action for theme at end of query  
       
   456 	 *
       
   457 	 * @since 0.9.0
       
   458 	 * @updated 1.1.9
       
   459 	 * can be hooked in functions.php xiliml_cur_lang_head
       
   460 	 * call by wp hook	 
       
   461 	 *   
       
   462 	 */
       
   463 	function xiliml_language_wp() {
       
   464 		$this->curlang = $this->xiliml_cur_lang_head();
       
   465 		$this->curlang_dir = $this->get_dir_of_cur_language($this->curlang); /* general dir of the theme */
       
   466 		if (!defined('THEME_TEXTDOMAIN')) _e('xili-language plugin : THEME_TEXTDOMAIN UNDEFINED','xili-language'); /* here because not visible in admin UI */
       
   467 		
       
   468 		if ($this->locale_method) {
       
   469 			$this->xiliml_load_theme_textdomain (THEME_TEXTDOMAIN); /* new method for cache compatibility - tests */
       
   470 		} else {
       
   471 			$this->set_mofile($this->curlang);
       
   472 		}
       
   473 	}
       
   474 	
       
   475 	/**
       
   476 	 * locale hook when load_theme_textdomain is present in functions.php
       
   477 	 *
       
   478 	 * @since 1.1.9
       
   479 	 *
       
   480 	 * call by locale hook
       
   481 	 */
       
   482 	function xiliml_setlocale ($locale) {
       
   483 		if ($this->theme_locale === true) {
       
   484 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false,'slug' => $this->curlang));
       
   485 			return $listlanguages[0]->name;
       
   486 		} else {
       
   487 			return $locale; 	
       
   488 		}		
       
   489 	}
       
   490 	
       
   491 	/**
       
   492 	 * locale hook when load_theme_textdomain is present in functions.php
       
   493 	 *
       
   494 	 * @since 1.1.9
       
   495 	 *
       
   496 	 * call by locale hook
       
   497 	 */
       
   498 	function xiliml_load_theme_textdomain ($domain) {
       
   499 		$this->theme_locale = true;
       
   500 		$langfolder = (defined('THEME_LANGS_FOLDER')) ? THEME_LANGS_FOLDER : $this->xili_settings['langs_folder'];
       
   501 		$langfolder = '/'.str_replace("/","",$langfolder); /* please no lang folder in sub-subfolder */
       
   502 		$langfolder = ($langfolder == "/") ? "" : $langfolder;
       
   503 		load_theme_textdomain($domain, get_template_directory() . $langfolder);
       
   504 		$this->theme_locale = false;		
       
   505 	}
       
   506 	
       
   507 	/**
       
   508 	 * select .mo file 
       
   509 	 * @since 0.9.0
       
   510 	 * @updated 0.9.7.1 - 1.1.9
       
   511 	 * call by function xiliml_language_wp()
       
   512 	 * @param $curlang .
       
   513 	 */
       
   514 	function set_mofile($curlang) {
       
   515 		// load_theme_textdomain(THEME_TEXTDOMAIN); - replaced to be flexible -
       
   516 		if (defined('THEME_TEXTDOMAIN')) {$themetextdomain = THEME_TEXTDOMAIN; } else {$themetextdomain = 'ttd-not-defined';  }
       
   517 		$langfolder = (defined('THEME_LANGS_FOLDER')) ? THEME_LANGS_FOLDER : $this->xili_settings['langs_folder'];
       
   518 		$langfolder = '/'.str_replace("/","",$langfolder); /* please no lang folder in sub-subfolder */
       
   519 		$langfolder = ($langfolder == "/") ? "" : $langfolder;
       
   520 		$listlanguages = get_terms(TAXONAME, array('hide_empty' => false,'slug' => $curlang));
       
   521 		$filename = $listlanguages[0]->name;
       
   522 		$filename .= '.mo';
       
   523 		if ('' != $filename) {
       
   524 			$mofile = get_template_directory() .$langfolder."/$filename";	
       
   525 			load_textdomain($themetextdomain,$mofile);
       
   526 		}
       
   527 	}
       
   528 	
       
   529 	/**
       
   530 	 * default rules - set curlang in head according rules 
       
   531 	 *
       
   532 	 * @since 0.9.7
       
   533 	 * @updated 0.9.7.1 - if no posts 0.9.9.1 - 0.9.9.4 
       
   534 	 * @updated 1.3.2 - Option for home.php
       
   535 	 * 
       
   536 	 * default filter of xiliml_cur_lang_head
       
   537 	 * @return $curlang .
       
   538 	 */
       
   539 	function xiliml_cur_lang_head () {
       
   540 		if (has_filter('xiliml_cur_lang_head')) return apply_filters('xiliml_cur_lang_head',''); /* '' warning on some server need one arg by default*/
       
   541 		/* default */
       
   542 		global $post,$wp_query, $query_string;
       
   543 			if (have_posts()) {
       
   544 				if(!is_front_page()) { /* every pages */
       
   545 					$curlangdir = $this->get_cur_language($post->ID);
       
   546 					$curlang = $curlangdir['lang']; /* the first post give the current lang*/
       
   547 					if ($curlangdir == false) $curlang = DEFAULTSLUG; /* can be changed if use hook */
       
   548 					if (is_page()) {
       
   549 						if (isset($_GET["loclang"])) {
       
   550 			    			$curlang=$_GET["loclang"];
       
   551 			    		/* get var to override the selected lang - ex. in bi-lingual contact*/
       
   552 						}
       
   553 					} 
       
   554 					elseif (is_search() && isset($_GET["lang"])) {
       
   555 						$curlang=$_GET["lang"]; /*useful when no result*/
       
   556 					}
       
   557 				} else { /* front page */
       
   558 					if ( '' != $wp_query->query_vars[QUETAG] ) {
       
   559 						$curlang = $wp_query->query_vars[QUETAG];	/* home series type*/
       
   560 					} else {
       
   561 						$showpage = get_settings('show_on_front');
       
   562 						$page_front = get_settings('page_on_front');
       
   563 						$hcurlang = (isset($_GET["hlang"])) ? $_GET["hlang"] : $this->choice_of_browsing_language() ; 
       
   564 						$target = get_post_meta($page_front, 'lang-'.$hcurlang, true);
       
   565 						if ($showpage == "page") {
       
   566 							if ($target && $target != $post->ID) { /* only if present and diff */
       
   567 								query_posts('page_id='.$target); 
       
   568 								if (have_posts()) {
       
   569 									the_post();
       
   570 									$curlang = get_cur_language($post->ID);
       
   571 									rewind_posts();
       
   572 								} else {
       
   573 									query_posts('page_id='.$page_front); /* restore */
       
   574 									$curlang = get_cur_language($page_front);
       
   575 								}
       
   576 							} else {
       
   577 								$curlang = get_cur_language($post->ID);	
       
   578 							}
       
   579 						} else { /* home.php - 1.3.2 */
       
   580 							$curlang = $this->choice_of_browsing_language();
       
   581 							if ($this->xili_settings['homelang'] == 'modify') query_posts($query_string."&lang=".$curlang);
       
   582 						}	
       
   583 					}	
       
   584 				}
       
   585 			} else { /*no posts for instance in category + lang */
       
   586 			 	if (isset($_GET["lang"])) {
       
   587 			    		$curlang=$_GET["lang"];
       
   588 			    } else {
       
   589 			    		$curlang = $this->choice_of_browsing_language();//strtolower(WPLANG); /* select here the default language of the site */
       
   590 			   	}
       
   591 			}	
       
   592 			return $curlang; /* as in external hook for filter*/
       
   593 	}
       
   594 	
       
   595 	/**
       
   596 	 * modify  language_attributes() output
       
   597 	 *
       
   598 	 * @since 0.9.7.6
       
   599 	 *  
       
   600 	 * The - language_attributes() -  template tag is use in header of theme file in html tag 
       
   601 	 *   
       
   602 	 * @param $output
       
   603 	 */
       
   604 	function head_language_attributes($output) {
       
   605 		/* hook head_language_attributes */
       
   606 		if (has_filter('head_language_attributes')) return apply_filters('head_language_attributes',$output);
       
   607 		$attributes = array();
       
   608 		$output = '';
       
   609 
       
   610 		if ( $dir = get_bloginfo('text_direction') ) /*use hook for future use */
       
   611 			$attributes[] = "dir=\"$dir\"";
       
   612 		if ($this->langstate == true) {	
       
   613 			$lang = str_replace('_','-',substr($this->curlang,0,3).strtoupper(substr($this->curlang,-2)));
       
   614 		} else {
       
   615 			//use hook if you decide to display limited list of languages for use by instance in frontpage 
       
   616 			$listlang = array();
       
   617 			//$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   618 			$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
   619 			foreach ($listlanguages as $language) {
       
   620 				$listlang[] = str_replace('_','-',$language->name);
       
   621 			}	
       
   622 			$lang = $listlang[0]; // implode(', ',$listlang); // not w3c compatible
       
   623 		}
       
   624 		if ( get_option('html_type') == 'text/html')
       
   625 				$attributes[] = "lang=\"$lang\"";
       
   626 	
       
   627 		if ( get_option('html_type') != 'text/html')
       
   628 			$attributes[] = "xml:lang=\"$lang\"";	
       
   629 
       
   630 		$output = implode(' ', $attributes);
       
   631 		return $output;
       
   632 	}
       
   633 	
       
   634 	/**
       
   635 	 * modify  insert language metas in head (via wp_head)
       
   636 	 *
       
   637 	 * @since 0.9.7.6
       
   638 	 * @updated 1.1.8 
       
   639 	 * @must be defined in functions.php according general theme design (wp_head) 
       
   640 	 *   
       
   641 	 * @param $curlang
       
   642 	 */
       
   643 	function head_insert_language_metas($curlang,$undefined=true) {
       
   644 		$curlang = $this->curlang;
       
   645 		$undefined = $this->langstate;
       
   646 		echo "<!-- multilingual website powered with xili-language v. ".XILILANGUAGE_VER." WP plugin of dev.xiligroup.com -->\n";
       
   647 			if (has_filter('head_insert_language_metas')) return apply_filters('head_insert_language_metas',$curlang,$undefined);
       
   648 	}
       
   649 	
       
   650 	/**
       
   651 	 * Translate texts of widgets  
       
   652 	 *
       
   653 	 * @since 0.9.8.1
       
   654 	 * @ return
       
   655 	 */
       
   656 	function widget_texts ($value){
       
   657 		return __($value,THEME_TEXTDOMAIN);
       
   658 	}
       
   659 			
       
   660 	/**
       
   661 	 * insert other language of wp_list_categories 
       
   662 	 *
       
   663 	 * @since 0.9.0
       
   664 	 * @updated 0.9.8.4 - 1.4.1 = no original term in ()
       
   665 	 * can be hooked by filter add_filter('xiliml_cat_language','yourfunction',2,3) in functions.php
       
   666 	 * call by do_filter list_cats 
       
   667 	 * @param $content, $category
       
   668 	 */
       
   669 	function xiliml_cat_language ($content, $category = null) {
       
   670 		if (has_filter('xiliml_cat_language')) return apply_filters('xiliml_cat_language',$content, $category,$this->curlang);
       
   671 	    $new_cat_name = (!is_admin()) ? __($category->name,THEME_TEXTDOMAIN) : $content ;  /*to detect admin UI*/
       
   672 	    return $new_cat_name;
       
   673 	 } 
       
   674 	
       
   675 	/**
       
   676 	 * add the language key in category links of current pages
       
   677 	 *
       
   678 	 * @since 0.9.0
       
   679 	 * update 0.9.7
       
   680 	 * can be hooked by filter add_filter('xiliml_link_append_lang','yourfunction',10,2) in functions.php
       
   681 	 * call by do_filter 
       
   682 	 * @param $content,
       
   683 	 */
       
   684 	function xiliml_link_append_lang( $link ) {
       
   685 		if (has_filter('xiliml_link_append_lang')) return apply_filters('xiliml_link_append_lang',$link,$this->curlang);
       
   686 		/*default*/
       
   687 	  		if ($this->curlang) :
       
   688 	  			$link .= '&amp;'.QUETAG.'='.$this->curlang ;
       
   689 	  		endif;
       
   690 	  	
       
   691 	  return $link;
       
   692 	}
       
   693 	
       
   694 	/**
       
   695 	 * Setup global post data.
       
   696 	 *
       
   697 	 * @since 0.9.4
       
   698 	 * update 0.9.7
       
   699 	 * can be hooked by filter add_filter('xiliml_taglink_append_lang','yourfunction',2,3) in functions.php
       
   700 	 *
       
   701 	 * @param $taglink, $tag_id.
       
   702 	 * @return $taglink.
       
   703 	 */
       
   704 	function xiliml_taglink_append_lang ( $taglink, $tag_id=null ) {
       
   705 		if (has_filter('xiliml_taglink_append_lang')) return apply_filters('xiliml_taglink_append_lang',$taglink,$tag_id,$this->curlang);
       
   706 		/* no yet default */
       
   707 		/* global $curlang; 
       
   708 		
       
   709 	  		if ($curlang) :
       
   710 	  			$taglink .= '&amp;'.QUETAG.'='.$curlang ;
       
   711 	  		endif;
       
   712 	  	
       
   713 	  	*/
       
   714 	 return $taglink;
       
   715 	} 
       
   716 	 
       
   717 	/**
       
   718 	 * to cancel sub select by lang in cat 1 by default 
       
   719 	 *
       
   720 	 * @since 0.9.2
       
   721 	 * update 0.9.7
       
   722 	 * can be hooked by filter add_filter('xiliml_modify_querytag','yourfunction') in functions.php
       
   723 	 *
       
   724 	 *
       
   725 	 */
       
   726 	function xiliml_modify_querytag() {
       
   727 		if (has_filter('xiliml_modify_querytag')) {
       
   728 			apply_filters('xiliml_modify_querytag','');
       
   729 		} else {	
       
   730 		/*default*/
       
   731 		global $wp_query;
       
   732 		if (!defined('XILI_CATS_ALL')) define('XILI_CATS_ALL','1'); /* change in functions.php or use hook in cat 1 by default*/
       
   733 			$excludecats = explode(",", XILI_CATS_ALL);
       
   734 			if (!empty($wp_query->query_vars['cat'])) {
       
   735 				if 	(in_array($wp_query->query_vars['cat'],$excludecats)) {
       
   736 					$wp_query->query_vars[QUETAG] = "";	/* to cancel sub select */
       
   737 				}
       
   738 			}
       
   739 		}	
       
   740 	}	
       
   741 	/**
       
   742 	 * filters for wp_get_archives 
       
   743 	 *
       
   744 	 * @since 0.9.2
       
   745 	 * @params $join or $where and template params
       
   746 	 *
       
   747 	 */
       
   748 	function xiliml_getarchives_join($join,$r) {
       
   749 		global $wpdb;
       
   750 		if (has_filter('xiliml_getarchives_join')) return apply_filters('xiliml_getarchives_join',$join,$r,$this->curlang);
       
   751 		extract( $r, EXTR_SKIP );
       
   752 		$this->get_archives_called = $r;
       
   753 		if (isset($lang)) {
       
   754 			if ("" == $lang ) { /* used for link */
       
   755 				$this->get_archives_called['lang'] = $this->curlang;
       
   756 			} else {
       
   757 				$this->get_archives_called['lang'] = $lang;
       
   758 			}
       
   759 			$join = " INNER JOIN $wpdb->term_relationships as tr ON ($wpdb->posts.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) ";
       
   760 		
       
   761 		}
       
   762 		return $join;
       
   763 		
       
   764 	}
       
   765 	
       
   766 	function xiliml_getarchives_where($where,$r) {
       
   767 		global $wpdb;
       
   768 		if (has_filter('xiliml_getarchives_where')) return apply_filters('xiliml_getarchives_where',$where,$r,$this->curlang);
       
   769 		extract( $r, EXTR_SKIP );
       
   770 		if (isset($lang)) {
       
   771 			if ("" == $lang ) {
       
   772 				$curlang = $this->curlang;
       
   773 			} else {
       
   774 				$curlang = $lang;
       
   775 			}
       
   776 			$reqtag = is_term( $curlang, TAXONAME );
       
   777 				if (''!= $reqtag) {
       
   778 					$wherereqtag = $reqtag['term_id'];
       
   779 				} else {
       
   780 					$wherereqtag = 0;	
       
   781 				}
       
   782 				$where .= " AND tt.taxonomy = '".TAXONAME."' ";
       
   783 				$where .= " AND tt.term_id = $wherereqtag ";
       
   784 		}		
       
   785 		return $where;
       
   786 	}
       
   787 	
       
   788 	/* here basic translation - to improve depending theme features : use hook 'xiliml_get_archives_link' */
       
   789 	function xiliml_get_archives_link($link_html) {
       
   790 		if (has_filter('xiliml_link_translate_desc')) return apply_filters('xiliml_get_archives_link', $link_html,$this->get_archives_called, $this->curlang);
       
   791 		extract( $this->get_archives_called, EXTR_SKIP );
       
   792 		if ('' != $lang) {
       
   793 			$permalink = get_option('permalink_structure');
       
   794 			$sep = ('' == $permalink) ? "&amp;lang=" : "?lang=";
       
   795 			if ($format != 'option' && $format != 'link' && $type != 'postbypost' && $type != 'alpha') {
       
   796 				/* text extract */
       
   797 				$i = preg_match_all("/'>(.*)<\/a>/Ui", $link_html, $matches,PREG_PATTERN_ORDER);
       
   798 				$line = $matches[1][0];
       
   799 				/* link extract */
       
   800 				$i = preg_match_all("/href='(.*)' title/Ui", $link_html, $matches,PREG_PATTERN_ORDER);
       
   801 				if ( '' == $type || 'monthly' == $type) {
       
   802 					if ('' == $permalink) {
       
   803 						$archivedate = str_replace(get_bloginfo('siteurl').'/?' , "" , $matches[1][0]);
       
   804 						$r = wp_parse_args( $archivedate, array());
       
   805 						extract($r, EXTR_SKIP );
       
   806 						$month = substr($m,-2);
       
   807 						$year = substr($m,0,4);
       
   808 					} else {
       
   809 						/* Due to prevents post ID and date permalinks from overlapping using /date/ v 1.1.9 
       
   810 						 * no / at end for "numeric" permalink giving /archives/date/2009/04
       
   811 						 */
       
   812 						$thelink = $matches[1][0];
       
   813 						$i = preg_match_all("/\/([0-9]{4})\/([0-9]{2})/Ui", $thelink, $results,PREG_PATTERN_ORDER);
       
   814 						if ($i) { //print_r($results);
       
   815 							$month = $results[2][0];
       
   816 							$year = $results[1][0];
       
   817 						}
       
   818 					}	
       
   819 					$time = strtotime($month.'/1/'.$year);
       
   820 					$line2print = the_xili_local_time('%B %Y',$time); /* use server local*/
       
   821 					$link_html = str_replace($line , $line2print , $link_html);
       
   822 				}	
       
   823 				$link_html = str_replace("' titl" , $sep.$lang."' titl" , $link_html);	
       
   824 			} elseif ($format == 'option') {
       
   825 				/* need improve with regex */
       
   826 				$link_html = str_replace("'>" , $sep.$lang."'>" , $link_html);
       
   827 			}
       
   828 		}	
       
   829 		return $link_html;
       
   830 	}
       
   831  
       
   832 	 /**
       
   833 	 * translate description of categories
       
   834 	 *
       
   835 	 * @since 0.9.0
       
   836 	 * update 0.9.7 - 0.9.9.4
       
   837 	 * can be hooked by filter add_filter('xiliml_link_translate_desc','yourfunction',2,4) in functions.php
       
   838 	 *
       
   839 	 *
       
   840 	 */
       
   841 	function xiliml_link_translate_desc( $description, $category=null,$context='') {
       
   842 		if (has_filter('xiliml_link_translate_desc')) return apply_filters('xiliml_link_translate_desc',$description,$category,$context,$this->curlang);
       
   843 	  	$translated_desc = ($this->curlang && ''!= $description) ? __($description,THEME_TEXTDOMAIN) : $description ;
       
   844 	 	return $translated_desc;
       
   845 	}
       
   846 	
       
   847 	/**
       
   848 	 * filters for wp_title() translation - single_cat_title - 
       
   849 	 * since 1.4.1
       
   850 	 *
       
   851 	 */
       
   852 	function xiliml_single_cat_title_translate ($cat_name) {
       
   853 		if (has_filter('xiliml_single_cat_title_translate')) return apply_filters('xiliml_single_cat_title_translate',$cat_name);
       
   854 		$translated = ($this->curlang && ''!= $cat_name) ? __($cat_name,THEME_TEXTDOMAIN) : $cat_name;
       
   855 	 	return $translated;	
       
   856 	}
       
   857 		
       
   858 	/**
       
   859 	 * Return the list of preferred languages for displaying pages (see in firefox prefs)
       
   860 	 * thanks to php.net comments HTTP_ACCEPT_LANGUAGE
       
   861 	 * @since 0.9.7.5
       
   862 	 * 
       
   863 	 * @return array (non sorted)
       
   864 	 */
       
   865 	function the_preferred_languages() {	 
       
   866 		$preferred_languages = array();
       
   867 			if(preg_match_all("#([^;,]+)(;[^,0-9]*([0-9\.]+)[^,]*)?#i",$_SERVER["HTTP_ACCEPT_LANGUAGE"], $matches, PREG_SET_ORDER)) {
       
   868 				foreach($matches as $match) {
       
   869 					$preferred_languages[$match[1]] = floatval($match[3]);
       
   870 					if($match[3]==NULL) $preferred_languages[$match[1]] = 1.0;
       
   871 				}
       
   872 				return $preferred_languages;	
       
   873 			} else {
       
   874 				return false;	
       
   875 			}	
       
   876 	}
       
   877 	/**
       
   878 	 * Return the lang defined by admin UI if no browser
       
   879 	 *
       
   880 	 * @since 1.0
       
   881 	 *
       
   882 	 */	
       
   883 	function choice_of_home_selected_lang() {
       
   884 		if ($this->browseroption == 'browser') {
       
   885 			return choice_of_browsing_language();
       
   886 		} elseif ($this->browseroption != '') { /* slug of the lang*/
       
   887 			return $this->browseroption;
       
   888 		} else {	
       
   889 	 		return strtolower($this->default_lang);
       
   890 		}
       
   891 	}
       
   892 	
       
   893 	/**
       
   894 	 * Return the list of preferred languages for displaying pages (see in firefox prefs)
       
   895 	 * thanks to php.net comments HTTP_ACCEPT_LANGUAGE
       
   896 	 * @since 0.9.7.5
       
   897 	 * @update 0.9.9.4
       
   898 	 * @return array (non sorted)
       
   899 	 */
       
   900 	function choice_of_browsing_language() {
       
   901 		if (has_filter('choice_of_browsing_language')) return apply_filters('choice_of_browsing_language');
       
   902 		if ($this->browseroption != 'browser') return $this->choice_of_home_selected_lang(); /* in settings UI - after filter to hook w/o UI */
       
   903 		$listofprefs = $this->the_preferred_languages();
       
   904 		if (is_array($listofprefs)) {
       
   905 			arsort($listofprefs, SORT_NUMERIC);
       
   906 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   907 			$sitelanguage = $this->match_languages ($listofprefs,$listlanguages);
       
   908 			if ($sitelanguage) return $sitelanguage->slug;
       
   909 			return strtolower($this->default_lang);
       
   910 		} else {
       
   911 			return strtolower($this->default_lang);
       
   912 		}
       
   913 	}
       
   914 	
       
   915 	function match_languages ($listofprefs,$listlanguages) {
       
   916 		
       
   917 			foreach($listofprefs as $browserlanguage => $priority) {
       
   918 					/* match root languages to give similar in site  - first : five chars langs*/
       
   919 					foreach($listlanguages as $sitelanguage) {
       
   920 						if ($sitelanguage->slug == str_replace('-','_',$browserlanguage)) return $sitelanguage;
       
   921 					}
       
   922 			}
       
   923 			foreach($listofprefs as $browserlanguage => $priority) {
       
   924 					/* match root languages to give similar in site  - second : two first chars langs*/
       
   925 					foreach($listlanguages as $sitelanguage) {
       
   926 						if (str_replace('-','_',$browserlanguage) == substr($sitelanguage->slug,0,2)) return $sitelanguage;
       
   927 					}	
       
   928 			}
       
   929 	}
       
   930 	
       
   931 	/********************************** ADMIN UI ***********************************/
       
   932 			
       
   933 	/**
       
   934 	 * add admin menu and associated pages of admin UI
       
   935 	 *
       
   936 	 * @since 0.9.0
       
   937 	 * @updated 0.9.6 - only for WP 2.7.X - do registering of new meta boxes and JS
       
   938 	 *
       
   939 	 */
       
   940 	function xili_add_pages() {
       
   941 		 $this->thehook = add_options_page(__('Languages','xili-language'), __('Languages','xili-language'), 'manage_options', 'language_page', array(&$this,'languages_settings'));
       
   942 		 add_action('load-'.$this->thehook, array(&$this,'on_load_page'));
       
   943 	}
       
   944 	
       
   945 	function on_load_page() {
       
   946 			wp_enqueue_script('common');
       
   947 			wp_enqueue_script('wp-lists');
       
   948 			wp_enqueue_script('postbox');
       
   949 			add_meta_box('xili-language-sidebox-1', __('Message','xili-language'), array(&$this,'on_sidebox_1_content'), $this->thehook , 'side', 'core');
       
   950 			add_meta_box('xili-language-sidebox-2', __('Info','xili-language'), array(&$this,'on_sidebox_2_content'), $this->thehook , 'side', 'core');
       
   951 			add_meta_box('xili-language-sidebox-4', __('Special','xili-language'), array(&$this,'on_sidebox_4_content'), $this->thehook , 'side', 'core');
       
   952 			
       
   953 	}
       
   954 	
       
   955 	/**
       
   956 	 * Add action link(s) to plugins page
       
   957 	 * 
       
   958 	 * @since 0.9.3
       
   959 	 * @author MS
       
   960 	 * @copyright Dion Hulse, http://dd32.id.au/wordpress-plugins/?configure-link and scripts@schloebe.de
       
   961 	 */
       
   962 	function xililang_filter_plugin_actions($links, $file){
       
   963 		static $this_plugin;
       
   964 		if( !$this_plugin ) $this_plugin = plugin_basename(__FILE__);
       
   965 		if( $file == $this_plugin ){
       
   966 			$settings_link = '<a href="options-general.php?page=language_page">' . __('Settings') . '</a>';
       
   967 			$links = array_merge( array($settings_link), $links); // before other links
       
   968 		}
       
   969 		return $links;
       
   970 	}
       
   971 	
       
   972 	/* UI added in sidebar of post admin (write , edit)
       
   973 	 *
       
   974 	 * @since 0.9.0
       
   975 	 * @updated 0.9.5 : add a no-lang radio - again in top of sidebar admin post's UI
       
   976 	 * @updated 0.9.8.3 : if new post and checked in settings : default language = author's browser's language !
       
   977 	 * @updated 1.3.0
       
   978 	 */
       
   979 	function xili_language_checkboxes_n() { 
       
   980 		global $post_ID ;
       
   981 		//$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
   982 		$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
   983 		if ($this->authorbrowseroption == 'authorbrowser') { // setting = select language of author's browser
       
   984 			$listofprefs = $this->the_preferred_languages();
       
   985 			if (is_array($listofprefs)) {
       
   986 				arsort($listofprefs, SORT_NUMERIC);
       
   987 				$sitelanguage = $this->match_languages ($listofprefs,$listlanguages);
       
   988 				if ($sitelanguage) {
       
   989 					$defaultlanguage = $sitelanguage->name;
       
   990 				} else {
       
   991 					$defaultlanguage = "";
       
   992 				}	
       
   993 				$mention = '('.__('Browser language', 'xili-language').')';
       
   994 			} else {
       
   995 				$defaultlanguage = ""; /* undefined */
       
   996 			}	
       
   997 		} else {
       
   998 			$defaultlanguage = ""; /* undefined */
       
   999 			$mention = "";
       
  1000 		}	
       
  1001 		
       
  1002 		if(0 != $post_ID){
       
  1003 			$ress = wp_get_object_terms($post_ID, TAXONAME);
       
  1004 			
       
  1005 			/*Array ( [0] => stdClass Object ( [term_id] => 18 [name] => [slug] => 18 [term_group] => 0 [term_taxonomy_id] => 19 [taxonomy] => language [description] => [parent] => 0 [count] => 1 ) )*/
       
  1006 			$obj_term = $ress[0];
       
  1007 			if ('' != $obj_term->name) :
       
  1008 				$curlangname = $obj_term->name;
       
  1009 			else :
       
  1010 				$curlangname = ""; /* when created before plugin */
       
  1011 			endif;
       
  1012 			
       
  1013 		} else {
       
  1014 			if (isset($_GET['xltgt_lang'])) {
       
  1015 				$curlangname = $_GET['xltgt_lang']; /* since 1.3.0 */
       
  1016 				$mention = '<br />('.__('From other post', 'xili-language').': '.$_GET['xlfrom_id'].'&nbsp;'.$_GET['xlfrom_lang'].')';
       
  1017 			} else {
       
  1018 				$curlangname = $defaultlanguage; /* new post */
       
  1019 			}
       
  1020 		}
       
  1021 		echo __('Selected language', 'xili-language').' : <strong>'.$curlangname.'</strong> '.((0 == $post_ID) ? $mention : "").'<br /><br />' ; /*link to bottom of sidebar*/
       
  1022 		foreach ($listlanguages as $language) { ?> 
       
  1023 		<label for="xili_language_check_<?php echo $language->slug ; ?>" class="selectit"><input id="xili_language_check_<?php echo $language->slug ; ?>" name="xili_language_set" type="radio" value="<?php echo $language->slug ; ?>"  <?php if($curlangname==$language->name) echo 'checked="checked"' ?> /> <?php echo _e($language->description, 'xili-language'); ?></label>
       
  1024 	  
       
  1025 		<?php } /*link to top of sidebar*/?> 
       
  1026 		<label for="xili_language_check" class="selectit"><input id="xili_language_check" name="xili_language_set" type="radio" value="" <?php if($curlangname=="") echo 'checked="checked"' ?> /> <?php _e('undefined','xili-language') ?></label><br />
       
  1027 	  	<br /><small>© xili-language</small>
       
  1028 	<?php 
       
  1029 	}
       
  1030 	
       
  1031 	/**
       
  1032 	 * to display the linked posts in post edit UI
       
  1033 	 *
       
  1034 	 * @since 0.9.8
       
  1035 	 * @updated 1.3.0
       
  1036 	 *
       
  1037 	 */
       
  1038 	function xili_language_linked_posts() { 
       
  1039 		global $post_ID, $post;
       
  1040 		$update_nonce = wp_create_nonce('oklinked');
       
  1041 		$postlang = '';
       
  1042 		if(0 != $post_ID){
       
  1043 			$ress = wp_get_object_terms($post_ID, TAXONAME);
       
  1044 			$obj_term = $ress[0];
       
  1045 			$postlang = ('' != $obj_term->slug) ? $obj_term->slug : ""; /* when created before plugin */
       
  1046 		} else {
       
  1047 			$postlang = ""; /* new post */
       
  1048 		}
       
  1049 		//$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
  1050 		$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1051 		if ($post->post_type == 'post') { ?>
       
  1052 			<p><em><?php _e('ID of posts in other languages:','xili-language'); ?></em></p>
       
  1053 		<?php $theid = 'Post ID';
       
  1054 			$autosearchmess = __('to auto search linked posts. (read docs)','xili-language');
       
  1055 			$post_type = 'post';
       
  1056 		} else {
       
  1057 		?>	<p><em><?php _e('ID of pages in other languages:','xili-language'); ?></em></p>
       
  1058 		<?php $theid = 'Page ID';
       
  1059 			$autosearchmess = __('to auto search linked pages. (read docs)','xili-language');
       
  1060 			$post_type = 'page'; 
       
  1061 		}
       
  1062 		?>
       
  1063 		<table width="100%" cellspacing="4" cellpadding="2">
       
  1064 		<thead>
       
  1065 		<tr ><th><?php _e('Language','xili-language'); ?></th><th align="left"><?php _e($theid,'xili-language'); ?></th><th align="left"><?php _e('Display','xili-language'); ?></th><th align="left"><?php _e('Edit'); ?></th></tr>
       
  1066 		</thead>
       
  1067 		<tbody id='the-linked' class='list:linked'>
       
  1068 			<?php
       
  1069 			if (0 != $post_ID) $autosearch = get_post_meta($post_ID,'_xl-search-linked',true);
       
  1070 			foreach ($listlanguages as $language) {
       
  1071 				$output = "";
       
  1072 				$otherpost = "";
       
  1073 				$line = true;
       
  1074 				if ($language->slug == $postlang ) {
       
  1075 					if ($post->post_status == 'publish' || $post->post_status == 'pending') {
       
  1076 						$line = false;
       
  1077 					} else {
       
  1078 						$line = true;
       
  1079 					}	
       
  1080 				} 
       
  1081 				if ($line) {
       
  1082 					if (0 != $post_ID) {
       
  1083 						$otherpost = get_post_meta($post_ID, QUETAG.'-'.$language->slug, true);
       
  1084 						$otherpostr = $otherpost; 
       
  1085 						/* since 1.3.0 - 1.4.1 */
       
  1086 						if ('' != $autosearch && "" != $postlang) { 
       
  1087 							$source_ID = $this->search_pinger_post($post_ID,QUETAG.'-'.$postlang,$language->term_id, $post_type);
       
  1088 						 	if (0 != $source_ID) { 
       
  1089 						 		$otherpost = $source_ID;
       
  1090 						 		$otherpostr = "-1"; /* to be refreshed */
       
  1091 						 	}	
       
  1092 						}
       
  1093 					} else { /* since 1.3.0 */
       
  1094 						if (isset($_GET['xlfrom_id'])) {
       
  1095 							if ($_GET['xlfrom_lang'] == $language->slug) {
       
  1096 								$otherpost = $_GET['xlfrom_id'];
       
  1097 								$otherpostr = "-1";
       
  1098 							} else {
       
  1099 								/* pre-fill linked posts from source post */
       
  1100 								$otherpost = get_post_meta($_GET['xlfrom_id'], QUETAG.'-'.$language->slug, true);
       
  1101 								$otherpostr = "-1";
       
  1102 							}
       
  1103 						}
       
  1104 					}
       
  1105 				
       
  1106 				?>
       
  1107 				<tr ><th>
       
  1108 				<label for="xili_language_<?php echo QUETAG.'-'.$language->slug ; ?>"><?php _e($language->description,'xili-language') ; ?>&nbsp;</label></th><td align="left"><input id="xili_language_<?php echo QUETAG.'-'.$language->slug ; ?>" name="xili_language_<?php echo QUETAG.'-'.$language->slug ; ?>"  value="<?php echo $otherpost; ?>" size="5" /><input type="hidden" name="xili_language_rec_<?php echo QUETAG.'-'.$language->slug ; ?>" value="<?php echo $otherpostr; ?>"/>
       
  1109 				
       
  1110 				<?php
       
  1111 				if ('' != $otherpost  ) {
       
  1112 					$output = "</td><td><a target='_blank' href='".get_permalink($otherpost)."' >"." ".__($language->description,'xili-language') ."</a></td><td><a target='_blank' href='post.php?action=edit&post=".$otherpost."' >"." ".__('Edit') ."</a></td></tr>";
       
  1113 				} else {
       
  1114 					$output = $this->newlinkedpost($postlang, $language->name); /* if possible */
       
  1115 				}
       
  1116 			}
       
  1117 				echo $output; 
       
  1118 			} 
       
  1119 			/* since 1.4.1 */
       
  1120 			if (0 != $post_ID && "" != $postlang) {
       
  1121 			?>	
       
  1122 			<tr ><th><?php _e('Check','xili-language'); ?></th><td><input id="xili_language_search_lang" name="xili_language_search_lang" type="checkbox" value="searchlinked" <?php if('' != $autosearch) echo 'checked="checked"' ?> /></td><td colspan = 2 ><small><?php echo$autosearchmess ; ?></small></td></tr>
       
  1123 			<?php } ?>
       
  1124 				</tbody></table>
       
  1125 			<br /><small>© xili-language</small>
       
  1126 			<?php if ($this->post_ajax) { ?>
       
  1127 			<div id='formstatus'></div><span id='loading' class='hidden'><?php _e('Saving...','xili-language') ?></span><span id='loading2' class='hidden'><?php _e('Refreshing...','xili-language') ?></span><div class='submit'>
       
  1128 			<input id='updatelink' name='updatelinked' type='submit' tabindex='6' value='Update' /><small>© xili-language</small></div><?php echo wp_nonce_field( 'oklinked', '_ajax_nonce', true, false );/**/ ?><?php /* echo wp_nonce_field( 'customrefresh', '_ajax_nonce', false, false );*/ ?>
       
  1129 			<script  type='text/javascript'>
       
  1130 			<!--
       
  1131 	
       
  1132 	jQuery(document).ready(function(){
       
  1133 		jQuery('#updatelink').click(function() { //start function when Random button is clicked
       
  1134 			jQuery.ajax({
       
  1135 				type: "post",url: "admin-ajax.php",
       
  1136 				data: {
       
  1137 					action: 'oklinked', 
       
  1138 					<?php
       
  1139 					foreach ($listlanguages as $language) {
       
  1140 						echo "xili_language_".$language->slug.": "."escape( jQuery( '#"."xili_language_".QUETAG."-".$language->slug."' ).val()),";	
       
  1141 					}
       
  1142 					echo "post_id: '".$post_ID."',";	
       
  1143 					?>
       
  1144 					_ajax_nonce: '<?php echo $update_nonce; ?>' 
       
  1145 					},
       
  1146 				beforeSend: function() {jQuery("#loading").fadeIn('fast');jQuery("#formstatus").fadeIn("fast");}, //when link is clicked
       
  1147 				success: function(html){ //so, if data is retrieved, store it in html
       
  1148 					jQuery("#loading").fadeOut('slow');
       
  1149 					jQuery("#formstatus").html( html );
       
  1150 					jQuery.ajax({ // refresh custom fields list
       
  1151 						type: "post",url: "admin-ajax.php",
       
  1152 					 	data: {
       
  1153 					 	action: 'customrefresh',
       
  1154 					 	<?php
       
  1155 							echo "post_id: '".$post_ID."',"; ?>
       
  1156 					 	_ajax_nonce: '<?php echo $update_nonce; ?>' 
       
  1157 					 	},
       
  1158 					 	beforeSend: function() {jQuery("#loading2").fadeIn('fast');},
       
  1159 						success: function(html){
       
  1160 					 	jQuery("#the-list").html( html );
       
  1161 					 	jQuery("#loading2").fadeOut('slow');
       
  1162 					 	}		
       
  1163 			 });
       
  1164 				}
       
  1165 			}); //close jQuery.ajax
       
  1166 			return false;
       
  1167 		})
       
  1168 	})
       
  1169 	-->
       
  1170 	</script><?php }
       
  1171 	}
       
  1172 	/**
       
  1173 	 * to create a linked post in target language
       
  1174 	 *
       
  1175 	 * @since 1.3.0
       
  1176 	 * @updated 1.3.1 - Add New specific for pages
       
  1177 	 *
       
  1178 	 */
       
  1179 	function newlinkedpost($postlang = "" , $targetlang = "") {
       
  1180 		global $post;
       
  1181 		$whatnew = ($post->post_type == 'post') ? 'post' : 'page';	
       
  1182 		if ($post->post_status == 'publish' || $post->post_status == 'pending' || $post->post_status == 'draft') {
       
  1183 			if ($postlang != strtolower($targetlang)) {
       
  1184 			return "</td><td><small>".__('*','xili-language')."</small></td><td><a href='".$whatnew."-new.php?xlfrom_id=".$post->ID."&xlfrom_lang=".$postlang."&xltgt_lang=".$targetlang."' target='_blank' >".__('Add New')."</a></td><tr>";
       
  1185 			}
       
  1186 		} else {
       
  1187 			return "</td></tr>";
       
  1188 		}
       
  1189 	}
       
  1190 	
       
  1191 	/**
       
  1192 	 * to research post linking current post in the language
       
  1193 	 * @since 1.4.1
       
  1194 	 * @params ID of post, lang of this post as meta_key, lang of searched pinger post, type of post (page)
       
  1195 	 *
       
  1196 	 */
       
  1197 	function search_pinger_post ($targetID,$langmeta,$pingerlang,$posttype) {
       
  1198 		global $wpdb;
       
  1199 		
       
  1200 		$query = "SELECT ID FROM $wpdb->posts as pp LEFT JOIN $wpdb->postmeta as pm ON (pp.ID = pm.post_id) LEFT JOIN $wpdb->term_relationships as tr ON (pp.ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE 1=1 AND pm.meta_key = '$langmeta' AND pm.meta_value = '$targetID' AND pp.post_type = '$posttype' AND (pp.post_status IN ('publish','private','draft','pending')) AND tt.taxonomy = 'language' AND tt.term_id = $pingerlang ORDER BY pp.post_date DESC LIMIT 0, 1";
       
  1201 		//echo $query;
       
  1202 		$r = @$wpdb->get_col($query);
       
  1203 		//print_r($r);
       
  1204 		if (!empty($r)) {
       
  1205 			$id = $r[0]; /* if multiple take the most recent */
       
  1206 		} else {
       
  1207 		 	$id = 0;	
       
  1208 		}
       
  1209 //echo '->'.$id.$langmeta.$targetID.'</br>'; 
       
  1210 		return $id;
       
  1211 	}
       
  1212 	/* obsolete */
       
  1213 	function custom_refresh() {
       
  1214 		check_ajax_referer( "oklinked" );
       
  1215 		$post_ID = $_POST['post_id'];
       
  1216 		$count = 0;
       
  1217 		$metadata = has_meta($post_ID);
       
  1218 		$list ="";
       
  1219 		$output = '';//<tr><td>Refreshed by xili-language</td></tr>';
       
  1220 		if ($metadata)
       
  1221 			foreach ( $metadata as $entry ) { $list .= _list_meta_row( $entry, $count );}
       
  1222 		$output .= $list;
       
  1223 		echo $output."<!--- end updated by xili-language -->";
       
  1224 		die();
       
  1225 	}
       
  1226 	
       
  1227 	function ok_linked() {
       
  1228 		check_ajax_referer( "oklinked" );
       
  1229 		
       
  1230 		$post_ID = $_POST['post_id'];
       
  1231 				
       
  1232 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
  1233 			$linked = array ();
       
  1234 			foreach ($listlanguages as $language) {
       
  1235 				$key = $language->slug;
       
  1236 				$linked[$key] = $_POST['xili_language_'.$key];
       
  1237 				$linkid = $linked[$key];
       
  1238 				//$reclinkid = $_POST[$recinputid]; /* hidden previous value */
       
  1239 				$langslug = QUETAG.'-'.$key ;
       
  1240 				//if ($reclinkid != $linkid) { /* only if changed value */
       
  1241 					if ((is_numeric($linkid) && $linkid == 0) || '' == $linkid ) {
       
  1242 						delete_post_meta($post_ID, $langslug);
       
  1243 					} elseif (is_numeric($linkid) && $linkid > 0) {
       
  1244 						update_post_meta($post_ID, $langslug, $linkid);
       
  1245 						$mess .= " ".$key;	
       
  1246 					}
       
  1247 			}
       
  1248 			echo '<p>All is OK '.$post_id.' ('.$mess.')</p>'; // voir bannière //
       
  1249 			die();
       
  1250 		
       
  1251 	}
       
  1252 		
       
  1253 	/**
       
  1254 	 * to display the languages settings admin UI
       
  1255 	 *
       
  1256 	 * @since 0.9.0
       
  1257 	 * @updated 0.9.6 - only for WP 2.7.X - do new meta boxes and JS
       
  1258 	 *
       
  1259 	 */
       
  1260 	function languages_settings() { 
       
  1261 		$formtitle = 'Add a language'; /* translated in form */
       
  1262 		$submit_text = __('Add &raquo;','xili-language');
       
  1263 		$cancel_text = __('Cancel');
       
  1264 		
       
  1265 		if (isset($_POST['reset'])) {
       
  1266 			$action=$_POST['reset'];
       
  1267 		} elseif (isset($_POST['updateoptions'])) {
       
  1268 			$action='updateoptions';
       
  1269 		} elseif (isset($_POST['updateundefined'])) {
       
  1270 			$action='updateundefined';
       
  1271 			
       
  1272 		} elseif (isset($_POST['action'])) {
       
  1273 			$action=$_POST['action'];
       
  1274 		}
       
  1275 		
       
  1276 		if (isset($_GET['action'])) :
       
  1277 			$action=$_GET['action'];
       
  1278 			$term_id = $_GET['term_id'];
       
  1279 		endif;
       
  1280 		$message = $action ;
       
  1281 		switch($action) {
       
  1282 			case 'updateundefined';
       
  1283 				$targetlang = $_POST['xili_language_toset'];
       
  1284 				$fromcats = $_POST['from_categories'];
       
  1285 				if (""!= $targetlang) {
       
  1286 					$q = xiliml_setlang_of_undefined_posts ($targetlang, $fromcats, 50);
       
  1287 					$message .= " _ $q ".__('posts are set in:','xili-language')." ".$targetlang." ".__("category")." =[$fromcats]";
       
  1288 				} else {
       
  1289 					$q = xiliml_setlang_of_undefined_posts ($targetlang, $fromcats, 50);
       
  1290 					$message .= " _ around $q ".__('posts are undefined in','xili-language')." ".__("category")."  = [$fromcats]";	
       
  1291 				}
       
  1292 				$actiontype = "reset";
       
  1293 				break;
       
  1294 			case 'updateoptions';
       
  1295 				$this->browseroption = $_POST['xili_language_check_option'];
       
  1296 				$this->authorbrowseroption = $_POST['xili_language_check_option_author'];
       
  1297 				$this->functions_enable = $_POST['xili_language_check_functions_enable'];
       
  1298 				$this->xili_settings['browseroption'] = $this->browseroption;
       
  1299 				$this->xili_settings['authorbrowseroption'] = $this->authorbrowseroption;
       
  1300 				$this->xili_settings['functions_enable'] = $this->functions_enable; 
       
  1301 				
       
  1302 				$this->xili_settings['homelang'] = $_POST['xili_language_home_lang']; // 1.3.2 
       
  1303 				
       
  1304 				update_option('xili_language_settings', $this->xili_settings);
       
  1305 				$message .= " - ".__('Option is updated.','xili-language')." (=> ".$this->browseroption.") (".$this->authorbrowseroption.") (".$this->functions_enable.")";
       
  1306 				$this->insert_gold_functions (); 
       
  1307 				$actiontype = "reset";
       
  1308 				break;
       
  1309 		
       
  1310 			case 'add':
       
  1311 				$term = $_POST['language_name'];
       
  1312 				$args = array( 'alias_of' => '', 'description' => $_POST['language_description'], 'parent' => 0, 'slug' =>$_POST['language_nicename']);
       
  1313 			    $theids = wp_insert_term( $term, TAXONAME, $args);
       
  1314 			    wp_set_object_terms($theids['term_id'], 'the-langs-group', TAXOLANGSGROUP);
       
  1315 			    update_term_order ($theids['term_id'],$this->langs_group_tt_id,$_POST['language_order']);
       
  1316 			    $actiontype = "add";
       
  1317 			    $message .= " - ".__('A new language was added.','xili-language');
       
  1318 			     break;
       
  1319 			    
       
  1320 			case 'edit';
       
  1321 			    $actiontype = "edited";
       
  1322 			    //echo $term_id;
       
  1323 			    //$language = get_term($term_id,TAXONAME,OBJECT,'edit');
       
  1324 			    $language = get_term_and_order ($term_id,$this->langs_group_tt_id,TAXONAME);
       
  1325 			    $submit_text = __('Update &raquo;');
       
  1326 			    $formtitle = 'Edit language';
       
  1327 			    $message .= " - ".__('Language to update.','xili-language');
       
  1328 			    break;
       
  1329 			    
       
  1330 			case 'edited';
       
  1331 			    $actiontype = "add";
       
  1332 			    $term = $_POST['language_term_id'];
       
  1333 			    
       
  1334 				$args = array( 'alias_of' => '', 'description' => $_POST['language_description'], 'parent' => 0, 'slug' =>$_POST['language_nicename']);
       
  1335 				$theids = wp_update_term( $term, TAXONAME, $args);
       
  1336 				wp_set_object_terms($theids['term_id'], 'the-langs-group', TAXOLANGSGROUP);
       
  1337 				update_term_order ($theids['term_id'],$this->langs_group_tt_id,$_POST['language_order']);
       
  1338 				$message .= " - ".__('A language was updated.','xili-language');
       
  1339 				
       
  1340 			    break;
       
  1341 			    
       
  1342 			case 'delete';
       
  1343 			    $actiontype = "deleting";
       
  1344 			    $submit_text = __('Delete &raquo;','xili-language');
       
  1345 			    $formtitle = 'Delete language ?';
       
  1346 			    //$language = get_term($term_id,TAXONAME,OBJECT,'edit');
       
  1347 			    $language = get_term_and_order ($term_id,$this->langs_group_tt_id,TAXONAME);
       
  1348 			    $message .= " - ".__('A language to delete.','xili-language');
       
  1349 			    
       
  1350 			    break;
       
  1351 			    
       
  1352 			case 'deleting';
       
  1353 			    $actiontype = "add";
       
  1354 			    $term = $_POST['language_term_id'];
       
  1355 			    wp_delete_object_term_relationships( $term, TAXOLANGSGROUP );
       
  1356 			    wp_delete_term( $term, TAXONAME, $args);
       
  1357 			    $message .= " - ".__('A language was deleted.','xili-language');
       
  1358 			    break; 
       
  1359 			case 'reset';    
       
  1360 			    $actiontype = "add";
       
  1361 			    break;
       
  1362 			default :
       
  1363 			    $actiontype = "add";
       
  1364 			    $message .= __('Find above the list of languages.','xili-language');
       
  1365 			    
       
  1366 			    
       
  1367 		}
       
  1368 		/* register the main boxes always available */
       
  1369 		add_meta_box('xili-language-normal-1', __('List of languages','xili-language'), array(&$this,'on_normal_1_content'), $this->thehook , 'normal', 'core'); 
       
  1370 		add_meta_box('xili-language-normal-2', __('Language','xili-language'), array(&$this,'on_normal_2_content'), $this->thehook , 'normal', 'core');
       
  1371 		add_meta_box('xili-language-sidebox-3', __('Settings','xili-language'), array(&$this,'on_sidebox_3_content'), $this->thehook , 'side', 'core');
       
  1372 		
       
  1373 		/* form datas in array for do_meta_boxes() */
       
  1374 		$data = array('message'=>$message,'messagepost'=>$messagepost,'action'=>$action, 'formtitle'=>$formtitle, 'language'=>$language,'submit_text'=>$submit_text,'cancel_text'=>$cancel_text,'browseroption'=>$this->browseroption, 'authorbrowseroption'=>$this->authorbrowseroption , 'functions_enable'=>$this->functions_enable);
       
  1375 		?>
       
  1376 		
       
  1377 		<div id="xili-language-settings" class="wrap" style="min-width:750px">
       
  1378 			<?php screen_icon('options-general'); ?>
       
  1379 			<h2><?php _e('Languages','xili-language') ?></h2>
       
  1380 			<form name="add" id="add" method="post" action="options-general.php?page=language_page">
       
  1381 				<input type="hidden" name="action" value="<?php echo $actiontype ?>" />
       
  1382 				<?php wp_nonce_field('xili-language-settings'); ?>
       
  1383 				<?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false ); ?>
       
  1384 				<?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false ); 
       
  1385 				/* 0.9.9.2 add has-right-sidebar for next wp 2.8*/ ?>
       
  1386 				<div id="poststuff" class="metabox-holder has-right-sidebar">
       
  1387 					<div id="side-info-column" class="inner-sidebar">
       
  1388 						<?php do_meta_boxes($this->thehook, 'side', $data); ?>
       
  1389 					</div>
       
  1390 				
       
  1391 					<div id="post-body" class="has-sidebar has-right-sidebar">
       
  1392 						<div id="post-body-content" class="has-sidebar-content" style="min-width:360px">
       
  1393 					
       
  1394 	   					<?php do_meta_boxes($this->thehook, 'normal', $data); ?>
       
  1395 						</div>
       
  1396 					<h4><a href="http://dev.xiligroup.com/xili-language" title="Plugin page and docs" target="_blank" style="text-decoration:none" ><img style="vertical-align:middle" src="<?php echo WP_PLUGIN_URL.'/'.dirname(plugin_basename(__FILE__)).'/xililang-logo-32.gif'; ?>" alt="xili-language logo"/>  xili-language</a> - © <a href="http://dev.xiligroup.com" target="_blank" title="<?php _e('Author'); ?>" >xiligroup.com</a>™ - msc 2007-9 - v. <?php echo XILILANGUAGE_VER; ?></h4>		
       
  1397 					</div>
       
  1398 				</div>
       
  1399 		</form>
       
  1400 		</div>
       
  1401 		<script type="text/javascript">
       
  1402 			//<![CDATA[
       
  1403 			jQuery(document).ready( function($) {
       
  1404 				// close postboxes that should be closed
       
  1405 				$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
       
  1406 				// postboxes setup
       
  1407 				postboxes.add_postbox_toggles('<?php echo $this->thehook; ?>');
       
  1408 			});
       
  1409 			//]]>
       
  1410 		</script> 
       
  1411 	<?php	//end settings div
       
  1412 		}
       
  1413 	
       
  1414 		//* display xililanguage in lists *//
       
  1415 	function xili_manage_column($name, $id) {
       
  1416 			if($name != TAXONAME)
       
  1417 				return;
       
  1418 			$terms = wp_get_object_terms($id, TAXONAME);
       
  1419 			$first = true;
       
  1420 			foreach($terms AS $term) {
       
  1421 				if($first)
       
  1422 					$first = false;
       
  1423 				else
       
  1424 					echo ', ';
       
  1425 				echo '<a href="' . 'options-general.php?page=language_page'.'">'; /* see more precise link ?*/
       
  1426 				echo $term->name;
       
  1427 				echo '</a>';
       
  1428 			}
       
  1429 		}
       
  1430 	function xili_manage_column_name($cols) {
       
  1431 			$ends = array('comments', 'date', 'rel', 'visible');
       
  1432 			$end = array();
       
  1433 			foreach($cols AS $k=>$v) {
       
  1434 				if(in_array($k, $ends)) {
       
  1435 					$end[$k] = $v;
       
  1436 					unset($cols[$k]);
       
  1437 				}
       
  1438 			}
       
  1439 			$cols[TAXONAME] = __('Language','xili-language');
       
  1440 			$cols = array_merge($cols, $end);
       
  1441 			return $cols;
       
  1442 		}
       
  1443 
       
  1444 	/**
       
  1445 	 * Set language plugin 
       
  1446 	 * 
       
  1447 	 *
       
  1448 	 * @updated 1.1.9
       
  1449 	 * also include automatic search of domain and lang subfolder in current theme
       
  1450 	 */
       
  1451 	function init_textdomain() {
       
  1452 	/*multilingual for admin pages and menu*/
       
  1453 		load_plugin_textdomain('xili-language',PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));
       
  1454 		if (!defined('THEME_TEXTDOMAIN')) { 
       
  1455 			if ($this->xili_settings['theme_domain'] != "") {
       
  1456 				define('THEME_TEXTDOMAIN',$this->xili_settings['theme_domain']);
       
  1457 				
       
  1458 			} else { /* search it inside theme folder index.php */
       
  1459 				if( is_file( get_template_directory().'/index.php') )  { 
       
  1460 					$lines = @file( get_template_directory().'/index.php'); 
       
  1461 					foreach ($lines as $line) { 
       
  1462 						$i = preg_match_all("/_[_e]\('(.*)', ?'(.*)'/Ui", $line, $matches,PREG_PATTERN_ORDER);
       
  1463 	 					if ($i > 0) { 
       
  1464 							$resultterms = array_merge ($resultterms, $matches[1]);
       
  1465 							//print_r($matches[1]);
       
  1466 							//print_r($matches[2]);
       
  1467 							$domain = $matches[2][0];
       
  1468 							$this->xili_settings['theme_domain'] = $domain;
       
  1469 							update_option('xili_language_settings', $this->xili_settings);
       
  1470 							define('THEME_TEXTDOMAIN',$this->xili_settings['theme_domain']);
       
  1471 							break; 
       
  1472 						}
       
  1473 			 		}
       
  1474 			 		if ($domain == "")
       
  1475 			 			_e('no theme domain in index.php','xili-language');
       
  1476 				} else {
       
  1477 					_e('no index.php in theme, domain not set','xili-language');
       
  1478 				}
       
  1479 			}	
       
  1480 		}
       
  1481 		if (!defined('THEME_LANGS_FOLDER')) { /* set or detect */
       
  1482 			if ($this->xili_settings['langs_folder'] == "") {
       
  1483 				$this->find_files(get_template_directory(), '/.mo$/', array(&$this,'searchpath'));
       
  1484 				update_option('xili_language_settings', $this->xili_settings);
       
  1485 			}
       
  1486 			
       
  1487 			define('THEME_LANGS_FOLDER',$this->xili_settings['langs_folder']); // for bkwd compatibility with xili-dictionary
       
  1488 		}
       
  1489 	}
       
  1490 	function searchpath($path, $filename) {
       
  1491 		$this->xili_settings['langs_folder'] = str_replace(get_template_directory(),'',$path);
       
  1492 	}
       
  1493 	/**
       
  1494 	 * Reset values when theme was changed... updated by previous function
       
  1495 	 * @since 1.1.9
       
  1496 	 */ 
       
  1497 	function theme_switched ($theme) {
       
  1498 		$this->xili_settings['langs_folder'] ="";
       
  1499 		$this->xili_settings['theme_domain'] =""; /* to force future search in new theme */
       
  1500 		update_option('xili_language_settings', $this->xili_settings);
       
  1501 	}
       
  1502 	
       
  1503 	/**
       
  1504 	 * private functions for languages_settings 
       
  1505 	 * @since 0.9.6
       
  1506 	 *
       
  1507 	 * fill the content of the boxes (right side and normal)
       
  1508 	 * 
       
  1509 	 */
       
  1510 	function  on_sidebox_1_content($data) { 
       
  1511 		extract($data);
       
  1512 		?>
       
  1513 	 	<h4><?php _e('Note:','xili-language') ?></h4>
       
  1514 		<p><?php echo $message;?></p>
       
  1515 		<?php
       
  1516 	}
       
  1517 	
       
  1518 	function  on_sidebox_2_content() { ?>
       
  1519 	 	
       
  1520 		<p><?php _e("This plugin was developed with the taxonomies, terms tables and tags specifications. <br /> Here a new taxonomy was created and used for languages of posts and pages. <br /> New radiobuttons are available in Post (and Page) write and edit admin pages for selection by author. It is updated for WP 2.9 since 1.1",'xili-language') ?></p>
       
  1521 		<?php
       
  1522 	}	
       
  1523 	
       
  1524 	function  on_sidebox_3_content($data) { /* where to choose if browser language preferences is tested or not */
       
  1525 		extract($data);
       
  1526 		$update_nonce = wp_create_nonce('xilimloptions');
       
  1527 		/* 1.0 browser - default - languages */
       
  1528 		?>
       
  1529 		<fieldset style="margin:2px; padding:12px 6px; border:1px solid #ccc;"><legend><?php echo _e('Select language of the home page', 'xili-language'); ?></legend>
       
  1530 			<select name="xili_language_check_option" id="xili_language_check_option" style="width:100%;">
       
  1531 				<?php  if ($browseroption == 'browser')
       
  1532 						$checked = 'selected = "selected"';
       
  1533 						else 
       
  1534 						$checked = '';
       
  1535 				?>
       
  1536 				<option value="" ><?php _e('Software defined','xili-language'); ?></option>
       
  1537 				<option value="browser" <?php echo $checked; ?> ><?php _e("Language of visitor's browser",'xili-language'); ?></option>
       
  1538 				<?php $listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1539 			foreach ($listlanguages as $language) {
       
  1540 				if ($browseroption == $language->slug) 
       
  1541 						$checked = 'selected = "selected"';
       
  1542 					else 
       
  1543 						$checked = '';
       
  1544 				echo '<option value="'.$language->slug.'" '.$checked.' >'.__($language->description,'xili-language').'</option>';
       
  1545 			}
       
  1546 				?>
       
  1547 			</select>
       
  1548 			<?php  if ('page' != get_settings('show_on_front')) { ?>
       
  1549 				<br /> &nbsp;&nbsp;<label for="xili_language_home_lang"><?php _e('Modify home query','xili-language') ?> <input id="xili_language_home_lang" name="xili_language_home_lang" type="checkbox" value="modify" <?php if($this->xili_settings['homelang'] == 'modify') echo 'checked="checked"' ?> /></label>
       
  1550 				<?php }  ?>
       
  1551 	
       
  1552 	
       
  1553 		</fieldset>
       
  1554 		<br /><br />
       
  1555 		<label for="xili_language_check_option_author" class="selectit"><input id="xili_language_check_option_author" name="xili_language_check_option_author" type="checkbox" value="authorbrowser"  <?php if($authorbrowseroption=='authorbrowser') echo 'checked="checked"' ?> /> <?php echo _e('For new post, pre-select by default: browser language of author', 'xili-language'); ?></label>
       
  1556 		<br /><br />
       
  1557 		<?php if (file_exists(XILIFUNCTIONSPATH)) { /* test if folder exists - ready to add functions.php inside - since 1.0 */?>
       
  1558 		<label for="xili_language_check_functions_enable" class="selectit"><input id="xili_language_check_functions_enable" name="xili_language_check_functions_enable" type="checkbox" value="enable"  <?php if($functions_enable =='enable') echo 'checked="checked"' ?> /> <?php echo _e('Enable gold functions', 'xili-language'); ?></label>
       
  1559 		<?php } else {	
       
  1560 		echo '<input type="hidden" name="xili_language_check_functions_enable" value="'.$functions_enable.'" />';
       
  1561 		} ?>		
       
  1562 	 	<div id='formstatus'></div><span id='loading' class='hidden'><?php _e('Updating...','xili-language') ?></span><div class='submit'>
       
  1563 		<input id='updateoptions' name='updateoptions' type='submit' tabindex='6' value="<?php _e('Update','xili-language') ?>" /></div>
       
  1564 		<?php echo wp_nonce_field( 'xilimloptions', '_ajax_nonce', true, false );/**/ ?>
       
  1565 		<div style="clear:both; height:1px"></div><?php
       
  1566 	}
       
  1567 	
       
  1568 	function  on_sidebox_4_content() { 
       
  1569 	 	$update_nonce = wp_create_nonce('xilimloptions');
       
  1570 	 	?>
       
  1571 	 	<fieldset style="margin:2px; padding:12px 6px; border:1px solid #ccc;"><legend><?php echo __("Theme's informations:",'xili-language').' ('.get_option("template").')'; ?></legend>
       
  1572 	 	<p><?php 
       
  1573 	 	if (defined('THEME_TEXTDOMAIN')) {
       
  1574 	 		echo __('theme_domain:','xili-language').' '.THEME_TEXTDOMAIN.'<br />'.__('as function like:','xili-language').'<i> _e(\'-->\',\''.THEME_TEXTDOMAIN.'\');</i>'; }
       
  1575 	 		else {
       
  1576 	 			_e('Theme domain NOT defined','xili-language');
       
  1577 	 		} ?><br />
       
  1578 	 	<?php echo __("Languages sub-folder:",'xili-language').' '.$this->xili_settings['langs_folder']; ?><br />
       
  1579 	 	<?php _e('Available MO files:','xili-language'); echo '<br />';
       
  1580 	 	$this->find_files(get_template_directory(), "/.mo$/", array(&$this,"available_mo_files")) ;?>
       
  1581 	 	</p>
       
  1582 	 	</fieldset>
       
  1583 		<p><?php _e("Special Gold Actions",'xili-language') ?></p>
       
  1584 		
       
  1585 		<?php
       
  1586 		//echo  '---'.$this->functions_enable;
       
  1587 		if ($this->functions_enable !='' && function_exists('xiliml_setlang_of_undefined_posts')) {
       
  1588 			xiliml_special_UI_undefined_posts ($this->langs_group_id);
       
  1589 		}
       
  1590 	}
       
  1591 
       
  1592 	function on_normal_1_content($data) { 
       
  1593 		extract($data); ?>
       
  1594 		<?php //if (!isset($action) || $action=='add' || $action=='edited' || $action=='deleting') :?>
       
  1595 					<table class="widefat">
       
  1596 						<thead>
       
  1597 						<tr>
       
  1598 						<th scope="col" style="text-align: center"><?php _e('ID') ?></th>
       
  1599 	        			<th scope="col"><?php _e('Name','xili-language') ?></th>
       
  1600 	        			<th scope="col"><?php _e('Full name','xili-language') ?></th>
       
  1601 	        			<th scope="col"><?php _e('Language slug','xili-language') ?></th>
       
  1602 	        			<th scope="col"><?php _e('Order','xili-language') ?></th>
       
  1603 	        			<th scope="col" width="90" style="text-align: center"><?php _e('Posts') ?></th>
       
  1604 	        			<th colspan="2" style="text-align: center"><?php _e('Action') ?></th>
       
  1605 						</tr>
       
  1606 						</thead>
       
  1607 						<tbody id="the-list">
       
  1608 							<?php $this->xili_lang_row(); /* the lines */?>
       
  1609 						</tbody>
       
  1610 					</table>
       
  1611 					<?php if ($action=='edit' || $action=='delete') :?>
       
  1612 					<p>(<a href="?action=add&page=language_page"><?php _e('Add a language','xili-language') ?></a>)</p>
       
  1613 	   				<?php endif; ?>	
       
  1614 	<?php	
       
  1615 	}	
       
  1616 	
       
  1617 	function on_normal_2_content($data) { 
       
  1618 		extract($data);
       
  1619 	 /* the create - edit - delete form */ ?>
       
  1620 		
       
  1621 		<h2 id="addlang" <?php if ($action=='delete') echo 'style="color:#FF1111;"'; ?>><?php _e($formtitle,'xili-language') ?></h2>
       
  1622 		<?php if ($action=='edit' || $action=='delete') :?>
       
  1623 			<input type="hidden" name="language_term_id" value="<?php echo $language->term_id ?>" />
       
  1624 		<?php endif; ?>
       
  1625 		<table class="editform" width="100%" cellspacing="2" cellpadding="5">
       
  1626 			<tr>
       
  1627 				<th width="33%" scope="row" valign="top" align="right"><label for="language_name"><?php _e('Name') ?></label>:&nbsp;</th>
       
  1628 				<td width="67%"><input name="language_name" id="language_name" type="text" value="<?php echo attribute_escape($language->name); ?>" size="40" <?php if($action=='delete') echo 'disabled="disabled"' ?> /></td>
       
  1629 			</tr>
       
  1630 			<tr>
       
  1631 				<th scope="row" valign="top" align="right"><label for="language_nicename"><?php _e('Language slug','xili-language') ?></label>:&nbsp;</th>
       
  1632 				<td><input name="language_nicename" id="language_nicename" type="text" value="<?php echo attribute_escape($language->slug); ?>" size="40" <?php if($action=='delete') echo 'disabled="disabled"' ?> /></td>
       
  1633 			</tr>
       
  1634 			<tr>
       
  1635 				<th scope="row" valign="top" align="right"><label for="language_description"><?php _e('Full name','xili-language') ?></label>:&nbsp;</th>
       
  1636 				<td><input name="language_description" id="language_description" size="40" value="<?php echo $language->description; ?>" <?php if($action=='delete') echo 'disabled="disabled"' ?> /></td>
       
  1637 				
       
  1638 			</tr>
       
  1639 			<tr>
       
  1640 				<th scope="row" valign="top" align="right"><label for="language_order"><?php _e('Order','xili-language') ?></label>:&nbsp;</th>
       
  1641 				<td><input name="language_order" id="language_order" size="3" value="<?php echo $language->term_order; ?>" <?php if($action=='delete') echo 'disabled="disabled"' ?> /></td>
       
  1642 				
       
  1643 			</tr>
       
  1644 			<tr>
       
  1645 			<th><p class="submit"><input type="submit" name="reset" value="<?php echo $cancel_text ?>" /></p></th>
       
  1646 			<td>
       
  1647 			<p class="submit"><input type="submit" name="submit" value="<?php echo $submit_text ?>" /></p>
       
  1648 			</td>
       
  1649 			</tr>
       
  1650 		</table>
       
  1651 	<?php	
       
  1652 	}
       
  1653 
       
  1654 	/**
       
  1655 	 * private functions for admin page : the language list
       
  1656 	 * @since 0.9.0
       
  1657 	 *
       
  1658 	 * @update 0.9.5 : two default languages if taxonomy languages is empty
       
  1659 	 * 
       
  1660 	 */
       
  1661 	function xili_lang_row() { 	
       
  1662 		/*list of languages*/
       
  1663 				//$listlanguages = get_terms_with_order($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1664 		$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1665 		
       
  1666 		if (empty($listlanguages)) : /*create two default lines with the default language (as in config)*/
       
  1667 		  	/* language of WP */
       
  1668 			$term = 'en_US';
       
  1669 			$args = array( 'alias_of' => '', 'description' => 'english', 'parent' => 0, 'slug' =>'');
       
  1670 			$theids = wp_insert_term( $term, TAXONAME, $args);
       
  1671 			wp_set_object_terms($theids['term_id'], 'the-langs-group', TAXOLANGSGROUP);
       
  1672 		 	$term = $this->default_lang;
       
  1673 		 	$desc = $this->default_lang;
       
  1674 		 	if (!defined('WPLANG') || $this->default_lang == 'en_US' ) {$term = 'fr_FR'; $desc = 'french';}
       
  1675 		 	$args = array( 'alias_of' => '', 'description' => $desc, 'parent' => 0, 'slug' =>'');
       
  1676 		 	$theids = wp_insert_term( $term, TAXONAME, $args);
       
  1677 			wp_set_object_terms($theids['term_id'], 'the-langs-group', TAXOLANGSGROUP); /* create the group */
       
  1678 			$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1679 		endif;
       
  1680 		foreach ($listlanguages as $language) {	
       
  1681 			$class = ((defined('DOING_AJAX') && DOING_AJAX) || " class='alternate'" == $class ) ? '' : " class='alternate'";
       
  1682 			$language->count = number_format_i18n( $language->count );
       
  1683 			$posts_count = ( $language->count > 0 ) ? "<a href='edit.php?lang=$language->slug'>$language->count</a>" : $language->count;	
       
  1684 		
       
  1685 			$edit = "<a href='?action=edit&amp;page=language_page&amp;term_id=".$language->term_id."' >".__( 'Edit' )."</a></td>";	
       
  1686 			/* delete link*/
       
  1687 			$edit .= "<td><a href='?action=delete&amp;page=language_page&amp;term_id=".$language->term_id."' class='delete'>".__( 'Delete' )."</a>";	
       
  1688 			
       
  1689 		$line="<tr id='cat-$language->term_id'$class>
       
  1690 			<th scope='row' style='text-align: center'>$language->term_id</th>
       
  1691 			<td>" .$language->name. "</td>
       
  1692 			<td>$language->description</td>
       
  1693 			<td>$language->slug</td>
       
  1694 			<td>$language->term_order</td>
       
  1695 			<td align='center'>$posts_count</td> 
       
  1696 			<td>$edit</td>\n\t</tr>\n"; /*to complete - 0.9.8.1 count to post*/
       
  1697 			echo $line;
       
  1698 			//print_r($language);
       
  1699 		}	
       
  1700 	}
       
  1701 	
       
  1702 	//********************************************//
       
  1703 	// Functions for themes (hookable by add_action() in functions.php - 0.9.7
       
  1704 	//********************************************//
       
  1705 
       
  1706 	/**
       
  1707 	 * List of available languages.
       
  1708 	 *
       
  1709 	 * @since 0.9.0
       
  1710 	 * @updated 0.9.7.4 - 0.9.8.3 - 0.9.9.6
       
  1711 	 * can be hooked by add_action in functions.php
       
  1712 	 * with : add_action('xili_language_list','my_infunc_language_list',10,3);
       
  1713 	 *
       
  1714 	 * for multiple widgets since 0.9.9.6 : incorporate top option (without flag) but with example rules
       
  1715 	 *
       
  1716 	 * @param $before = '<li>', $after ='</li>'.
       
  1717 	 * @return list of languages of site for sidebar list.
       
  1718 	 */
       
  1719 	function xili_language_list($before = '<li>', $after ='</li>',$option='') {
       
  1720 		$listlanguages = get_terms_of_groups_lite ($this->langs_group_id,TAXOLANGSGROUP,TAXONAME,'ASC');
       
  1721 		if ($option == 'typeone') {
       
  1722 			/* the rules : don't display the current lang if set and add link of category if is_category()*/
       
  1723 			if (is_category()) {  
       
  1724 				$catcur = xiliml_get_category_link();
       
  1725 				$currenturl = $catcur.'&amp;'; 
       
  1726 			} else {
       
  1727 		 		$currenturl = get_bloginfo('siteurl').'/?';
       
  1728 			}
       
  1729 			foreach ($listlanguages as $language) {
       
  1730 				if ($language->slug != $this->curlang ) {
       
  1731 					$a .= $before ."<a href='".$currenturl.QUETAG."=".$language->slug."' title='".__('Posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>". __('in '.$language->description,THEME_TEXTDOMAIN) ."</a>".$after;
       
  1732 				}
       
  1733 			}
       
  1734 			echo $a;
       
  1735 		} else {	/* current list */
       
  1736 			foreach ($listlanguages as $language) {
       
  1737 				$a .= $before ."<a href='".get_bloginfo('siteurl')."/?".QUETAG."=".$language->slug."' title='".__('Posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>". __('in '.$language->description,THEME_TEXTDOMAIN) ."</a>".$after;
       
  1738 			}
       
  1739 			echo $a;
       
  1740 		}	
       
  1741 	}
       
  1742 	
       
  1743 	/**
       
  1744 	 * language of current post used in loop
       
  1745 	 * @since 0.9.0
       
  1746 	 * 
       
  1747 	 *
       
  1748 	 * @param $before = '<span class"xili-lang">(', $after =')</span>'.
       
  1749 	 * @return language of post.
       
  1750 	 */
       
  1751 	function xili_post_language($before = '<span class="xili-lang">(', $after =')</span>') {
       
  1752 		global $post;
       
  1753 		$ress = wp_get_object_terms($post->ID, TAXONAME);
       
  1754 		$obj_term = $ress[0];
       
  1755 		if ('' != $obj_term->name) :
       
  1756 				$curlangname = $obj_term->name;
       
  1757 		else :
       
  1758 				$curlangname = __('undefined',THEME_TEXTDOMAIN);
       
  1759 		endif;
       
  1760 		$a = $before . $curlangname .$after.'';  
       
  1761 		echo $a;
       
  1762 	}
       
  1763 		
       
  1764 	/** 
       
  1765 	 * for one post create a link list of the corresponding posts in other languages
       
  1766 	 *
       
  1767 	 * @since 0.9.0
       
  1768 	 * @updated 0.9.9.2 / 3 $separator replace $after, $before contains pre-text to echo a better list.
       
  1769 	 * @updated 1.1 - see hookable same name function outside class
       
  1770 	 * can be hooked by add_action in functions.php
       
  1771 	 *
       
  1772 	 *
       
  1773 	 */
       
  1774 	function xiliml_the_other_posts ($post_ID, $before = "This post in", $separator = ", ", $type = "display") {
       
  1775 		/* default here*/
       
  1776 			$outputarr = array();
       
  1777 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
  1778 			$langpost = $this->get_cur_language($post_ID); // to be used in multilingual loop since 1.1
       
  1779 			$post_lang = $langpost['lang'];
       
  1780 			foreach ($listlanguages as $language) {
       
  1781 				$otherpost = get_post_meta($post_ID, 'lang-'.$language->slug, true);
       
  1782 				
       
  1783 				if ($type == "display") {
       
  1784 					if ('' != $otherpost && $language->slug != $post_lang ) {
       
  1785 						$outputarr[] = "<a href='".get_permalink($otherpost)."' >".__($language->description,THEME_TEXTDOMAIN) ."</a>";
       
  1786 					}
       
  1787 				} elseif ($type == "array") { // here don't exclude cur lang
       
  1788 					if ('' != $otherpost)
       
  1789 						$outputarr[$language->slug] = $otherpost;
       
  1790 				}
       
  1791 			}
       
  1792 			if ($type == "display") {
       
  1793 				if (!empty($outputarr))
       
  1794 					$output =  (($before !="") ? __($before,THEME_TEXTDOMAIN)." " : "" ).implode ($separator, $outputarr);
       
  1795 				if ('' != $output) { echo $output;}	
       
  1796 			} elseif ($type == "array") {
       
  1797 				if (!empty($outputarr)) {
       
  1798 					$outputarr[$post_ID] = $post_lang; 
       
  1799 					// add a key with curid to give his lang (empty if undefined)
       
  1800 					return $outputarr;
       
  1801 				} else {
       
  1802 					return false;	
       
  1803 				}
       
  1804 			}	
       
  1805 	}
       
  1806 	
       
  1807 	/**
       
  1808 	 * the_category() rewritten to keep new features of multilingual (and amp & pbs in link)
       
  1809 	 *
       
  1810 	 * @since 0.9.0
       
  1811 	 * @updated 0.9.9.4
       
  1812 	 * can be hooked by add_action xiliml_the_category in functions.php
       
  1813 	 *
       
  1814 	 */
       
  1815 	function xiliml_the_category($post_ID, $separator = ', ' ,$echo = true) {
       
  1816 		/* default here*/
       
  1817 		$the_cats_list = wp_get_object_terms($post_ID, 'category');
       
  1818 		$i = 0;
       
  1819 		foreach ($the_cats_list as $the_cat) {
       
  1820 			if ( 0 < $i )
       
  1821 				$thelist .= $separator . ' ';
       
  1822 			$desc4title = trim(attribute_escape(apply_filters( 'category_description', $the_cat->description, $the_cat->term_id )));
       
  1823 			
       
  1824 			$title = ('' == $desc4title) ? __($the_cat->name,THEME_TEXTDOMAIN) : $desc4title;
       
  1825 			$the_catlink = '<a href="' . get_category_link($the_cat->term_id) . '" title="' . $title . '" ' . $rel . '>';
       
  1826 			//if ($curlang != DEFAULTSLUG) :
       
  1827 	      	$the_catlink .=  __($the_cat->name,THEME_TEXTDOMAIN).'</a>';;
       
  1828 	      	//else :
       
  1829 	      		 //$the_catlink .=  $the_cat->name.'</a>';;
       
  1830 	      	//endif;
       
  1831 			$thelist .= $the_catlink;
       
  1832 			++$i;
       
  1833 		}
       
  1834 		if ($echo) :
       
  1835 			echo $thelist;
       
  1836 			return true;
       
  1837 		else :
       
  1838 			return $thelist;
       
  1839 		endif;	
       
  1840 	}	
       
  1841 	
       
  1842 	/**
       
  1843 	 * Add list of languages in radio input - for search form.
       
  1844 	 *
       
  1845 	 * @since 0.9.7
       
  1846 	 * can be hooked by add_action in functions.php
       
  1847 	 *
       
  1848 	 * @updated 0.9.9.5
       
  1849 	 *
       
  1850 	 * $before, $after each line of radio input
       
  1851 	 *
       
  1852 	 * @param $before, $after. 
       
  1853 	 * @return echo the form.
       
  1854 	 */
       
  1855 	function xiliml_langinsearchform ($before='',$after='') {
       
  1856 			/* default here*/
       
  1857 			$listlanguages = get_terms(TAXONAME, array('hide_empty' => false));
       
  1858 			foreach ($listlanguages as $language) {
       
  1859 				$a = $before.'<input type="radio" name="'.QUETAG.'" value="'.$language->slug.'" id="'.$language->slug.'" />&nbsp;'.__($language->description,THEME_TEXTDOMAIN).' '.$after;
       
  1860 			echo $a;
       
  1861 			}			
       
  1862 		    echo $before.'<input type="radio" name="alllang" value="yes" /> '.__('All',THEME_TEXTDOMAIN).' '.$after;	 // this query alllang is unused -		
       
  1863 	}
       
  1864 	
       
  1865 	/**
       
  1866 	 * Select latest comments in current lang.
       
  1867 	 *
       
  1868 	 * @since 0.9.9.4
       
  1869 	 * used by widget xili-recent-comments
       
  1870 	 *
       
  1871 	 * $before, $after each line of radio input
       
  1872 	 *
       
  1873 	 * @param $before, $after. 
       
  1874 	 * @return echo the form.
       
  1875 	 */
       
  1876 	function xiliml_recent_comments ($number = 5) {
       
  1877 		global $comments, $wpdb ;
       
  1878 		if ( !$comments = wp_cache_get( 'xili_language_recent_comments', 'widget' ) ) {
       
  1879 				$join = "";
       
  1880 				$where = "";
       
  1881 				$reqtag = is_term( $this->curlang, TAXONAME );
       
  1882 					if (''!= $reqtag) {
       
  1883 						$wherereqtag = $reqtag['term_id'];
       
  1884 						$join = " LEFT JOIN $wpdb->term_relationships as tr ON ($wpdb->comments.comment_post_ID = tr.object_id) LEFT JOIN $wpdb->term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) ";
       
  1885 					    $where = " AND tt.taxonomy = '".TAXONAME."' ";
       
  1886 					    $where .= " AND tt.term_id = $wherereqtag ";
       
  1887 					}
       
  1888 				$query = "SELECT * FROM $wpdb->comments".$join." WHERE comment_approved = '1' ".$where." ORDER BY comment_date_gmt DESC LIMIT $number"; 
       
  1889 				echo $query;
       
  1890 				$comments = $wpdb->get_results($query);
       
  1891 				wp_cache_add( 'xili_language_recent_comments', $comments, 'widget' );
       
  1892 		}
       
  1893 		return $comments;
       
  1894 	}
       
  1895 	
       
  1896 	/**
       
  1897 	 * Recursive search of files in a path
       
  1898 	 * @since 1.1.9 
       
  1899 	 * @update 1.2.1
       
  1900 	 *
       
  1901 	 */
       
  1902 	 function find_files($path, $pattern, $callback) {
       
  1903  		//$path = rtrim(str_replace("\\", "/", $path), '/') . '/';
       
  1904 		  $matches = Array();
       
  1905 		  $entries = Array();
       
  1906 		  $dir = dir($path);
       
  1907 		  
       
  1908 		  while (false !== ($entry = $dir->read())) {
       
  1909 		    $entries[] = $entry;
       
  1910 		  }
       
  1911 		  $dir->close();
       
  1912 		  foreach ($entries as $entry) {
       
  1913 		    $fullname = $path .$this->ossep. $entry;
       
  1914 		    if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
       
  1915 		      $this->find_files($fullname, $pattern, $callback);
       
  1916 		    } else if (is_file($fullname) && preg_match($pattern, $entry)) {
       
  1917 		      call_user_func($callback, $path , $entry);
       
  1918 		    }
       
  1919 		  }
       
  1920 	}
       
  1921 	/**
       
  1922 	 * display lines of files in special sidebox
       
  1923 	 * @since 1.1.9
       
  1924 	 */
       
  1925 	function available_mo_files($path , $filename) {
       
  1926   		//echo $filename . " in : " . "/".str_replace("/","",str_replace(get_template_directory(),'',$path)) . "<br />";
       
  1927   		echo str_replace(".mo","",$filename ). " (".$this->ossep.str_replace($this->ossep,"",str_replace(get_template_directory(),'',$path)).")<br />";
       
  1928 	}
       
  1929 	
       
  1930 	/**
       
  1931  	* Enable to add functions and filters that are not in theme's functions.php
       
  1932  	* These filters are common even if you change default theme...
       
  1933  	* Place your functions.php in folder plugins/xilidev-libraries/
       
  1934  	* if you have a filter in this file, avoid to have similar one in functions.php of the theme !!!
       
  1935 	*
       
  1936  	*/
       
  1937 	function insert_gold_functions () {
       
  1938 		if ($this->functions_enable !='' && file_exists(XILIFUNCTIONSPATH . '/functions.php') )
       
  1939 			include_once (XILIFUNCTIONSPATH . '/functions.php');
       
  1940 	}
       
  1941 
       
  1942 			
       
  1943 } /* end of xili-language class */
       
  1944 
       
  1945 
       
  1946 /**** Functions that improve taxinomy.php ****/
       
  1947 
       
  1948 /**
       
  1949  * get terms and add order in term's series that are in a taxonomy 
       
  1950  * (not in class for general use)
       
  1951  *
       
  1952  * @since 0.9.8.2 - full version is in xili-tidy-tags
       
  1953  * @uses $wpdb
       
  1954  */
       
  1955 function get_terms_of_groups_lite ($group_ids, $taxonomy, $taxonomy_child, $order = '') {
       
  1956 	global $wpdb;
       
  1957 	if ( !is_array($group_ids) )
       
  1958 		$group_ids = array($group_ids);
       
  1959 	$group_ids = array_map('intval', $group_ids);
       
  1960 	$group_ids = implode(', ', $group_ids);
       
  1961 	$theorderby = '';
       
  1962 	
       
  1963 	// lite release
       
  1964 	if ($order == 'ASC' || $order == 'DESC') $theorderby = ' ORDER BY tr.term_order '.$order ;
       
  1965 		
       
  1966 	$query = "SELECT t.*, tt2.term_taxonomy_id, tt2.description,tt2.parent, tt2.count, tt2.taxonomy, tr.term_order FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->terms AS t ON t.term_id = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt2 ON tt2.term_id = tr.object_id WHERE tt.taxonomy IN ('".$taxonomy."') AND tt2.taxonomy = '".$taxonomy_child."' AND tt.term_id IN (".$group_ids.") ".$theorderby;
       
  1967 	
       
  1968 	$listterms = $wpdb->get_results($query);
       
  1969 	if ( ! $listterms )
       
  1970 		return array();
       
  1971 
       
  1972 	return $listterms;
       
  1973 }
       
  1974 /* for backward compatibility - soon obsolete - please modify your theme's function.php */
       
  1975 function get_terms_with_order ($group_ids, $taxonomy, $taxonomy_child, $order = 'ASC') {
       
  1976 	return get_terms_of_groups_lite ($group_ids, $taxonomy, $taxonomy_child, $order);
       
  1977 }	
       
  1978 
       
  1979 /**
       
  1980  * function that improve taxinomy.php 
       
  1981  * @since 0.9.8
       
  1982  *
       
  1983  * update term order in relationships (for terms of langs group defined by his taxonomy_id)
       
  1984  *
       
  1985  * @param $object_id, $taxonomy_id, $term_order
       
  1986  * 
       
  1987  */
       
  1988 function update_term_order ($object_id,$term_taxonomy_id,$term_order) {
       
  1989 	global $wpdb;
       
  1990 	$wpdb->update( $wpdb->term_relationships, compact( 'term_order' ), array( 'term_taxonomy_id' => $term_taxonomy_id,'object_id' => $object_id ) );
       
  1991 }
       
  1992 
       
  1993 /**
       
  1994  * function that improve taxinomy.php 
       
  1995  * @since 0.9.8
       
  1996  *
       
  1997  * get one term and order of it in relationships
       
  1998  *
       
  1999  * @param term_id and $group_ttid (taxonomy id of group)
       
  2000  * @return object with term_order
       
  2001  */
       
  2002 function get_term_and_order ($term_id,$group_ttid,$taxonomy) {
       
  2003 	global $wpdb;
       
  2004 	$term = get_term($term_id,$taxonomy,OBJECT,'edit');
       
  2005 	$term->term_order = $wpdb->get_var("SELECT term_order FROM $wpdb->term_relationships WHERE object_id =  $term_id AND term_taxonomy_id = $group_ttid ");
       
  2006 	return $term;
       
  2007 }
       
  2008 
       
  2009 /**** Functions using the class ****/
       
  2010 /**
       
  2011  * Return the current language of theme.
       
  2012  *
       
  2013  * @since 0.9.7
       
  2014  * use for other function elsewhere
       
  2015  *
       
  2016  * @return the slug of language (used in query).
       
  2017  */
       
  2018 function the_curlang() {
       
  2019 	global $xili_language;
       
  2020 	return $xili_language->curlang;
       
  2021 }
       
  2022 
       
  2023 /**
       
  2024  * Return the current language and dir of theme.
       
  2025  *
       
  2026  * @since 0.9.9
       
  2027  * use for other function elsewhere
       
  2028  *
       
  2029  * @return array with slug of language (used in query) and dir (ltr or rtl).
       
  2030  */
       
  2031 function the_cur_lang_dir() {
       
  2032 	global $xili_language;
       
  2033 	return array('lang'=>$xili_language->curlang, 'direction'=>$xili_language->curlang_dir);
       
  2034 }
       
  2035 
       
  2036 /**
       
  2037  * Return the current group of languages
       
  2038  *
       
  2039  * @since 0.9.8.3
       
  2040  */
       
  2041 function the_cur_langs_group_id() {
       
  2042 	global $xili_language;
       
  2043 	return $xili_language->langs_group_id;
       
  2044 } 
       
  2045 
       
  2046 /**
       
  2047  * Return the current date or a date formatted with strftime.
       
  2048  *
       
  2049  * @since 0.9.7.1
       
  2050  * can be used in theme for multilingual date
       
  2051  * @param format and time (if no time = current date-time)
       
  2052  * @return the formatted date.
       
  2053  */
       
  2054 function the_xili_local_time($format='%B %d, %Y',$time = null) {
       
  2055 	global $xili_language;
       
  2056 	if ($time == null ) $time = time();
       
  2057 	$curlang = $xili_language->curlang;
       
  2058 	$curlang = substr($curlang,0,3).strtoupper(substr($curlang,-2));
       
  2059 	setlocale(LC_TIME, $curlang); /* work if server is ready */
       
  2060 	return htmlentities(strftime(__($format,THEME_TEXTDOMAIN),$time),ENT_COMPAT); /* ,'UTF-8' entities for some server */
       
  2061 }
       
  2062 
       
  2063 /**
       
  2064  * Return the language of current post in loop.
       
  2065  *
       
  2066  * @since 0.9.7.0
       
  2067  * @updated 0.9.9
       
  2068  * useful for functions in functions.php or other plugins
       
  2069  * 
       
  2070  * @param ID of the post
       
  2071  * @return the name of language as ISO code (en_US).
       
  2072  */
       
  2073 function get_cur_language($post_ID) {
       
  2074 	global $xili_language;
       
  2075 	$langpost = $xili_language->get_cur_language($post_ID);
       
  2076 	return $langpost['lang'];
       
  2077 }
       
  2078 
       
  2079 /**
       
  2080  * Return the lang and dir of language of current post in loop.
       
  2081  *
       
  2082  * @since 0.9.9
       
  2083  * useful for functions in functions.php or other plugins
       
  2084  * 
       
  2085  * @param ID of the post
       
  2086  * @return array two params : lang and direction of lang (ltr or rtl).
       
  2087  */
       
  2088 function get_cur_post_lang_dir($post_ID) {
       
  2089 	global $xili_language;
       
  2090 	return $xili_language->get_cur_language($post_ID);
       
  2091 }
       
  2092 
       
  2093 /**
       
  2094  * Return language object of a post.
       
  2095  *
       
  2096  * @since 1.1.8
       
  2097  * useful for functions in functions.php or other plugins
       
  2098  * 
       
  2099  * @param ID of the post
       
  2100  * @return false or object with params as in current term (->description = full name of lang, ->count = number of posts in this language,...
       
  2101  */
       
  2102 function xiliml_get_lang_object_of_post($post_ID) {
       
  2103 	
       
  2104 	$ress = wp_get_object_terms($post_ID, TAXONAME); /* lang of target post */
       
  2105 	if ($ress == array()) {
       
  2106 		return false;
       
  2107 	} else {
       
  2108 		//print_r($ress[0]);
       
  2109 		return $ress[0];
       
  2110 	}
       
  2111 }
       
  2112 
       
  2113 /**
       
  2114  * Return the language of current browser.
       
  2115  *
       
  2116  * @since 0.9.7.6
       
  2117  * @updated 0.9.9
       
  2118  * useful for functions in functions.php or other plugins
       
  2119  * 
       
  2120  * @param no
       
  2121  * @return the best choice.
       
  2122  */
       
  2123 function choice_of_browsing_language() {
       
  2124 	global $xili_language;
       
  2125 	return $xili_language->choice_of_browsing_language();
       
  2126 }
       
  2127 
       
  2128 /**
       
  2129  * Return the lang and dir of current browser.
       
  2130  *
       
  2131  * @since 0.9.9
       
  2132  * useful for functions in functions.php or other plugins
       
  2133  * 
       
  2134  * @param no
       
  2135  * @return array of the best choice lang and his dir.
       
  2136  */
       
  2137 function choice_of_browsing_lang_dir() {
       
  2138 	global $xili_language;
       
  2139 	$lang = $xili_language->choice_of_browsing_language();
       
  2140 	$dir = $xili_language->get_dir_of_cur_language($lang);
       
  2141 	return array('lang'=>$lang,'direction'=>$dir);
       
  2142 }
       
  2143 
       
  2144 /**
       
  2145  * Activate hooks of plugin in class.
       
  2146  *
       
  2147  * @since 0.9.7.4
       
  2148  * can be used in functions.php for special action
       
  2149  *
       
  2150  * @param filter name and function
       
  2151  * 
       
  2152  */
       
  2153 function add_again_filter($filtername,$filterfunction) {
       
  2154 	global $xili_language;
       
  2155 	$xili_language->add_filter($filtername,$filterfunction);
       
  2156 }
       
  2157 
       
  2158 /**
       
  2159  * Replace get_category_link to bypass hook from xili_language
       
  2160  *
       
  2161  * @since 0.9.7.4
       
  2162  * @updated 1.0.1
       
  2163  * can be used in functions.php for special action needing permalink
       
  2164  
       
  2165  * @param category ID 
       
  2166  * @return the permalink of passed cat_id.
       
  2167  */
       
  2168 function xiliml_get_category_link($catid = 0) {
       
  2169 			global $xili_language;
       
  2170 			if ($catid == 0) {
       
  2171 				global $wp_query;
       
  2172 				$catid = $wp_query->query_vars['cat'];
       
  2173 			}
       
  2174 			remove_filter('category_link', $xili_language->idx['xiliml_link_append_lang']);
       
  2175 				$catcur = get_category_link($catid); 
       
  2176 			add_again_filter('category_link', 'xiliml_link_append_lang');
       
  2177 	return $catcur;
       
  2178 }
       
  2179 /* used by xili widget - usable if you need to create your own template tag 
       
  2180  *
       
  2181  * @since 0.9.9.4
       
  2182  * @param quantity of comments
       
  2183  *
       
  2184  * @return comments objects...
       
  2185  */
       
  2186 function xiliml_recent_comments($number = 5) {
       
  2187 	global $xili_language;
       
  2188 	return $xili_language->xiliml_recent_comments($number);
       
  2189 }
       
  2190 
       
  2191 /**
       
  2192  * Return full object of a language
       
  2193  * @since 1.1.8
       
  2194  * @param name (fr_FR) or slug (fr_fr)
       
  2195  * @return false or full language object (example ->description = full as set in admin UI)
       
  2196  */
       
  2197 function xiliml_get_language($lang_nameorslug="") {
       
  2198 	$language = is_term( $lang_nameorslug, TAXONAME );
       
  2199 	if ($language) {
       
  2200 		return get_term($language['term_id'],TAXONAME,OBJECT,'edit');
       
  2201 	} else {
       
  2202 		return false;	
       
  2203 	}
       
  2204 }
       
  2205  
       
  2206  	/* 
       
  2207 	 **
       
  2208 	 * Template Tags for themes (with current do_action tool some are hookable functions) 
       
  2209 	 **
       
  2210 	 */
       
  2211 	 
       
  2212 /**
       
  2213  * Template Tag insertable in search form for sub-selection of a language
       
  2214  *
       
  2215  * @since 0.9.7
       
  2216  * can be used in theme template
       
  2217  * example: if(class_exists('xili_language')) xiliml_langinsearchform()
       
  2218  *
       
  2219  * hook: add_action('xiliml_langinsearchform','your_xiliml_langinsearchform',10,2) to change its behaviour elsewhere
       
  2220  * @param html tags 
       
  2221  * @return echo the list as radio-button
       
  2222  */	 
       
  2223 function xiliml_langinsearchform ($before='',$after='') { /* list of radio buttons for search form*/
       
  2224 	global $xili_language;
       
  2225 	if ($xili_language->this_has_filter('xiliml_langinsearchform')){ 
       
  2226 		remove_filter('xiliml_langinsearchform',$xili_language->idx['xiliml_langinsearchform']); /*no default from class*/
       
  2227 	}
       
  2228 	do_action('xiliml_langinsearchform',$before,$after);	
       
  2229 }
       
  2230 
       
  2231 /**
       
  2232  * Template Tag - replace the_category() tag of WP Core
       
  2233  *
       
  2234  * @since 0.9.0
       
  2235  * can be used in theme template in each post in loop
       
  2236  * example: if(class_exists('xili_language')) xiliml_the_category($post->ID)
       
  2237  *
       
  2238  * hook: add_action('xiliml_the_category','your_xiliml_the_category',10,3) to change its behaviour elsewhere
       
  2239  * @param post_id separator echo (true by default) 
       
  2240  * @return echo (by default) the list of cats with comma separated...
       
  2241  */
       
  2242 function xiliml_the_category($post_ID, $separator = ', ' ,$echo = true) { /* replace the_category() */
       
  2243 	global $xili_language;
       
  2244 	if ($xili_language->this_has_filter('xiliml_the_category')){ 
       
  2245 		remove_filter('xiliml_the_category',$xili_language->idx['xiliml_the_category']); /*no default from class*/
       
  2246 	}
       
  2247 	do_action('xiliml_the_category',$post_ID,$separator,$echo);
       
  2248 }
       
  2249 
       
  2250 /**
       
  2251  * Template Tag - in loop display the link of other posts defined as in other languages
       
  2252  *
       
  2253  * @since 0.9.0
       
  2254  * @updated 0.9.9.2, 1.1 (can return an array of lang + id)
       
  2255  * can be used in theme template in single.php under the title
       
  2256  * example: if(class_exists('xili_language')) xiliml_the_other_posts($post->ID)
       
  2257  *
       
  2258  * hook: add_action('xiliml_the_other_posts','your_xiliml_the_other_posts',10,3) to change its behaviour elsewhere
       
  2259  * @param post_id before separator type (echo, array)
       
  2260  * @return echo (by default) the list 
       
  2261  */
       
  2262 function xiliml_the_other_posts ($post_ID,$before = "This post in", $separator = ", ", $type = "display") { /* display the other posts defined as in other lang */
       
  2263 	global $xili_language;
       
  2264 	if ($xili_language->this_has_filter('xiliml_the_other_posts')){ 
       
  2265 		remove_filter('xiliml_the_other_posts',$xili_language->idx['xiliml_the_other_posts']); /*no default from class*/
       
  2266 	}
       
  2267 	return apply_filters('xiliml_the_other_posts',$post_ID, $before, $separator,$type);
       
  2268 }
       
  2269 
       
  2270 /**
       
  2271  * Template Tag - in loop display the language of the post
       
  2272  *
       
  2273  * @since 0.9.0
       
  2274  * can be used in theme template in loop under the title
       
  2275  * example: if(class_exists('xili_language')) xili_post_language()
       
  2276  *
       
  2277  * hook: add_action('xili_post_language','your_xili_post_language',10,2) to change its behaviour elsewhere
       
  2278  * @param before after 
       
  2279  * @return echo (by default) the language 
       
  2280  */
       
  2281 function xili_post_language($before = '<span class="xili-lang">(', $after =')</span>') { /* post language in loop*/
       
  2282 	do_action('xili_post_language',$before, $after);
       
  2283 }
       
  2284 
       
  2285 /**
       
  2286  * Template Tag - outside loop (sidebar) display the languages of the site (used also by widget)
       
  2287  *
       
  2288  * @since 0.9.0
       
  2289  * @updated 0.9.7.4
       
  2290  * can be used in theme template in sidebar menu or header menu
       
  2291  * example: if(class_exists('xili_language')) xili_language_list()
       
  2292  * theoption param is used to define type of display according places (sidebar / header) in theme (see dev.xiligroup.com)
       
  2293  *
       
  2294  * hook: add_action('xili_language_list','your_xili_language_list',10,3) to change its behaviour elsewhere
       
  2295  * @param before after theoption
       
  2296  * @return echo the list of languages
       
  2297  */
       
  2298 function xili_language_list($before = '<li>', $after ='</li>', $theoption='') { /* list of languages i.e. in sidebar */
       
  2299 	global $xili_language;
       
  2300 	if ($xili_language->this_has_filter('xili_language_list')){ 
       
  2301 		remove_filter('xili_language_list',$xili_language->idx['xili_language_list']); /*no default from class*/
       
  2302 	}	
       
  2303 	do_action('xili_language_list',$before,$after,$theoption); 
       
  2304 }
       
  2305 
       
  2306 /*
       
  2307  * sub selection of pages for wp_list_pages()
       
  2308  * @ since 090504 - exemple of new function add here or addable in functions.php
       
  2309  * © xiligroup.dev
       
  2310  *
       
  2311  * only called if xili-language plugin is active and query tag 'lang' is in wp_list_pages template tag
       
  2312  *
       
  2313  * example 1 : wp_list_pages('title_li=&lang='.the_curlang() ); will display only pages of current lang
       
  2314  *
       
  2315  * example 2 : wp_list_pages('title_li=&setlang=0&lang='.the_curlang() ); will display pages of current lang AND pages with lang undefined (polyglot pages).
       
  2316  *
       
  2317  */
       
  2318 function ex_pages_by_lang ($pages, $r) {
       
  2319 	if (isset($r['lang']) && !empty($pages) && function_exists('get_cur_post_lang_dir')) {
       
  2320 		$keepundefined = null;
       
  2321 		if (isset($r['setlang'])) { 
       
  2322 			if ($r['setlang'] == 0 || $r['setlang'] == 'false') $keepundefined = false;
       
  2323 			if ($r['setlang'] == 1 || $r['setlang'] == 'true') $keepundefined = true;
       
  2324 		}	
       
  2325 		$resultingpages = array();
       
  2326 		foreach ($pages as $page) {
       
  2327 			$post_lang_dir = get_cur_post_lang_dir($page->ID);
       
  2328 			if ($post_lang_dir === $keepundefined) {
       
  2329 					$resultingpages[] = $page;
       
  2330 			} elseif ($post_lang_dir['lang'] == $r['lang'] ) {
       
  2331 					$resultingpages[] = $page;
       
  2332 			}
       
  2333 		}
       
  2334 		return $resultingpages;	
       
  2335 	} else {	
       
  2336 	 	return $pages;
       
  2337 	}
       
  2338 }
       
  2339 add_filter('get_pages','ex_pages_by_lang',10,2);
       
  2340 
       
  2341 /**
       
  2342  * functions to change and restore loop's query tag
       
  2343  * (useful for sidebar widget - see functions table)
       
  2344  * @since 1.3.0
       
  2345  * @param  lang to modify query_tag - 
       
  2346  *
       
  2347  */
       
  2348 
       
  2349 function xiliml_force_loop_lang ($lang_query_tag){
       
  2350 	global $xili_language, $wp_query;
       
  2351 	$xili_language->temp_lang_query_tag = $wp_query->query_vars[QUETAG];
       
  2352 	$wp_query->query_vars[QUETAG] = $lang_query_tag;
       
  2353 	$xili_language->current_lang_query_tag = $wp_query->query_vars[QUETAG];
       
  2354 }
       
  2355 
       
  2356 function xiliml_restore_loop_lang (){
       
  2357 	global $xili_language, $wp_query;
       
  2358 	$wp_query->query_vars[QUETAG] = $xili_language->temp_lang_query_tag;
       
  2359 	$xili_language->current_lang_query_tag = $wp_query->query_vars[QUETAG];
       
  2360 }
       
  2361 /**
       
  2362  * functions to permit lang query tag
       
  2363  * (useful for WP_Query)
       
  2364  * @since 1.3.2
       
  2365  * example: add_action('parse_query','xiliml_add_lang_to_parsed_query');
       
  2366  *		$r = new WP_Query($thequery);
       
  2367  *		remove_filter('parse_query','xiliml_add_lang_to_parsed_query'); 
       
  2368  * used by class xili_Widget_Recent_Posts
       
  2369  */
       
  2370 function xiliml_add_lang_to_parsed_query ($theclass = array()) {
       
  2371 		global $wp_query;
       
  2372 		$query = $theclass->query;
       
  2373 		if (is_array($query)) {
       
  2374 			$r = $query;
       
  2375 		} else {
       
  2376 			parse_str($query, $r); 	
       
  2377 		}
       
  2378 		if (array_key_exists(QUETAG,$r)) $wp_query->query_vars[QUETAG] = $r[QUETAG];
       
  2379 }
       
  2380 
       
  2381 
       
  2382 
       
  2383 /**
       
  2384  * instantiation of xili_language class
       
  2385  *
       
  2386  * @since 0.9.7 - 0.9.9.4 =& for vintage server with php4 !!!! - 1.1.9
       
  2387  *
       
  2388  * @param 1st metabox (for other posts in post edit UI - to replace custom fields - beta tests)
       
  2389  * @param 2nd ajax ( true if ajax is activated for post edit admin UI - alpha tests )
       
  2390  * 1.1.9
       
  2391  * @param 3rd locale_method (true for cache compatibility... in current tests with johan.eenfeldt@kostdoktorn.se)
       
  2392  */
       
  2393 $xili_language =& new xili_language(true,false,false); 
       
  2394 
       
  2395 /**
       
  2396  * Enable to add functions and filters that are not in theme's functions.php
       
  2397  * These filters are common even if you change default theme...
       
  2398  * Place your functions.php in folder plugins/xilidev-libraries/
       
  2399  * if you have a filter in this file, avoid to have similar one in functions.php of the theme !!!
       
  2400  * 
       
  2401  * (for tests, check / uncheck 'enable gold functions' in settings UI)
       
  2402  * @since 1.0
       
  2403  * @updated 1.4.0
       
  2404  */
       
  2405 $xili_language->insert_gold_functions ();
       
  2406 ?>