0 ) { // Batches keep each query short; the limit keeps one run from taking too long. for ( $i = 0; $i < 50; $i++ ) { if ( NovoVerify_DB::delete_checks_older_than( $days ) < 1 ) { break; } } } if ( class_exists( 'NovoVerify_Import' ) ) { NovoVerify_Import::delete_stale_files(); } /** * Fires after the daily cleanup. */ do_action( 'novoverify_after_cleanup' ); } /** * Starts re-normalizing when the matching settings change. * * @param mixed $old_value Previous settings. * @param mixed $value New settings. */ public static function on_settings_updated( $old_value, $value ) { $old_value = array_merge( NovoVerify_Settings::defaults(), is_array( $old_value ) ? $old_value : array() ); $value = array_merge( NovoVerify_Settings::defaults(), is_array( $value ) ? $value : array() ); if ( (bool) $old_value['case_sensitive'] !== (bool) $value['case_sensitive'] || (bool) $old_value['ignore_separators'] !== (bool) $value['ignore_separators'] ) { self::start_renormalize(); } } /** * Same as on_settings_updated() for the very first save. * * @param string $option Option name. * @param mixed $value New settings. */ public static function on_settings_added( $option, $value ) { self::on_settings_updated( NovoVerify_Settings::defaults(), $value ); } /** * Starts (or restarts) re-normalizing all stored codes. */ public static function start_renormalize() { update_option( self::RENORMALIZE_OPTION, array( 'after_id' => 0, 'updated' => 0, 'conflicts' => 0, 'done' => false, 'total' => NovoVerify_DB::count_codes(), ), false ); wp_clear_scheduled_hook( self::RENORMALIZE_HOOK ); // The first batch runs right away, so small code lists are finished immediately. self::renormalize(); } /** * Processes one batch and schedules the next one. */ public static function renormalize() { $state = get_option( self::RENORMALIZE_OPTION ); if ( ! is_array( $state ) || ! empty( $state['done'] ) ) { return; } $rows = NovoVerify_DB::get_code_batch( (int) $state['after_id'], self::RENORMALIZE_BATCH ); foreach ( $rows as $row ) { $state['after_id'] = (int) $row->id; $source = '' !== (string) $row->display_code ? (string) $row->display_code : (string) $row->code; $code = NovoVerify_Codes::normalize_with_settings( $source ); if ( $code === $row->code ) { continue; } // A code that would become invalid, or equal to another code, keeps its old value. if ( NovoVerify_Codes::is_valid( $code ) && NovoVerify_DB::set_code_value( (int) $row->id, $code ) ) { ++$state['updated']; } else { ++$state['conflicts']; } } if ( count( $rows ) < self::RENORMALIZE_BATCH ) { $state['done'] = true; } else { wp_schedule_single_event( time(), self::RENORMALIZE_HOOK ); } update_option( self::RENORMALIZE_OPTION, $state, false ); } /** * Current re-normalization state, or null when none ran. * * @return array|null */ public static function renormalize_state() { $state = get_option( self::RENORMALIZE_OPTION ); return is_array( $state ) ? $state : null; } }