<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO

if(@txpinterface == 'public'){
    if( 
function_exists('aks_filter_add') ){
        
aks_filter_add('aks_meta__filter'20);
    }else{
//        trigger_error("Unable to include required plugin 'aks_header' v0.4+", E_USER_ERROR);    // E_USER_WARNING
        
register_callback('aks_meta__callback','textpattern');    // standalone...
    
}

    if (
class_exists('\Textpattern\Tag\Registry')) {
        
Txp::get('\Textpattern\Tag\Registry')
            ->
register('aks_meta_head')
            ->
register('aks_meta_rel')
            ->
register('aks_meta')
            ->
register('aks_meta_title')
            ->
register('aks_meta_keywords')
            ->
register('aks_meta_description')
            ->
register('aks_meta_words')
        ;
    }
}

function 
aks_meta_head($atts$this NULL){
    global 
$variable;
    
extract(lAtts(array(
        
'name'        => '',
        
'value'        => ''
    
),$atts));
    if(!
$value && $this){ $value parse($this); }
    if( !
$value ){ return; }
    if( 
$name ){
        
$variable["aks_meta_head"][$name] = $value;
    }else{
        
$variable["aks_meta_head"][] = $value;
    }
}

function 
aks_meta_rel($atts$this NULL){
    global 
$variable;
    
extract(lAtts(array(
        
'name'        => '',
        
'title'        => '',
        
'url'        => ''
    
),$atts));
    if(!
$url && $this){ $url parse($this); }
    if( !
$url || !$name ){ return; }
    if(
$title){
        
$title trim(preg_replace('%\s+%su'' 'strip_tags($title) ));
        
$title trim(str_replace( array('"','>','<'), array('&quot;','&gt;','&lt;'), $title));
        
$title "title=\"{$title}\"";
    }else{
        
$title "";
    }
    
$txt "<link rel=\"{$name}\" {$title} href=\"".trim($url)."\" />";
    
$variable["aks_meta_head"]["aks_rel_$name"] = $txt;
}


function 
aks_meta($atts$this NULL){
    if( 
$mark=@$atts['mark'] ){ return "<!--aks_mark_{$mark}-->"; }
    if( !(
$name=@$atts['name']) ){ return; }
    static 
$s;    if( is_null($s) ){ $s = array();    $s['title']=" :: ";    $s['keywords']=",";    $s['description']=" "; }

    if( isset(
$atts['separator']) ){ $s[$name]=$atts['separator']; }
    
$def_strip=preg_match('/^(title|keywords|description)$/i'$name);

    
extract(lAtts(array(
        
'name' => '',
        
'add'  => '',            // 'first' or 'last'
        
'separator' => @$s[$name],    // used with `add` attribute
        
'value'  => '',
        
'strip'  => $def_strip
    
),$atts));

    global 
$variable;
    if(!
$value && $this){ $value=parse($this); }

    if(
$strip){
        
$value=trim(preg_replace('%\s+%su'' 'strip_tags($value) ));
        
$value=str_replace( array('"','>','<'), array('&quot;','&gt;','&lt;'), $value);
    }

    if(
$add){
        if(
$add=='first'  && $value){ @$variable["aks_meta_$name"]=$value.$separator.@$variable["aks_meta_$name"]; }
        if(
$add=='last'   && $value){ @$variable["aks_meta_$name"].=$separator.$value; }
    }else{
        
$variable["aks_meta_$name"]=$value;
    }
}

// short tags
function aks_meta_title($atts$this NULL){        $atts["name"]="title";        aks_meta($atts$this); }
function 
aks_meta_keywords($atts$this NULL){    $atts["name"]="keywords";    aks_meta($atts$this); }
function 
aks_meta_description($atts$this NULL){    $atts["name"]="description";    aks_meta($atts$this); }




function 
aks_meta__callback(){
    if(!
in_array('aks_meta__filter'ob_list_handlers() )){ ob_start('aks_meta__filter'); }
}


function 
aks_meta__filter($html){
    global 
$variable;
    
$meta="";
    if(@
$variable['aks_meta_title']){
        
$html=preg_replace('%([ \t]+)?<title[^>]*>.*?</title[^>]*>([\r\n]+)?%is'''$html);
        
$meta.="<title>{$variable['aks_meta_title']}</title>\n";
    }
    if(@
$variable['aks_meta_keywords']){
        
$keys=preg_replace('%[ \.,]?,[ \.,]?%'','$variable['aks_meta_keywords']);
        
$keys=aks_meta_words($keys30);
        
$html=preg_replace('%([ \t]+)?<meta [^>]*name[ =\"\']*keywords[^>]*>([\r\n]+)?%is'''$html);
        
$meta.='<meta name="keywords" content="'.$keys.'" />'.n;
    }
    if(@
$variable['aks_meta_description']){
        
$html=preg_replace('%([ \t]+)?<meta [^>]*name[ =\"\']*description[^>]*>([\r\n]+)?%is'''$html);
        
$meta.='<meta name="description" content="'.$variable['aks_meta_description'].'" />'.n;
    }
    if(@
is_array($variable['aks_meta_head']) ){
        
$meta.=join(""$variable['aks_meta_head']);
    }
    if(
$meta){
//        $html=preg_replace('%</head>%is', "$meta</head>", $html);        // insert before </head>
//        $html=preg_replace('%(<head[^>]*>)%is', "$1\n$meta", $html);        // insert after <head>
        
$html=str_replace("<head>""<head>$meta"$html);
    }
    
$html=preg_replace_callback('/<!--aks_mark_(.*?)-->/xsi'"_aks_mark"$html);
    return 
$html;
}

function 
_aks_mark($mm){
    global 
$variable;
    if( isset( 
$variable["aks_meta_$mm[1]"] ) ){ return $variable["aks_meta_$mm[1]"]; }
}

function 
aks_meta_words($txt=''$words=50) {
    if(
$words){
//        $word = split(" ", $value);
//        if(count($word) >$words){
        
$txt=implode(","array_slice(preg_split("%[,]+%"$txt), 0$words));
    }
    return 
$txt;
}