-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathac-export-data.php
More file actions
127 lines (105 loc) · 4.92 KB
/
ac-export-data.php
File metadata and controls
127 lines (105 loc) · 4.92 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Hook: ac/export/data
* Fires after the export data has been collected, before the CSV is generated.
* Use this hook to add custom columns, modify cell values, or change column headers.
*
* @param ACP\Export\Exporter\TableData $data The export data object. Use add_header() and add_cell() to overwrite the output.
* @param AC\Type\ValueCollection $rows Collection of row identifiers (e.g. post IDs) included in the export.
* @param AC\ColumnIterator $columns Iterator over the columns being exported.
* @param AC\TableScreen $table_screen The current table screen context (e.g. posts, users, media).
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Add your logic here
}, 10, 4);
/**
* Example: Add a custom column with values from post meta
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Add a custom column to the table and populate its values
$column_id = 'custom_column';
// Add the column header to the table
$data->add_header($column_id, 'Custom column');
foreach ($rows as $row) {
$value = get_post_meta($row->get_id(), 'my_custom_field', true) ?: 'empty value';
// Add the cell value to the table for the current row and column
$data->add_cell($row->get_id(), $column_id, $value);
}
}, 10, 4);
/**
* Example: Overwrite a column header label
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Overwrite a column label by its ID (replaces the existing label)
$data->add_header('2838234c19db22', 'My Column Label');
}, 10, 4);
/**
* Example: Remove a specific column from the export
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Remove a column by its ID (removes both the header and all cell values)
$data->remove_column('2838234c19db22');
}, 10, 4);
/**
* Example: Modify cell values in an existing column
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
$column_id = '2838234c19db22';
if ( ! $data->has_column($column_id)) {
return;
}
// Format all date values in a column to a different format
foreach ($data->get_column($column_id) as $row_id => $value) {
$timestamp = strtotime($value);
if ($timestamp) {
$data->add_cell((string)$row_id, $column_id, date('d/m/Y', $timestamp));
}
}
}, 10, 4);
/**
* Example: Remove rows based on a condition
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Exclude draft posts from the export
foreach ($rows as $row) {
if (get_post_status($row->get_id()) === 'draft') {
$data->remove_row($row->get_id());
}
}
}, 10, 4);
/**
* Example: Only modify export data on a specific list table
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
// Only apply to the 'product' post type
if ($table_screen->get_key() !== 'product') {
return;
}
$data->add_header('sku', 'SKU');
foreach ($rows as $row) {
$product = wc_get_product($row->get_id());
if ($product) {
$data->add_cell($row->get_id(), 'sku', $product->get_sku());
}
}
}, 10, 4);
/**
* Example: Combine two columns into one
*/
add_action('ac/export/data', static function (ACP\Export\Exporter\TableData $data, AC\Type\ValueCollection $rows, AC\ColumnIterator $columns, AC\TableScreen $table_screen) {
$first_name_column_id = '1a2ef234b3c';
$last_name_column_id = '4da3bc15e6f';
if ( ! $data->has_column($first_name_column_id) || ! $data->has_column($last_name_column_id)) {
return;
}
// Create a new "Full Name" column from first and last name
$data->add_header('full_name', 'Full Name');
foreach ($data->get_rows() as $row_id => $row) {
$first_name = $data->get_cell((string)$row_id, $first_name_column_id) ?? '';
$last_name = $data->get_cell((string)$row_id, $last_name_column_id) ?? '';
$data->add_cell((string)$row_id, 'full_name', trim($first_name . ' ' . $last_name));
}
// Remove the original columns
$data->remove_column($first_name_column_id);
$data->remove_column($last_name_column_id);
}, 10, 4);