page_slug, array( $this, 'render_page' ) ); } /** * 清理并保存上次使用的表单偏好。 * * @param array $input 原始输入。 * @return array */ public function sanitize_settings( $input ) { if ( ! is_array( $input ) ) { return array(); } $sanitized = array(); $fields = array( 'id_range_start', 'id_range_end', 'specific_ids', 'category_ids', 'date_from', 'date_to', 'new_date', 'date_mode', 'spread_mode', 'spread_step_minutes', 'spread_random_min', 'spread_random_max', ); foreach ( $fields as $field ) { if ( isset( $input[ $field ] ) ) { $sanitized[ $field ] = sanitize_text_field( (string) $input[ $field ] ); } } return $sanitized; } public function enqueue_assets( $hook ) { if ( false === strpos( $hook, $this->page_slug ) ) { return; } $batch_size = (int) apply_filters( 'lao_ud_batch_size', 50 ); if ( $batch_size < 1 ) { $batch_size = 50; } wp_enqueue_style( 'lao-ud-admin', LAO_UD_PLUGIN_URL . 'admin/css/admin.css', array(), LAO_UD_VERSION ); wp_enqueue_script( 'lao-ud-admin', LAO_UD_PLUGIN_URL . 'admin/js/admin.js', array( 'jquery' ), LAO_UD_VERSION, true ); wp_localize_script( 'lao-ud-admin', 'laoUd', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'lao_ud_nonce' ), 'batchSize' => $batch_size, 'i18n' => array( 'preview' => __( '预览', 'laoupdatedate' ), 'execute' => __( '执行更新', 'laoupdatedate' ), 'loading' => __( '处理中...', 'laoupdatedate' ), 'confirm' => __( '确定要更新这些文章的发布日期吗?此操作不可撤销。', 'laoupdatedate' ), 'success' => __( '更新成功!', 'laoupdatedate' ), 'error' => __( '操作失败,请查看错误信息。', 'laoupdatedate' ), 'noPosts' => __( '未找到符合条件的文章。', 'laoupdatedate' ), /* translators: %d: number of posts */ 'previewCount' => __( '共匹配 %d 篇文章', 'laoupdatedate' ), /* translators: %d: number of posts */ 'previewCountTrunc' => __( '共匹配 %d 篇文章,仅显示前 50 个 ID', 'laoupdatedate' ), /* translators: %d: post ID */ 'postIdLine' => __( '文章 ID: %d', 'laoupdatedate' ), 'noMatch' => __( '无', 'laoupdatedate' ), 'specifyDateAlert' => __( '请选择指定日期时间,或选择「更新为当前最新时间」。', 'laoupdatedate' ), /* translators: 1: number updated */ 'updatedOk' => __( '成功更新 %d 篇文章', 'laoupdatedate' ), /* translators: 1: updated count, 2: failed count */ 'updatedPartial' => __( '成功更新 %1$d 篇,失败 %2$d 篇', 'laoupdatedate' ), /* translators: 1: number can update, 2: failed count */ 'dryRunDone' => __( '预演完成:可更新 %1$d 篇,跳过 %2$d 篇(未写入数据库)', 'laoupdatedate' ), 'clearLogsConfirm' => __( '确定要清理所有操作日志吗?此操作不可撤销。', 'laoupdatedate' ), 'clearLogsSuccess' => __( '日志已清理。', 'laoupdatedate' ), 'spreadStepAlert' => __( '递增分散时,间隔分钟须为大于等于 1 的整数。', 'laoupdatedate' ), /* translators: 1: processed count, 2: total count */ 'progress' => __( '进度:已处理 %1$d / %2$d', 'laoupdatedate' ), ), ) ); } public function ajax_preview() { check_ajax_referer( 'lao_ud_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( '权限不足。', 'laoupdatedate' ) ) ); } $params = $this->get_filter_params_from_request(); $validation = Lao_UD_Core::validate_filter_params( $params ); if ( is_wp_error( $validation ) ) { wp_send_json_error( array( 'message' => $validation->get_error_message() ) ); } $page = Lao_UD_Core::get_post_ids_page( $params, 0, 50, true ); wp_send_json_success( array( 'count' => (int) $page['total'], 'ids' => $page['ids'], 'total' => (int) $page['total'], ) ); } public function ajax_execute() { check_ajax_referer( 'lao_ud_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( '权限不足。', 'laoupdatedate' ) ) ); } $params = $this->get_filter_params_from_request(); $validation = Lao_UD_Core::validate_filter_params( $params ); if ( is_wp_error( $validation ) ) { wp_send_json_error( array( 'message' => $validation->get_error_message() ) ); } $new_date = $this->get_post_text( 'new_date' ); $date_mode = $this->get_post_text( 'date_mode', 'specified' ); $dry_run = ( '1' === $this->get_post_text( 'dry_run' ) ); $offset = isset( $_POST['offset'] ) ? absint( wp_unslash( $_POST['offset'] ) ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing $batch_size = (int) apply_filters( 'lao_ud_batch_size', 50 ); if ( $batch_size < 1 ) { $batch_size = 50; } if ( isset( $_POST['batch_size'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $requested = absint( wp_unslash( $_POST['batch_size'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( $requested > 0 ) { $batch_size = min( $batch_size, $requested ); } } if ( 'current' === $date_mode ) { $new_date = 'current'; } elseif ( $new_date && false !== strpos( $new_date, 'T' ) ) { $new_date = str_replace( 'T', ' ', substr( $new_date, 0, 16 ) ) . ':00'; } $spread = array( 'spread_mode' => sanitize_key( $this->get_post_text( 'spread_mode', 'none' ) ), 'spread_step_minutes' => absint( $this->get_post_text( 'spread_step_minutes', '15' ) ), 'spread_random_min' => (int) $this->get_post_text( 'spread_random_min', '-60' ), 'spread_random_max' => (int) $this->get_post_text( 'spread_random_max', '60' ), ); $spread_valid = Lao_UD_Core::validate_spread_params( $spread ); if ( is_wp_error( $spread_valid ) ) { wp_send_json_error( array( 'message' => $spread_valid->get_error_message() ) ); } $job_key = 'lao_ud_job_' . get_current_user_id(); if ( 0 === $offset ) { $count_page = Lao_UD_Core::get_post_ids_page( $params, 0, 1, true ); $total = (int) $count_page['total']; $job = array( 'total' => $total, 'updated' => 0, 'failed' => 0, 'errors' => array(), 'errors_truncated' => 0, 'anchor_datetime' => '', 'params' => $params, 'date_mode' => $date_mode, 'new_date' => $new_date, 'dry_run' => $dry_run, 'spread' => $spread, 'started' => time(), ); set_transient( $job_key, $job, HOUR_IN_SECONDS ); $this->persist_form_preferences( $params, $date_mode, $new_date, $spread ); } else { $job = get_transient( $job_key ); if ( ! is_array( $job ) ) { wp_send_json_error( array( 'message' => __( '任务已过期,请重新执行。', 'laoupdatedate' ) ) ); } // 续跑时沿用首批任务参数,避免中途篡改。 $params = isset( $job['params'] ) && is_array( $job['params'] ) ? $job['params'] : $params; $date_mode = isset( $job['date_mode'] ) ? sanitize_text_field( (string) $job['date_mode'] ) : $date_mode; $new_date = isset( $job['new_date'] ) ? sanitize_text_field( (string) $job['new_date'] ) : $new_date; $dry_run = ! empty( $job['dry_run'] ); if ( isset( $job['spread'] ) && is_array( $job['spread'] ) ) { $spread = $job['spread']; } } $total = isset( $job['total'] ) ? (int) $job['total'] : 0; if ( $total < 1 ) { delete_transient( $job_key ); wp_send_json_error( array( 'message' => __( '未找到符合条件的文章。', 'laoupdatedate' ) ) ); } $page = Lao_UD_Core::get_post_ids_page( $params, $offset, $batch_size, false ); $ids = $page['ids']; $anchor = isset( $job['anchor_datetime'] ) ? (string) $job['anchor_datetime'] : ''; $anchor = $this->get_post_text( 'anchor_datetime', $anchor ); $is_last = ( $offset + count( $ids ) ) >= $total || empty( $ids ); $result = Lao_UD_Core::update_post_dates( $ids, $new_date, array( 'dry_run' => $dry_run, 'spread_mode' => $spread['spread_mode'], 'spread_step_minutes' => $spread['spread_step_minutes'], 'spread_random_min' => $spread['spread_random_min'], 'spread_random_max' => $spread['spread_random_max'], 'list_offset' => $offset, 'anchor_datetime' => $anchor, 'is_complete' => $is_last, 'total' => $total, ) ); $job['updated'] += (int) $result['updated']; $job['failed'] += (int) $result['failed']; if ( ! empty( $result['anchor_datetime'] ) ) { $job['anchor_datetime'] = $result['anchor_datetime']; } $max_errors = (int) apply_filters( 'lao_ud_max_errors', 20 ); if ( $max_errors < 1 ) { $max_errors = 20; } if ( ! empty( $result['errors'] ) && is_array( $result['errors'] ) ) { foreach ( $result['errors'] as $error_text ) { if ( count( $job['errors'] ) < $max_errors ) { $job['errors'][] = $error_text; } else { $job['errors_truncated']++; } } } $next_offset = $offset + count( $ids ); $done = $is_last || $next_offset >= $total; $response = array( 'updated' => (int) $job['updated'], 'failed' => (int) $job['failed'], 'errors' => $job['errors'], 'errors_truncated' => (int) $job['errors_truncated'], 'dry_run' => (bool) $dry_run, 'total' => $total, 'offset' => $offset, 'next_offset' => $next_offset, 'done' => $done, 'batch_updated' => (int) $result['updated'], 'batch_failed' => (int) $result['failed'], 'anchor_datetime' => isset( $job['anchor_datetime'] ) ? $job['anchor_datetime'] : '', 'spread_mode' => $spread['spread_mode'], 'processed' => min( $next_offset, $total ), ); if ( ! empty( $job['errors_truncated'] ) ) { $truncated_msg = sprintf( /* translators: 1: shown error count, 2: hidden error count */ __( '仅显示前 %1$d 条错误,另有 %2$d 条未列出。', 'laoupdatedate' ), $max_errors, (int) $job['errors_truncated'] ); if ( ! in_array( $truncated_msg, $response['errors'], true ) ) { $response['errors'][] = $truncated_msg; } } if ( $done ) { $this->add_operation_log( array( 'time' => current_time( 'mysql' ), 'user_id' => get_current_user_id(), 'dry_run' => $dry_run, 'total' => $total, 'updated' => (int) $job['updated'], 'failed' => (int) $job['failed'], 'date_mode' => $date_mode, 'new_date' => $new_date, 'filters' => $params, 'spread_mode' => $spread['spread_mode'], 'spread_step' => $spread['spread_step_minutes'], 'spread_rmin' => $spread['spread_random_min'], 'spread_rmax' => $spread['spread_random_max'], ) ); delete_transient( $job_key ); } else { set_transient( $job_key, $job, HOUR_IN_SECONDS ); } wp_send_json_success( $response ); } public function render_page() { $lao_ud_settings = get_option( $this->settings_option, array() ); if ( ! is_array( $lao_ud_settings ) ) { $lao_ud_settings = array(); } $lao_ud_logs = $this->get_operation_logs(); include LAO_UD_PLUGIN_DIR . 'admin/views/settings-page.php'; } public function ajax_clear_logs() { check_ajax_referer( 'lao_ud_nonce', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( '权限不足。', 'laoupdatedate' ) ) ); } delete_option( $this->log_option ); wp_send_json_success( array( 'message' => __( '日志已清理。', 'laoupdatedate' ) ) ); } /** * 从请求读取筛选参数。 * * @return array */ private function get_filter_params_from_request() { return array( 'id_range_start' => $this->get_post_text( 'id_range_start' ), 'id_range_end' => $this->get_post_text( 'id_range_end' ), 'specific_ids' => $this->get_post_text( 'specific_ids' ), 'category_ids' => $this->get_post_text( 'category_ids' ), 'date_from' => $this->get_post_text( 'date_from' ), 'date_to' => $this->get_post_text( 'date_to' ), ); } /** * 安全读取 POST 文本字段。 * * @param string $key 字段名。 * @param string $default 默认值。 * @return string */ private function get_post_text( $key, $default = '' ) { if ( ! isset( $_POST[ $key ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing return $default; } return sanitize_text_field( wp_unslash( $_POST[ $key ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing } /** * 持久化表单偏好,便于下次打开回填。 * * @param array $params 筛选参数。 * @param string $date_mode 日期模式。 * @param string $new_date 新日期。 * @param array $spread 分散参数。 */ private function persist_form_preferences( $params, $date_mode, $new_date, $spread ) { $new_date_field = ''; if ( 'current' !== $new_date && '' !== $new_date ) { // datetime-local 需要 Y-m-d\TH:i。 $new_date_field = str_replace( ' ', 'T', substr( $new_date, 0, 16 ) ); } $input = array_merge( $params, array( 'date_mode' => $date_mode, 'new_date' => $new_date_field, 'spread_mode' => isset( $spread['spread_mode'] ) ? $spread['spread_mode'] : 'none', 'spread_step_minutes' => isset( $spread['spread_step_minutes'] ) ? (string) $spread['spread_step_minutes'] : '15', 'spread_random_min' => isset( $spread['spread_random_min'] ) ? (string) $spread['spread_random_min'] : '-60', 'spread_random_max' => isset( $spread['spread_random_max'] ) ? (string) $spread['spread_random_max'] : '60', ) ); update_option( $this->settings_option, $this->sanitize_settings( $input ), false ); } private function add_operation_log( $entry ) { if ( ! is_array( $entry ) ) { return; } $logs = $this->get_operation_logs(); array_unshift( $logs, $entry ); $max_logs = (int) apply_filters( 'lao_ud_log_retention', 30 ); if ( $max_logs < 1 ) { $max_logs = 30; } if ( count( $logs ) > $max_logs ) { $logs = array_slice( $logs, 0, $max_logs ); } update_option( $this->log_option, $logs, false ); } private function get_operation_logs() { $logs = get_option( $this->log_option, array() ); return is_array( $logs ) ? $logs : array(); } }