Skip to content

Commit 7718f59

Browse files
committed
Give the import form core's own shape
wp_import_upload_form() is a .wp-upload-form with a *visible* label saying what to choose, the upload limit beside it, a plain file input and a submit button. A file input is what core uses — hiding one behind a styled button is the part that would not be core-like — so what was missing was the label, the limit and the spacing, not the input. Export, import and reset are three separate groups now rather than two, because a file input and an Export button on one line was the shape that looked cramped.
1 parent 9e1c9c8 commit 7718f59

2 files changed

Lines changed: 49 additions & 12 deletions

File tree

‎src/SettingFields.php‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,22 @@ public function render_screen_tools( string $settings, $screen ): string {
772772

773773
if ( $this->config['export_import'] ) {
774774
$tools .= $this->tool(
775-
__( 'Settings file', 'arraypress' ),
776-
__( 'Download this page\'s settings, or read a file back. Encrypted values are not exported — they cannot be read on another site.', 'arraypress' ),
777-
$this->action_form( 'export', __( 'Export', 'arraypress' ), 'button' ) . $this->import_form()
775+
__( 'Export', 'arraypress' ),
776+
__( 'Download this page\'s settings as a JSON file. Encrypted values are left out — they cannot be read on another site.', 'arraypress' ),
777+
$this->action_form( 'export', __( 'Export settings', 'arraypress' ), 'button' )
778+
);
779+
780+
$tools .= $this->tool(
781+
__( 'Import', 'arraypress' ),
782+
__( 'Read a settings file back. Every value is sanitized by its own field, and anything that is not a field on this page is ignored.', 'arraypress' ),
783+
$this->import_form()
778784
);
779785
}
780786

781787
if ( $this->config['reset_button'] ) {
782788
$tools .= $this->tool(
783789
__( 'Reset', 'arraypress' ),
784-
__( 'Restore every setting on this tab to its default.', 'arraypress' ),
790+
__( 'Restore every setting on the tab you are viewing to its default.', 'arraypress' ),
785791
$this->action_form(
786792
'reset',
787793
__( 'Reset this tab', 'arraypress' ),
@@ -857,20 +863,35 @@ private function action_form( string $action, string $label, string $classes, st
857863
* @return string
858864
*/
859865
private function import_form(): string {
860-
// Numbered throughout: the id appears twice, and mixing numbered
861-
// with unnumbered placeholders advances two separate counters.
866+
// core's own import form, from wp_import_upload_form(): a
867+
// .wp-upload-form, a *visible* label saying what to choose, the size
868+
// limit beside it, and a plain file input. A file input is what core
869+
// uses — hiding one behind a styled button is the part that would not
870+
// be core-like.
871+
//
872+
// Numbered throughout: the id appears twice, and mixing numbered with
873+
// unnumbered placeholders advances two separate counters.
874+
$bytes = wp_max_upload_size();
875+
862876
return sprintf(
863-
'<form method="post" enctype="multipart/form-data" action="%1$s">%2$s' .
877+
'<form method="post" enctype="multipart/form-data" action="%1$s" class="wp-upload-form">%2$s' .
864878
'<input type="hidden" name="action" value="%3$s" />' .
865-
'<label class="screen-reader-text" for="%4$s-import">%5$s</label>' .
866-
'<input type="file" id="%4$s-import" name="import" accept="application/json,.json" required />' .
867-
'<button type="submit" class="button">%6$s</button></form>',
879+
'<input type="hidden" name="max_file_size" value="%7$d" />' .
880+
'<p><label for="%4$s-import">%5$s</label> <span class="description">(%8$s)</span></p>' .
881+
'<p><input type="file" id="%4$s-import" name="import" accept="application/json,.json" required /></p>' .
882+
'<p class="submit"><button type="submit" class="button">%6$s</button></p></form>',
868883
esc_url( admin_url( 'admin-post.php' ) ),
869884
wp_nonce_field( $this->action_slug( 'import' ), '_wpnonce', false, false ),
870885
esc_attr( $this->action_slug( 'import' ) ),
871886
esc_attr( $this->id ),
872-
esc_html__( 'Settings file to import', 'arraypress' ),
873-
esc_html__( 'Import', 'arraypress' )
887+
esc_html__( 'Choose a settings file from your computer:', 'arraypress' ),
888+
esc_html__( 'Import', 'arraypress' ),
889+
(int) $bytes,
890+
sprintf(
891+
/* translators: %s: maximum allowed upload size */
892+
esc_html__( 'Maximum size: %s', 'arraypress' ),
893+
esc_html( size_format( $bytes ) )
894+
)
874895
);
875896
}
876897

‎tests/stubs.php‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,19 @@ public function set_help_sidebar( string $content ): void {
371371
$this->help_sidebar = $content;
372372
}
373373
}
374+
375+
if ( ! function_exists( 'wp_max_upload_size' ) ) {
376+
function wp_max_upload_size() {
377+
return 8 * MB_IN_BYTES;
378+
}
379+
}
380+
381+
if ( ! function_exists( 'size_format' ) ) {
382+
function size_format( $bytes, $decimals = 0 ) {
383+
return round( (int) $bytes / MB_IN_BYTES, $decimals ) . ' MB';
384+
}
385+
}
386+
387+
if ( ! defined( 'MB_IN_BYTES' ) ) {
388+
define( 'MB_IN_BYTES', 1024 * 1024 );
389+
}

0 commit comments

Comments
 (0)