对小工具进行了一些修复

This commit is contained in:
2026-08-26 11:17:22 +08:00
parent 523af723ae
commit 9ad6f41592
9 changed files with 17 additions and 223 deletions
-37
View File
@@ -9,12 +9,10 @@ defined('ABSPATH') || exit;
*/
final class AdminFormController {
public const AJAX_ACTION = 'aksrt_widget_form';
public const SEARCH_ACTION = 'aksrt_widget_remote_options';
private const NONCE_ACTION = 'aksrt_widget_form';
public static function register_hooks(): void {
add_action('wp_ajax_' . self::AJAX_ACTION, [self::class, 'ajax_form']);
add_action('wp_ajax_' . self::SEARCH_ACTION, [self::class, 'ajax_remote_options']);
add_action('admin_enqueue_scripts', [self::class, 'enqueue']);
add_action('customize_controls_enqueue_scripts', [self::class, 'enqueue']);
}
@@ -35,7 +33,6 @@ final class AdminFormController {
wp_localize_script('aksrt-widget-admin', 'aksrtWidgetAdmin', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'action' => self::AJAX_ACTION,
'searchAction' => self::SEARCH_ACTION,
'nonce' => wp_create_nonce(self::NONCE_ACTION),
'transferNonce' => wp_create_nonce('aksrt_widgets_transfer'),
'i18n' => [
@@ -67,39 +64,5 @@ final class AdminFormController {
$html = (string) ob_get_clean();
wp_send_json_success(['html' => $html]);
}
public static function ajax_remote_options(): void {
check_ajax_referer(self::NONCE_ACTION, 'nonce');
if (!current_user_can('edit_theme_options')) {
wp_send_json_error(['message' => __('权限不足。', 'aksrt')], 403);
}
$source = isset($_POST['source']) ? sanitize_key(wp_unslash($_POST['source'])) : '';
$search = isset($_POST['search']) ? sanitize_text_field(wp_unslash($_POST['search'])) : '';
$query_json = isset($_POST['queryArgs']) ? wp_unslash($_POST['queryArgs']) : '';
$query_args = is_string($query_json) ? json_decode($query_json, true) : [];
$query_args = is_array($query_args) ? $query_args : [];
$results = [];
if ($source === 'post' && sanitize_key((string) ($query_args['post_type'] ?? '')) === 'plate') {
$posts = get_posts([
'post_type' => 'plate',
'post_status' => 'publish',
'posts_per_page' => 20,
's' => $search,
'orderby' => 'relevance date',
'order' => 'DESC',
]);
foreach ($posts as $post) $results[] = ['id' => (string) $post->ID, 'text' => get_the_title($post) ?: '#' . $post->ID];
} elseif ($source === 'categories') {
$taxonomy = sanitize_key((string) ($query_args['taxonomy'] ?? ''));
if (in_array($taxonomy, ['forum_topic', 'forum_tag'], true) && taxonomy_exists($taxonomy)) {
$terms = get_terms(['taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => 20, 'search' => $search]);
if (!is_wp_error($terms)) foreach ($terms as $term) $results[] = ['id' => (string) $term->term_id, 'text' => (string) $term->name];
}
}
wp_send_json_success(['results' => $results]);
}
}