transfer = $transfer;
}
public function register() {
add_action( 'admin_menu', array( $this, 'menu' ), 20 );
add_action( 'admin_post_glovario_export', array( $this, 'export' ) );
add_action( 'admin_post_glovario_import', array( $this, 'import' ) );
}
public function menu() {
add_submenu_page( 'glovario', __( 'Import / Export', 'glovario' ), __( 'Import / Export', 'glovario' ), 'manage_options', 'glovario-tools', array( $this, 'page' ) );
}
public function page() {
$this->guard();
$formats = $this->formats();
?>
guard();
check_admin_referer( 'glovario_export' );
$formats = $this->formats();
$format = isset( $_POST['format'] ) ? sanitize_key( wp_unslash( $_POST['format'] ) ) : 'json';
if ( ! isset( $formats[ $format ] ) ) {
$this->redirect( __( 'Select a supported export format.', 'glovario' ), 'error' );
}
$file = $this->service()->export_file( $format );
if ( is_wp_error( $file ) ) {
$this->redirect( $file->get_error_message(), 'error' );
}
$filename = 'glovario-' . gmdate( 'Y-m-d' ) . '.' . $file['extension'];
nocache_headers();
header( 'Content-Type: ' . $file['mime'] );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
header( 'Content-Length: ' . strlen( $file['content'] ) );
echo $file['content']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Validated download response.
exit;
}
public function import() {
$this->guard();
check_admin_referer( 'glovario_import' );
$file = isset( $_FILES['import_file'] ) && is_array( $_FILES['import_file'] ) ? $_FILES['import_file'] : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Upload metadata is validated before use.
if ( empty( $file['tmp_name'] ) || ! isset( $file['size'], $file['name'] ) || ! is_uploaded_file( $file['tmp_name'] ) || (int) $file['size'] > self::MAX_FILE_SIZE ) {
$this->redirect( __( 'Upload a supported export file no larger than 5 MB.', 'glovario' ), 'error' );
}
$format = strtolower( pathinfo( sanitize_file_name( $file['name'] ), PATHINFO_EXTENSION ) );
$formats = $this->formats();
if ( ! isset( $formats[ $format ] ) ) {
$this->redirect( __( 'Upload a supported export file.', 'glovario' ), 'error' );
}
$content = file_get_contents( $file['tmp_name'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated uploaded temporary file.
if ( false === $content ) {
$this->redirect( __( 'The uploaded file could not be read.', 'glovario' ), 'error' );
}
$strategy = isset( $_POST['strategy'] ) && 'overwrite' === $_POST['strategy'] ? 'overwrite' : 'skip';
$dry_run = isset( $_POST['dry_run'] );
$result = $this->service()->import_file( $content, $format, $strategy, $dry_run );
if ( is_wp_error( $result ) ) {
$this->redirect( $result->get_error_message(), 'error' );
}
/* translators: 1: import mode, 2: created count, 3: updated count, 4: skipped count. */
$message = sprintf( __( 'Import %1$s: %2$d create, %3$d update, %4$d skip.', 'glovario' ), $dry_run ? __( 'preview', 'glovario' ) : __( 'complete', 'glovario' ), $result['created'], $result['updated'], $result['skipped'] );
if ( ! empty( $result['images_missing'] ) ) {
/* translators: %s: comma-separated variable keys. */
$message .= ' ' . sprintf( __( 'Choose the image again for: %s (the picture is not in this site\'s Media Library).', 'glovario' ), implode( ', ', $result['images_missing'] ) );
}
if ( ! empty( $result['rules'] ) ) {
if ( $dry_run ) {
/* translators: %d: number of alternative values (rules) in an import preview. */
$message .= ' ' . sprintf( _n( '%d alternative value would be imported.', '%d alternative values would be imported.', $result['rules'], 'glovario' ), $result['rules'] );
} else {
/* translators: %d: number of imported alternative values (rules). */
$message .= ' ' . sprintf( _n( '%d alternative value was imported.', '%d alternative values were imported.', $result['rules'], 'glovario' ), $result['rules'] );
}
}
$this->redirect( $message );
}
private function service() {
return apply_filters( 'glovario_transfer_service', $this->transfer );
}
private function formats() {
return apply_filters(
'glovario_transfer_formats',
array(
'json' => array(
'label' => __( 'JSON', 'glovario' ),
'accept' => 'application/json,.json',
),
)
);
}
private function guard() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to manage Site Variables.', 'glovario' ), 403 );
}
}
private function redirect( $message, $type = 'success' ) {
set_transient(
'glovario_notice_' . get_current_user_id(),
array(
'message' => $message,
'type' => $type,
),
60
);
wp_safe_redirect( admin_url( 'admin.php?page=glovario-tools' ) );
exit;
}
private function notice() {
$key = 'glovario_notice_' . get_current_user_id();
$notice = get_transient( $key );
if ( $notice ) {
delete_transient( $key );
printf( '', esc_attr( $notice['type'] ), esc_html( $notice['message'] ) );
}
}
}