-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
163 lines (147 loc) · 5.11 KB
/
Plugin.php
File metadata and controls
163 lines (147 loc) · 5.11 KB
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
<?php
/**
* 页面和文章自定义编辑字段插件<br>
* <i>Typecho插件定制QQ86199786</i>
*
*
* @package CustomContentField
* @author 清风
* @version 1.0.0
* @link http://www.quhe.net
*/
namespace TypechoPlugin\CustomContentField;
use Typecho\Plugin\PluginInterface;
use Typecho\Widget\Helper\Form;
use Typecho\Widget\Helper\Form\Element\Text;
use TypechoPlugin\CustomContentField\lib\Helper;
use Widget\Contents\Post\Edit as PostEdit;
use Widget\Contents\Page\Edit as PageEdit;
class Plugin implements PluginInterface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*/
public static function activate()
{
\Typecho\Plugin::factory('admin/write-post.php')->option = __CLASS__ . '::postRender';
\Typecho\Plugin::factory('admin/write-page.php')->option = __CLASS__ . '::pageRender';
\Typecho\Plugin::factory(PostEdit::class)->finishPublish = __CLASS__ . '::postFinish';
\Typecho\Plugin::factory(PostEdit::class)->finishSave = __CLASS__ . '::postFinish';
\Typecho\Plugin::factory(PageEdit::class)->finishPublish = __CLASS__ . '::pageFinish';
\Typecho\Plugin::factory(PageEdit::class)->finishSave = __CLASS__ . '::pageFinish';
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*/
public static function deactivate()
{
}
/**
* 获取插件配置面板
*
* @param Form $form 配置面板
*/
public static function config(Form $form)
{
$postField = new Text(
'post_field',
null,
null,
_t('文章字段'),
'1.格式:数据库字段名|显示标签名<br>2.多个用逗号","隔开<br>3.例子:views|浏览量,agree|点赞数'
);
$pageField = new Text(
'page_field',
null,
null,
_t('页面字段'),
'1.格式:数据库字段名|显示标签名<br>2.多个用逗号","隔开<br>3.例子:views|浏览量,agree|点赞数'
);
$form->addInput($postField);
$form->addInput($pageField);
}
/**
* 个人用户的配置面板
*
* @param Form $form
*/
public static function personalConfig(Form $form)
{
}
/**
* @param $page Widget\Contents\Page\Edit
* @return void
* @throws \Typecho\Plugin\Exception
* @throws \Typecho\Db\Exception
*/
public static function pageRender($page)
{
$pageField = trim(\Typecho\Widget::widget('Widget_Options')->plugin('CustomField')->page_field);
if ($pageField) {
$html = '<hr style="height: 1px;border:none;border-top: 1px dashed #009688;">';
$fieldsValue = Helper::getContentFieldsById($page->cid, Helper::getFields('page'));
foreach (explode(',', $pageField) as $field) {
$fieldArr = explode('|', $field);
$fieldLabel = $fieldArr[1] ?? $fieldArr[0];
$fieldValue = $fieldsValue[$fieldArr[0]];
$html .= <<<HTML
<section class="typecho-post-option">
<label class="typecho-label">{$fieldLabel}</label>
<p><input id="{$fieldArr[0]}" name="{$fieldArr[0]}" type="text" value="{$fieldValue}" class="w-100 text"/></p>
</section>
HTML;
}
$html .= '<hr style="height: 1px;border:none;border-top: 1px dashed #009688;">';
echo $html;
}
}
/**
* @param $post Widget\Contents\Post\Edit
* @return void
* @throws \Typecho\Plugin\Exception
* @throws \Typecho\Db\Exception
*/
public static function postRender($post)
{
$postField = Helper::getFields('post', true);
if ($postField) {
$html = '<hr style="height: 1px;border:none;border-top: 1px dashed #009688;">';
$fieldsValue = Helper::getContentFieldsById($post->cid, Helper::getFields('post'));
foreach ($postField as $field) {
$fieldArr = explode('|', $field);
$fieldLabel = $fieldArr[1] ?? $fieldArr[0];
$fieldValue = $fieldsValue[$fieldArr[0]];
$html .= <<<HTML
<section class="typecho-post-option">
<label class="typecho-label">{$fieldLabel}</label>
<p><input id="{$fieldArr[0]}" name="{$fieldArr[0]}" type="text" value="{$fieldValue}" class="w-100 text"/></p>
</section>
HTML;
}
$html .= '<hr style="height: 1px;border:none;border-top: 1px dashed #009688;">';
echo $html;
}
}
/**
* @param $contents
* @param $edit PageEdit
* @return array
* @throws \Typecho\Plugin\Exception
*/
public static function pageFinish($contents, $edit)
{
$customFiledInput = $edit->request->from(Helper::getFields('page'));
Helper::updateFields($edit->cid, $customFiledInput);
}
/**
* @param $contents
* @param $edit PostEdit
* @return array
* @throws \Typecho\Plugin\Exception
*/
public static function postFinish($contents, $edit)
{
$customFiledInput = $edit->request->from(Helper::getFields('post'));
Helper::updateFields($edit->cid, $customFiledInput);
}
}