forked from Moidea/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
139 lines (128 loc) · 4.17 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* URL后添加 ?theme={主题目录} | 为空则删除cookie,恢复默认
*
* @category system
* @package ThemeDemo
* @author doudou
* @version 1.0.1
* @link https://github.com/doudoutime
*/
class ThemeDemo_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Archive')->handleInit = array('ThemeDemo_Plugin', 'setTheme');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function setTheme($widget)
{
$cookie = Array (
'name' => '__typecho_theme',
'expire' => 86400, //默认cookie存活时间
);
$options = Typecho_Widget::widget('Widget_Options');
if (isset($widget->request->theme) && $widget->request->isGet()) {
if ($widget->request->theme) {
$theme = $widget->request->theme;
if (static::check($theme)) {
Typecho_Cookie::set($cookie['name'], $widget->request->theme, $options->gmtTime + $cookie['expire'], $options->siteUrl);
} else {
$widget->response->redirect(Typecho_Common::url($widget->request->getPathInfo(), $options->siteUrl));
}
} else {
Typecho_Cookie::delete($cookie['name']); //直接提交?theme将删除cookie,恢复默认主题
return;
}
} else {
$theme = Typecho_Cookie::get($cookie['name']);
if (!$theme) return;
if (!static::check($theme)) {
Typecho_Cookie::delete($cookie['name']);
return;
}
}
/** 删除旧主题的相关设置 */
$themeRow = 'theme:' . $options->theme;
if (isset($options->{$themeRow})) {
$config = unserialize($options->{$themeRow});
$options->{$themeRow} = '';
foreach ($config as $row => $value) {
$options->{$row} = '';
}
}
/** 载入新主题的相关设置 参考var/Widget/Themes/Edit.php */
$themeDir = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR;
$configFile = $themeDir . 'functions.php';
if (file_exists($configFile)) {
require_once $configFile;
if (function_exists('themeConfig')) {
$form = new Typecho_Widget_Helper_Form();
themeConfig($form);
$config = $form->getValues();
if ($config) {
$options->{'theme:' . $theme} = serialize($config);
foreach ($config as $row => $value) {
$options->{$row} = $value;
}
}
}
}
/** 修改$this->options->theme */
$options->theme = $theme;
/** 修改$this->_themeDir */
$widget->setThemeDir($themeDir);
}
/**
* 检查主题目录是否存在
*
* @access public
* @return void
*/
public static function check($theme)
{
$themeDir = __TYPECHO_ROOT_DIR__ . __TYPECHO_THEME_DIR__ . DIRECTORY_SEPARATOR . $theme;
if (is_dir($themeDir)) {
return true;
}
return false;
}
}