<?php

if(!defined('ABSPATH'))
    exit;

class SyrusFilters {

    public function initialize() {

        add_filter('robots_txt', [$this,'show_virtual_robots_txt'], 10,  2);
        add_filter('query_vars',[$this,'query_vars']);
        add_filter('the_content',[$this,'replace_span_tags']);
        add_filter('wpseo_metadesc',[$this,'wpseo_metadesc']);
        add_filter('wpseo_opengraph_desc',[$this,'wpseo_opengraph_desc']);
		add_filter('wp_kses_allowed_html',[$this,'wp_kses_allowed_html'], 10, 2 );
		
    }

    public function show_virtual_robots_txt($output, $public) {
        global $syrus;

        if($syrus->get_option("file",'abilita_robots_txt') != 1)
            return $output;
    
    
        $robots_txt = $syrus->get_option("file","robots_txt");
        return $robots_txt;
    }

    public function query_vars($query_vars) {
        $query_vars[] = "related_posts";
        return $query_vars;
    }

    public function replace_span_tags($text) {
        global $syrus;
        global $wp_embed;

        if(!$text)  
            return $text;
        
        $span_replace_chk = $syrus->get_option("generali","span_replace");
        $p_cleaner_chk = $syrus->get_option("generali","p_cleaner");
        $http_cleaner_chk = $syrus->get_option("generali","http_cleaner");
        

        $encoding = mb_detect_encoding($text);

        if($http_cleaner_chk) {
            $text = str_replace('http://','https://',$text);
        }
        
        $text = mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8');

        $html = new DOMDocument();
        @$html->loadHTML('<div>'.$text.'</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
        
        $ptag = $html->getElementsByTagName('p');
        
        $span_tags = $html->getElementsByTagName('span');
        
        if($span_replace_chk) {
            foreach($span_tags as $span){
            $span->removeAttribute('style');
            $span->removeAttribute('class');
            $span->removeAttribute('data-contrast');
            $span->removeAttribute('data-ccp-props');
            $span->removeAttribute('data-offset-key');
            }
        }
        
        
        if($p_cleaner_chk) {
            foreach($ptag as $p) {
                $p->removeAttribute('style');
            }
        }
        
        $root = $html->documentElement;
        
        $result = "";
        
        foreach($root->childNodes as $child) {
            $result .= $html->saveHTML($child);
        }
        
        $text = $result;
        
        if($span_replace_chk){
            $text = str_replace('<span>','',$text);
            $text = str_replace('</span>','',$text);
        }

        return wp_kses_post($wp_embed->autoembed($text));
    }

    public function wpseo_metadesc($description) {
        if(is_category() && is_paged() && $description != "")
            $description = "Page " . get_query_var('paged') . " - " . $description;
    
        return $description;
    }

    public function wpseo_opengraph_desc($description) {
        if(is_category() && is_paged() && $description != "")
            $description = "Page " . get_query_var('paged') . " - " . $description;

        return $description;
    }
	
	public function wp_kses_allowed_html( $tags, $context ) {
		if ( 'post' === $context ) {
			$tags['iframe'] = array(
				'src'             => true,
				'height'          => true,
				'width'           => true,
				'frameborder'     => true,
				'allowfullscreen' => true,
			);

            $tags['blockquote'] = [];
		}

		return $tags;
	}
          
}