Rollbakr_Rest::status_data(), 'rest' => rest_url(), // One piece of an upload: 4 MB, or less when the server allows less (never below 256 KB). 'piece' => (int) max( 256 * KB_IN_BYTES, min( 4 * MB_IN_BYTES, wp_max_upload_size() - 64 * KB_IN_BYTES ) ), 'urls' => array( 'list' => admin_url( 'admin.php?page=rollbakr' ), 'progress' => admin_url( 'admin.php?page=rollbakr' ), 'logs' => admin_url( 'admin.php?page=rollbakr-logs' ), 'restore' => admin_url( 'admin.php?page=rollbakr&restore=' ), ), ) ) . ';', 'before' ); } /** * Version string of one of our files: the plugin version plus the file's modification time. * * @param string $file Path relative to the plugin folder. * @return string */ public static function asset_version( $file ) { $time = @filemtime( ROLLBAKR_PATH . $file ); // phpcs:ignore WordPress.PHP.NoSilencedErrors return ROLLBAKR_VERSION . ( $time ? '.' . $time : '' ); } /** * Draw a template from templates/admin/. * * @param string $name Template name without .php. * @param array $vars Variables for the template. * @return void */ public static function render( $name, array $vars = array() ) { $file = ROLLBAKR_PATH . 'templates/admin/' . $name . '.php'; if ( is_file( $file ) ) { extract( $vars, EXTR_SKIP ); // phpcs:ignore WordPress.PHP.DontExtract include $file; } } /** * Backups (N1). * * @return void */ public static function backups_screen() { $status = Rollbakr_Rest::status_data(); $check = $status['check']; $job = $status['job']; // ?job=: the progress screen (N2 for a backup, N4 for a restore) of the current job. $wanted = isset( $_GET['job'] ) ? sanitize_text_field( wp_unslash( $_GET['job'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification if ( $job && $wanted === $job['id'] && Rollbakr_Upload::TYPE === $job['type'] ) { // An upload being checked: back on N7, which picks the job up. self::render( 'upload', array( 'status' => $status, 'job' => $job, ) ); return; } // phpcs:ignore WordPress.Security.NonceVerification if ( isset( $_GET['upload'] ) ) { self::render( 'upload', array( 'status' => $status, 'job' => null, ) ); return; } if ( $job && $wanted === $job['id'] && in_array( $job['type'], array( Rollbakr_Backup::TYPE, Rollbakr_Restore::TYPE ), true ) ) { self::render( Rollbakr_Restore::TYPE === $job['type'] ? 'restore-progress' : 'progress', array( 'job' => $job, 'status' => $status, ) ); return; } if ( $job && $wanted === $job['id'] ) { // A job of a type this plugin does not know (an add-on's): the plain progress screen, drawn // from how the type describes itself, so that the link to it never dead-ends. self::render( 'job-progress', array( 'job' => $job, 'status' => $status, 'meta' => Rollbakr_Job::meta( (string) $job['type'] ), ) ); return; } // ?restore=: the confirmation screen (N3). $restore = isset( $_GET['restore'] ) ? sanitize_text_field( wp_unslash( $_GET['restore'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification $m = '' !== $restore ? Rollbakr_Backups::get( $restore ) : null; if ( $m ) { self::render( 'restore', array( 'backup' => Rollbakr_Rest::backup_data( $m ), 'check' => Rollbakr_Restore::check( $m ), 'status' => $status, 'settings' => Rollbakr_Settings::all(), 'labels' => Rollbakr_Files::labels(), ) ); return; } self::render( 'backups', array( 'check' => $check, 'status' => $status, 'backups' => self::backup_rows( Rollbakr_Backups::all() ), // Null when they are not cached: the screen shows "counting" and asks REST for them. 'sizes' => Rollbakr_Files::sizes_cached(), 'labels' => Rollbakr_Files::labels(), 'settings' => Rollbakr_Settings::all(), ) ); } /** * The rows of the backup list (N1), one per backup, as the template draws them: the manifest as * Rollbakr_Rest::backup_data() gives it, plus * locations where the backup is, as marks [ 'key' => ..., 'label' => ... ] (here: this server) * status one line under the date (empty here; an add-on may say "being sent" and the like) * mark a short mark in the middle column when the row has no "saved" mark (empty here; an * add-on's "Only in the cloud") * actions the links of the row: [ 'label' => ..., 'url' => ... ] or [ 'label' => ..., 'js' => data * attribute name (set to the id), 'attrs' => more data attributes ], plus 'danger' * (red), 'confirm' (a question asked before the click goes through) and 'icon' * ('trash': drawn as an icon button, the label being its name and its tooltip) * An add-on may add rows (a backup kept elsewhere only), locations and actions through the filter. * * @param array> $manifests Rollbakr_Backups::all(). * @return array> */ public static function backup_rows( array $manifests ) { $local = Rollbakr_Storage::is_local(); $rows = array(); foreach ( $manifests as $m ) { $b = Rollbakr_Rest::backup_data( $m ); $b['locations'] = array( array( 'key' => 'server', 'label' => __( 'This server', 'rollbakr' ), ), ); $b['status'] = ''; $b['actions'] = array( array( 'label' => __( 'Restore', 'rollbakr' ), 'url' => admin_url( 'admin.php?page=rollbakr&restore=' . $b['id'] ), ), array( 'label' => __( 'Log', 'rollbakr' ), 'url' => admin_url( 'admin.php?page=rollbakr-logs&log=' . $b['id'] ), ), array( 'label' => $local ? __( 'Delete from the server', 'rollbakr' ) : __( 'Delete', 'rollbakr' ), 'js' => 'rollbakr-delete', 'danger' => true, 'icon' => 'trash', 'attrs' => array( 'rollbakr-desc' => $b['created_text'] . ' · ' . $b['parts_label'] . ' · ' . $b['bytes_text'] ), ), ); $rows[] = $b; } /** * The rows of the backup list (see backup_rows()). * * @param array> $rows Rows, newest first. */ $rows = (array) apply_filters( 'rollbakr_backup_rows', $rows ); // A row an add-on added with only some of the keys draws as empty cells, not as warnings. $blank = array( 'id' => '', 'kind' => '', 'created_text' => '', 'kind_label' => '', 'parts_label' => '', 'bytes_text' => '', 'archives' => array(), 'downloads' => array(), 'labels' => array(), 'locations' => array(), 'status' => '', 'mark' => '', 'actions' => array(), 'downloaded' => false, 'downloaded_at' => 0, 'saved_at' => 0, ); $out = array(); foreach ( $rows as $row ) { if ( is_array( $row ) ) { $out[] = array_merge( $blank, $row ); } } return $out; } /** * Settings (N5). * * @return void */ public static function settings_screen() { global $wpdb; $all_tables = Rollbakr_Db_Export::objects( true ); $own = count( array_filter( array_keys( $all_tables ), static fn( $t ) => str_starts_with( $t, $wpdb->prefix ) ) ); self::render( 'settings', array( 'check' => Rollbakr_Storage::last_check(), 'settings' => Rollbakr_Settings::all(), 'tables' => count( $all_tables ), 'own_tables' => $own, 'used' => Rollbakr_Storage::used_space(), // phpcs:ignore WordPress.Security.NonceVerification -- flags set by our own redirect after saving. 'saved' => isset( $_GET['saved'] ), // phpcs:ignore WordPress.Security.NonceVerification 'rejected' => isset( $_GET['dir_rejected'] ) ? sanitize_key( wp_unslash( $_GET['dir_rejected'] ) ) : '', 'suggested' => Rollbakr_Storage::suggest_dir(), ) ); } /** * admin-post rollbakr_settings: save the settings form and come back to it. * * @return void */ public static function save_settings() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'rollbakr' ), 403 ); } check_admin_referer( 'rollbakr_settings' ); // Each field is read by name and cleaned on the spot, with the narrowest function for what it holds; // Rollbakr_Settings::apply() (and the 'rollbakr_settings_saved' action) only ever see clean values. $input = array(); if ( isset( $_POST['destination'] ) ) { $input['destination'] = sanitize_key( wp_unslash( $_POST['destination'] ) ); // 'local' or 'server'. } if ( isset( $_POST['dir_form'] ) ) { $input['dir_form'] = '1'; // Only whether the folder field was on the form. if ( isset( $_POST['dir'] ) ) { // A path on one line; Rollbakr_Settings::apply() then judges it (is_allowed_dir() and the like). $input['dir'] = sanitize_text_field( wp_unslash( $_POST['dir'] ) ); } } if ( isset( $_POST['include'] ) ) { // The parts are fixed words (database, themes ...): sanitize_key() each; sanitize() keeps the known ones. $input['include'] = array_map( 'sanitize_key', (array) wp_unslash( $_POST['include'] ) ); } if ( isset( $_POST['all_tables'] ) ) { $input['all_tables'] = '1'; // A checkbox: only whether it was ticked. } if ( isset( $_POST['exclude'] ) ) { // One folder per line: the line breaks are kept. $input['exclude'] = sanitize_textarea_field( wp_unslash( $_POST['exclude'] ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- the settings field of folders left out of backups, not a query argument. } if ( isset( $_POST['keep'] ) ) { $input['keep'] = absint( wp_unslash( $_POST['keep'] ) ); } if ( isset( $_POST['pre_restore_backup'] ) ) { $input['pre_restore_backup'] = '1'; } // ROLLBAKR Pro's fields in this form: checkboxes / markers, only whether they were sent. if ( isset( $_POST['rollbakr_pro_form'] ) ) { $input['rollbakr_pro_form'] = '1'; } if ( isset( $_POST['rollbakr_pro_manual_cloud'] ) ) { $input['rollbakr_pro_manual_cloud'] = '1'; } $result = Rollbakr_Settings::apply( $input ); delete_transient( Rollbakr_Files::SIZES ); delete_transient( Rollbakr_Files::WALK ); // A count under way was made with the old excludes. if ( $result['dir_changed'] || $result['destination_changed'] ) { // A new folder, or backups now kept on the server: make the folder, and find out right away // whether it can be written (only "keep on the server" needs to know). Rollbakr_Storage::protect(); if ( ! Rollbakr_Storage::is_local() ) { Rollbakr_Storage::check( true ); } } $args = array( 'saved' => 1 ); if ( $result['dir_rejected'] ) { $args['dir_rejected'] = $result['dir_reason']; } wp_safe_redirect( add_query_arg( $args, admin_url( 'admin.php?page=rollbakr-settings' ) ) ); exit; } /** * Logs (N6). * * @return void */ public static function logs_screen() { $records = Rollbakr_Logs::all(); // phpcs:ignore WordPress.Security.NonceVerification -- which record to open, from a link of ours. $open = isset( $_GET['log'] ) ? sanitize_text_field( wp_unslash( $_GET['log'] ) ) : ''; self::render( 'logs', array( 'records' => $records, 'open' => Rollbakr_Storage::is_valid_id( $open ) ? $open : '', 'job' => Rollbakr_Rest::status_data()['job'], ) ); } /** * Human size, e.g. 2.1 GB. * * @param int|null $bytes Bytes. * @return string */ public static function size( $bytes ) { if ( null === $bytes ) { return __( 'unknown', 'rollbakr' ); } return size_format( (int) $bytes, $bytes >= GB_IN_BYTES ? 1 : 0 ); } /** * Label of an archive for the download links: "Database", "wp-content data", or "wp-content data 2/3" * when the files are split. * * @param string $name Archive name. * @param int $total How many files-N archives the backup has (0 or 1: no number). * @return string */ public static function archive_label( $name, $total = 0 ) { if ( 'database.sql.gz' === $name ) { return __( 'Database', 'rollbakr' ); } if ( preg_match( '/^files-(\d+)\.tar\.gz$/', $name, $m ) ) { if ( $total > 1 ) { /* translators: 1: part number, 2: how many parts */ return sprintf( __( 'wp-content data %1$d/%2$d', 'rollbakr' ), (int) $m[1], (int) $total ); } return __( 'wp-content data', 'rollbakr' ); } return $name; } /** * Labels of a backup's archives for the screen: name => label. * * @param string[] $names Archive names. * @return array */ public static function archive_labels( array $names ) { $total = count( array_filter( $names, static fn( $n ) => (bool) preg_match( '/^files-\d+\.tar\.gz$/', (string) $n ) ) ); $out = array(); foreach ( $names as $name ) { $out[ $name ] = self::archive_label( $name, $total ); } return $out; } /** * Labels of a backup's downloads (see Rollbakr_Backups::bundles()): "Download", or "Download 1/3" * when the archives are split over several. * * @param array $bundles Bundles. * @return array Download name => label. */ public static function bundle_labels( array $bundles ) { $out = array(); foreach ( $bundles as $key => $b ) { $out[ $key ] = (int) ( $b['count'] ?? 1 ) > 1 /* translators: 1: part number, 2: how many parts */ ? sprintf( __( 'Download %1$d/%2$d', 'rollbakr' ), (int) $b['index'], (int) $b['count'] ) : __( 'Download', 'rollbakr' ); } return $out; } /** * The site's time format on a 24-hour clock (no am/pm on ROLLBAKR's screens). * * @return string */ public static function time_format() { $f = (string) get_option( 'time_format' ); // The letters not escaped by a backslash. $plain = preg_replace( '/\\\\./', '', $f ); if ( ! preg_match( '/[aAgh]/', (string) $plain ) ) { return '' === $f ? 'H:i' : $f; } return str_contains( (string) $plain, 's' ) ? 'H:i:s' : 'H:i'; } /** * The date format of ROLLBAKR's screens: year, month, day (2026年11月17日) on a Japanese admin * screen, whatever the site's setting ("F j, Y" would read "11月 17, 2026"); the site's own * date format in every other language. * * @return string */ public static function date_format() { if ( str_starts_with( determine_locale(), 'ja' ) ) { return 'Y年n月j日'; } $f = (string) get_option( 'date_format' ); return '' === $f ? 'F j, Y' : $f; } /** * date_format() with time_format(). * * @return string */ public static function datetime_format() { return self::date_format() . ' ' . self::time_format(); } /** * A length of time, e.g. 6 min 32 s. * * @param int $seconds Seconds. * @return string */ public static function duration( $seconds ) { $seconds = max( 0, (int) $seconds ); if ( $seconds < 60 ) { /* translators: %d: seconds */ return sprintf( __( '%d s', 'rollbakr' ), $seconds ); } /* translators: 1: minutes, 2: seconds */ return sprintf( __( '%1$d min %2$d s', 'rollbakr' ), intdiv( $seconds, 60 ), $seconds % 60 ); } /** * The check's state for the screen: label, colour class and detail. * * @param array|null $check Result of Rollbakr_Storage::check(). * @return array{label:string,class:string,detail:string} */ public static function check_state( $check ) { if ( ! $check ) { return array( 'label' => __( 'Not checked yet', 'rollbakr' ), 'class' => 'is-unknown', 'detail' => __( 'The folder is checked when "Keep on the server" is chosen.', 'rollbakr' ), ); } $when = sprintf( /* translators: %s: time */ __( 'checked at %s', 'rollbakr' ), wp_date( Rollbakr_Admin::time_format(), (int) $check['checked'] ) ); switch ( $check['status'] ) { case 'ok': return array( 'label' => __( 'A browser cannot reach it', 'rollbakr' ), 'class' => 'is-ok', 'detail' => sprintf( /* translators: %s: "checked at