-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
72 lines (61 loc) · 2.77 KB
/
example.php
File metadata and controls
72 lines (61 loc) · 2.77 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
<?php
/**
* Example: Table-to-Table Relation (has_one).
* See readme.md for the explanation.
*/
declare(strict_types=1);
use ACA\DataSources\DataSource;
use ACA\DataSources\DataSource\ColumnLabelResolver\HumanReadableResolver;
use ACA\DataSources\DataSource\ColumnType;
use ACA\DataSources\DataSource\Config;
use ACA\DataSources\DataSourceRegistry;
use ACA\DataSources\DataSourceRegistry\Entry;
use ACA\DataSources\Facade;
use ACA\DataSources\Repository\Database\Table\Filter;
use ACA\DataSources\Type\DataSourceId;
add_action('acp/data-sources/register', static function (DataSourceRegistry $registry): void {
// Step 1: Register the foreign Data Source first, without a menu page. This is
// required for its columns to be exposed via the relation below.
$post_columns = Config\Columns::create()
->with_column(ColumnType\TextType::for('post_title'))
->with_column(ColumnType\TextType::for('post_content'))
->with_label_resolver(new HumanReadableResolver());
$post_table = Facade\Table::from('wp_posts')
->filter(new Filter\Name(['ID', 'post_title', 'post_content']));
$post_data_source = new DataSource(
new DataSourceId('related_posts'),
$post_table,
$post_columns
);
$registry->register(new Entry($post_data_source));
// Step 2: Register the comment meta table as a Data Source. Using the
// `Facade\DataSource::from()` shortcut when no custom columns are needed.
$comment_meta = Facade\DataSource::from('wp_commentmeta', 'related_comment_meta');
$registry->register(new Entry($comment_meta));
// Step 3: Register the primary table (Comments) and attach the other
// Data Sources as relations. Each comment row references one post via
// `comment_post_ID` (the local foreign key to `wp_posts.ID`).
$comments = new DataSource(
new DataSourceId('comments_with_related_posts'),
Facade\Table::from('wp_comments'),
Config\Columns::create()
->with_label_resolver(new HumanReadableResolver()),
new Facade\Relations([
// Table relation: join one row from wp_posts per comment.
Facade\Relation\Table::has_one($post_data_source, 'ID', 'comment_post_ID'),
// Attribute relation: exposes a configurable column where the
// user picks which `meta_key` to display. Can be added multiple
// times to show different keys (like the Custom Field column).
Facade\Relation\Attribute::has_one(
$comment_meta,
'comment_id',
'Comment Meta',
'meta_key',
'meta_value'
),
])
);
$registry->register(
Entry::create($comments)->set_submenu('04. Comments with related Post', 'ac-ds-cookbook')
);
});