Completed
Push — master ( 09840a...ec6da7 )
by Justin
05:26
created

WPSC_Digital_Contents_Table   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 15
lcom 2
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 7 2
B fetch_items() 0 62 5
A __construct() 0 8 1
A column_product() 0 14 2
B column_contents() 0 27 5
1
<?php
2
3
require_once( WPSC_TE_V2_CLASSES_PATH . '/table.php' );
4
5
class WPSC_Digital_Contents_Table extends WPSC_Table {
6
7
	public $per_page    = 10;
8
	public $offset      = 0;
9
	public $total_items = 0;
10
	private $digital_items;
11
	private static $instance;
12
13
	public static function get_instance() {
14
		if ( empty( self::$instance ) ) {
15
			self::$instance = new WPSC_Digital_Contents_Table();
16
		}
17
18
		return self::$instance;
19
	}
20
21
	public function fetch_items() {
22
		global $wpdb;
23
24
		$vars = array(
25
			WPSC_Purchase_Log::ACCEPTED_PAYMENT,
26
			WPSC_Purchase_Log::JOB_DISPATCHED,
27
			WPSC_Purchase_Log::CLOSED_ORDER,
28
			get_current_user_id(),
29
		);
30
31
		$sql = $wpdb->prepare( "
32
			SELECT
33
				d.*
34
			FROM " . WPSC_TABLE_DOWNLOAD_STATUS . " AS d
35
			INNER JOIN " . WPSC_TABLE_PURCHASE_LOGS . " AS p
36
			ON
37
				d.purchid = p.id
38
			WHERE
39
				d.active = 1 AND
40
				p.processed IN (%d, %d, %d) AND
41
				p.user_ID = %d
42
			ORDER BY p.id DESC
43
		", $vars );
44
45
		$downloadables = $wpdb->get_results( $sql );
46
47
		if ( empty( $downloadables ) ) {
48
			$this->digital_items = array();
49
			return;
50
		}
51
		
52
		$product_ids = wp_list_pluck( $downloadables, 'product_id' );
53
		$product_ids = array_unique( array_map( 'absint', $product_ids ) );
54
		$this->items = get_posts( array(
55
			'post_type' => 'wpsc-product',
56
			'post__in'  => $product_ids )
57
		);
58
		$this->total_items = count( $this->items );
59
60
		$this->digital_items = array();
61
62
		foreach ( $downloadables as $file ) {
63
			if ( ! in_array( $file->product_id, $product_ids ) ) {
64
				continue;
65
			}
66
67
			if ( ! array_key_exists( $file->product_id, $this->digital_items ) ) {
68
				$this->digital_items[ $file->product_id ] = array();
69
			}
70
71
			$this->digital_items[ $file->product_id ][] = $file;
72
		}
73
74
		// cache files
75
		$files = wp_list_pluck( $downloadables, 'fileid' );
76
77
		get_posts( array(
78
			'post_type' => 'wpsc-product-file',
79
			'post__in'  => $files,
80
		) );
81
82
	}
83
84
	public function __construct() {
85
		parent::__construct();
86
87
		$this->columns = array(
88
			'product'  => _x( 'Product', 'customer account - digital contents - table header', 'wp-e-commerce' ),
89
			'contents' => _x( 'Digital Contents', 'customer account - digital contents - table header', 'wp-e-commerce' ),
90
		);
91
	}
92
93
	public function column_product( $item ) {
94
?>
95
	<div class="wpsc-digital-product-title">
96
		<strong><a href="<?php wpsc_product_permalink( $item->ID ); ?>"><?php wpsc_product_title( '', '', $item->ID ); ?></a></strong>
97
	</div>
98
	<div class="wpsc-thumbnail wpsc-product-thumbnail">
99
		<?php if ( wpsc_has_product_thumbnail( $item->ID ) ): ?>
100
			<?php echo wpsc_get_product_thumbnail( $item->ID, 'cart' ); ?>
101
		<?php else: ?>
102
			<?php wpsc_product_no_thumbnail_image( 'cart' ); ?>
103
		<?php endif; ?>
104
	</div>
105
<?php
106
	}
107
108
	public function column_contents( $item ) {
109
110
		if ( empty( $this->digital_items ) ) {
111
			return;
112
		}
113
114
		echo '<div class="wpsc-digital-product-items">';
115
		echo '<ul>';
116
		foreach ( $this->digital_items[ $item->ID ] as $file ) {
117
			echo '<li>';
118
			$post = get_post( $file->fileid );
119
			if ( ! $post ) {
120
				echo '<em class="deleted">' . sprintf( __( 'File ID #%s has been removed.', 'wp-e-commerce' ), $file->id ) . '</em>';
121
				continue;
122
			}
123
124
			$file_name  = get_the_title( $file->fileid );
125
			$downloadid = empty( $file->uniqueid ) ? $file->id : $file->uniqueid;
126
			$url = add_query_arg( 'downloadid', $downloadid, home_url() );
127
128
			echo '<a href="' . esc_url( $url ) . '"">' . $file_name . '</a>';
129
130
			echo '</li>';
131
		}
132
		echo '</ul>';
133
		echo '</div>';
134
	}
135
}
136