<?php
defined('ABSPATH') || die('Cheatin\' uh?');

class STRB_Controllers_Frontend extends STRB_Classes_FrontController
{

    /**
     * 
     *
     * @var STRB_Models_Frontend 
     */
    public $model;

    public function __construct()
    {

        if (is_admin() || is_network_admin() || STRB_Classes_Helpers_Tools::isAjax()) {
            return;
        }

        add_filter('the_content', function ($content){
            
            $themes=["standard/sidebar1","standard/sidebar2","standard/sidebar3.1","standard/sidebar3.2","standard/sidebar3.3","standard/sidebar4","standard/sidebar5","standard/sidebar6"];
            $classic_themes=["classic/business","classic/business2","classic/business3","classic/business4","classic/business5","classic/business6","classic/business7","classic/business8","classic/business9","classic/business10","classic/business11","classic/business12"];
            
            $aut=get_post();
            $user_id=$aut->post_author;
            $STB_theme_data = new STRB_Models_ThemeUserData();  
            $STB_user_data = new STRB_Models_ThemeUserData();  
            $STB_theme_data->loadThemeDataByID($user_id);
            $STB_user_data->loadUserDataByID($user_id);
            $theme= $STB_theme_data->getData('user_theme');
            $img = $STB_theme_data->getData('background_img');

            if(empty($STB_theme_data->getData('user_theme'))){    
                  $author_obj =get_user_by('email',"strb@starbox.com");
                  if($author_obj!=null){
                    $id=$author_obj->ID;
                    $STB_theme_data2 = new STRB_Models_ThemeUserData();
                    $STB_user_data2 = new STRB_Models_ThemeUserData();
                    $STB_theme_data2->loadThemeDataByID($id);
                    $STB_user_data2 ->loadUserDataByID($id);
                    $theme= $STB_theme_data2->getData('user_theme');  
                    $img = $STB_theme_data2->getData('background_img');
                    $STB_theme_data->setData("brightness",$STB_theme_data2->getData('brightness'));  
                    $STB_theme_data->setData("opacity",$STB_theme_data2->getData('opacity'));  
                    $STB_theme_data->setData("grayscale",$STB_theme_data2->getData('grayscale'));  
                    $STB_theme_data->setData("saturation",$STB_theme_data2->getData('saturation'));  
                    $STB_theme_data->setData("pnl_color",$STB_theme_data2->getData('pnl_color'));  
                    $STB_theme_data->setData("brd_color",$STB_theme_data2->getData('brd_color'));  
                    $STB_theme_data->setData("pnl_opacity",$STB_theme_data2->getData('pnl_opacity'));  
                    $STB_theme_data->setData("pnl_up",$STB_theme_data2->getData('pnl_up'));  
                    $STB_theme_data->setData("pnl_left",$STB_theme_data2->getData('pnl_left'));

                    STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_theme',$STB_theme_data->toMysql(),$user_id);

                    $STB_user_data->setData("img",$STB_user_data2->getData("img"));
                    STRB_Classes_Helpers_Tools::saveUserMeta('strb_user_main_meta',$STB_user_data->toMysql(),$user_id);}
            }

            $content .= "";
          
            STRB_Classes_ObjController::getClass('STRB_Classes_HookController')->getShortcodes();
            $autorBox = $this->getThemeViewImg($user_id,$theme,$img,TRUE);
            for($p=1;$p<21;$p++){
                $themes[]="sidebar/sidebar".$p;
            }
            if((in_array(get_post_type(),STRB_Classes_Helpers_Tools::getOption('field-check') ) || STRB_Classes_Helpers_Tools::getOption('allPost')=='true') && empty(get_user_meta($user_id,"strb_dont_show_author_box",true))){    
                if(!in_array($theme,$themes)){
                    if(in_array($theme,$classic_themes)){
                        $content .= "<div style='width:100%;display:flex;align-items:center;justify-content:center;margin-top:2vw;'>
                                        <div style='width:100%;'>$autorBox</div>
                                    </div>";
                    }else
                    $content .= "<div style='width:100%;display:flex;align-items:center;justify-content:center;margin-top:2vw;'>
                                    <div style='width:100%;display:flex;align-items:center;justify-content:center;'>$autorBox</div>
                                </div>";
                }
            }

            return $content;
        });
        
        add_action('dynamic_sidebar_before', function($content){
            for($p=1;$p<21;$p++){
                $themes[]="sidebar/sidebar".$p;
            }

            $aut=get_post();
            $user_id=$aut->post_author;

            $STB_theme_data = new STRB_Models_ThemeUserData();  
            $STB_user_data = new STRB_Models_ThemeUserData();  

            $STB_theme_data->loadThemeDataByID($user_id);
            $STB_user_data->loadUserDataByID($user_id);
            $theme= $STB_theme_data->getData('user_theme');
            $img = $STB_theme_data->getData('background_img');
            $autorBox=$this->getThemeViewImg($user_id,$theme,$img,TRUE);

            STRB_Classes_ObjController::getClass('STRB_Classes_HookController')->getShortcodes();
            
            if($content==="sidebar-1" || $content=="sidebar-woocommerce"){
                if((in_array(get_post_type(),STRB_Classes_Helpers_Tools::getOption('field-check') ) || STRB_Classes_Helpers_Tools::getOption('allPost')=='true') && empty(get_user_meta($user_id,"strb_dont_show_author_box",true))){    
                    if(in_array($theme,$themes)){
                        $content = "<div style='width:100%;margin-bottom:10px;'>$autorBox</div>";
                        echo $content;
                    }
                }
            }
        },false);
    
    }
}
