-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.php
More file actions
216 lines (198 loc) · 7.99 KB
/
settings.php
File metadata and controls
216 lines (198 loc) · 7.99 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
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
212
213
214
215
216
<?php
require_once __DIR__ . '/scheduler.php';
require_once __DIR__ . '/git.php';
require_once __DIR__ . '/fs.php';
$_schedules = wp_get_schedules();
$gitau_avaliable_intervals = array('daily', 'weekly', 'monthly');
$gitau_avaliable_intervals_descriptions = array_map(function ($interval) use ($_schedules) {
return $_schedules[$interval]['display'];
}, $gitau_avaliable_intervals);
// Register a new menu item and page
function gitau_add_settings_page()
{
add_options_page(
'Git Auto Update Settings',
'Git Auto Update',
'manage_options',
'gitau-settings',
'gitau_render_settings_page'
);
}
add_action('admin_menu', 'gitau_add_settings_page');
// Register a new settings group, section and fields
function gitau_register_settings()
{
register_setting(
'gitau_settings',
'gitau_settings',
array(
"sanitize_callback" => "gitau_validate_settings",
)
);
add_settings_section(
'gitau_update_frequency',
'Update Frequency',
function () {
echo '<p>' .
esc_html__('Choose how often you want to check for theme updates from the git repository.', 'git-autoupdate')
. '</p>';
echo '<p>' .
esc_html__('If you want to disable autoupdate, just deactivate this plugin.', 'git-autoupdate')
. '</p>';
},
'gitau-settings'
);
// 定时任务执行频率设置
add_settings_field(
'gitau_cron_interval',
'Select how often you want to check for updates',
'gitau_cron_interval_callback',
'gitau-settings',
'gitau_update_frequency'
);
}
add_action('admin_init', 'gitau_register_settings');
// Render the settings page
function gitau_render_settings_page()
{
?>
<h2><?= __("Git Auto Update Settings", "git-autoupdate") ?></h2>
<h3><?= __("Execution History", "git-autoupdate") ?></h3>
<table>
<thead>
<tr>
<th><?= __("Theme Name", "git-autoupdate") ?></th>
<th><?= __("DateTime", "git-autoupdate") ?></th>
<th><?= __("Version Before Update", "git-autoupdate") ?></th>
<th><?= __("Version After Update", "git-autoupdate") ?></th>
<th><?= __("Error Message", "git-autoupdate") ?></th>
</tr>
<?php
foreach (gitau_read_log() as $key => $row) {
echo $row->isError ? '<tr style="background-color:#f006;">' : "<tr>";
echo "<td>$row->theme_name</td>";
echo "<td>$row->datetime</td>";
echo "<td>$row->theme_version_pre</td>";
echo "<td>$row->theme_version_post</td>";
echo "<td>$row->msg</td>";
echo "</tr>";
}
?>
</thead>
</table>
<form action="options.php" method="post">
<?php
settings_fields('gitau_settings');
do_settings_sections('gitau-settings');
submit_button();
?>
</form>
<?php
}
// Validate and save the settings
function gitau_validate_settings($input)
{
global $gitau_avaliable_intervals;
// Get the current settings
$options = get_option('gitau_settings');
// Validate the input for the cron interval field
foreach ($input as $key => $value) {
switch ($key) {
case 'gitau_cron_interval':
if (in_array($value, $gitau_avaliable_intervals)) {
$options['gitau_cron_interval'] = $value;
add_settings_error('gitau_messages', 'gitau_message', __('Settings saved', 'git-autoupdate'), 'success');
} else {
add_settings_error('gitau_messages', 'gitau_message', __('Invalid input', 'git-autoupdate'), 'error');
}
break;
}
}
// Return the validated and updated options
return $options;
}
// Render the update frequency section
function gitau_update_frequency_callback()
{
echo '<p>' . esc_html__('Choose how often you want to check for theme updates from the git repository.', 'git-autoupdate') . '</p>';
}
// Render the cron interval field
function gitau_cron_interval_callback()
{
global $gitau_avaliable_intervals;
global $gitau_avaliable_intervals_descriptions;
// Get the current settings
$options = get_option('gitau_settings');
// Get the current value of the cron interval option, or set a default value if not set
$value = isset($options['gitau_cron_interval']) ? $options['gitau_cron_interval'] : 'weekly';
// Output a select element with three options: hourly, twicedaily and daily
echo '<select id="gitau_cron_interval" name="gitau_settings[gitau_cron_interval]">';
foreach ($gitau_avaliable_intervals as $key => $interval) {
echo '<option value="' . $interval . '"' . selected($value, $interval, false) . '>'
. esc_html__(ucfirst($gitau_avaliable_intervals_descriptions[$key]), 'git-autoupdate')
. '</option>';
}
echo '</select>';
}
// 处理选项更新
function gitau_update_option()
{
gitau_cancel_schedule_cron();
gitau_schedule_cron();
}
add_action('update_option_gitau_settings', 'gitau_update_option', 10);
// 提示定时任务的状态
function gitau_show_cron_status()
{
// Check if the current screen is the settings page
$screen = get_current_screen();
if ($screen->id == 'settings_page_gitau-settings') {
// Get the next scheduled time for the cron event
$next_time = wp_next_scheduled('gitau_update_theme');
// Get the transient for the cron status
$status = get_transient('gitau_cron_status');
// Check if the transient exists
if ($status) {
// Calculate the elapsed time since the cron event started
$elapsed_time = time() - $status['start_time'];
// Output a notice that the cron event is running
echo '<div class="notice notice-info">';
echo '<p>' . GITAU_MSG_PREFIX . sprintf(__('The cron event is running. It started %d seconds ago.', 'git-autoupdate'), $elapsed_time) . '</p>';
echo '</div>';
} else {
// Check if the next scheduled time exists
if ($next_time) {
// Format the next scheduled time
$next_time = date_i18n(
get_option('date_format') . ' ' . get_option('time_format'),
$next_time + get_option('gmt_offset') * 3600
/**时区修正 */
);
// Output a notice that the cron event is scheduled
echo '<div class="notice notice-success">';
echo '<p>' . GITAU_MSG_PREFIX . sprintf(__('The cron event is scheduled. It will run at %s (timezone set in your setting).', 'git-autoupdate'), $next_time) . '</p>';
echo '</div>';
} else {
// Output a notice that the cron event is not scheduled
echo '<div class="notice notice-error">';
echo '<p>' . GITAU_MSG_PREFIX . __('Autoupdate is not scheduled. Apply a new setting to schedule a new autoupdate event.', 'git-autoupdate') . '</p>';
echo '</div>';
}
}
$theme = wp_get_theme();
$is_support = gitau_check_theme_support($theme);
// https://developer.wordpress.org/reference/hooks/admin_notices/
echo '<div class="notice notice-' . ($is_support ? 'success' : 'warning') . '"><p>' .
sprintf(
_x('You are using %1$s, ', "you are using {theme_name}", 'git-autoupdate'),
'<a href="' . $theme->get('ThemeURI') . '" rel="nofollow">'
. $theme->get('Name')
. '</a>'
) .
($is_support ?
__('git-autoupdate support this theme.', 'git-autoupdate') :
__('git-autoupdate is not support this theme.', 'git-autoupdate')) .
'</p></div>';
}
}
add_action('admin_notices', 'gitau_show_cron_status');