仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ app_plugins/hello_demo/bootstrap.php 3.4 KB · 105 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2if (!defined('IN_CRONLITE')) {
3 exit;
4}
5
6mnbt_plugin_register('hello_demo', [
7 'name' => '示例插件 Hello',
8]);
9
10mnbt_register_menu('admin', [
11 'title' => 'Hello 示例',
12 'page' => 'index',
13 'icon' => 'mdi-hand-okay',
14 'order' => 10,
15 'multitabs' => true,
16]);
17
18mnbt_register_page('admin', 'index', 'admin/index.php', 'Hello 示例');
19
20mnbt_register_ajax('admin', 'p_hello_demo_ping', function () {
21 mnbt_plugin_require_admin();
22 $msg = mnbt_plugin_option_get('hello_demo', 'welcome', '你好,MNBT 插件!');
23 $count = (int)mnbt_plugin_option_get('hello_demo', 'ping_count', 0);
24 $count++;
25 mnbt_plugin_option_set('hello_demo', 'ping_count', (string)$count);
26 json_exit_success($msg, ['ping_count' => $count]);
27});
28
29mnbt_register_ajax('admin', 'p_hello_demo_save', function () {
30 mnbt_plugin_require_admin();
31 $welcome = isset($_POST['welcome']) ? trim((string)$_POST['welcome']) : '';
32 if ($welcome === '') {
33 json_exit_error('欢迎语不能为空');
34 }
35 if (mb_strlen($welcome) > 200) {
36 json_exit_error('欢迎语过长');
37 }
38 mnbt_plugin_option_set('hello_demo', 'welcome', $welcome);
39 json_exit_success('已保存');
40});
41
42mnbt_add_action('host.created', function ($host, $ctx = []) {
43 $u = is_array($host) ? ($host['user'] ?? '') : '';
44 $src = is_array($ctx) ? ($ctx['source'] ?? '') : '';
45 $line = date('Y-m-d H:i:s') . " created user={$u} source={$src}";
46 $log = mnbt_plugin_option_get('hello_demo', 'host_events', []);
47 if (!is_array($log)) {
48 $log = [];
49 }
50 array_unshift($log, $line);
51 $log = array_slice($log, 0, 50);
52 mnbt_plugin_option_set('hello_demo', 'host_events', $log);
53});
54
55mnbt_add_action('host.deleted', function ($host, $ctx = []) {
56 $u = is_array($host) ? ($host['user'] ?? '') : '';
57 $src = is_array($ctx) ? ($ctx['source'] ?? '') : '';
58 $line = date('Y-m-d H:i:s') . " deleted user={$u} source={$src}";
59 $log = mnbt_plugin_option_get('hello_demo', 'host_events', []);
60 if (!is_array($log)) {
61 $log = [];
62 }
63 array_unshift($log, $line);
64 $log = array_slice($log, 0, 50);
65 mnbt_plugin_option_set('hello_demo', 'host_events', $log);
66});
67
68mnbt_add_action('host.paused', function ($host, $ctx = []) {
69 $u = is_array($host) ? ($host['user'] ?? '') : '';
70 $line = date('Y-m-d H:i:s') . " paused user={$u}";
71 $log = mnbt_plugin_option_get('hello_demo', 'host_events', []);
72 if (!is_array($log)) {
73 $log = [];
74 }
75 array_unshift($log, $line);
76 $log = array_slice($log, 0, 50);
77 mnbt_plugin_option_set('hello_demo', 'host_events', $log);
78});
79
80mnbt_add_action('host.renewed', function ($host, $ctx = []) {
81 $u = is_array($host) ? ($host['user'] ?? '') : '';
82 $old = is_array($ctx) ? ($ctx['old_date'] ?? '') : '';
83 $new = is_array($ctx) ? ($ctx['new_date'] ?? '') : '';
84 $line = date('Y-m-d H:i:s') . " renewed user={$u} {$old}=>{$new}";
85 $log = mnbt_plugin_option_get('hello_demo', 'host_events', []);
86 if (!is_array($log)) {
87 $log = [];
88 }
89 array_unshift($log, $line);
90 $log = array_slice($log, 0, 50);
91 mnbt_plugin_option_set('hello_demo', 'host_events', $log);
92});
93
94mnbt_add_action('order.paid', function ($order, $ctx = []) {
95 $ddh = is_array($order) ? ($order['ddh'] ?? '') : '';
96 $je = is_array($order) ? ($order['je'] ?? '') : '';
97 $line = date('Y-m-d H:i:s') . " order.paid ddh={$ddh} je={$je}";
98 $log = mnbt_plugin_option_get('hello_demo', 'host_events', []);
99 if (!is_array($log)) {
100 $log = [];
101 }
102 array_unshift($log, $line);
103 $log = array_slice($log, 0, 50);
104 mnbt_plugin_option_set('hello_demo', 'host_events', $log);
105});