*/ public static function defaults() { return array( // Where backups go: 'local' (download to the computer, then delete from the server) or 'server' // (kept in a folder outside the web root). 'destination' => 'local', // The backup folder (absolute path). Empty = inside wp-content, which only 'local' may use. // ROLLBAKR_BACKUP_DIR wins over this. 'dir' => '', // Parts included by default on the backup screen. 'include' => array( 'database', 'themes', 'plugins', 'uploads', 'other' ), // Also dump tables that do not start with the site's prefix. 'all_tables' => false, // Folders excluded from the files, one per line (relative to wp-content, or a bare folder name). 'exclude' => ".git\nnode_modules", // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude // How many completed backups to keep on the server. 'keep' => 5, // Back up the current state before restoring. 'pre_restore_backup' => true, ); } /** * All settings, with defaults filled in. * * @return array */ public static function all() { $saved = get_option( self::OPTION ); return self::sanitize( is_array( $saved ) ? $saved : array() ); } /** * One setting. * * @param string $key Setting name. * @return mixed */ public static function get( $key ) { $all = self::all(); return $all[ $key ] ?? null; } /** * Save settings (merged into the saved ones). * * @param array $values Values to change. * @return array The settings after saving. */ public static function update( array $values ) { $all = self::sanitize( array_merge( self::all(), $values ) ); update_option( self::OPTION, $all, false ); return $all; } /** * Take what the settings form (N5) sent, save it, and say what happened. * * @param array $input The form fields, already unslashed and cleaned by the caller * (see Rollbakr_Admin::save_settings()). * @return array{settings:array,dir_changed:bool,dir_rejected:bool,dir_reason:string,destination_changed:bool} * dir_reason: '' | 'not_allowed' | 'inside' | 'empty' (why the folder was not taken). */ public static function apply( array $input ) { $before = self::all(); $dest = isset( $input['destination'] ) ? (string) $input['destination'] : $before['destination']; $dest = in_array( $dest, array( 'local', 'server' ), true ) ? $dest : $before['destination']; // The folder field comes with 'dir_form' (absent when ROLLBAKR_BACKUP_DIR disables it, or when a // caller only saves other fields): without it the folder stays as it is. $has_dir = ! empty( $input['dir_form'] ); $dir = $has_dir ? trim( (string) ( $input['dir'] ?? '' ) ) : $before['dir']; // With "download to your computer" and no folder chosen so far, the folder ROLLBAKR suggests is not a // choice: taking it would move the folder, and the backups waiting in the old one would leave the list. if ( 'local' === $dest && '' === $before['dir'] && '' !== $dir && Rollbakr_Storage::normalize( $dir ) === Rollbakr_Storage::suggest_dir() ) { $dir = ''; } $reason = ''; if ( $has_dir && '' !== $dir && ! Rollbakr_Storage::is_allowed_dir( $dir ) ) { $reason = 'not_allowed'; } elseif ( 'server' === $dest && '' === $dir && ! defined( 'ROLLBAKR_BACKUP_DIR' ) ) { // Keeping backups on the server needs a folder outside the web root: the default (inside // wp-content) will not do. $reason = 'empty'; } elseif ( 'server' === $dest && '' !== $dir && Rollbakr_Storage::is_web_reachable( $dir ) ) { $reason = 'inside'; } $rejected = '' !== $reason; $values = array( 'destination' => $dest, // A folder that is refused leaves the folder as it was: never a silent fall back to the // default, which a browser may reach. 'dir' => $rejected ? $before['dir'] : $dir, 'include' => (array) ( $input['include'] ?? array() ), 'all_tables' => ! empty( $input['all_tables'] ), 'exclude' => (string) ( $input['exclude'] ?? '' ), // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude 'keep' => (int) ( $input['keep'] ?? $before['keep'] ), 'pre_restore_backup' => ! empty( $input['pre_restore_backup'] ), ); $after = self::update( $values ); /** * The settings form was saved (ROLLBAKR Pro saves its own fields here). * * @param array $input The form fields, unslashed and cleaned (checkboxes and markers as '1' when sent; ROLLBAKR Pro's rollbakr_pro_* fields only as '1' markers). * @param array $after The free settings after saving. */ do_action( 'rollbakr_settings_saved', $input, $after ); return array( 'settings' => $after, 'dir_changed' => $after['dir'] !== $before['dir'], 'dir_rejected' => $rejected, 'dir_reason' => $reason, 'destination_changed' => $after['destination'] !== $before['destination'], ); } /** * Clean values and fill in defaults. * * @param array $values Raw values. * @return array */ public static function sanitize( array $values ) { $d = self::defaults(); $out = array(); $dest = (string) ( $values['destination'] ?? $d['destination'] ); $out['destination'] = in_array( $dest, array( 'local', 'server' ), true ) ? $dest : $d['destination']; // Only an absolute folder that is not the install or one of its main folders (see Rollbakr_Storage::is_allowed_dir()). $dir = isset( $values['dir'] ) ? trim( (string) $values['dir'] ) : ''; $out['dir'] = '' !== $dir && Rollbakr_Storage::is_allowed_dir( $dir ) ? Rollbakr_Storage::normalize( $dir ) : ''; $parts = is_array( $values['include'] ?? null ) ? $values['include'] : $d['include']; $out['include'] = array_values( array_intersect( $d['include'], array_map( 'strval', $parts ) ) ); $out['all_tables'] = ! empty( $values['all_tables'] ); $lines = preg_split( '/\r\n|\r|\n/', (string) ( $values['exclude'] ?? $d['exclude'] ) ); $lines = array_filter( array_map( 'trim', $lines ), 'strlen' ); $lines = array_map( static fn( $l ) => trim( wp_normalize_path( $l ), '/' ), $lines ); // ".." and "." have no meaning under wp-content: they are dropped from each line. $lines = array_map( static fn( $l ) => implode( '/', array_filter( explode( '/', $l ), static fn( $seg ) => '' !== $seg && '.' !== $seg && '..' !== $seg ) ), $lines ); $out['exclude'] = implode( "\n", array_unique( array_filter( $lines, 'strlen' ) ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude $keep = (int) ( $values['keep'] ?? $d['keep'] ); $out['keep'] = max( 1, min( 50, $keep ) ); $out['pre_restore_backup'] = ! isset( $values['pre_restore_backup'] ) || ! empty( $values['pre_restore_backup'] ); return $out; } }