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.  
  18. wp_add_dashboard_widget( 'woocommerce_dashboard_status', __( 'WooCommerce Status', 'woocommerce' ), array( $this, 'status_widget' ) ); 
  19.  
  20. /** 
  21. * Show status widget. 
  22. */ 
  23. public function status_widget() { 
  24. global $wpdb; 
  25.  
  26. include_once( 'reports/class-wc-admin-report.php' ); 
  27. include_once( 'reports/class-wc-report-sales-by-date.php' ); 
  28.  
  29. $reports = new WC_Admin_Report(); 
  30. $sales_by_date = new WC_Report_Sales_By_Date(); 
  31. $sales_by_date->start_date = strtotime( date( 'Y-m-01', current_time( 'timestamp' ) ) ); 
  32. $sales_by_date->end_date = current_time( 'timestamp' ); 
  33. $sales_by_date->chart_groupby = 'day'; 
  34. $sales_by_date->group_by_query = 'YEAR(posts.post_date), MONTH(posts.post_date), DAY(posts.post_date)'; 
  35. $report_data = $sales_by_date->get_report_data(); 
  36.  
  37. // Get top seller 
  38. $query = array(); 
  39. $query['fields'] = "SELECT SUM( order_item_meta.meta_value ) as qty, order_item_meta_2.meta_value as product_id 
  40. FROM {$wpdb->posts} as posts"; 
  41. $query['join'] = "INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id "; 
  42. $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 "; 
  43. $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 "; 
  44. $query['where'] = "WHERE posts.post_type IN ( '" . implode( "', '", wc_get_order_types( 'order-count' ) ) . "' ) "; 
  45. $query['where'] .= "AND posts.post_status IN ( 'wc-" . implode( "', 'wc-", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "' ) "; 
  46. $query['where'] .= "AND order_item_meta.meta_key = '_qty' "; 
  47. $query['where'] .= "AND order_item_meta_2.meta_key = '_product_id' "; 
  48. $query['where'] .= "AND posts.post_date >= '" . date( 'Y-m-01', current_time( 'timestamp' ) ) . "' "; 
  49. $query['where'] .= "AND posts.post_date <= '" . date( 'Y-m-d H:i:s', current_time( 'timestamp' ) ) . "' "; 
  50. $query['groupby'] = "GROUP BY product_id"; 
  51. $query['orderby'] = "ORDER BY qty DESC"; 
  52. $query['limits'] = "LIMIT 1"; 
  53.  
  54. $top_seller = $wpdb->get_row( implode( ' ', apply_filters( 'woocommerce_dashboard_status_widget_top_seller_query', $query ) ) ); 
  55.  
  56. // Counts 
  57. $on_hold_count = 0; 
  58. $processing_count = 0; 
  59.  
  60. foreach ( wc_get_order_types( 'order-count' ) as $type ) { 
  61. $counts = (array) wp_count_posts( $type ); 
  62. $on_hold_count += isset( $counts['wc-on-hold'] ) ? $counts['wc-on-hold'] : 0; 
  63. $processing_count += isset( $counts['wc-processing'] ) ? $counts['wc-processing'] : 0; 
  64.  
  65. // Get products using a query - this is too advanced for get_posts :( 
  66. $stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); 
  67. $nostock = absint( max( get_option( 'woocommerce_notify_no_stock_amount' ), 0 ) ); 
  68. $transient_name = 'wc_low_stock_count'; 
  69.  
  70. if ( false === ( $lowinstock_count = get_transient( $transient_name ) ) ) { 
  71. $query_from = apply_filters( 'woocommerce_report_low_in_stock_query_from', "FROM {$wpdb->posts} as posts 
  72. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id 
  73. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id 
  74. WHERE 1=1 
  75. AND posts.post_type IN ( 'product', 'product_variation' ) 
  76. AND posts.post_status = 'publish' 
  77. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' 
  78. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$stock}' 
  79. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) > '{$nostock}' 
  80. " ); 
  81. $lowinstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); 
  82. set_transient( $transient_name, $lowinstock_count, DAY_IN_SECONDS * 30 ); 
  83.  
  84. $transient_name = 'wc_outofstock_count'; 
  85.  
  86. if ( false === ( $outofstock_count = get_transient( $transient_name ) ) ) { 
  87. $query_from = apply_filters( 'woocommerce_report_out_of_stock_query_from', "FROM {$wpdb->posts} as posts 
  88. INNER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id 
  89. INNER JOIN {$wpdb->postmeta} AS postmeta2 ON posts.ID = postmeta2.post_id 
  90. WHERE 1=1 
  91. AND posts.post_type IN ( 'product', 'product_variation' ) 
  92. AND posts.post_status = 'publish' 
  93. AND postmeta2.meta_key = '_manage_stock' AND postmeta2.meta_value = 'yes' 
  94. AND postmeta.meta_key = '_stock' AND CAST(postmeta.meta_value AS SIGNED) <= '{$nostock}' 
  95. " ); 
  96. $outofstock_count = absint( $wpdb->get_var( "SELECT COUNT( DISTINCT posts.ID ) {$query_from};" ) ); 
  97. set_transient( $transient_name, $outofstock_count, DAY_IN_SECONDS * 30 ); 
  98. ?> 
  99. <ul class="wc_status_list"> 
  100. <li class="sales-this-month"> 
  101. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=orders&range=month' ); ?>"> 
  102. <?php echo $reports->sales_sparkline( '', max( 7, date( 'd', current_time( 'timestamp' ) ) ) ); ?> 
  103. <?php printf( __( "<strong>%s</strong> net sales this month", 'woocommerce' ), wc_price( $report_data->net_sales ) ); ?> 
  104. </a> 
  105. </li> 
  106. <?php if ( $top_seller && $top_seller->qty ) : ?> 
  107. <li class="best-seller-this-month"> 
  108. <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 ); ?>"> 
  109. <?php echo $reports->sales_sparkline( $top_seller->product_id, max( 7, date( 'd', current_time( 'timestamp' ) ) ), 'count' ); ?> 
  110. <?php printf( __( "%s top seller this month (sold %d)", 'woocommerce' ), "<strong>" . get_the_title( $top_seller->product_id ) . "</strong>", $top_seller->qty ); ?> 
  111. </a> 
  112. </li> 
  113. <?php endif; ?> 
  114. <li class="processing-orders"> 
  115. <a href="<?php echo admin_url( 'edit.php?post_status=wc-processing&post_type=shop_order' ); ?>"> 
  116. <?php printf( _n( "<strong>%s order</strong> awaiting processing", "<strong>%s orders</strong> awaiting processing", $processing_count, 'woocommerce' ), $processing_count ); ?> 
  117. </a> 
  118. </li> 
  119. <li class="on-hold-orders"> 
  120. <a href="<?php echo admin_url( 'edit.php?post_status=wc-on-hold&post_type=shop_order' ); ?>"> 
  121. <?php printf( _n( "<strong>%s order</strong> on-hold", "<strong>%s orders</strong> on-hold", $on_hold_count, 'woocommerce' ), $on_hold_count ); ?> 
  122. </a> 
  123. </li> 
  124. <li class="low-in-stock"> 
  125. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=low_in_stock' ); ?>"> 
  126. <?php printf( _n( "<strong>%s product</strong> low in stock", "<strong>%s products</strong> low in stock", $lowinstock_count, 'woocommerce' ), $lowinstock_count ); ?> 
  127. </a> 
  128. </li> 
  129. <li class="out-of-stock"> 
  130. <a href="<?php echo admin_url( 'admin.php?page=wc-reports&tab=stock&report=out_of_stock' ); ?>"> 
  131. <?php printf( _n( "<strong>%s product</strong> out of stock", "<strong>%s products</strong> out of stock", $outofstock_count, 'woocommerce' ), $outofstock_count ); ?> 
  132. </a> 
  133. </li> 
  134.  
  135. <?php do_action( 'woocommerce_after_dashboard_status_widget', $reports ); ?> 
  136. </ul> 
  137. <?php 
  138.  
  139. /** 
  140. * Recent reviews widget. 
  141. */ 
  142. public function recent_reviews() { 
  143. global $wpdb; 
  144. $comments = $wpdb->get_results( "SELECT *, SUBSTRING(comment_content, 1, 100) AS comment_excerpt 
  145. FROM $wpdb->comments 
  146. LEFT JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) 
  147. WHERE comment_approved = '1' 
  148. AND comment_type = '' 
  149. AND post_password = '' 
  150. AND post_type = 'product' 
  151. ORDER BY comment_date_gmt DESC 
  152. LIMIT 8" ); 
  153.  
  154. if ( $comments ) { 
  155. echo '<ul>'; 
  156. foreach ( $comments as $comment ) { 
  157.  
  158. echo '<li>'; 
  159.  
  160. echo get_avatar( $comment->comment_author, '32' ); 
  161.  
  162. $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ); 
  163.  
  164. echo '<div class="star-rating" title="' . esc_attr( $rating ) . '"> 
  165. <span style="width:' . ( $rating * 20 ) . '%">' . $rating . ' ' . __( 'out of 5', 'woocommerce' ) . '</span></div>'; 
  166.  
  167. 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> ' . __( 'reviewed by', 'woocommerce' ) . ' ' . esc_html( $comment->comment_author ) .'</h4>'; 
  168. echo '<blockquote>' . wp_kses_data( $comment->comment_excerpt ) . ' [...]</blockquote></li>'; 
  169.  
  170. echo '</ul>'; 
  171. } else { 
  172. echo '<p>' . __( 'There are no product reviews yet.', 'woocommerce' ) . '</p>'; 
  173.