| 1 |
<?php |
| 2 |
|
| 3 |
function ctrbIsPremium() |
| 4 |
{ |
| 5 |
return CTRB_HAS_PRO ? cb_fs()->can_use_premium_code() : false; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Check if WooCommerce HPOS (High-Performance Order Storage) is enabled. |
| 10 |
* |
| 11 |
* @return bool |
| 12 |
*/ |
| 13 |
function ctrbIsHposEnabled() |
| 14 |
{ |
| 15 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && |
| 20 |
method_exists( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) ) { |
| 21 |
return \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); |
| 22 |
} |
| 23 |
|
| 24 |
return false; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Get total WooCommerce sales. |
| 29 |
* |
| 30 |
* @return float |
| 31 |
*/ |
| 32 |
function ctrbGetWcSales() |
| 33 |
{ |
| 34 |
global $wpdb; |
| 35 |
$value = 0; |
| 36 |
|
| 37 |
if ( ctrbIsHposEnabled() ) { |
| 38 |
$table_name = $wpdb->prefix . 'wc_orders'; |
| 39 |
$value = $wpdb->get_var( "SELECT SUM(total_amount) FROM $table_name WHERE status IN ('wc-completed', 'wc-processing') AND type = 'shop_order'" ); |
| 40 |
} else { |
| 41 |
$value = $wpdb->get_var( "SELECT SUM(pm.meta_value) FROM $wpdb->postmeta pm JOIN $wpdb->posts p ON pm.post_id = p.ID WHERE pm.meta_key = '_order_total' AND p.post_type = 'shop_order' AND p.post_status IN ('wc-completed', 'wc-processing')" ); |
| 42 |
} |
| 43 |
|
| 44 |
return max( 0, (float) $value ); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
/** |
| 49 |
* Get WooCommerce orders count. |
| 50 |
* |
| 51 |
* @return int |
| 52 |
*/ |
| 53 |
function ctrbGetWcOrdersCount() |
| 54 |
{ |
| 55 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
return wc_orders_count( 'completed' ) + wc_orders_count( 'processing' ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get WooCommerce products count. |
| 64 |
* |
| 65 |
* @return int |
| 66 |
*/ |
| 67 |
function ctrbGetWcProductsCount() |
| 68 |
{ |
| 69 |
$counts = wp_count_posts( 'product' ); |
| 70 |
return isset( $counts->publish ) ? (int) $counts->publish : 0; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get WooCommerce customers count. |
| 75 |
* |
| 76 |
* @return int |
| 77 |
*/ |
| 78 |
function ctrbGetWcCustomersCount() |
| 79 |
{ |
| 80 |
global $wpdb; |
| 81 |
|
| 82 |
if ( ! class_exists( 'WooCommerce' ) ) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
// Try to get count from WooCommerce customer lookup table (includes guests) |
| 87 |
$table_name = $wpdb->prefix . 'wc_customer_lookup'; |
| 88 |
if ( $wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) === $table_name ) { |
| 89 |
$count = $wpdb->get_var( "SELECT COUNT(customer_id) FROM $table_name" ); |
| 90 |
if ( null !== $count ) { |
| 91 |
return (int) $count; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// Fallback to only registered customers if lookup table is not available |
| 96 |
$query = new WP_User_Query( array( |
| 97 |
'role' => 'customer', |
| 98 |
'fields' => 'ID', |
| 99 |
'number' => 1 |
| 100 |
) ); |
| 101 |
|
| 102 |
return $query->get_total(); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get WordPress posts count. |
| 107 |
* |
| 108 |
* @return int |
| 109 |
*/ |
| 110 |
function ctrbGetWpPostsCount() |
| 111 |
{ |
| 112 |
$counts = wp_count_posts(); |
| 113 |
return isset( $counts->publish ) ? (int) $counts->publish : 0; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get WordPress pages count. |
| 118 |
* |
| 119 |
* @return int |
| 120 |
*/ |
| 121 |
function ctrbGetWpPagesCount() |
| 122 |
{ |
| 123 |
$counts = wp_count_posts( 'page' ); |
| 124 |
return isset( $counts->publish ) ? (int) $counts->publish : 0; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get WordPress comments count. |
| 129 |
* |
| 130 |
* @return int |
| 131 |
*/ |
| 132 |
function ctrbGetWpCommentsCount() |
| 133 |
{ |
| 134 |
$counts = wp_count_comments(); |
| 135 |
return isset( $counts->approved ) ? (int) $counts->approved : 0; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Get WordPress users count. |
| 140 |
* |
| 141 |
* @return int |
| 142 |
*/ |
| 143 |
function ctrbGetWpUsersCount() |
| 144 |
{ |
| 145 |
$user_counts = count_users(); |
| 146 |
return isset( $user_counts['total_users'] ) ? (int) $user_counts['total_users'] : 0; |
| 147 |
} |
| 148 |
|