xili-language : hooks, filters, class and so on…

This post is an introduction for experts in php and wordpress about filters used in xili-language.
The changes from wordpress’s version 2.7.x to 2.8, was an opportunity to verify filters and hooks used by xili-language plugin and for the plugin itself. And how to collect unique id of each filter (to, by instance, to temporary remove it or to cancel it…)
Two cases : the filter is already inside WordPress and the filter is created by the plugin itself.

In these two cases, the unique id of an added function differs when added inside a class as the class of xili_language. The unique id is currently the name of the function added in the filtering queue but if added during class instantiation, this unique id contains also as prefix the class name and a suffix (suffix different in WP 2.7.x and WP 2.8.x). So, since latest versions, we decide to store these filters id in class array var (idx[]).

First example : the filter is created by the plugin itself

by instance xili_language_list()

If the default plugin function detects that you have add you function to customize the language’s list, the function remove the filter (action) built by default in the class :

1935
1936
1937
1938
1939
1940
1941
function xili_language_list($before = '<li>', $after ='</li>', $theoption='') { /* list of languages i.e. in sidebar */
	global $xili_language;
	if ($xili_language->this_has_filter('xili_language_list')){ 
		remove_filter('xili_language_list',$xili_language->idx['xili_language_list']); /*no default from class*/
	}	
	do_action('xili_language_list',$before,$after,$theoption); 
}
Second example : the class add a filter

here to adapt category_link
This function is used in customization functions when we need to know « category link » without xili-language filtering (by instance as in wp_categories_list()).

1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
/**
 * Replace get_category_link to bypass hook from xili_language
 *
 * @since 0.9.7.4
 * @updated 1.0.1
 * can be used in functions.php for special action needing permalink
 
 * @param category ID 
 * @return the permalink of passed cat_id.
 */
function xiliml_get_category_link($catid = 0) {
			global $xili_language;
			if ($catid == 0) {
				global $wp_query;
				$catid = $wp_query->query_vars['cat'];
			}
			remove_filter('category_link', $xili_language->idx['xiliml_link_append_lang']);
				$catcur = get_category_link($catid); 
			add_again_filter('category_link', 'xiliml_link_append_lang');
	return $catcur;
}

It is possible because class memorize unique id when filter is added :

129
130
131
132
add_filter('category_link', array(&$this,'xiliml_link_append_lang'));
		$filter = 'category_link';
		$function = 'xiliml_link_append_lang';
		$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 */

By offering API and hooks, wordpress opens wide world of customizations but needs rigourous coding…

In this coding extract, some lines about how, here on this website, the language’s list on sidebar is adapted via a function inside functions.php :

} elseif ($theoption == 'siderss') {
		if (is_category()) {  
			$catcururl = xiliml_get_category_link();
			$currenturl = $catcururl.'&amp;';
			//print_r();
			$cat_ID = $wp_query->query_vars['cat'];
			$currentrss = get_bloginfo('siteurl').'?feed=rss2&amp;cat='.$cat_ID; 
		} else { // home
		 	$currenturl = get_bloginfo('siteurl').'/?';
		 	$currentrss = get_bloginfo('siteurl').'/?feed=rss2';
		}
		$listlanguages = get_terms_of_groups_lite (the_cur_langs_group_id(),TAXOLANGSGROUP,TAXONAME,'ASC');	
 
		foreach ($listlanguages as $language) {
			if ($language->slug != 'ar_ar' && $language->slug != 'ar_ma') {
				if ($before=='<li>') {
					if (the_curlang() == $language->slug) { 
						$beforee = '<li class="current-cat" >';
					} else {
						$beforee ='<li>';
					}
				}
 
				$a .= $beforee ."<a href='".$currenturl.QUETAG."=".$language->slug."' title='".__('Posts selected',THEME_TEXTDOMAIN)." ".__('in '.$language->description,THEME_TEXTDOMAIN)."'>"." <img src='".get_bloginfo('template_directory')."/images/flags/".$language->slug.".png' alt='' /> ". __('in '.$language->description,THEME_TEXTDOMAIN) ."</a> <a href='".$currentrss.'&amp;'.QUETAG."=".$language->slug."'><img src='".get_bloginfo('template_directory')."/images/rss.png' alt='rss'/></a>".$after;
			}
 
		}
 
		if (is_category()) {
			$currenturl = xiliml_get_category_link();
			$a .= $before."<a href='".$currenturl."' title='".__('Posts of current category in all languages',THEME_TEXTDOMAIN)."' >"." <img src='".get_bloginfo('template_directory')."/images/flags/www.png' alt='' /> ".__('in all languages',THEME_TEXTDOMAIN)."</a> <a href='".$currentrss."'><img src='".get_bloginfo('template_directory')."/images/rss.png' alt='rss'/></a>".$after;
			}
		echo $a;	
 
	}

Hope that post will help and begin comments…

 
 

xili-language: others posts for expert
  1. xili-language plugin : release 2.1
  2. Q&A about xili-language plugin, release 1.1
  3. xili-language : hooks, filters, class and so on...
  4. From a theme with localization to a multilingual theme with xili-language : technical and general notes.
  5. xili-language - 1.4.1 - new libraries
  6. Xili-language toolbox in one table
  7. xili-language and the time
  8. xili-language : Affichage des dates et heures
 
 

This entry was posted in Experts corner, xili-language and tagged , , , , . Bookmark the permalink.

Laisser un commentaire