<?php
/*
Plugin Name: 主题自动生成边栏自定义组件
Version: 1.0
Plugin URL:https://owoii.com/EMLOG/104.html
Description: 或许会是主题开发者的重要的插件之一，教程-->主题开发者前来，使用者不用来了</a>
Author: 听风
Author URL: https://owoii.com
*/

!defined('EMLOG_ROOT') && exit('access deined!');

//判断文件是否存在
function dC_getTemplateDefinedOptions($template) {
    if (!is_dir(TPLS_PATH . $template)) {
		return false;
	}
	if (!is_file($optionFile = TPLS_PATH . $template . '/dCSettings.php')) {
		return false;
	}
	include $optionFile;
	if (!isset($dCSettings) || !is_array($dCSettings)) {
		return false;
	}
	if (strpos(file_get_contents($optionFile), '@support dC') !== false) {
		return $dCSettings;
	}
	return false;
}



function dCsidebar($options_cache){
    
    
    $template = Option::get('nonce_templet');
    $dCSettings = dC_getTemplateDefinedOptions($template);
    //如果主题的自定义组件配置存在，那么将执行操作
    if($dCSettings!==false){
    
        $widgets = !empty($options_cache['widgets1']) ? unserialize($options_cache['widgets1']) : array();
        $dCcustom_widget =unserialize($options_cache['custom_widget']);
    	doAction('diff_side');
    	foreach ($widgets as $val) {
    		$widget_title = @unserialize($options_cache['widget_title']);
    		$custom_widget = @unserialize($options_cache['custom_widget']);
    		if (strpos($val, 'custom_wg_') === 0) {
    		    
    		    //判断是否是主题指定的自定义边栏组件
                foreach ($dCSettings as $tmVal){
                    if($template.'主题_'.$tmVal['name']==$dCcustom_widget[$val]['title']){

                        $callback = 'dc_'.$tmVal['point'];
            			if (function_exists($callback)) {
            				call_user_func($callback, htmlspecialchars($custom_widget[$val]['content']));
            			}else{
            			    $callback = 'widget_custom_text';
                			if (function_exists($callback)) {
                				call_user_func($callback, htmlspecialchars($custom_widget[$val]['title']), $custom_widget[$val]['content']);
                			}
            			}
            			
                    }
                }
    			
    		} else {
    			$callback = 'widget_' . $val;
    			if (function_exists($callback)) {
    				preg_match("/^.*\s\((.*)\)/", $widget_title[$val], $matchs);
    				$wgTitle = isset($matchs[1]) ? $matchs[1] : $widget_title[$val];
    				call_user_func($callback, htmlspecialchars($wgTitle));
    			}
    			
    		}
    	}
    	
    
        
    }else{
        $widgets = !empty($options_cache['widgets1']) ? unserialize($options_cache['widgets1']) : array();
    	doAction('diff_side');
    	foreach ($widgets as $val) {
    		$widget_title = @unserialize($options_cache['widget_title']);
    		$custom_widget = @unserialize($options_cache['custom_widget']);
    		if (strpos($val, 'custom_wg_') === 0) {
    			$callback = 'widget_custom_text';
    			if (function_exists($callback)) {
    				call_user_func($callback, htmlspecialchars($custom_widget[$val]['title']), $custom_widget[$val]['content']);
    			}
    		} else {
    			$callback = 'widget_' . $val;
    			if (function_exists($callback)) {
    				preg_match("/^.*\s\((.*)\)/", $widget_title[$val], $matchs);
    				$wgTitle = isset($matchs[1]) ? $matchs[1] : $widget_title[$val];
    				call_user_func($callback, htmlspecialchars($wgTitle));
    			}
    		}
    	}
    }
    
    
    
    

    
}

function admConfig(){
    if($_SERVER['PHP_SELF'] =='/admin/widgets.php'){
        
        $template = Option::get('nonce_templet');
        $dCSettings = dC_getTemplateDefinedOptions($template);
        //如果主题的自定义组件配置存在，那么将执行操作
        if($dCSettings!==false){
            
            if (!is_file($dC_config = TPLS_PATH . $template . '/dC_config.txt')) {
        		//还未生成配置，执行配置生成
        		/**********配置生成**********/
                
                //以读写方式打写指定文件，如果文件不存则创建
                if( ($TxtRes=fopen ($dC_config,"w+")) === FALSE){
                echo("创建可写文件：".$dC_config."失败");
                exit();
                }
    
                $StrConents = "dc插件的配置插件配置，如果您主题的边栏自定义组件不小心删除了，可通过删除此文件恢复(前提是把之前主题边栏组件删除了)";//要 写进文件的内容
                if(!fwrite ($TxtRes,$StrConents)){ //将信息写入文件
                fclose($TxtRes);
                exit();
                }
                fclose ($TxtRes); //关闭指针
                /**********配置生成结束**********/
                
                /*****************开始执行**********************/
                
                
        		global $CACHE;
        		foreach ($dCSettings as $vals){
        		    $custom_widget = Option::get('custom_widget');
        		   //确定组件索引
        			$i = 0;
        			$maxKey = 0;
        			if (is_array($custom_widget)) {
        				foreach ($custom_widget as $key => $val) {
        					preg_match("/^custom_wg_(\d+)/", $key, $matches);
        					$k = $matches[1];
        					if ($k > $i) {
        						$maxKey = $k;
        					}
        					$i = $k;
        				}
        			}
        			$custom_wg_index = $maxKey + 1;
        			$custom_wg_index = 'custom_wg_' . $custom_wg_index;
        			$custom_widget[$custom_wg_index] = array('title' => $template.'主题_'.$vals['name'], 'content' => $vals['name']."标题在后台边栏组件定义");
        			$custom_widget_str = addslashes(serialize($custom_widget));

        			Option::updateOption('custom_widget', $custom_widget_str); 
        		    $CACHE->updateCache('options');
        		    
        		}
        		emDirect("./widgets.php?activated=1");
        		/*****************开始执行**********************/
        		
        		
        		
        		
        		
    	    }
            
        }
        
?>
<script>
    $(function(){
        <?php 
        foreach ($dCSettings as $val){
        ?>
        $('input[value="<?php echo $template.'主题_'.$val['name']; ?>"]').hide().after("<li>标题</li>");
        <?
        }
        ?>
       
    });
</script>
<?php

    }
}
addAction('adm_footer', 'admConfig');
