0, 'orphans' => 0, 'reports' => 0, 'rows' => 0, 'summaries' => 0, 'more' => false, ); $days = max( 1, (int) apply_filters( 'catalogrelay_file_retention_days', 7 ) ); $now = time(); if ( Plugin::wooReady() ) { ImportJobType::finishLost(); // Before the files go: the failed-rows CSV of such a job is read from its file. } foreach ( FileStore::all() as $ref => $mtime ) { if ( $budget <= 0 ) { $out['more'] = true; break; } $meta = FileStore::meta( $ref ); $created = $meta && ! empty( $meta['created'] ) ? (int) strtotime( $meta['created'] . ' UTC' ) : $mtime; if ( $created >= $now - DAY_IN_SECONDS || self::referenced( $ref, true ) ) { continue; // Still in the wizard, or a job that has not ended reads it. } if ( $created < $now - $days * DAY_IN_SECONDS ) { FileStore::delete( $ref ); ++$out['files']; --$budget; } elseif ( $created < $now - DAY_IN_SECONDS && ! self::referenced( $ref ) ) { FileStore::delete( $ref ); ++$out['orphans']; --$budget; } } $rows_days = max( 1, (int) apply_filters( 'catalogrelay_rows_retention_days', 30 ) ); // 0 would delete them as a job ends. $sum_days = max( 1, (int) apply_filters( 'catalogrelay_summary_retention_days', 90 ) ); foreach ( FileStore::reports() as $file => $mtime ) { if ( $budget <= 0 ) { $out['more'] = true; break; } if ( $mtime < $now - $rows_days * DAY_IN_SECONDS ) { wp_delete_file( $file ); ++$out['reports']; --$budget; } } if ( $budget > 0 ) { $n = self::deleteRows( "status <> 'job'", gmdate( 'Y-m-d H:i:s', $now - $rows_days * DAY_IN_SECONDS ), $budget ); $out['rows'] = $n; $out['more'] = $out['more'] || $n >= $budget; $budget -= $n; } if ( $budget > 0 ) { $n = self::deleteRows( "status = 'job'", gmdate( 'Y-m-d H:i:s', $now - $sum_days * DAY_IN_SECONDS ), $budget ); $out['summaries'] = $n; $out['more'] = $out['more'] || $n >= $budget; } if ( $out['more'] && ! wp_next_scheduled( self::CRON_CONTINUE ) ) { wp_schedule_single_event( time() + MINUTE_IN_SECONDS, self::CRON_CONTINUE ); } return $out; } /** * Delete row-log rows older than a cutoff, except those of a job that has not ended or ended after the cutoff, and those * of an import with such a retry (a retry reads and resolves the import's failed rows). * * @param string $status SQL condition on the status (a constant of this class's callers). * @param string $cutoff UTC datetime. * @param int $limit Rows at most. * @return int Rows deleted. */ private static function deleteRows( string $status, string $cutoff, int $limit ): int { global $wpdb; $ended = Job::TERMINAL; $live = $wpdb->prepare( 'status NOT IN (%s, %s, %s, %s) OR finished_at >= %s', $ended[0], $ended[1], $ended[2], $ended[3], $cutoff ); $keep = array_map( 'intval', (array) $wpdb->get_col( "SELECT id FROM {$wpdb->prefix}catalogrelay_jobs WHERE {$live}" ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery -- table name from the prefix, condition prepared above. foreach ( (array) $wpdb->get_col( $wpdb->prepare( "SELECT params_json FROM {$wpdb->prefix}catalogrelay_jobs WHERE job_type = %s AND ({$live})", RetryJobType::TYPE ) ) as $json ) { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery -- table name from the prefix, condition prepared above. $params = json_decode( (string) $json, true ); $keep[] = is_array( $params ) ? (int) ( $params['retry_of'] ?? 0 ) : 0; } $keep = array_values( array_unique( array_filter( $keep ) ) ); $not = $keep ? ' AND job_id NOT IN (' . implode( ', ', $keep ) . ')' : ''; // Integers. return (int) $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE {$status} AND created_at < %s{$not} LIMIT %d", Schema::table(), $cutoff, $limit ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- fixed condition and a list of integers. } /** * Whether any job (or any job that has not ended) refers to a file reference. * * @param string $ref Reference. * @param bool $running Only jobs that have not ended. * @return bool */ private static function referenced( string $ref, bool $running = false ): bool { global $wpdb; $like = '%' . $wpdb->esc_like( $ref ) . '%'; if ( $running ) { $ended = Job::TERMINAL; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}catalogrelay_jobs WHERE params_json LIKE %s AND status NOT IN (%s, %s, %s, %s)", $like, $ended[0], $ended[1], $ended[2], $ended[3] ) ) > 0; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. } return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->prefix}catalogrelay_jobs WHERE params_json LIKE %s", $like ) ) > 0; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from the prefix. } }