forked from nakbo/Dynamics-typecho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
211 lines (192 loc) · 6.07 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
include_once 'Dynamics.php';
/**
* 我的动态 - 南博助手
* @package Dynamics
* @author 权那他
* @version 1.3
*/
class Dynamics_Plugin implements Typecho_Plugin_Interface
{
const DYNAMICS_ROUTE = '/dynamics/';
private static $instance;
public static $homeUrl;
public static $themeDirUrl;
public static $themeName;
// 激活插件
public static function activate()
{
Helper::addPanel(3, 'Dynamics/manage-dynamics.php', '我的动态', '我的动态列表', 'administrator');
Helper::addAction('dynamics-manage', 'Dynamics_Action');
Helper::addRoute('dynamics-index-route', Dynamics_Plugin::DYNAMICS_ROUTE, 'Dynamics_Action', 'dispatchIndex');
Helper::addRoute('dynamics-route', Dynamics_Plugin::DYNAMICS_ROUTE . "[slug]/", 'Dynamics_Action', 'dispatch');
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
/** @noinspection SqlResolve */
/** @noinspection SqlNoDataSourceInspection */
$db->query('CREATE TABLE IF NOT EXISTS `' . $prefix . 'dynamics` (
`did` int(11) unsigned NOT NULL AUTO_INCREMENT,
`authorId` int(11) DEFAULT NULL,
`text` text,
`status` varchar(16) DEFAULT "publish",
`created` int(10) DEFAULT 0,
`modified` int(10) DEFAULT 0,
PRIMARY KEY (`did`)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;');
return _t('插件已经激活');
}
// 禁用插件
public static function deactivate()
{
Helper::removePanel(3, 'Dynamics/manage-dynamics.php');
Helper::removeAction('dynamics-manage');
Helper::removeRoute('dynamics-index-route');
Helper::removeRoute('dynamics-route');
return _t('插件已被禁用');
}
/**
* 动态首页的路径
* @param string $path
* @param bool $isReturn
* @return string
*/
public static function homeUrl($path = "", $isReturn = false)
{
if (self::$homeUrl == null) {
try {
self::$homeUrl = Typecho_Common::url(Dynamics_Plugin::DYNAMICS_ROUTE, Typecho_Widget::widget('Widget_Options')->index);
} catch (Typecho_Exception $e) {
}
}
$url = self::$homeUrl . $path;
if ($isReturn) {
return $url;
} else {
echo $url;
}
}
/**
* 主题的路径
* @param string $path
* @param bool $isReturn
* @return string
*/
public static function themeDirUrl($path = "", $isReturn = false)
{
if (self::$themeDirUrl == null) {
try {
self::$themeDirUrl = Typecho_Common::url('/Dynamics/' . Dynamics_Plugin::themeName() . "/", Typecho_Widget::widget('Widget_Options')->pluginUrl);
} catch (Typecho_Exception $e) {
}
}
$url = self::$themeDirUrl . $path;
if ($isReturn) {
return $url;
} else {
echo $url;
}
}
public static function themeName()
{
if (self::$themeName == null) {
try {
self::$themeName = "themes/" . Typecho_Widget::widget('Widget_Options')->Plugin('Dynamics')->theme;
} catch (Typecho_Exception $e) {
}
}
return self::$themeName;
}
/**
* 根据did 计算动态的链接
* @param $did
* @param bool $isReturn
* @return string
*/
public static function applyUrl($did, $isReturn = false)
{
if ($isReturn) {
return self::homeUrl(base64_encode($did), true);
} else {
self::homeUrl(base64_encode($did), false);
}
}
/**
* 根据 slug 反解 did
* @param $slug
* @return int|string|null
*/
public static function parseUrl($slug)
{
$did = base64_decode($slug);
return is_numeric($did) ? $did : null;
}
/**
* 在主题中直接调用
*
* @access public
* @param null $pattern
* @param int $num
*/
public static function output($pattern = NULL, $num = 5)
{
$action = new Dynamics_Action(
Typecho_Request::getInstance(),
Typecho_Response::getInstance()
);
$action->showPage();
}
public static function get($params = array())
{
$dynamics = new Dynamics(
Typecho_Request::getInstance(),
Typecho_Response::getInstance()
);
$dynamics->parse($params);
return $dynamics;
}
public static function getInstance($params = array())
{
if (self::$instance == null) {
self::$instance = self::get($params);
}
return self::$instance;
}
public static function getList()
{
$list = array();
$themes = glob(__TYPECHO_ROOT_DIR__ . __TYPECHO_PLUGIN_DIR__ . '/Dynamics/themes/*', GLOB_ONLYDIR);
if ($themes) {
foreach ($themes as $key => $theme) {
$themeFile = $theme . '/index.php';
if (file_exists($themeFile)) {
$list[basename($theme)] = basename($theme);
}
}
}
return $list;
}
// 插件配置面板
public static function config(Typecho_Widget_Helper_Form $form)
{
$radio = new Typecho_Widget_Helper_Form_Element_Text(
'pageSize', null, '5',
'动态首页每页数目', '此数目用于动态首页每页显示的文章数目.');
$form->addInput($radio);
$radio = new Typecho_Widget_Helper_Form_Element_Radio(
'isPjax', array(
'0' => '未启用',
'1' => '已启用',
), '0', 'Pjax状态', '是否开启Pjax状态,未完善');
$form->addInput($radio);
$radio = new Typecho_Widget_Helper_Form_Element_Radio(
'theme', self::getList(), 'default', _t('模板选择'), "选择一个动态的主题");
$form->addInput($radio);
}
// 个人用户配置面板
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
public static function form($action = NULL)
{
}
}