1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| <?php
add_action("admin_menu", function () {
add_menu_page(
"Import-export"
, "Import-export"
, "manage_options"
, "MonExtension__import_export"
, function () {
$message_fichier_importe =
isset($_GET["message"])
&& ("fichier_importe" === $_GET["message"])
;
?>
<h2>Exportation</h2>
<div>
<a href="admin-post.php?action=MonExtension__exportation_produits">
Exportation des produits</a>
</div>
<h2>Importation</h2>
<?php if ($message_fichier_importe) {?>
<div id="message" class="updated notice"><p>
Le fichier a été importé.
</p></div>
<?php }?>
<form
action="admin-post.php?action=MonExtension__charger_fichier_importation"
method="POST"
enctype="multipart/form-data"
>
<input type="file" name="fichier"/>
<input type="submit"/>
</form>
<?php
}
);
});
add_action("admin_init", function () {
if ( !isset($_GET["action"])
|| ("MonExtension__exportation_produits" !== $_GET["action"])
) {
return;
}
// ... préparation du contenu
$contenu_fichier = "données csv";
// téléchargement
$nom_fichier = "export.csv";
$taille_fichier = strlen($contenu_fichier);
$nom_fichier = str_replace('"', '\\"', $nom_fichier);
header('Content-Type: application/octet-stream');
header("Content-Length: $taille_fichier");
header("Content-Disposition: attachment; filename=\"$nom_fichier\"");
echo $contenu_fichier;
exit();
});
add_action("admin_init", function () {
if ( !isset($_GET["action"])
|| ("MonExtension__charger_fichier_importation" !== $_GET["action"])
) {
return;
}
$contenu_fichier = file_get_contents($_FILES["fichier"]["tmp_name"]);
// ... traitement du fichier
// redirection
$url = "admin.php?page=MonExtension__import_export&message=fichier_importe";
wp_redirect($url);
exit();
}); |