-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
119 lines (106 loc) · 3.42 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
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 多模板切换
*
* @package MultiTheme
* @author HackRose
* @version 1.0.0
* @link http://hackrose.com
*/
class MultiTheme_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('index.php')->begin = array('MultiTheme_Plugin', 'load');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
* 设置切换主题的cookie字段
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
Typecho_Widget::widget('Widget_Options')->to($options);
Typecho_Widget::widget('Widget_Themes_List')->to($themes);
$availableThemes = array();
while($themes->next()){
$availableThemes[$themes->name] = $themes->title;
}
$field = new Typecho_Widget_Helper_Form_Element_Text('field', NULL, 'theme', _t('url中带有此参数时切换主题'));
$form->addInput($field);
$theme = new Typecho_Widget_Helper_Form_Element_Select(
'default', $availableThemes, $options->theme,
'默认主题', '设置你前台默认的主题');
$form->addInput($theme);
$allowExchange = new Typecho_Widget_Helper_Form_Element_Checkbox(
'allow', $availableThemes, array_keys($availableThemes),
'可选主题', '设置你经常使用的默认允许在前台切换的主题');
$form->addInput($allowExchange);
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 支持切换的主题
* @return mixed
* @throws Typecho_Widget_Exception
*/
public static function themes(){
Typecho_Widget::widget('Widget_Options')->to($options);
$options->theme = $options->plugin('MultiTheme')->default;
return $options->plugin('MultiTheme')->allow;
}
/**
* 切换主题
*
* @access public
* @return void
*/
public static function load()
{
Typecho_Widget::widget('Widget_Options')->to($options);
$options->theme = $options->plugin('MultiTheme')->default;
$themeByKey = $options->plugin('MultiTheme')->field;
$allowThemes = $options->plugin('MultiTheme')->allow;
$request = Typecho_Request::getInstance();
$themeFromRequest = $request->get($themeByKey);
if($themeFromRequest){
if(in_array($themeFromRequest, $allowThemes)) {
$theme = $themeFromRequest;
}else{
$theme = null;
}
}else{
$theme = Typecho_Cookie::get('__typecho_multi_theme');
}
if($theme && in_array($theme, $allowThemes)){
Typecho_Cookie::set('__typecho_multi_theme', $theme);
$options->theme = $theme;
}
return;
}
}