forked from tomtaozhou/skill-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill.php
More file actions
141 lines (116 loc) · 5.19 KB
/
skill.php
File metadata and controls
141 lines (116 loc) · 5.19 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
<?php
/*
Plugin Name: SKILL Plugin
Description: Control and send notifications to ActivityPub devices based on post values from selected user.
Author: Tao Zhou
Version: 1.0
*/
// 创建插件设置页面
function skill_plugin_menu() {
add_options_page('SKILL Control Panel', 'SKILL Control', 'manage_options', 'skill-plugin-menu', 'skill_plugin_main_page');
}
add_action('admin_menu', 'skill_plugin_menu');
// 插件主页
function skill_plugin_main_page() {
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Handle form submission
if (isset($_POST['submit_settings'])) {
foreach ($_POST['devices'] as $device_id => $settings) {
update_user_meta($device_id, 'device_condition', sanitize_text_field($settings['condition']));
update_user_meta($device_id, 'device_threshold', intval($settings['threshold']));
update_user_meta($device_id, 'device_message', sanitize_text_field($settings['message']));
}
// Update Mastodon connection settings
update_option('mastodon_api_url', sanitize_text_field($_POST['mastodon_api_url']));
update_option('mastodon_token', sanitize_text_field($_POST['mastodon_token']));
echo '<div class="updated"><p>Settings updated successfully!</p></div>';
}
// Fetch saved Mastodon settings
$mastodon_api_url = get_option('mastodon_api_url', '');
$mastodon_token = get_option('mastodon_token', '');
// Fetch all users (assuming they represent devices)
$users = get_users();
echo '<div class="wrap">';
echo '<h1>SKILL Control Panel</h1>';
echo '<form method="post" action="">';
// Display settings for each device user
foreach ($users as $user) {
echo '<h2>Device: ' . esc_html($user->display_name) . '</h2>';
$selected_condition = get_user_meta($user->ID, 'device_condition', true);
$selected_threshold = get_user_meta($user->ID, 'device_threshold', true);
$selected_message = get_user_meta($user->ID, 'device_message', true);
// Condition settings for the device
echo '<select name="devices[' . $user->ID . '][condition]">';
echo '<option value="=" ' . selected($selected_condition, '=', false) . '>Equal to</option>';
echo '<option value=">" ' . selected($selected_condition, '>', false) . '>Greater than</option>';
echo '<option value="<" ' . selected($selected_condition, '<', false) . '>Less than</option>';
echo '</select>';
echo '<input type="number" name="devices[' . $user->ID . '][threshold]" value="' . esc_attr($selected_threshold) . '">';
echo '<input type="text" name="devices[' . $user->ID . '][message]" value="' . esc_attr($selected_message) . '"><br>';
}
// Mastodon settings
echo '<h2>Mastodon Settings</h2>';
echo 'API URL: <input type="text" name="mastodon_api_url" value="' . esc_attr($mastodon_api_url) . '"><br>';
echo 'Token: <input type="text" name="mastodon_token" value="' . esc_attr($mastodon_token) . '"><br>';
// Submit button
echo '<input type="submit" name="submit_settings" value="Save Settings">';
echo '</form>';
echo '</div>';
}
// Check post values and send notification if necessary
add_action('save_post', 'check_post_value_and_notify');
function check_post_value_and_notify($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
$post_author = get_post_field('post_author', $post_id);
$post_content = get_post_field('post_content', $post_id);
preg_match('/\[value=(\d+)\]/', $post_content, $matches);
if (isset($matches[1])) {
$post_value = intval($matches[1]);
$condition = get_user_meta($post_author, 'device_condition', true);
$threshold_value = get_user_meta($post_author, 'device_threshold', true);
$should_notify = false;
switch ($condition) {
case '=':
$should_notify = ($post_value == $threshold_value);
break;
case '>':
$should_notify = ($post_value > $threshold_value);
break;
case '<':
$should_notify = ($post_value < $threshold_value);
break;
}
if ($should_notify) {
$message = get_user_meta($post_author, 'device_message', true);
send_notification_to_mastodon($message);
}
}
}
// Send a notification to Mastodon
function send_notification_to_mastodon($message) {
$api_url = get_option('mastodon_api_url', '');
$token = get_option('mastodon_token', '');
if (empty($api_url) || empty($token)) {
return;
}
$endpoint = rtrim($api_url, '/') . '/api/v1/statuses';
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
),
'body' => json_encode(array(
'status' => $message,
'visibility' => 'public'
)),
'method' => 'POST'
);
$response = wp_remote_request($endpoint, $args);
if (is_wp_error($response)) {
error_log('Failed to send notification to Mastodon: ' . $response->get_error_message());
}
}
?>