284 lines
12 KiB
PHP
284 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* @package AKSRT
|
|
*/
|
|
|
|
namespace AKSRT\Admin;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
trait GlobalControllerPart1 {
|
|
|
|
public function rest_reorder_footer_tabbar(\WP_REST_Request $request): \WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$ordered_ids = $params['ids'] ?? [];
|
|
|
|
if (!is_array($ordered_ids)) {
|
|
return new \WP_REST_Response(['code' => 'error', 'message' => __('参数错误', 'aksrt')], 400);
|
|
}
|
|
|
|
if (count($ordered_ids) > 5) {
|
|
return new \WP_REST_Response(['code' => 'error', 'message' => __('Footer tabbar supports at most 5 items.', 'aksrt')], 400);
|
|
}
|
|
|
|
$tabbar = get_option('aksrt_footer_tabbar', []);
|
|
$items = $tabbar['items'] ?? [];
|
|
|
|
$order_map = array_flip($ordered_ids);
|
|
usort($items, function ($a, $b) use ($order_map) {
|
|
$idx_a = $order_map[($a['id'] ?? '')] ?? 999;
|
|
$idx_b = $order_map[($b['id'] ?? '')] ?? 999;
|
|
return $idx_a <=> $idx_b;
|
|
});
|
|
|
|
foreach ($items as $index => &$item) {
|
|
$item['sort_order'] = $index;
|
|
}
|
|
|
|
$tabbar['items'] = $items;
|
|
update_option('aksrt_footer_tabbar', $tabbar);
|
|
|
|
return rest_ensure_response(['code' => 'success', 'message' => __('Order saved.', 'aksrt')]);
|
|
}
|
|
|
|
private function sanitize_tabbar_item(array $item): array {
|
|
$valid_types = ['home', 'category', 'link', 'user', 'auth', 'search', 'add'];
|
|
$type = sanitize_text_field($item['type'] ?? 'link');
|
|
if (!in_array($type, $valid_types, true)) {
|
|
$type = 'link';
|
|
}
|
|
$badge_mode = sanitize_text_field($item['badge_mode'] ?? 'static');
|
|
if (!in_array($badge_mode, ['static', 'dynamic'], true)) {
|
|
$badge_mode = 'static';
|
|
}
|
|
|
|
$add_items = [];
|
|
if (!empty($item['add_items']) && is_array($item['add_items'])) {
|
|
foreach ($item['add_items'] as $sub_item) {
|
|
$label = sanitize_text_field($sub_item['label'] ?? '');
|
|
$icon = sanitize_text_field($sub_item['icon'] ?? 'fa fas fa-pen');
|
|
$url = esc_url_raw($sub_item['url'] ?? '');
|
|
if ($label === '') {
|
|
continue;
|
|
}
|
|
$add_items[] = [
|
|
'id' => sanitize_text_field($sub_item['id'] ?? uniqid('add_')),
|
|
'label' => $label,
|
|
'icon' => $icon,
|
|
'url' => $url,
|
|
];
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => sanitize_text_field($item['id'] ?? uniqid('tab_')),
|
|
'type' => $type,
|
|
'icon' => sanitize_text_field($item['icon'] ?? ''),
|
|
'text' => sanitize_text_field($item['text'] ?? ''),
|
|
'link' => esc_url_raw($item['link'] ?? ''),
|
|
'badge' => sanitize_text_field($item['badge'] ?? ''),
|
|
'badge_mode' => $badge_mode,
|
|
'category_id' => absint($item['category_id'] ?? 0),
|
|
'search_default_type' => (function() use ($item) {
|
|
$v = $item['search_default_type'] ?? 'post';
|
|
return in_array($v, ['post', 'category', 'tag'], true) ? $v : 'post';
|
|
})(),
|
|
'new_window' => !empty($item['new_window']),
|
|
'add_items' => $add_items,
|
|
'enabled' => !empty($item['enabled']),
|
|
'sort_order' => (int) ($item['sort_order'] ?? 0),
|
|
'ontop' => !empty($item['ontop']),
|
|
];
|
|
}
|
|
|
|
public function rest_handle_floating_buttons(\WP_REST_Request $request): \WP_REST_Response {
|
|
if ($request->get_method() === 'GET') {
|
|
$settings = class_exists('\AKSRT\Settings\Settings')
|
|
? \AKSRT\Settings\Settings::get_floating_buttons_settings()
|
|
: [
|
|
'enabled' => true,
|
|
'style' => 'round',
|
|
'position' => 'bottom',
|
|
'scroll_hide' => false,
|
|
'scroll_down_hide' => true,
|
|
'filter_blur' => [],
|
|
'items' => [],
|
|
];
|
|
|
|
return rest_ensure_response(['code' => 'success', 'data' => $settings]);
|
|
}
|
|
|
|
$params = $this->get_json_params($request);
|
|
$items = isset($params['items']) ? array_map([$this, 'sanitize_floating_button_item'], (array) $params['items']) : [];
|
|
|
|
$filter_blur = [];
|
|
if (!empty($params['filter_blur']) && is_array($params['filter_blur'])) {
|
|
foreach ($params['filter_blur'] as $device) {
|
|
$device = sanitize_text_field((string) $device);
|
|
if (in_array($device, ['pc', 'mobile'], true)) {
|
|
$filter_blur[] = $device;
|
|
}
|
|
}
|
|
$filter_blur = array_values(array_unique($filter_blur));
|
|
}
|
|
|
|
$settings = [
|
|
'enabled' => !empty($params['enabled']),
|
|
'style' => in_array($params['style'] ?? '', ['round', 'square'], true) ? $params['style'] : 'round',
|
|
'position' => in_array($params['position'] ?? '', ['bottom', 'center'], true) ? $params['position'] : 'bottom',
|
|
'scroll_hide' => !empty($params['scroll_hide']),
|
|
'scroll_down_hide' => !empty($params['scroll_down_hide']),
|
|
'max' => max(1, min(10, (int) ($params['max'] ?? 5))),
|
|
'size' => max(32, min(64, (int) ($params['size'] ?? 44))),
|
|
'spacing' => max(4, min(24, (int) ($params['spacing'] ?? 10))),
|
|
'shadow' => !empty($params['shadow']),
|
|
'animation' => !empty($params['animation']),
|
|
'opacity' => max(30, min(100, (int) ($params['opacity'] ?? 100))),
|
|
'active_color' => sanitize_hex_color($params['active_color'] ?? '') ?: '',
|
|
'bg_color' => sanitize_hex_color($params['bg_color'] ?? '') ?: '',
|
|
'pc_show' => !empty($params['pc_show']),
|
|
'm_show' => !empty($params['m_show']),
|
|
'filter_blur' => $filter_blur,
|
|
'items' => $items,
|
|
];
|
|
|
|
update_option('aksrt_floating_buttons', $settings);
|
|
|
|
if (class_exists('\AKSRT\Settings\Settings')) {
|
|
\AKSRT\Settings\Settings::flush_settings_cache();
|
|
}
|
|
|
|
return rest_ensure_response(['code' => 'success', 'message' => __('Floating button settings saved.', 'aksrt')]);
|
|
}
|
|
|
|
public function rest_reorder_floating_buttons(\WP_REST_Request $request): \WP_REST_Response {
|
|
$params = $this->get_json_params($request);
|
|
$ordered_ids = $params['ids'] ?? [];
|
|
|
|
if (!is_array($ordered_ids)) {
|
|
return new \WP_REST_Response(['code' => 'error', 'message' => __('参数错误', 'aksrt')], 400);
|
|
}
|
|
|
|
$settings = get_option('aksrt_floating_buttons', []);
|
|
$items = $settings['items'] ?? [];
|
|
|
|
$order_map = array_flip($ordered_ids);
|
|
usort($items, function ($a, $b) use ($order_map) {
|
|
$idx_a = $order_map[($a['id'] ?? '')] ?? 999;
|
|
$idx_b = $order_map[($b['id'] ?? '')] ?? 999;
|
|
return $idx_a <=> $idx_b;
|
|
});
|
|
|
|
foreach ($items as $index => &$item) {
|
|
$item['sort_order'] = $index;
|
|
}
|
|
unset($item);
|
|
|
|
$settings['items'] = $items;
|
|
update_option('aksrt_floating_buttons', $settings);
|
|
|
|
if (class_exists('\AKSRT\Settings\Settings')) {
|
|
\AKSRT\Settings\Settings::flush_settings_cache();
|
|
}
|
|
|
|
return rest_ensure_response(['code' => 'success', 'message' => __('Order saved.', 'aksrt')]);
|
|
}
|
|
|
|
private function sanitize_floating_button_item(array $item): array {
|
|
$valid_types = ['back_top', 'theme_mode', 'qrcode', 'service_qq', 'service_wechat', 'pay_vip', 'custom', 'add'];
|
|
$type = sanitize_text_field($item['type'] ?? 'custom');
|
|
if (!in_array($type, $valid_types, true)) {
|
|
$type = 'custom';
|
|
}
|
|
|
|
$hover_width = (int) ($item['hover_width'] ?? 200);
|
|
if ($hover_width < 120) {
|
|
$hover_width = 120;
|
|
}
|
|
if ($hover_width > 480) {
|
|
$hover_width = 480;
|
|
}
|
|
|
|
$qq = preg_replace('/\D+/', '', (string) ($item['qq'] ?? ''));
|
|
$action_mode = sanitize_text_field($item['action_mode'] ?? 'link');
|
|
if (!in_array($action_mode, ['link', 'qrcode'], true)) {
|
|
$action_mode = 'link';
|
|
}
|
|
$qr_size = (int) ($item['qr_size'] ?? 120);
|
|
if ($qr_size < 80) {
|
|
$qr_size = 80;
|
|
}
|
|
if ($qr_size > 240) {
|
|
$qr_size = 240;
|
|
}
|
|
|
|
$add_items = [];
|
|
if (!empty($item['add_items']) && is_array($item['add_items'])) {
|
|
foreach ($item['add_items'] as $add_item) {
|
|
if (!is_array($add_item)) {
|
|
continue;
|
|
}
|
|
$label = sanitize_text_field($add_item['label'] ?? '');
|
|
$url = esc_url_raw($add_item['url'] ?? '');
|
|
$icon_item = sanitize_text_field($add_item['icon'] ?? '');
|
|
if ($label === '' && $url === '') {
|
|
continue;
|
|
}
|
|
$add_items[] = [
|
|
'id' => sanitize_text_field($add_item['id'] ?? uniqid('add_')),
|
|
'label' => $label,
|
|
'url' => $url,
|
|
'icon' => $icon_item,
|
|
];
|
|
}
|
|
}
|
|
|
|
$icon = sanitize_text_field($item['icon'] ?? '');
|
|
$icon_dark = sanitize_text_field($item['icon_dark'] ?? '');
|
|
$icon_light = sanitize_text_field($item['icon_light'] ?? '');
|
|
if ($type === 'theme_mode') {
|
|
$icon = 'fa fas fa-sun';
|
|
$icon_dark = '';
|
|
$icon_light = '';
|
|
}
|
|
// VIP float always uses modal path; drop stale link/hover and normalize icons.
|
|
$link = esc_url_raw($item['link'] ?? '');
|
|
$hover = wp_kses_post($item['hover'] ?? '');
|
|
if ($type === 'pay_vip') {
|
|
$link = '';
|
|
$hover = '';
|
|
if ($icon === '' || str_contains($icon, 'fa-gem') || $icon === 'fa fas fa-gem') {
|
|
$icon = 'fa fas fa-gem';
|
|
}
|
|
if (!preg_match('/^fa\s+(?:fas|far|fab)\s+fa-[a-z0-9-]+(?:\s+fa-[a-z0-9-]+)*$/i', $icon)) {
|
|
$icon = 'fa fas fa-gem';
|
|
}
|
|
}
|
|
|
|
return [
|
|
'id' => sanitize_text_field($item['id'] ?? uniqid('float_')),
|
|
'type' => $type,
|
|
'icon' => $icon,
|
|
'icon_dark' => $icon_dark,
|
|
'icon_light' => $icon_light,
|
|
'text' => sanitize_text_field($item['text'] ?? ''),
|
|
'desc' => sanitize_text_field($item['desc'] ?? ''),
|
|
'action_mode' => $action_mode,
|
|
'qq' => $qq,
|
|
'qrcode_img' => esc_url_raw($item['qrcode_img'] ?? ''),
|
|
'wechat_img' => esc_url_raw($item['wechat_img'] ?? ''),
|
|
'link' => $link,
|
|
'add_items' => $add_items,
|
|
'hover' => $hover,
|
|
'qr_size' => $qr_size,
|
|
'hover_width' => $hover_width,
|
|
'pc_s' => !empty($item['pc_s']),
|
|
'm_s' => !empty($item['m_s']),
|
|
'color' => sanitize_text_field($item['color'] ?? ''),
|
|
'bg' => sanitize_text_field($item['bg'] ?? ''),
|
|
'enabled' => !empty($item['enabled']),
|
|
'sort_order' => (int) ($item['sort_order'] ?? 0),
|
|
];
|
|
}
|
|
}
|