Conditions | 8 |
Paths | 40 |
Total Lines | 119 |
Code Lines | 81 |
Lines | 27 |
Ratio | 22.69 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
46 | public function status_widget() { |
||
47 | global $wpdb; |
||
48 | |||
49 | include_once( 'reports/class-wc-admin-report.php' ); |
||
50 | include_once( 'reports/class-wc-report-sales-by-date.php' ); |
||
51 | |||
52 | $reports = new WC_Admin_Report(); |
||
53 | $sales_by_date = new WC_Report_Sales_By_Date(); |
||
54 | $sales_by_date->start_date = strtotime( date( 'Y-m-01', current_time( 'timestamp' ) ) ); |
||
|
|||
55 | $sales_by_date->end_date = current_time( 'timestamp' ); |
||
56 | $sales_by_date->chart_groupby = 'day'; |
||
57 | $sales_by_date->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; |
||
58 | $report_data = $sales_by_date->get_report_data(); |
||
59 | |||
60 | // Get top seller |
||
61 | $query = array(); |
||
62 | $query['fields'] = "SELECT SUM( order_item_meta.meta_value ) as qty, order_item_meta_2.meta_value as product_id |
||
63 | FROM {$wpdb->posts} as posts"; |
||
64 | $query['join'] = "INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id "; |
||
65 | $query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id "; |
||
66 | $query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id "; |
||
67 | $query['where'] = "WHERE posts.post_type IN ( '" . implode( "','", wc_get_order_types( 'order-count' ) ) . "' ) "; |
||
68 | $query['where'] .= "AND posts.post_status IN ( 'wc-" . implode( "','wc-", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "' ) "; |
||
69 | $query['where'] .= "AND order_item_meta.meta_key = '_qty' "; |
||
70 | $query['where'] .= "AND order_item_meta_2.meta_key = '_product_id' "; |
||
71 | $query['where'] .= "AND posts.post_date >= '" . date( 'Y-m-01', current_time( 'timestamp' ) ) . "' "; |
||
72 | $query['where'] .= "AND posts.post_date <= '" . date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) . "' "; |
||
73 | $query['groupby'] = "GROUP BY product_id"; |
||
74 | $query['orderby'] = "ORDER BY qty DESC"; |
||
75 | $query['limits'] = "LIMIT 1"; |
||
76 | |||
77 | $top_seller = $wpdb->get_row( implode( ' ', apply_filters( 'woocommerce_dashboard_status_widget_top_seller_query', $query ) ) ); |
||
78 | |||
79 | // Counts |
||
80 | $on_hold_count = 0; |
||
81 | $processing_count = 0; |
||
82 | |||
83 | foreach ( wc_get_order_types( 'order-count' ) as $type ) { |
||
84 | $counts = (array) wp_count_posts( $type ); |
||
85 | $on_hold_count += isset( $counts['wc-on-hold'] ) ? $counts['wc-on-hold'] : 0; |
||
86 | $processing_count += isset( $counts['wc-processing'] ) ? $counts['wc-processing'] : 0; |
||
87 | } |
||
88 | |||
89 | // Get products using a query - this is too advanced for get_posts :( |
||
90 | $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); |
||
91 | $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); |
||
92 | $transient_name = 'wc_low_stock_count'; |
||
93 | |||
94 | View Code Duplication | if ( false === ( $lowinstock_count = get_transient( $transient_name ) ) ) { |
|
95 | $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts |
||
96 | INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id |
||
97 | INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id |
||
98 | WHERE 1=1 |
||
99 | AND posts.post_type IN ( 'product', 'product_variation' ) |
||
100 | AND posts.post_status = 'publish' |
||
101 | AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' |
||
102 | AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' |
||
103 | AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' |
||
104 | " ); |
||
105 | $lowinstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); |
||
106 | set_transient( $transient_name, $lowinstock_count, DAY_IN_SECONDS * 30 ); |
||
107 | } |
||
108 | |||
109 | $transient_name = 'wc_outofstock_count'; |
||
110 | |||
111 | View Code Duplication | if ( false === ( $outofstock_count = get_transient( $transient_name ) ) ) { |
|
112 | $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts |
||
113 | INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id |
||
114 | INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id |
||
115 | WHERE 1=1 |
||
116 | AND posts.post_type IN ( 'product', 'product_variation' ) |
||
117 | AND posts.post_status = 'publish' |
||
118 | AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' |
||
119 | AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}' |
||
120 | " ); |
||
121 | $outofstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); |
||
122 | set_transient( $transient_name, $outofstock_count, DAY_IN_SECONDS * 30 ); |
||
123 | } |
||
124 | ?> |
||
125 | <ul class="wc_status_list"> |
||
126 | <li class="sales-this-month"> |
||
127 | <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=orders&range=month' ); ?>"> |
||
128 | <?php echo $reports->sales_sparkline( '', max( 7, date( 'd', current_time( 'timestamp' ) ) ) ); ?> |
||
129 | <?php printf( __( "<strong>%s</strong> net sales this month", 'woocommerce' ), wc_price( $report_data->net_sales ) ); ?> |
||
130 | </a> |
||
131 | </li> |
||
132 | <?php if ( $top_seller && $top_seller->qty ) : ?> |
||
133 | <li class="best-seller-this-month"> |
||
134 | <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=orders&report=sales_by_product&range=month&product_ids=' . $top_seller->product_id ); ?>"> |
||
135 | <?php echo $reports->sales_sparkline( $top_seller->product_id, max( 7, date( 'd', current_time( 'timestamp' ) ) ), 'count' ); ?> |
||
136 | <?php printf( __( "%s top seller this month (sold %d)", 'woocommerce' ), "<strong>" . get_the_title( $top_seller->product_id ) . "</strong>", $top_seller->qty ); ?> |
||
137 | </a> |
||
138 | </li> |
||
139 | <?php endif; ?> |
||
140 | <li class="processing-orders"> |
||
141 | <a href="<?php echo admin_url( 'edit.php?post_status=wc-processing&post_type=shop_order' ); ?>"> |
||
142 | <?php printf( _n( "<strong>%s order</strong> awaiting processing", "<strong>%s orders</strong> awaiting processing", $processing_count, 'woocommerce' ), $processing_count ); ?> |
||
143 | </a> |
||
144 | </li> |
||
145 | <li class="on-hold-orders"> |
||
146 | <a href="<?php echo admin_url( 'edit.php?post_status=wc-on-hold&post_type=shop_order' ); ?>"> |
||
147 | <?php printf( _n( "<strong>%s order</strong> on-hold", "<strong>%s orders</strong> on-hold", $on_hold_count, 'woocommerce' ), $on_hold_count ); ?> |
||
148 | </a> |
||
149 | </li> |
||
150 | <li class="low-in-stock"> |
||
151 | <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=low_in_stock' ); ?>"> |
||
152 | <?php printf( _n( "<strong>%s product</strong> low in stock", "<strong>%s products</strong> low in stock", $lowinstock_count, 'woocommerce' ), $lowinstock_count ); ?> |
||
153 | </a> |
||
154 | </li> |
||
155 | <li class="out-of-stock"> |
||
156 | <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=out_of_stock' ); ?>"> |
||
157 | <?php printf( _n( "<strong>%s product</strong> out of stock", "<strong>%s products</strong> out of stock", $outofstock_count, 'woocommerce' ), $outofstock_count ); ?> |
||
158 | </a> |
||
159 | </li> |
||
160 | |||
161 | <?php do_action( 'woocommerce_after_dashboard_status_widget', $reports ); ?> |
||
162 | </ul> |
||
163 | <?php |
||
164 | } |
||
165 | |||
209 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.