WC_Admin_Dashboard

WC_Admin_Dashboard Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-dashboard.php  
  1. class WC_Admin_Dashboard { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. */ 
  6. public function __construct() { 
  7. // Only hook in admin parts if the user has admin access 
  8. if ( current_user_can( 'view_woocommerce_reports' ) || current_user_can( 'manage_woocommerce' ) || current_user_can( 'publish_shop_orders' ) ) { 
  9. add_action( 'wp_dashboard_setup', array( $this, 'init' ) ); 
  10.  
  11. /** 
  12. * Init dashboard widgets. 
  13. */ 
  14. public function init() { 
  15. if ( current_user_can( 'publish_shop_orders' ) ) { 
  16. wp_add_dashboard_widget( 'woocommerce_dashboard_recent_reviews', __( 'WooCommerce recent reviews', 'woocommerce' ), array( $this, 'recent_reviews' ) ); 
  17. wp_add_dashboard_widget( 'woocommerce_dashboard_status', __( 'WooCommerce status', 'woocommerce' ), array( $this, 'status_widget' ) ); 
  18.  
  19. /** 
  20. * Get top seller from DB. 
  21. * @return object 
  22. */ 
  23. private function get_top_seller() { 
  24. global $wpdb; 
  25.  
  26. $query = array(); 
  27. $query['fields'] = "SELECT SUM( order_item_meta.meta_value ) as qty, order_item_meta_2.meta_value as product_id 
  28. FROM {$wpdb->posts} as posts"; 
  29. $query['join'] = "INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id "; 
  30. $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 "; 
  31. $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 "; 
  32. $query['where'] = "WHERE posts.post_type IN ( '" . implode( "', '", wc_get_order_types( 'order-count' ) ) . "' ) "; 
  33. $query['where'] .= "AND posts.post_status IN ( 'wc-" . implode( "', 'wc-", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "' ) "; 
  34. $query['where'] .= "AND order_item_meta.meta_key = '_qty' "; 
  35. $query['where'] .= "AND order_item_meta_2.meta_key = '_product_id' "; 
  36. $query['where'] .= "AND posts.post_date >= '" . date( 'Y-m-01', current_time( 'timestamp' ) ) . "' "; 
  37. $query['where'] .= "AND posts.post_date <= '" . date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) . "' "; 
  38. $query['groupby'] = "GROUP BY product_id"; 
  39. $query['orderby'] = "ORDER BY qty DESC"; 
  40. $query['limits'] = "LIMIT 1"; 
  41.  
  42. return $wpdb->get_row( implode( ' ', apply_filters( 'woocommerce_dashboard_status_widget_top_seller_query', $query ) ) ); 
  43.  
  44. /** 
  45. * Get sales report data. 
  46. * @return object 
  47. */ 
  48. private function get_sales_report_data() { 
  49. include_once( dirname( __FILE__ ) . '/reports/class-wc-report-sales-by-date.php' ); 
  50.  
  51. $sales_by_date = new WC_Report_Sales_By_Date(); 
  52. $sales_by_date->start_date = strtotime( date( 'Y-m-01', current_time( 'timestamp' ) ) ); 
  53. $sales_by_date->end_date = current_time( 'timestamp' ); 
  54. $sales_by_date->chart_groupby = 'day'; 
  55. $sales_by_date->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; 
  56.  
  57. return $sales_by_date->get_report_data(); 
  58.  
  59. /** 
  60. * Show status widget. 
  61. */ 
  62. public function status_widget() { 
  63. include_once( dirname( __FILE__ ) . '/reports/class-wc-admin-report.php' ); 
  64.  
  65. $reports = new WC_Admin_Report(); 
  66.  
  67. echo '<ul class="wc_status_list">'; 
  68.  
  69. if ( current_user_can( 'view_woocommerce_reports' ) && ( $report_data = $this->get_sales_report_data() ) ) { 
  70. ?> 
  71. <li class="sales-this-month"> 
  72. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=orders&range=month' ); ?>"> 
  73. <?php echo $reports->sales_sparkline( '', max( 7, date( 'd', current_time( 'timestamp' ) ) ) ); ?> 
  74. <?php 
  75. /** translators: %s: net sales */ 
  76. printf( 
  77. __( '%s net sales this month', 'woocommerce' ),  
  78. '<strong>' . wc_price( $report_data->net_sales ) . '</strong>' 
  79. ); 
  80. ?> 
  81. </a> 
  82. </li> 
  83. <?php 
  84.  
  85. if ( current_user_can( 'view_woocommerce_reports' ) && ( $top_seller = $this->get_top_seller() ) && $top_seller->qty ) { 
  86. ?> 
  87. <li class="best-seller-this-month"> 
  88. <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 ); ?>"> 
  89. <?php echo $reports->sales_sparkline( $top_seller->product_id, max( 7, date( 'd', current_time( 'timestamp' ) ) ), 'count' ); ?> 
  90. <?php 
  91. /** translators: 1: top seller product title 2: top seller quantity */ 
  92. printf( 
  93. __( '%1$s top seller this month (sold %2$d)', 'woocommerce' ),  
  94. '<strong>' . get_the_title( $top_seller->product_id ) . '</strong>',  
  95. $top_seller->qty 
  96. ); 
  97. ?> 
  98. </a> 
  99. </li> 
  100. <?php 
  101.  
  102. $this->status_widget_order_rows(); 
  103. $this->status_widget_stock_rows(); 
  104.  
  105. do_action( 'woocommerce_after_dashboard_status_widget', $reports ); 
  106. echo '</ul>'; 
  107.  
  108. /** 
  109. * Show order data is status widget. 
  110. */ 
  111. private function status_widget_order_rows() { 
  112. if ( ! current_user_can( 'edit_shop_orders' ) ) { 
  113. return; 
  114. $on_hold_count = 0; 
  115. $processing_count = 0; 
  116.  
  117. foreach ( wc_get_order_types( 'order-count' ) as $type ) { 
  118. $counts = (array) wp_count_posts( $type ); 
  119. $on_hold_count += isset( $counts['wc-on-hold'] ) ? $counts['wc-on-hold'] : 0; 
  120. $processing_count += isset( $counts['wc-processing'] ) ? $counts['wc-processing'] : 0; 
  121. ?> 
  122. <li class="processing-orders"> 
  123. <a href="<?php echo admin_url( 'edit.php?post_status=wc-processing&post_type=shop_order' ); ?>"> 
  124. <?php 
  125. /** translators: %s: order count */ 
  126. printf( 
  127. _n( '<strong>%s order</strong> awaiting processing', '<strong>%s orders</strong> awaiting processing', $processing_count, 'woocommerce' ),  
  128. $processing_count 
  129. ); 
  130. ?> 
  131. </a> 
  132. </li> 
  133. <li class="on-hold-orders"> 
  134. <a href="<?php echo admin_url( 'edit.php?post_status=wc-on-hold&post_type=shop_order' ); ?>"> 
  135. <?php 
  136. /** translators: %s: order count */ 
  137. printf( 
  138. _n( '<strong>%s order</strong> on-hold', '<strong>%s orders</strong> on-hold', $on_hold_count, 'woocommerce' ),  
  139. $on_hold_count 
  140. ); 
  141. ?> 
  142. </a> 
  143. </li> 
  144. <?php 
  145.  
  146. /** 
  147. * Show stock data is status widget. 
  148. */ 
  149. private function status_widget_stock_rows() { 
  150. global $wpdb; 
  151.  
  152. // Get products using a query - this is too advanced for get_posts :( 
  153. $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); 
  154. $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); 
  155. $transient_name = 'wc_low_stock_count'; 
  156.  
  157. if ( false === ( $lowinstock_count = get_transient( $transient_name ) ) ) { 
  158. $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts 
  159. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id 
  160. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id 
  161. WHERE 1=1 
  162. AND posts.post_type IN ( 'product', 'product_variation' ) 
  163. AND posts.post_status = 'publish' 
  164. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' 
  165. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' 
  166. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' 
  167. " ); 
  168. $lowinstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); 
  169. set_transient( $transient_name, $lowinstock_count, DAY_IN_SECONDS * 30 ); 
  170.  
  171. $transient_name = 'wc_outofstock_count'; 
  172.  
  173. if ( false === ( $outofstock_count = get_transient( $transient_name ) ) ) { 
  174. $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts 
  175. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id 
  176. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id 
  177. WHERE 1=1 
  178. AND posts.post_type IN ( 'product', 'product_variation' ) 
  179. AND posts.post_status = 'publish' 
  180. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' 
  181. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}' 
  182. " ); 
  183. $outofstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); 
  184. set_transient( $transient_name, $outofstock_count, DAY_IN_SECONDS * 30 ); 
  185. ?> 
  186. <li class="low-in-stock"> 
  187. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=low_in_stock' ); ?>"> 
  188. <?php 
  189. /** translators: %s: order count */ 
  190. printf( 
  191. _n( '<strong>%s product</strong> low in stock', '<strong>%s products</strong> low in stock', $lowinstock_count, 'woocommerce' ),  
  192. $lowinstock_count 
  193. ); 
  194. ?> 
  195. </a> 
  196. </li> 
  197. <li class="out-of-stock"> 
  198. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=out_of_stock' ); ?>"> 
  199. <?php 
  200. /** translators: %s: order count */ 
  201. printf( 
  202. _n( '<strong>%s product</strong> out of stock', '<strong>%s products</strong> out of stock', $outofstock_count, 'woocommerce' ),  
  203. $outofstock_count 
  204. ); 
  205. ?> 
  206. </a> 
  207. </li> 
  208. <?php 
  209.  
  210. /** 
  211. * Recent reviews widget. 
  212. */ 
  213. public function recent_reviews() { 
  214. global $wpdb; 
  215. $comments = $wpdb->get_results( " 
  216. SELECT posts.ID, posts.post_title, comments.comment_author, comments.comment_ID, SUBSTRING(comments.comment_content, 1, 100) AS comment_excerpt 
  217. FROM $wpdb->comments comments 
  218. LEFT JOIN $wpdb->posts posts ON (comments.comment_post_ID = posts.ID) 
  219. WHERE comments.comment_approved = '1' 
  220. AND comments.comment_type = '' 
  221. AND posts.post_password = '' 
  222. AND posts.post_type = 'product' 
  223. ORDER BY comments.comment_date_gmt DESC 
  224. LIMIT 5 
  225. " ); 
  226.  
  227. if ( $comments ) { 
  228. echo '<ul>'; 
  229. foreach ( $comments as $comment ) { 
  230.  
  231. echo '<li>'; 
  232.  
  233. echo get_avatar( $comment->comment_author, '32' ); 
  234.  
  235. $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ); 
  236.  
  237. /** translators: %s: rating */ 
  238. echo '<div class="star-rating"><span style="width:' . ( $rating * 20 ) . '%">' . sprintf( __( '%s out of 5', 'woocommerce' ), $rating ) . '</span></div>'; 
  239.  
  240. /** translators: %s: review author */ 
  241. echo '<h4 class="meta"><a href="' . get_permalink( $comment->ID ) . '#comment-' . absint( $comment->comment_ID ) . '">' . esc_html( apply_filters( 'woocommerce_admin_dashboard_recent_reviews', $comment->post_title, $comment ) ) . '</a> ' . sprintf( __( 'reviewed by %s', 'woocommerce' ), esc_html( $comment->comment_author ) ) . '</h4>'; 
  242. echo '<blockquote>' . wp_kses_data( $comment->comment_excerpt ) . ' [...]</blockquote></li>'; 
  243.  
  244. echo '</ul>'; 
  245. } else { 
  246. echo '<p>' . __( 'There are no product reviews yet.', 'woocommerce' ) . '</p>';