*/ public static function defaults(): array { return array( 'enabled' => false, 'frequency' => ScheduleCalc::DAILY, 'hour' => 2, 'minute' => 0, 'weekday' => 1, 'monthday' => 1, 'retention_enabled' => true, 'retention_max' => 5, // Retention only counts backups this schedule made, unless this is on. A backup someone made by // hand before a risky change must not be deleted by an automatic clean-up they forgot about. 'retention_manual' => false, // Empty means "the site's administrator e-mail", read when the message is sent. 'notify_email' => '', 'notify_success' => false, 'notify_failure' => true, ); } /** * @return array Saved settings over the defaults, every value cleaned. */ public static function get(): array { $saved = get_option( self::OPTION, array() ); return self::clean( array_merge( self::defaults(), is_array( $saved ) ? $saved : array() ) ); } /** * Validates a submission. Nothing is stored here. * * @param array $input Raw values, as the form sends them. * @return array{0:array,1:string[]} The cleaned settings and a list of problems (empty when valid). */ public static function validate( array $input ): array { $errors = array(); $base = self::get(); if ( isset( $input['frequency'] ) && ! in_array( (string) $input['frequency'], ScheduleCalc::frequencies(), true ) ) { $errors[] = __( 'Choose daily, weekly or monthly.', 'migratex' ); } foreach ( array( 'hour' => array( 0, 23, __( 'The hour must be between 0 and 23.', 'migratex' ) ), 'minute' => array( 0, 59, __( 'The minute must be between 0 and 59.', 'migratex' ) ), 'weekday' => array( 0, 6, __( 'Choose a day of the week.', 'migratex' ) ), 'monthday' => array( 1, 31, __( 'The day of the month must be between 1 and 31.', 'migratex' ) ), ) as $key => $rule ) { if ( isset( $input[ $key ] ) && ( ! is_numeric( $input[ $key ] ) || (int) $input[ $key ] < $rule[0] || (int) $input[ $key ] > $rule[1] ) ) { $errors[] = $rule[2]; } } if ( isset( $input['retention_max'] ) && ( ! is_numeric( $input['retention_max'] ) || (int) $input['retention_max'] < 1 || (int) $input['retention_max'] > self::MAX_BACKUPS_LIMIT ) ) { $errors[] = sprintf( /* translators: %d: the largest number allowed */ __( 'Maximum backups to retain must be a whole number from 1 to %d.', 'migratex' ), self::MAX_BACKUPS_LIMIT ); } // Validated as typed, never "repaired" (see FeedbackController): a value with a line break or a second // address would otherwise become some other, valid-looking one. if ( isset( $input['notify_email'] ) ) { $email = trim( (string) $input['notify_email'] ); if ( '' !== $email && ( 1 === preg_match( '/[\r\n\x00<>,;]/', $email ) || ! is_email( $email ) ) ) { $errors[] = __( 'Please enter one valid email address for notifications.', 'migratex' ); } } return array( self::clean( array_merge( $base, array_intersect_key( $input, $base ) ) ), $errors ); } /** * Stores settings that have already passed validate(). * * @param array $settings */ public static function save( array $settings ): void { update_option( self::OPTION, self::clean( array_merge( self::defaults(), $settings ) ), false ); } /** * @param array $raw * @return array */ private static function clean( array $raw ): array { $d = self::defaults(); $frequency = (string) $raw['frequency']; $email = trim( (string) $raw['notify_email'] ); return array( 'enabled' => rest_sanitize_boolean( $raw['enabled'] ), 'frequency' => in_array( $frequency, ScheduleCalc::frequencies(), true ) ? $frequency : $d['frequency'], 'hour' => self::bounded( $raw['hour'], 0, 23, $d['hour'] ), 'minute' => self::bounded( $raw['minute'], 0, 59, $d['minute'] ), 'weekday' => self::bounded( $raw['weekday'], 0, 6, $d['weekday'] ), 'monthday' => self::bounded( $raw['monthday'], 1, 31, $d['monthday'] ), 'retention_enabled' => rest_sanitize_boolean( $raw['retention_enabled'] ), 'retention_max' => self::bounded( $raw['retention_max'], 1, self::MAX_BACKUPS_LIMIT, $d['retention_max'] ), 'retention_manual' => rest_sanitize_boolean( $raw['retention_manual'] ), 'notify_email' => ( '' !== $email && 1 !== preg_match( '/[\r\n\x00<>,;]/', $email ) && is_email( $email ) ) ? $email : '', 'notify_success' => rest_sanitize_boolean( $raw['notify_success'] ), 'notify_failure' => rest_sanitize_boolean( $raw['notify_failure'] ), ); } /** * @param mixed $value */ private static function bounded( $value, int $min, int $max, int $fallback ): int { return is_numeric( $value ) ? max( $min, min( $max, (int) $value ) ) : $fallback; } /** * Where notifications go: the saved address, or the administrator's. */ public static function recipient( array $settings ): string { $address = (string) $settings['notify_email']; return '' !== $address ? $address : (string) get_option( 'admin_email' ); } // ------------------------------------------------------------------ what the last run did /** * @return array */ public static function state(): array { $state = get_option( self::STATE_OPTION, array() ); return array_merge( array( 'status' => '', // success, failed, skipped, postponed, running 'finished' => 0, 'file' => '', 'size' => '', 'message' => '', 'job_id' => '', 'postponed' => 0, 'pruned' => array(), 'started' => 0, ), is_array( $state ) ? $state : array() ); } /** * @param array $changes */ public static function updateState( array $changes ): void { update_option( self::STATE_OPTION, array_merge( self::state(), $changes ), false ); } /** * Archive names this schedule has made, so that retention can tell them from manual backups. * * @return string[] */ public static function scheduledFiles(): array { $files = get_option( self::FILES_OPTION, array() ); return is_array( $files ) ? array_values( array_filter( array_map( 'strval', $files ) ) ) : array(); } public static function rememberFile( string $file ): void { $files = self::scheduledFiles(); $files[] = $file; update_option( self::FILES_OPTION, array_values( array_unique( $files ) ), false ); } /** * @param string[] $files */ public static function setScheduledFiles( array $files ): void { update_option( self::FILES_OPTION, array_values( array_unique( $files ) ), false ); } public static function deleteAll(): void { delete_option( self::OPTION ); delete_option( self::STATE_OPTION ); delete_option( self::FILES_OPTION ); } }