Downloads.php
1.83 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
<?php
namespace ACA\WC\Column\Order;
use AC;
use ACP;
use WC_DateTime;
class Downloads extends AC\Column implements ACP\Export\Exportable, ACP\ConditionalFormat\Formattable {
use ACP\ConditionalFormat\FilteredHtmlFormatTrait;
public function __construct() {
$this->set_type( 'column-order_downloads' )
->set_label( __( 'Downloads', 'codepress-admin-columns' ) )
->set_group( 'woocommerce' );
}
public function get_value( $id ) {
$order = wc_get_order( $id );
$downloads = $order ? $order->get_downloadable_items() : [];
if ( empty( $downloads ) ) {
return $this->get_empty_char();
}
$values = [];
foreach ( $downloads as $download ) {
$product = wc_get_product( $download['product_id'] );
$download_url = $product->get_file_download_path( $download['download_id'] );
$label = ac_helper()->html->link( $download_url, $download['download_name'] ?: $download['product_name'] );
$values[] = ac_helper()->html->tooltip( $label, $this->get_description( $download ) );
}
return implode( ', ', $values );
}
private function get_description( $download ) {
$product = wc_get_product( $download['product_id'] );
$description = [
wc_get_filename_from_url( $product->get_file_download_path( $download['download_id'] ) ),
];
if ( ! empty( $download['downloads_remaining'] ) ) {
$description[] = __( 'Downloads remaining', 'woocommerce' ) . ': ' . $download['downloads_remaining'];
}
if ( ! empty( $download['access_expires'] ) ) {
/* @var WC_DateTime $date */
$date = $download['access_expires'];
if ( $date->getTimestamp() > time() ) {
$description[] = __( 'Access expires', 'woocommerce' ) . ': ' . human_time_diff( $date->getTimestamp() );
}
}
return implode( '<br/>', $description );
}
public function export() {
return new ACP\Export\Model\StrippedValue( $this );
}
}