'; echo '
%s
' . esc_html__( 'A template package is one .zip holding layouts, content, content types, media and settings. Importing one adds everything it carries to this site; exporting builds one from what is here now.', 'aumframe' ) . '
'; echo '' . esc_html__( 'This server has no ZipArchive extension, so packages cannot be built here.', 'aumframe' ) . '
'; } else { echo ''; } echo ''; } private static function guard(): void { if (!current_user_can(self::CAP)) { wp_die(esc_html__('You do not have permission to manage template packages.', 'aumframe'), '', ['response' => 403]); } $nonce = isset($_POST['_wpnonce']) ? sanitize_text_field(wp_unslash($_POST['_wpnonce'])) : ''; if (!wp_verify_nonce($nonce, self::NONCE)) { wp_die(esc_html__('That link expired. Go back and try again.', 'aumframe'), '', ['response' => 403]); } } private static function back(bool $ok, string $msg): void { set_transient('aumframe_kit_notice', ['ok' => $ok, 'msg' => $msg], 60); wp_safe_redirect(admin_url('admin.php?page=' . self::SLUG)); exit; } public static function handle_import(): void { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The nonce and the capability are both checked by self::guard(), the first statement of this method. if (empty($_FILES['kit']['name'])) { self::back(false, __('No file was uploaded.', 'aumframe')); } require_once ABSPATH . 'wp-admin/includes/file.php'; $file = wp_handle_upload( // phpcs:ignore WordPress.Security.ValidatedSanitizedInput, WordPress.Security.NonceVerification.Missing -- wp_handle_upload() validates and sanitises the upload itself; the nonce is checked by self::guard(), the first statement of this method. $_FILES['kit'], [ 'test_form' => false, 'mimes' => ['zip' => 'application/zip'], ] ); if (!is_array($file) || !empty($file['error'])) { self::back(false, is_array($file) ? (string) $file['error'] : __('The upload failed.', 'aumframe')); } $res = Package::import($file['file']); wp_delete_file($file['file']); if (empty($res['ok'])) { self::back(false, (string) ($res['error'] ?? __('The package could not be imported.', 'aumframe'))); } $warn = (array) ($res['warnings'] ?? []); self::back(true, $warn /* translators: %d: number of warnings reported by the import. */ ? sprintf(__('Imported, with %d thing(s) worth checking: ', 'aumframe'), count($warn)) . implode(' ยท ', array_map('strval', array_slice($warn, 0, 5))) : __('Imported.', 'aumframe')); } public static function handle_export(): void { self::guard(); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- The nonce is checked by self::guard(), the first statement of this method. $name = isset($_POST['kit_name']) ? sanitize_text_field(wp_unslash($_POST['kit_name'])) : ''; $name = $name !== '' ? $name : (string) get_bloginfo('name'); $slug = sanitize_title($name) ?: 'template'; $up = wp_upload_dir(); $out = trailingslashit($up['basedir']) . $slug . '-' . gmdate('Ymd-His') . '.zip'; $res = Package::export(['name' => $name, 'slug' => $slug, 'version' => '1.0.0'], $out); if (empty($res['ok'])) { self::back(false, (string) ($res['error'] ?? __('The package could not be built.', 'aumframe'))); } self::back(true, sprintf( /* translators: %s: URL of the package that was just built. */ __('Package built: %s', 'aumframe'), trailingslashit($up['baseurl']) . basename($out) )); } }