| @@ -54,15 +54,17 @@ | ||
| 54 | 54 | */ |
| 55 | 55 | public function archive_sites_changes( $before_timestamp = 0, $by_limit = 0, $dismiss = false ) { |
| 56 | 56 | $before_timestamp = 1000000 * $before_timestamp; |
| 57 | 57 | $where = ''; |
| 58 | - $order = ''; | |
| 58 | + $order_by = ''; | |
| 59 | + $batch_limit = 0; | |
| 59 | 60 | if ( ! empty( $before_timestamp ) ) { |
| 60 | 61 | $where .= ' AND created < ' . (int) $before_timestamp; |
| 61 | 62 | } |
| 62 | 63 | |
| 63 | 64 | if ( ! empty( $by_limit ) ) { |
| 64 | - $order .= ' ORDER BY created ASC LIMIT ' . (int) $by_limit; | |
| 65 | + $order_by .= ' created ASC '; | |
| 66 | + $batch_limit = (int) $by_limit; | |
| 65 | 67 | } |
| 66 | 68 | |
| 67 | 69 | if ( false !== $dismiss ) { |
| 68 | 70 | $where .= ' AND dismiss = ' . intval( $dismiss ); |
| @@ -67,9 +69,9 @@ | ||
| 67 | 69 | if ( false !== $dismiss ) { |
| 68 | 70 | $where .= ' AND dismiss = ' . intval( $dismiss ); |
| 69 | 71 | } |
| 70 | 72 | |
| 71 | - $this->bulk_archive_logs( $where, $order ); | |
| 73 | + $this->bulk_archive_logs( $where, $order_by, $batch_limit ); | |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | |
| 75 | 77 | /** |
| @@ -75,14 +77,14 @@ | ||
| 75 | 77 | /** |
| 76 | 78 | * Bulk archive logs + meta with batching (no transaction). |
| 77 | 79 | * |
| 78 | 80 | * @param string $where SQL WHERE condition (MUST be prepared). |
| 79 | - * @param string $order SQL ORDER BY clause (e.g. "ORDER BY created ASC"). | |
| 80 | - * @param int $batch_size Number of rows per batch. | |
| 81 | + * @param string $order_by Column and direction for ORDER BY (e.g. "created ASC"). | |
| 82 | + * @param int $batch_limit Number of rows per batch. | |
| 81 | 83 | * |
| 82 | 84 | * @return int Total archived rows. |
| 83 | 85 | */ |
| 84 | - public function bulk_archive_logs( $where, $order, $batch_size = 1000 ) { | |
| 86 | + public function bulk_archive_logs( $where, $order_by, $batch_limit = 1000 ) { | |
| 85 | 87 | global $wpdb; |
| 86 | 88 | |
| 87 | 89 | $table_logs = $this->table_name( 'wp_logs' ); |
| 88 | 90 | $table_logs_archive = $this->table_name( 'wp_logs_archive' ); |
| @@ -96,10 +98,10 @@ | ||
| 96 | 98 | // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL fragments are built from integer-cast archive inputs; table name is internal. |
| 97 | 99 | $ids = $this->wpdb->get_col( |
| 98 | 100 | "SELECT log_id |
| 99 | 101 | FROM $table_logs |
| 100 | - WHERE 1 $where $order | |
| 101 | - LIMIT " . intval( $batch_size ) | |
| 102 | + WHERE 1 $where " . ( ! empty( $order_by ) ? " ORDER BY $order_by " : '' ) . // phpcs:ignore -- NOSONAR -no user input | |
| 103 | + ( $batch_limit ? ' LIMIT ' . intval( $batch_limit ) : '' ) | |
| 102 | 104 | ); |
| 103 | 105 | |
| 104 | 106 | if ( empty( $ids ) ) { |
| 105 | 107 | break; |
| @@ -140,9 +142,9 @@ | ||
| 140 | 142 | ); |
| 141 | 143 | |
| 142 | 144 | $total_archived += count( $ids ); |
| 143 | 145 | |
| 144 | - } while ( count( $ids ) === $batch_size ); | |
| 146 | + } while ( count( $ids ) === $batch_limit ); // Continue if we hit the batch limit. | |
| 145 | 147 | |
| 146 | 148 | return $total_archived; |
| 147 | 149 | } |
| 148 | 150 | |