Completed
Pull Request — master (#11971)
by Aristeides
07:37
created

WC_Report_Stock::column_default()   D

Complexity

Conditions 16
Paths 36

Size

Total Lines 81
Code Lines 53

Duplication

Lines 5
Ratio 6.17 %

Importance

Changes 0
Metric Value
cc 16
eloc 53
nc 36
nop 2
dl 5
loc 81
rs 4.9906
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit; // Exit if accessed directly
5
}
6
7
if ( ! class_exists( 'WP_List_Table' ) ) {
8
	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
9
}
10
11
/**
12
 * WC_Report_Stock.
13
 *
14
 * @author      WooThemes
15
 * @category    Admin
16
 * @package     WooCommerce/Admin/Reports
17
 * @version     2.1.0
18
 */
19
class WC_Report_Stock extends WP_List_Table {
20
21
	/**
22
	 * Max items.
23
	 *
24
	 * @var int
25
	 */
26
	protected $max_items;
27
28
	/**
29
	 * Constructor.
30
	 */
31
	public function __construct() {
32
33
		parent::__construct( array(
34
			'singular'  => __( 'Stock', 'woocommerce' ),
35
			'plural'    => __( 'Stock', 'woocommerce' ),
36
			'ajax'      => false,
37
		) );
38
	}
39
40
	/**
41
	 * No items found text.
42
	 */
43
	public function no_items() {
44
		_e( 'No products found.', 'woocommerce' );
45
	}
46
47
	/**
48
	 * Don't need this.
49
	 *
50
	 * @param string $position
51
	 */
52
	public function display_tablenav( $position ) {
53
54
		if ( 'top' !== $position ) {
55
			parent::display_tablenav( $position );
56
		}
57
	}
58
59
	/**
60
	 * Output the report.
61
	 */
62
	public function output_report() {
63
64
		$this->prepare_items();
65
		echo '<div id="poststuff" class="woocommerce-reports-wide">';
66
		$this->display();
67
		echo '</div>';
68
	}
69
70
	/**
71
	 * Get column value.
72
	 *
73
	 * @param mixed $item
74
	 * @param string $column_name
75
	 */
76
	public function column_default( $item, $column_name ) {
77
		global $product;
78
79
		if ( ! $product || $product->id !== $item->id ) {
80
			$product = wc_get_product( $item->id );
81
		}
82
83
		switch ( $column_name ) {
84
85
			case 'product' :
86
				if ( $sku = $product->get_sku() ) {
87
					echo $sku . ' - ';
88
				}
89
90
				echo $product->get_title();
91
92
				// Get variation data
93
				if ( $product->is_type( 'variation' ) ) {
94
					$list_attributes = array();
95
					$attributes = $product->get_variation_attributes();
96
97
					foreach ( $attributes as $name => $attribute ) {
98
						$list_attributes[] = wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ': <strong>' . $attribute . '</strong>';
99
					}
100
101
					echo '<div class="description">' . implode( ', ', $list_attributes ) . '</div>';
102
				}
103
				break;
104
105
			case 'parent' :
106
				if ( $item->parent ) {
107
					echo get_the_title( $item->parent );
108
				} else {
109
					echo '-';
110
				}
111
				break;
112
113
			case 'stock_status' :
114 View Code Duplication
				if ( $product->is_in_stock() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
					$stock_html = '<mark class="instock">' . __( 'In stock', 'woocommerce' ) . '</mark>';
116
				} else {
117
					$stock_html = '<mark class="outofstock">' . __( 'Out of stock', 'woocommerce' ) . '</mark>';
118
				}
119
				echo apply_filters( 'woocommerce_admin_stock_html', $stock_html, $product );
120
				break;
121
122
			case 'stock_level' :
123
				echo $product->get_stock_quantity();
124
				break;
125
126
			case 'wc_actions' : ?>
127
				<p>
128
					<?php
129
					$actions = array();
130
					$action_id = $product->is_type( 'variation' ) ? $item->parent : $item->id;
131
132
					$actions['edit'] = array(
133
						'url'       => admin_url( 'post.php?post=' . $action_id . '&action=edit' ),
134
						'name'      => __( 'Edit', 'woocommerce' ),
135
						'action'    => "edit",
136
					);
137
138
					if ( $product->is_visible() ) {
139
						$actions['view'] = array(
140
							'url'       => get_permalink( $action_id ),
141
							'name'      => __( 'View', 'woocommerce' ),
142
							'action'    => "view",
143
						);
144
					}
145
146
					$actions = apply_filters( 'woocommerce_admin_stock_report_product_actions', $actions, $product );
147
148
					foreach ( $actions as $action ) {
149
						printf( '<a class="button tips %s" href="%s" data-tip="%s ' . __( 'product', 'woocommerce' ) . '">%s</a>', $action['action'], esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
150
					}
151
					?>
152
				</p>
153
				<?php
154
				break;
155
		}
156
	}
157
158
	/**
159
	 * Get columns.
160
	 *
161
	 * @return array
162
	 */
163
	public function get_columns() {
164
165
		$columns = array(
166
			'product'      => __( 'Product', 'woocommerce' ),
167
			'parent'       => __( 'Parent', 'woocommerce' ),
168
			'stock_level'  => __( 'Units in stock', 'woocommerce' ),
169
			'stock_status' => __( 'Stock status', 'woocommerce' ),
170
			'wc_actions'   => __( 'Actions', 'woocommerce' ),
171
		);
172
173
		return $columns;
174
	}
175
176
	/**
177
	 * Prepare customer list items.
178
	 */
179
	public function prepare_items() {
180
181
		$this->_column_headers = array( $this->get_columns(), array(), $this->get_sortable_columns() );
182
		$current_page          = absint( $this->get_pagenum() );
183
		$per_page              = apply_filters( 'woocommerce_admin_stock_report_products_per_page', 20 );
184
185
		$this->get_items( $current_page, $per_page );
186
187
		/**
188
		 * Pagination.
189
		 */
190
		$this->set_pagination_args( array(
191
			'total_items' => $this->max_items,
192
			'per_page'    => $per_page,
193
			'total_pages' => ceil( $this->max_items / $per_page ),
194
		) );
195
	}
196
}
197