Export.php
1.07 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
<?php
declare( strict_types=1 );
namespace ACA\MLA\Service;
use AC;
use AC\Registerable;
use ACA\MLA\Export\Strategy;
use ACP;
class Export implements Registerable {
public function register() {
add_action( 'ac/export/headers', [ $this, 'fix_excel_issue' ], 10, 2 );
add_filter( 'ac/export/value', [ $this, 'strip_tags_value' ], 10, 3 );
}
public function strip_tags_value( $value, AC\Column $column ) {
if ( $column->get_group() === ColumnGroup::NAME ) {
$value = strip_tags( (string) $value );
}
return $value;
}
/**
* Error 'SYLK: File format is not valid' in Excel
* MS Excel 2003 and 2013 does not allow the first label to start with 'ID'
*/
public function fix_excel_issue( array $headers, ACP\Export\Strategy $strategy ) {
if ( ! $strategy instanceof Strategy ) {
return $headers;
}
foreach ( $headers as $name => $label ) {
$first = substr( $label, 0, 2 );
$end = substr( $label, 2 );
// Rename label 'ID' to 'id'
if ( 'ID' === $first ) {
$headers[ $name ] = strtolower( $first ) . $end;
}
break;
}
return $headers;
}
}