| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* 推送记录 |
| 5 |
*/ |
| 6 |
class Ggpush_Record |
| 7 |
{ |
| 8 |
/** |
| 9 |
* 推送记录 |
| 10 |
* @return void |
| 11 |
*/ |
| 12 |
public static function home() |
| 13 |
{ |
| 14 |
$action = ''; |
| 15 |
if (!empty($_GET['action'])) { |
| 16 |
$action = sanitize_title($_GET['action']); |
| 17 |
} |
| 18 |
if ('detail' === $action) { |
| 19 |
// 详� |
| 20 |
|
| 21 |
self::record_detail(); |
| 22 |
} else if ('delete' === $action) { |
| 23 |
// 删除 |
| 24 |
if (!empty($_GET['record_id'])) { |
| 25 |
$id = (int)$_GET['record_id']; |
| 26 |
self::record_delete($id); |
| 27 |
} else if (!empty($_GET['ids']) && is_array($_GET['ids'])) { |
| 28 |
$ids = array_map('absint', array_values(wp_unslash($_GET['ids']))); |
| 29 |
self::record_delete(0, $ids); |
| 30 |
} else { |
| 31 |
wp_die('删除失败'); |
| 32 |
} |
| 33 |
} else if ('delete_1' === $action) { |
| 34 |
self::record_delete(0, array(), 1); |
| 35 |
} else if ('delete_3' === $action) { |
| 36 |
self::record_delete(0, array(), 3); |
| 37 |
} else if ('delete_30' === $action) { |
| 38 |
self::record_delete(0, array(), 30); |
| 39 |
} else if ('delete_all' === $action) { |
| 40 |
self::record_delete(0, array(), 0); |
| 41 |
} else { |
| 42 |
self::record_list(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* 列表 |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public static function record_list() |
| 51 |
{ |
| 52 |
?> |
| 53 |
<div class="wrap"> |
| 54 |
<h1 class="wp-heading-inline">推送记录</h1> |
| 55 |
<form method="get"> |
| 56 |
<input type="hidden" name="page" value="ggpush-record"/> |
| 57 |
<?php |
| 58 |
$ggpush_record_table = new Ggpush_Record_Table(); |
| 59 |
$ggpush_record_table->prepare_items(); |
| 60 |
$ggpush_record_table->display(); |
| 61 |
?> |
| 62 |
</form> |
| 63 |
</div> |
| 64 |
<?php |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* 详� |
| 69 |
|
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public static function record_detail() |
| 73 |
{ |
| 74 |
global $wpdb; |
| 75 |
$table_name = $wpdb->prefix . 'ggpush_records'; |
| 76 |
$sql = 'select * from `' . $table_name . '` where `record_id` = %d limit 1'; |
| 77 |
$query = $wpdb->prepare( |
| 78 |
$sql, |
| 79 |
intval(Ggpush_Plugin::get('record_id', 0)) |
| 80 |
); |
| 81 |
$results = $wpdb->get_results($query, ARRAY_A); |
| 82 |
$record_data = array(); |
| 83 |
if (!empty($results)) { |
| 84 |
foreach ($results as $result) { |
| 85 |
$record_data = $result; |
| 86 |
} |
| 87 |
} |
| 88 |
if (empty($record_data)) { |
| 89 |
wp_die('暂无数据'); |
| 90 |
} |
| 91 |
?> |
| 92 |
<div class="wrap"> |
| 93 |
<h1 class="wp-heading-inline">详� |
| 94 |
</h1> |
| 95 |
<br class="clear"> |
| 96 |
<br class="clear"> |
| 97 |
<table class="wp-list-table widefat fixed"> |
| 98 |
<tbody> |
| 99 |
<tr> |
| 100 |
<th scope="col">Id</th> |
| 101 |
<td><?php echo esc_html($record_data['record_id']); ?></td> |
| 102 |
</tr> |
| 103 |
<tr> |
| 104 |
<th scope="col">推送平台</th> |
| 105 |
<td><?php echo esc_html(Ggpush_Api::format_record_platform($record_data['record_platform'])); ?></td> |
| 106 |
</tr> |
| 107 |
<tr> |
| 108 |
<th scope="col">推送方式</th> |
| 109 |
<td><?php echo esc_html(Ggpush_Api::format_record_mode($record_data['record_mode'])); ?></td> |
| 110 |
</tr> |
| 111 |
<tr> |
| 112 |
<th scope="col">推送链接数量</th> |
| 113 |
<td><?php echo esc_html($record_data['record_num']); ?></td> |
| 114 |
</tr> |
| 115 |
<tr> |
| 116 |
<th scope="col">推送状态</th> |
| 117 |
<td><?php echo esc_html(Ggpush_Api::format_result_status($record_data['record_result_status'])); ?></td> |
| 118 |
</tr> |
| 119 |
<tr> |
| 120 |
<th scope="col">推送结果状态码</th> |
| 121 |
<td><?php echo esc_html($record_data['record_result_code']); ?></td> |
| 122 |
</tr> |
| 123 |
<tr> |
| 124 |
<th scope="col">推送时间</th> |
| 125 |
<td><?php echo esc_html($record_data['record_date']); ?></td> |
| 126 |
</tr> |
| 127 |
</tbody> |
| 128 |
</table> |
| 129 |
<br class="clear"> |
| 130 |
<table class="wp-list-table widefat fixed"> |
| 131 |
<tr> |
| 132 |
<th>推送链接</th> |
| 133 |
</tr> |
| 134 |
<tr> |
| 135 |
<td> |
| 136 |
<textarea class="large-text code" readonly="readonly" |
| 137 |
rows="5"><?php echo esc_html(implode(PHP_EOL, json_decode($record_data['record_urls'], true))); ?></textarea> |
| 138 |
</td> |
| 139 |
</tr> |
| 140 |
</table> |
| 141 |
<br class="clear"> |
| 142 |
<table class="wp-list-table widefat fixed"> |
| 143 |
<tr> |
| 144 |
<th>推送响应数据</th> |
| 145 |
</tr> |
| 146 |
<tr> |
| 147 |
<td> |
| 148 |
<textarea class="large-text code" readonly="readonly" |
| 149 |
rows="5"><?php echo esc_html($record_data['record_result']); ?></textarea> |
| 150 |
</td> |
| 151 |
</tr> |
| 152 |
</table> |
| 153 |
<br class="clear"> |
| 154 |
<?php |
| 155 |
if (!empty($record_data['record_result_error'])) { |
| 156 |
?> |
| 157 |
<table class="wp-list-table widefat fixed"> |
| 158 |
<tr> |
| 159 |
<th>失败原因</th> |
| 160 |
</tr> |
| 161 |
<tr> |
| 162 |
<td> |
| 163 |
<textarea class="large-text code" readonly="readonly" |
| 164 |
rows="5"><?php echo esc_html($record_data['record_result_error']); ?></textarea> |
| 165 |
</td> |
| 166 |
</tr> |
| 167 |
</table> |
| 168 |
<br class="clear"> |
| 169 |
<?php |
| 170 |
} |
| 171 |
?> |
| 172 |
<a class="button button-primary" href="javascript:void(0);" |
| 173 |
onclick="history.back();">返回</a> |
| 174 |
</div> |
| 175 |
<?php |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* 删除 |
| 180 |
* @param int $id 删除单条记录 |
| 181 |
* @param array $ids 删除多条记录 |
| 182 |
* @param int $day 删除在此之前多少天的数据 |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
public static function record_delete($id = 0, $ids = array(), $day = -1) |
| 186 |
{ |
| 187 |
$current_url = self_admin_url('admin.php?page=ggpush-record'); |
| 188 |
$result = false; |
| 189 |
$last_error = '非法操作'; |
| 190 |
if (!empty($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'bulk-ggpush_records')) { |
| 191 |
global $wpdb; |
| 192 |
$table_name = $wpdb->prefix . 'ggpush_records'; |
| 193 |
$query = ''; |
| 194 |
if (!empty($id)) { |
| 195 |
$sql = 'DELETE FROM `' . $table_name . '` WHERE `record_id` = %d'; |
| 196 |
$query = $wpdb->prepare( |
| 197 |
$sql, |
| 198 |
$id |
| 199 |
); |
| 200 |
} else if (!empty($ids)) { |
| 201 |
$sql = 'DELETE FROM `' . $table_name . '` WHERE `record_id` in (' . implode(', ', array_fill(0, count($ids), '%d')) . ')'; |
| 202 |
$query = call_user_func_array(array($wpdb, 'prepare'), array_merge(array($sql), $ids)); |
| 203 |
} else if ($day >= 0) { |
| 204 |
if ($day <= 0) { |
| 205 |
$end_record_date_time = time(); |
| 206 |
} else if (1 === $day) { |
| 207 |
$end_record_date_time = strtotime('-1 day'); |
| 208 |
} else { |
| 209 |
$end_record_date_time = strtotime('-' . $day . ' days'); |
| 210 |
} |
| 211 |
$end_record_date = wp_date('Y-m-d H:i:s', $end_record_date_time); |
| 212 |
$sql = 'DELETE FROM `' . $table_name . '` where `record_date` <= %s'; |
| 213 |
$query = $wpdb->prepare( |
| 214 |
$sql, |
| 215 |
$end_record_date |
| 216 |
); |
| 217 |
} else { |
| 218 |
wp_die('删除失败'); |
| 219 |
} |
| 220 |
if (!empty($query)) { |
| 221 |
$result = $wpdb->query($query); |
| 222 |
$last_error = $wpdb->last_error; |
| 223 |
} |
| 224 |
} |
| 225 |
?> |
| 226 |
<div class="wrap"> |
| 227 |
<h1 class="wp-heading-inline">删除推送记录</h1> |
| 228 |
<p> |
| 229 |
<?php |
| 230 |
if ($result !== false) { |
| 231 |
?> |
| 232 |
删除<?php echo esc_html($result); ?>条推送记录成功 |
| 233 |
<?php |
| 234 |
} else { |
| 235 |
?> |
| 236 |
删除推送记录失败:<?php echo esc_html($last_error); ?> |
| 237 |
<?php |
| 238 |
} |
| 239 |
?> |
| 240 |
</p> |
| 241 |
<a class="button button-primary" href="<?php echo esc_url($current_url); ?>">返回</a> |
| 242 |
</div> |
| 243 |
<?php |
| 244 |
} |
| 245 |
} |