try_lock() ) { WP_CLI::error( __( 'Another backup is already running (from the screen or the schedule). Try again in a while.', 'omnimigrate' ) ); } // Las opciones viajan como las manda el navegador: step_init() las lee de $_POST. // Esto no procesa un formulario (aqui no hay navegador: es la consola): se guarda el // $_POST que hubiera para devolverlo tal cual unas lineas mas abajo. // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No se lee ningun dato enviado; solo se conserva y se restaura. $antes = $_POST; $_POST = array( 'only_db' => empty( $assoc_args['only-db'] ) ? '' : '1', 'exclude_media' => empty( $assoc_args['exclude-media'] ) ? '' : '1', 'exclude_plugins' => empty( $assoc_args['exclude-plugins'] ) ? '' : '1', 'exclude_themes' => empty( $assoc_args['exclude-themes'] ) ? '' : '1', ); if ( ! $callado ) { WP_CLI::log( __( 'Exporting the database and building the file list...', 'omnimigrate' ) ); } $inicio = microtime( true ); $respuesta = $export->run_step( 'init' ); $_POST = $antes; if ( empty( $respuesta['success'] ) ) { WP_CLI::error( self::motivo( $respuesta ) ); } $total = isset( $respuesta['data']['total_files'] ) ? (int) $respuesta['data']['total_files'] : 0; $barra = ( ! $callado && $total > 0 ) ? WP_CLI\Utils\make_progress_bar( __( 'Copying files', 'omnimigrate' ), $total ) : null; $hecho = 0; do { $respuesta = $export->run_step( 'content' ); if ( empty( $respuesta['success'] ) ) { if ( $barra ) { $barra->finish(); } WP_CLI::error( self::motivo( $respuesta ) ); } $ahora = isset( $respuesta['data']['offset'] ) ? (int) $respuesta['data']['offset'] : $total; if ( $barra && $ahora > $hecho ) { $barra->tick( $ahora - $hecho ); $hecho = $ahora; } } while ( empty( $respuesta['data']['done'] ) ); if ( $barra ) { $barra->finish(); } $final = $export->run_step( 'finish' ); if ( empty( $final['success'] ) ) { WP_CLI::error( self::motivo( $final ) ); } $fichero = isset( $final['data']['filename'] ) ? (string) $final['data']['filename'] : ''; if ( $callado ) { WP_CLI::line( $fichero ); return; } $ruta = $dir . '/' . $fichero; WP_CLI::success( sprintf( /* translators: 1: file name, 2: file size, 3: number of files, 4: seconds it took. */ __( '%1$s (%2$s, %3$s files, %4$s s)', 'omnimigrate' ), $fichero, file_exists( $ruta ) ? size_format( filesize( $ruta ) ) : '?', number_format_i18n( $total ), round( microtime( true ) - $inicio, 1 ) ) ); } /** * Listar las copias que hay en el servidor. * * ## OPTIONS * * [--format=] * : table, csv, json o yaml. * --- * default: table * options: * - table * - csv * - json * - yaml * --- * * ## EXAMPLES * * wp omni list * * @when after_wp_load */ public function list_( $args, $assoc_args ) { $dir = OmniMigrate_Main::backup_dir(); $ficheros = OmniMigrate_Main::backup_files( $dir ); usort( $ficheros, static function ( $a, $b ) { return filemtime( $b ) - filemtime( $a ); } ); $filas = array(); foreach ( $ficheros as $fichero ) { $filas[] = array( 'file' => basename( $fichero ), 'size' => size_format( filesize( $fichero ) ), 'date' => get_date_from_gmt( gmdate( 'Y-m-d H:i:s', filemtime( $fichero ) ), 'Y-m-d H:i' ), ); } if ( ! $filas ) { WP_CLI::log( __( 'There are no backups.', 'omnimigrate' ) ); return; } $formato = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; WP_CLI\Utils\format_items( $formato, $filas, array( 'file', 'size', 'date' ) ); } /** * Borrar una copia. * * ## OPTIONS * * * : El nombre del archivo .omni, tal como lo lista `wp omni list`. * * ## EXAMPLES * * wp omni delete ejemplo.local-20261021-180000-abc123.omni * * @when after_wp_load */ public function delete( $args, $assoc_args ) { $nombre = OmniMigrate_Main::safe_backup_name( isset( $args[0] ) ? $args[0] : '' ); if ( '' === $nombre ) { WP_CLI::error( __( 'That file name is not valid.', 'omnimigrate' ) ); } $ruta = OmniMigrate_Main::backup_dir() . '/' . $nombre; if ( ! file_exists( $ruta ) ) { WP_CLI::error( __( 'That backup is not there any more.', 'omnimigrate' ) ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink -- wp_delete_file() no devuelve nada y aqui hace falta saber si se borro. if ( ! @unlink( $ruta ) ) { WP_CLI::error( __( 'It could not be deleted (permissions?).', 'omnimigrate' ) ); } WP_CLI::success( sprintf( /* translators: %s: name of the deleted backup file. */ __( 'Deleted: %s', 'omnimigrate' ), $nombre ) ); } /** * Ver o cambiar las copias automaticas. * * ## OPTIONS * * [--on] * : Encender las copias automaticas. * * [--off] * : Apagarlas sin perder el dia y la hora que tuvieran puestos. * * [--freq=] * : daily o weekly. * * [--day=] * : Dia de la semana cuando la frecuencia es weekly: 0 domingo ... 6 sabado. * * [--hour=] * : Hora del dia, de 0 a 23. * * ## EXAMPLES * * wp omni schedule * wp omni schedule --on --freq=daily --hour=4 * wp omni schedule --on --freq=weekly --day=5 --hour=5 * wp omni schedule --off * * @when after_wp_load */ public function schedule( $args, $assoc_args ) { $cambios = array(); if ( ! empty( $assoc_args['on'] ) ) { $cambios['enabled'] = true; } if ( ! empty( $assoc_args['off'] ) ) { $cambios['enabled'] = false; } if ( isset( $assoc_args['freq'] ) ) { // 'off' sigue valiendo: es como se apagaba antes de la 2.4.0. $cambios['freq'] = (string) $assoc_args['freq']; } if ( isset( $assoc_args['day'] ) ) { $cambios['day'] = (int) $assoc_args['day']; } if ( isset( $assoc_args['hour'] ) ) { $cambios['hour'] = (int) $assoc_args['hour']; } if ( $cambios ) { OmniMigrate_Schedule::guardar( $cambios ); } $ajustes = OmniMigrate_Schedule::ajustes(); $proxima = OmniMigrate_Schedule::proxima(); $dias = OmniMigrate_Schedule::dias_de_la_semana(); WP_CLI::log( sprintf( /* translators: %s: on or off. */ __( 'Automatic backups: %s', 'omnimigrate' ), $ajustes['enabled'] ? __( 'on', 'omnimigrate' ) : __( 'off', 'omnimigrate' ) ) ); WP_CLI::log( sprintf( /* translators: %s: daily or weekly. */ __( 'Frequency: %s', 'omnimigrate' ), $ajustes['freq'] ) ); if ( 'weekly' === $ajustes['freq'] ) { WP_CLI::log( sprintf( /* translators: %s: name of the day of the week. */ __( 'Day: %s', 'omnimigrate' ), $dias[ $ajustes['day'] ] ) ); } WP_CLI::log( sprintf( /* translators: %s: hour of the day. */ __( 'Hour: %s', 'omnimigrate' ), $ajustes['hour'] . ':00' ) ); WP_CLI::log( sprintf( /* translators: %s: yes or no. */ __( 'Database only: %s', 'omnimigrate' ), $ajustes['only_db'] ? __( 'yes', 'omnimigrate' ) : __( 'no', 'omnimigrate' ) ) ); WP_CLI::log( sprintf( /* translators: %s: date and time of the next backup, or "none". */ __( 'Next: %s', 'omnimigrate' ), $proxima ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $proxima ), 'Y-m-d H:i' ) : __( 'none', 'omnimigrate' ) ) ); } private static function motivo( $respuesta ) { if ( isset( $respuesta['data']['message'] ) ) { return (string) $respuesta['data']['message']; } if ( isset( $respuesta['data'] ) && is_string( $respuesta['data'] ) ) { return $respuesta['data']; } return __( 'unknown error', 'omnimigrate' ); } }