-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportTemplates.php
More file actions
94 lines (80 loc) · 3.47 KB
/
Copy pathImportTemplates.php
File metadata and controls
94 lines (80 loc) · 3.47 KB
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
<?php
declare(strict_types=1);
namespace ACA\Examples\Bookings\Service;
use AC;
use AC\Type\ListScreenStatus;
use ACP\Import\ImportOptions;
use ACP\Import\ListScreenImporter;
use RuntimeException;
use SplFileInfo;
/**
* Imports the bundled column templates (the *.json files in /data) as real,
* saved Admin Columns views on first run, so the Hotel Bookings screen is fully
* configured out of the box — no manual "load template" step.
*
* This is the programmatic equivalent of clicking "Import" in the template
* picker. It delegates to ACP's public import API (ACP\Import\ListScreenImporter),
* which reads the *.json files in /data and, for each, duplicates the view so it
* gets a fresh id (and segment/rule keys) and saves the copy to writable storage
* (the wp_admin_columns table). The active status overwrite makes the views
* visible immediately.
*
* Runs once, guarded by an option flag, so it does not re-import on every
* request — a user can freely edit or delete the imported views afterwards
* without them reappearing.
*/
class ImportTemplates
{
private const IMPORTED_OPTION = 'aca_examples_bookings_templates_imported';
private SplFileInfo $dir;
public function __construct(SplFileInfo $dir)
{
$this->dir = $dir;
}
public function register(): void
{
// acp/init (after_setup_theme:2) hands us the same DI container ACP uses
// to build its own import handler. We can't import here, though — it
// runs before the admin environment is ready. So we grab the container
// and defer the actual work to admin_init.
add_action('acp/init', [$this, 'configure']);
}
public function configure(AC\DI\Container $container): void
{
// Run on admin_init, not init: decoding a data-source view builds its
// list table (a WP_List_Table subclass), which needs the admin screen
// environment (WP_List_Table, convert_to_screen(), the current screen).
// That isn't loaded during `init`, but is by admin_init — the same
// context ACP's own AJAX import handler runs in.
add_action('admin_init', function () use ($container): void {
$this->import($container);
});
}
private function import(AC\DI\Container $container): void
{
if (get_option(self::IMPORTED_OPTION)) {
return;
}
$importer = $container->get(ListScreenImporter::class);
try {
$imported = $importer->from_directory(
(string)$this->dir->getRealPath(),
ImportOptions::create()->with_status(ListScreenStatus::create_active())
);
} catch (RuntimeException $e) {
// Unreadable path or a malformed file: leave the flag unset so a
// later request retries, rather than fataling admin_init.
return;
}
// Only mark done once at least one template imported. A template can
// only be decoded once its data source resolves, which needs the demo
// tables (wp_hbk_*) to exist — before "Create & populate sample tables"
// has run, the importer skips it (has_list_screen() returns false) and
// the returned collection is empty. Leaving the flag unset lets a later
// request (e.g. the redirect right after the tables are created) retry
// and succeed, with no user action.
if ($imported->count() > 0) {
update_option(self::IMPORTED_OPTION, true, false);
}
}
}