Skip to content

Commit

Permalink
load wp_count_posts once and check it before accessing values
Browse files Browse the repository at this point in the history
  • Loading branch information
circlecube committed Mar 28, 2024
1 parent f1389ab commit 805734b
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions includes/Listeners/Commerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ public function products_count( $data ) {
if ( ! isset( $data['meta'] ) ) {
$data['meta'] = array();
}
if ( isset( wp_count_posts( 'product' )->publish ) ) {
$data['meta']['products_count'] = (int) wp_count_posts( 'product' )->publish;
$product_post_counts = wp_count_posts( 'product' );
if ( $product_post_counts && isset( $product_post_counts->publish ) ) {
$data['meta']['products_count'] = (int) $product_post_counts->publish;
}

return $data;
Expand All @@ -78,8 +79,9 @@ public function orders_count( $data ) {
if ( ! isset( $data['meta'] ) ) {
$data['meta'] = array();
}
if ( isset( wp_count_posts( 'shop_order' )->publish ) ) {
$data['meta']['orders_count'] = (int) wp_count_posts( 'shop_order' )->publish;
$shop_order_post_counts = wp_count_posts( 'shop_order' );
if ( $shop_order_post_counts && isset( $shop_order_post_counts->publish ) ) {
$data['meta']['orders_count'] = (int) $shop_order_post_counts->publish;
}

return $data;
Expand Down

0 comments on commit 805734b

Please sign in to comment.