'', 'client_secret' => '', 'redirect' => 'own', 'folder' => 'Site Replica', ), $out ); } elseif ( 's3' === $id ) { $out = array_merge( array( 'provider' => 'aws', 'region' => '', 'endpoint' => '', 'bucket' => '', 'access_key' => '', 'secret_key' => '', 'folder' => 'Site Replica', ), $out ); } return $out; } /** * Where cloud backups go: drive or s3. */ public static function active() { $all = Secrets::get_option( self::OPTION ); return 's3' === ( $all['active'] ?? 'drive' ) ? 's3' : 'drive'; } /** * Chooses where cloud backups go. * * @param string $id drive or s3. */ public static function set_active( $id ) { $all = Secrets::get_option( self::OPTION ); $all['active'] = 's3' === $id ? 's3' : 'drive'; Secrets::update_option( self::OPTION, $all ); } /** * Saves settings of one destination (merged into what is there). * * @param string $id Destination. * @param array $fields Fields. */ public static function update( $id, array $fields ) { $all = Secrets::get_option( self::OPTION ); $all[ $id ] = array_merge( isset( $all[ $id ] ) && is_array( $all[ $id ] ) ? $all[ $id ] : array(), $fields ); Secrets::update_option( self::OPTION, $all ); } /** * Schedule and cloud backup options (a plain option: restores keep each site's own, so a copy * of a site never starts backing up on the original's schedule). */ const SCHEDULE_OPTION = 'accessnow_replica_schedule'; /** * Schedule: {frequency: off|daily|weekly, time: HH:MM (site time), day: 0-6 (Sunday = 0), * what: all|db, keep: backups kept in the cloud, part_mb: part size, notify: failure|always|never, email}. * * @return array */ public static function schedule() { $saved = get_option( self::SCHEDULE_OPTION, array() ); return array_merge( array( 'frequency' => 'off', 'time' => '02:30', 'day' => 1, 'what' => 'all', 'keep' => 5, 'part_mb' => 50, 'notify' => 'failure', 'email' => (string) get_option( 'admin_email' ), ), is_array( $saved ) ? $saved : array() ); } /** * Saves the schedule. * * @param array $input Raw fields. * @return array Saved schedule. */ public static function save_schedule( array $input ) { $current = self::schedule(); $time = isset( $input['time'] ) && preg_match( '/^([01]?\d|2[0-3]):([0-5]\d)$/', (string) $input['time'] ) ? (string) $input['time'] : $current['time']; $email = isset( $input['email'] ) ? sanitize_email( (string) $input['email'] ) : $current['email']; $clean = array( 'frequency' => isset( $input['frequency'] ) && in_array( $input['frequency'], array( 'off', 'daily', 'weekly' ), true ) ? $input['frequency'] : $current['frequency'], 'time' => $time, 'day' => isset( $input['day'] ) ? max( 0, min( 6, (int) $input['day'] ) ) : $current['day'], 'what' => isset( $input['what'] ) && in_array( $input['what'], array( 'all', 'db' ), true ) ? $input['what'] : $current['what'], 'keep' => isset( $input['keep'] ) ? max( 1, min( 60, (int) $input['keep'] ) ) : $current['keep'], 'part_mb' => isset( $input['part_mb'] ) ? max( 10, min( 500, (int) $input['part_mb'] ) ) : $current['part_mb'], 'notify' => isset( $input['notify'] ) && in_array( $input['notify'], array( 'failure', 'always', 'never' ), true ) ? $input['notify'] : $current['notify'], 'email' => '' !== $email ? $email : (string) get_option( 'admin_email' ), ); update_option( self::SCHEDULE_OPTION, $clean, false ); return $clean; } /** * Whether the old WP Replica plugin left a Google app on this site. */ public static function legacy_available() { return '' !== (string) get_option( 'wpreplica_drive_client_id', '' ) && '' !== (string) get_option( 'wpreplica_drive_client_secret', '' ); } /** * Takes over the old plugin's Google app, and its access when it still has a refresh token. * * @return bool Whether a refresh token came with it. */ public static function import_legacy() { self::update( 'drive', array( 'client_id' => trim( (string) get_option( 'wpreplica_drive_client_id', '' ) ), 'client_secret' => trim( (string) get_option( 'wpreplica_drive_client_secret', '' ) ), 'redirect' => 'legacy', ) ); $token = get_option( 'wpreplica_drive_oauth_token', array() ); if ( is_string( $token ) ) { $decoded = json_decode( $token, true ); $token = is_array( $decoded ) ? $decoded : array(); } if ( is_array( $token ) && ! empty( $token['refresh_token'] ) ) { Google_Drive::adopt_refresh_token( (string) $token['refresh_token'] ); return true; } return false; } }