<?php

function zg2uni($html){
	global $converter, $zwsp;

//$converter->start_log();
$html = $converter->zg_uni($html);
//$converter->end_log();
$html = $zwsp->zwsp_mm3($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}

function zg2ayar($html){
	global $converter, $zwsp;
//$converter->start_log();
$html = $converter->zg_ayar($html);
//$converter->end_log();
$html = $zwsp->zwsp_ayar($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}

function ayar2zg($html){
	global $converter, $zwsp;
//$converter->start_log();
$html = $converter->ayar_zg($html);
//$converter->end_log();
$html = $zwsp->zwsp_zawgyi($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}
function ayar2uni($html){
	global $converter, $zwsp;
//$converter->start_log();
$html = $converter->ayar_uni($html);
//$converter->end_log();
$html = $zwsp->zwsp_mm3($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}

function uni2ayar($html){
	global $converter, $zwsp;
//$converter->start_log();
$html = $converter->uni_ayar($html);
//$converter->end_log();
$html = $zwsp->zwsp_ayar($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}

function uni2zg($html){
	global $converter, $zwsp;
//$converter->start_log();
$html = $converter->uni_zg($html);
//$converter->end_log();
$html = $zwsp->zwsp_zawgyi($html);

$info = sprintf(
        "<!-- \nPage : %s\nHTML size : %d bytes\n Generated by AyarWebKit Plugin-->",
        $_SERVER['REQUEST_URI'],
        strlen($html)
    );
return $html.$info;
}

function ayar_zg_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('ayar2zg');
}

function zg_ayar_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('zg2ayar');
}

function zg_uni_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('zg2uni');
}

function ayar_uni_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('ayar2uni');
}

function uni_ayar_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('uni2ayar');
}

function uni_zg_buffer(){
	//@credits http://w-shadow.com/blog/2010/05/20/how-to-filter-the-whole-page-in-wordpress/
    //Don't filter Dashboard pages
    if ( is_admin() ){
        return;
    }
    //Start buffering. Note that we don't need to
    //explicitly close the buffer - WP will do that
    //for use in the "shutdown" hook.
    ob_start('uni2zg');
}

function zawgyi_mm3(){
//	add_action('init','awk_rewrite_rules');
	//echo "zawgyi to mm3 is working";
	add_action('wp','zg_uni_buffer');
	}
	
function ayar_mm3(){
//	add_action('init','awk_rewrite_rules');
	add_action('wp','ayar_uni_buffer');
	}

function mm3_zawgyi(){
//	add_action('init','awk_rewrite_rules');
	//echo "mm3 to zawgyi is working";
	add_action('wp','uni_zg_buffer');
	}
	
function ayar_zawgyi(){
//	add_action('init','awk_rewrite_rules');
	//echo "ayar to zawgyi is working";
	add_action('wp','ayar_zg_buffer');

	}

function mm3_ayar(){
//	add_action('init','awk_rewrite_rules');
	//echo "mm3 to ayar is working";
	add_action('wp','uni_ayar_buffer');
	}

function zawgyi_ayar(){
//	add_action('init','awk_rewrite_rules');
	//echo "zawgyi to ayar is working";
	add_action('wp','zg_ayar_buffer');
	}

   // converts WP generated url to language specific based on plugin settings
   //This portion of code is taken from sitepress.class.php of sitepress multilingual cms
function convert_url($url){
        global $wpdb, $font_encoding, $written_font, $reader_font;
       // var_dump($url);

//var_dump($font_encoding);
        if($font_encoding && $font_encoding != $reader_font){
            $abshome = preg_replace('@\?font=[a-z0-9]@i','',get_option('home'));
                    // remove any previous value.
                    if (strpos($url, '?font=[a-z0-9]&') !== FALSE) {
                        $url = str_replace('?font=[a-z0-9]&', '', $url);
                    } elseif (strpos($url, '?font=[a-z0-9]/' ) !== FALSE) {
                        $url = str_replace('?font=[a-z0-9]/', '', $url);
                    } elseif (strpos($url, '?font=[a-z0-9]' ) !== FALSE) {
                        $url = str_replace('?font=[a-z0-9]', $url);
                    } elseif (strpos($url, '&font=[a-z0-9]/') !== FALSE) {
                        $url = str_replace('&font=[a-z0-9]/', '', $url);
                    } elseif (strpos($url, '&font=[a-z0-9]' ) !== FALSE) {
                        $url = str_replace('&font=[a-z0-9]', '', $url);
                    }
                    
                    if(false===strpos($url,'?')){
                        $url_glue = '?';
                    }else{

                        // special case post preview link
                        $db = debug_backtrace();
                        if(is_admin() && (@$db[6]['function'] == 'post_preview')){
                           $url_glue = '&';
                        }

                        elseif(isset($_POST['comment'])){ // will be used for a redirect
                            $url_glue = '&';
                        }else{
                            $url_glue = '&amp;';
                        }
                    }
                    $url .= $url_glue . 'font=' . $font_encoding;

        }
      return $url;
    }
    function permalink_filter($p){                
        global $wpdb, $font_encoding, $written_font, $reader_font;
        
        if(!empty($font_encoding) && $font_encoding && $reader_font != $font_encoding){
            $p = convert_url($p, $font_encoding);
        }elseif(isset($_POST['action']) && $_POST['action']=='sample-permalink'){ // check whether this is an autosaved draft
            $exp = explode('?', $_SERVER["HTTP_REFERER"]);
            if(isset($exp[1])) parse_str($exp[1], $args);
            if(isset($args['font']) && $written_font != $args['font']){
                $p = convert_url($p, $args['font']);
            }
        }
        if(is_feed()){
            $p = str_replace("&font=", "&#038;font=", $p);
        }
        return $p;
    }
    
    // filter for WP home_url function
    function awk_home_url($url){
        // only apply this for home url - not for posts or page permalinks since this filter is called there too
        if(did_action('template_redirect') && rtrim($url,'/') == rtrim(get_option('home'),'/')){
            $url = convert_url($url);
        }
        return $url;
    }
    
    function all_permalink_filter(){
		add_rewrite_endpoint('json', EP_ALL );
    }
// posts and pages links filters
add_filter('home_url', 'awk_home_url',1,2);
add_filter('post_link', 'permalink_filter',1,2);
add_filter('post_type_link', 'permalink_filter',1,2);
add_filter('page_link', 'permalink_filter',1,2);
add_action('wp_rewrite', 'all_permalink_filter');


