abstracts
5 years ago
admin
5 years ago
blocks
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
4 years ago
emails
5 years ago
export
5 years ago
gateways
4 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
6 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
rest-api
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
6 years ago
class-wc-privacy-exporters.php
6 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
class-wc-tracker.php
771 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Tracker |
| 4 | * |
| 5 | * The WooCommerce tracker class adds functionality to track WooCommerce usage based on if the customer opted in. |
| 6 | * No personal information is tracked, only general WooCommerce settings, general product, order and user counts and admin email for discount code. |
| 7 | * |
| 8 | * @class WC_Tracker |
| 9 | * @since 2.3.0 |
| 10 | * @package WooCommerce\Classes |
| 11 | */ |
| 12 | |
| 13 | use Automattic\Jetpack\Constants; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * WooCommerce Tracker Class |
| 19 | */ |
| 20 | class WC_Tracker { |
| 21 | |
| 22 | /** |
| 23 | * URL to the WooThemes Tracker API endpoint. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private static $api_url = 'https://tracking.woocommerce.com/v1/'; |
| 28 | |
| 29 | /** |
| 30 | * Hook into cron event. |
| 31 | */ |
| 32 | public static function init() { |
| 33 | add_action( 'woocommerce_tracker_send_event', array( __CLASS__, 'send_tracking_data' ) ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Decide whether to send tracking data or not. |
| 38 | * |
| 39 | * @param boolean $override Should override?. |
| 40 | */ |
| 41 | public static function send_tracking_data( $override = false ) { |
| 42 | // Don't trigger this on AJAX Requests. |
| 43 | if ( Constants::is_true( 'DOING_AJAX' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | if ( ! apply_filters( 'woocommerce_tracker_send_override', $override ) ) { |
| 48 | // Send a maximum of once per week by default. |
| 49 | $last_send = self::get_last_send_time(); |
| 50 | if ( $last_send && $last_send > apply_filters( 'woocommerce_tracker_last_send_interval', strtotime( '-1 week' ) ) ) { |
| 51 | return; |
| 52 | } |
| 53 | } else { |
| 54 | // Make sure there is at least a 1 hour delay between override sends, we don't want duplicate calls due to double clicking links. |
| 55 | $last_send = self::get_last_send_time(); |
| 56 | if ( $last_send && $last_send > strtotime( '-1 hours' ) ) { |
| 57 | return; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Update time first before sending to ensure it is set. |
| 62 | update_option( 'woocommerce_tracker_last_send', time() ); |
| 63 | |
| 64 | $params = self::get_tracking_data(); |
| 65 | wp_safe_remote_post( |
| 66 | self::$api_url, |
| 67 | array( |
| 68 | 'method' => 'POST', |
| 69 | 'timeout' => 45, |
| 70 | 'redirection' => 5, |
| 71 | 'httpversion' => '1.0', |
| 72 | 'blocking' => false, |
| 73 | 'headers' => array( 'user-agent' => 'WooCommerceTracker/' . md5( esc_url_raw( home_url( '/' ) ) ) . ';' ), |
| 74 | 'body' => wp_json_encode( $params ), |
| 75 | 'cookies' => array(), |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the last time tracking data was sent. |
| 82 | * |
| 83 | * @return int|bool |
| 84 | */ |
| 85 | private static function get_last_send_time() { |
| 86 | return apply_filters( 'woocommerce_tracker_last_send_time', get_option( 'woocommerce_tracker_last_send', false ) ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Test whether this site is a staging site according to the Jetpack criteria. |
| 91 | * |
| 92 | * With Jetpack 8.1+, Jetpack::is_staging_site has been deprecated. |
| 93 | * \Automattic\Jetpack\Status::is_staging_site is the replacement. |
| 94 | * However, there are version of JP where \Automattic\Jetpack\Status exists, but does *not* contain is_staging_site method, |
| 95 | * so with those, code still needs to use the previous check as a fallback. |
| 96 | * |
| 97 | * @return bool |
| 98 | */ |
| 99 | private static function is_jetpack_staging_site() { |
| 100 | if ( class_exists( '\Automattic\Jetpack\Status' ) ) { |
| 101 | // Preferred way of checking with Jetpack 8.1+. |
| 102 | $jp_status = new \Automattic\Jetpack\Status(); |
| 103 | if ( is_callable( array( $jp_status, 'is_staging_site' ) ) ) { |
| 104 | return $jp_status->is_staging_site(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get all the tracking data. |
| 113 | * |
| 114 | * @return array |
| 115 | */ |
| 116 | private static function get_tracking_data() { |
| 117 | $data = array(); |
| 118 | |
| 119 | // General site info. |
| 120 | $data['url'] = home_url(); |
| 121 | $data['email'] = apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) ); |
| 122 | $data['theme'] = self::get_theme_info(); |
| 123 | |
| 124 | // WordPress Info. |
| 125 | $data['wp'] = self::get_wordpress_info(); |
| 126 | |
| 127 | // Server Info. |
| 128 | $data['server'] = self::get_server_info(); |
| 129 | |
| 130 | // Plugin info. |
| 131 | $all_plugins = self::get_all_plugins(); |
| 132 | $data['active_plugins'] = $all_plugins['active_plugins']; |
| 133 | $data['inactive_plugins'] = $all_plugins['inactive_plugins']; |
| 134 | |
| 135 | // Jetpack & WooCommerce Connect. |
| 136 | |
| 137 | $data['jetpack_version'] = Constants::is_defined( 'JETPACK__VERSION' ) ? Constants::get_constant( 'JETPACK__VERSION' ) : 'none'; |
| 138 | $data['jetpack_connected'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active() ) ? 'yes' : 'no'; |
| 139 | $data['jetpack_is_staging'] = self::is_jetpack_staging_site() ? 'yes' : 'no'; |
| 140 | $data['connect_installed'] = class_exists( 'WC_Connect_Loader' ) ? 'yes' : 'no'; |
| 141 | $data['connect_active'] = ( class_exists( 'WC_Connect_Loader' ) && wp_next_scheduled( 'wc_connect_fetch_service_schemas' ) ) ? 'yes' : 'no'; |
| 142 | $data['helper_connected'] = self::get_helper_connected(); |
| 143 | |
| 144 | // Store count info. |
| 145 | $data['users'] = self::get_user_counts(); |
| 146 | $data['products'] = self::get_product_counts(); |
| 147 | $data['orders'] = self::get_orders(); |
| 148 | $data['reviews'] = self::get_review_counts(); |
| 149 | $data['categories'] = self::get_category_counts(); |
| 150 | |
| 151 | // Payment gateway info. |
| 152 | $data['gateways'] = self::get_active_payment_gateways(); |
| 153 | |
| 154 | // Shipping method info. |
| 155 | $data['shipping_methods'] = self::get_active_shipping_methods(); |
| 156 | |
| 157 | // Get all WooCommerce options info. |
| 158 | $data['settings'] = self::get_all_woocommerce_options_values(); |
| 159 | |
| 160 | // Template overrides. |
| 161 | $data['template_overrides'] = self::get_all_template_overrides(); |
| 162 | |
| 163 | // Template overrides. |
| 164 | $data['admin_user_agents'] = self::get_admin_user_agents(); |
| 165 | |
| 166 | // Cart & checkout tech (blocks or shortcodes). |
| 167 | $data['cart_checkout'] = self::get_cart_checkout_info(); |
| 168 | |
| 169 | // WooCommerce Admin info. |
| 170 | $data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no'; |
| 171 | |
| 172 | return apply_filters( 'woocommerce_tracker_data', $data ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the current theme info, theme name and version. |
| 177 | * |
| 178 | * @return array |
| 179 | */ |
| 180 | public static function get_theme_info() { |
| 181 | $theme_data = wp_get_theme(); |
| 182 | $theme_child_theme = wc_bool_to_string( is_child_theme() ); |
| 183 | $theme_wc_support = wc_bool_to_string( current_theme_supports( 'woocommerce' ) ); |
| 184 | |
| 185 | return array( |
| 186 | 'name' => $theme_data->Name, // @phpcs:ignore |
| 187 | 'version' => $theme_data->Version, // @phpcs:ignore |
| 188 | 'child_theme' => $theme_child_theme, |
| 189 | 'wc_support' => $theme_wc_support, |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Get WordPress related data. |
| 195 | * |
| 196 | * @return array |
| 197 | */ |
| 198 | private static function get_wordpress_info() { |
| 199 | $wp_data = array(); |
| 200 | |
| 201 | $memory = wc_let_to_num( WP_MEMORY_LIMIT ); |
| 202 | |
| 203 | if ( function_exists( 'memory_get_usage' ) ) { |
| 204 | $system_memory = wc_let_to_num( @ini_get( 'memory_limit' ) ); |
| 205 | $memory = max( $memory, $system_memory ); |
| 206 | } |
| 207 | |
| 208 | // WordPress 5.5+ environment type specification. |
| 209 | // 'production' is the default in WP, thus using it as a default here, too. |
| 210 | $environment_type = 'production'; |
| 211 | if ( function_exists( 'wp_get_environment_type' ) ) { |
| 212 | $environment_type = wp_get_environment_type(); |
| 213 | } |
| 214 | |
| 215 | $wp_data['memory_limit'] = size_format( $memory ); |
| 216 | $wp_data['debug_mode'] = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No'; |
| 217 | $wp_data['locale'] = get_locale(); |
| 218 | $wp_data['version'] = get_bloginfo( 'version' ); |
| 219 | $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No'; |
| 220 | $wp_data['env_type'] = $environment_type; |
| 221 | |
| 222 | return $wp_data; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get server related info. |
| 227 | * |
| 228 | * @return array |
| 229 | */ |
| 230 | private static function get_server_info() { |
| 231 | $server_data = array(); |
| 232 | |
| 233 | if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) { |
| 234 | $server_data['software'] = $_SERVER['SERVER_SOFTWARE']; // @phpcs:ignore |
| 235 | } |
| 236 | |
| 237 | if ( function_exists( 'phpversion' ) ) { |
| 238 | $server_data['php_version'] = phpversion(); |
| 239 | } |
| 240 | |
| 241 | if ( function_exists( 'ini_get' ) ) { |
| 242 | $server_data['php_post_max_size'] = size_format( wc_let_to_num( ini_get( 'post_max_size' ) ) ); |
| 243 | $server_data['php_time_limt'] = ini_get( 'max_execution_time' ); |
| 244 | $server_data['php_max_input_vars'] = ini_get( 'max_input_vars' ); |
| 245 | $server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No'; |
| 246 | } |
| 247 | |
| 248 | $database_version = wc_get_server_database_version(); |
| 249 | $server_data['mysql_version'] = $database_version['number']; |
| 250 | |
| 251 | $server_data['php_max_upload_size'] = size_format( wp_max_upload_size() ); |
| 252 | $server_data['php_default_timezone'] = date_default_timezone_get(); |
| 253 | $server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No'; |
| 254 | $server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No'; |
| 255 | $server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No'; |
| 256 | |
| 257 | return $server_data; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Get all plugins grouped into activated or not. |
| 262 | * |
| 263 | * @return array |
| 264 | */ |
| 265 | private static function get_all_plugins() { |
| 266 | // Ensure get_plugins function is loaded. |
| 267 | if ( ! function_exists( 'get_plugins' ) ) { |
| 268 | include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 269 | } |
| 270 | |
| 271 | $plugins = get_plugins(); |
| 272 | $active_plugins_keys = get_option( 'active_plugins', array() ); |
| 273 | $active_plugins = array(); |
| 274 | |
| 275 | foreach ( $plugins as $k => $v ) { |
| 276 | // Take care of formatting the data how we want it. |
| 277 | $formatted = array(); |
| 278 | $formatted['name'] = strip_tags( $v['Name'] ); |
| 279 | if ( isset( $v['Version'] ) ) { |
| 280 | $formatted['version'] = strip_tags( $v['Version'] ); |
| 281 | } |
| 282 | if ( isset( $v['Author'] ) ) { |
| 283 | $formatted['author'] = strip_tags( $v['Author'] ); |
| 284 | } |
| 285 | if ( isset( $v['Network'] ) ) { |
| 286 | $formatted['network'] = strip_tags( $v['Network'] ); |
| 287 | } |
| 288 | if ( isset( $v['PluginURI'] ) ) { |
| 289 | $formatted['plugin_uri'] = strip_tags( $v['PluginURI'] ); |
| 290 | } |
| 291 | if ( in_array( $k, $active_plugins_keys ) ) { |
| 292 | // Remove active plugins from list so we can show active and inactive separately. |
| 293 | unset( $plugins[ $k ] ); |
| 294 | $active_plugins[ $k ] = $formatted; |
| 295 | } else { |
| 296 | $plugins[ $k ] = $formatted; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return array( |
| 301 | 'active_plugins' => $active_plugins, |
| 302 | 'inactive_plugins' => $plugins, |
| 303 | ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Check to see if the helper is connected to woocommerce.com |
| 308 | * |
| 309 | * @return string |
| 310 | */ |
| 311 | private static function get_helper_connected() { |
| 312 | if ( class_exists( 'WC_Helper_Options' ) && is_callable( 'WC_Helper_Options::get' ) ) { |
| 313 | $authenticated = WC_Helper_Options::get( 'auth' ); |
| 314 | } else { |
| 315 | $authenticated = ''; |
| 316 | } |
| 317 | return ( ! empty( $authenticated ) ) ? 'yes' : 'no'; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /** |
| 322 | * Get user totals based on user role. |
| 323 | * |
| 324 | * @return array |
| 325 | */ |
| 326 | private static function get_user_counts() { |
| 327 | $user_count = array(); |
| 328 | $user_count_data = count_users(); |
| 329 | $user_count['total'] = $user_count_data['total_users']; |
| 330 | |
| 331 | // Get user count based on user role. |
| 332 | foreach ( $user_count_data['avail_roles'] as $role => $count ) { |
| 333 | $user_count[ $role ] = $count; |
| 334 | } |
| 335 | |
| 336 | return $user_count; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Get product totals based on product type. |
| 341 | * |
| 342 | * @return array |
| 343 | */ |
| 344 | public static function get_product_counts() { |
| 345 | $product_count = array(); |
| 346 | $product_count_data = wp_count_posts( 'product' ); |
| 347 | $product_count['total'] = $product_count_data->publish; |
| 348 | |
| 349 | $product_statuses = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); |
| 350 | foreach ( $product_statuses as $product_status ) { |
| 351 | $product_count[ $product_status->name ] = $product_status->count; |
| 352 | } |
| 353 | |
| 354 | return $product_count; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Get order counts. |
| 359 | * |
| 360 | * @return array |
| 361 | */ |
| 362 | private static function get_order_counts() { |
| 363 | $order_count = array(); |
| 364 | $order_count_data = wp_count_posts( 'shop_order' ); |
| 365 | foreach ( wc_get_order_statuses() as $status_slug => $status_name ) { |
| 366 | $order_count[ $status_slug ] = $order_count_data->{ $status_slug }; |
| 367 | } |
| 368 | return $order_count; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Combine all order data. |
| 373 | * |
| 374 | * @return array |
| 375 | */ |
| 376 | private static function get_orders() { |
| 377 | $order_dates = self::get_order_dates(); |
| 378 | $order_counts = self::get_order_counts(); |
| 379 | $order_totals = self::get_order_totals(); |
| 380 | $order_gateways = self::get_orders_by_gateway(); |
| 381 | |
| 382 | return array_merge( $order_dates, $order_counts, $order_totals, $order_gateways ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Get order totals. |
| 387 | * |
| 388 | * @since 5.4.0 |
| 389 | * @return array |
| 390 | */ |
| 391 | private static function get_order_totals() { |
| 392 | global $wpdb; |
| 393 | |
| 394 | $gross_total = $wpdb->get_var( |
| 395 | " |
| 396 | SELECT |
| 397 | SUM( order_meta.meta_value ) AS 'gross_total' |
| 398 | FROM {$wpdb->prefix}posts AS orders |
| 399 | LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID |
| 400 | WHERE order_meta.meta_key = '_order_total' |
| 401 | AND orders.post_status in ( 'wc-completed', 'wc-refunded' ) |
| 402 | GROUP BY order_meta.meta_key |
| 403 | " |
| 404 | ); |
| 405 | |
| 406 | if ( is_null( $gross_total ) ) { |
| 407 | $gross_total = 0; |
| 408 | } |
| 409 | |
| 410 | $processing_gross_total = $wpdb->get_var( |
| 411 | " |
| 412 | SELECT |
| 413 | SUM( order_meta.meta_value ) AS 'gross_total' |
| 414 | FROM {$wpdb->prefix}posts AS orders |
| 415 | LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID |
| 416 | WHERE order_meta.meta_key = '_order_total' |
| 417 | AND orders.post_status = 'wc-processing' |
| 418 | GROUP BY order_meta.meta_key |
| 419 | " |
| 420 | ); |
| 421 | |
| 422 | if ( is_null( $processing_gross_total ) ) { |
| 423 | $processing_gross_total = 0; |
| 424 | } |
| 425 | |
| 426 | return array( |
| 427 | 'gross' => $gross_total, |
| 428 | 'processing_gross' => $processing_gross_total, |
| 429 | ); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Get last order date. |
| 434 | * |
| 435 | * @return string |
| 436 | */ |
| 437 | private static function get_order_dates() { |
| 438 | global $wpdb; |
| 439 | |
| 440 | $min_max = $wpdb->get_row( |
| 441 | " |
| 442 | SELECT |
| 443 | MIN( post_date_gmt ) as 'first', MAX( post_date_gmt ) as 'last' |
| 444 | FROM {$wpdb->prefix}posts |
| 445 | WHERE post_type = 'shop_order' |
| 446 | AND post_status = 'wc-completed' |
| 447 | ", |
| 448 | ARRAY_A |
| 449 | ); |
| 450 | |
| 451 | if ( is_null( $min_max ) ) { |
| 452 | $min_max = array( |
| 453 | 'first' => '-', |
| 454 | 'last' => '-', |
| 455 | ); |
| 456 | } |
| 457 | |
| 458 | $processing_min_max = $wpdb->get_row( |
| 459 | " |
| 460 | SELECT |
| 461 | MIN( post_date_gmt ) as 'processing_first', MAX( post_date_gmt ) as 'processing_last' |
| 462 | FROM {$wpdb->prefix}posts |
| 463 | WHERE post_type = 'shop_order' |
| 464 | AND post_status = 'wc-processing' |
| 465 | ", |
| 466 | ARRAY_A |
| 467 | ); |
| 468 | |
| 469 | if ( is_null( $processing_min_max ) ) { |
| 470 | $processing_min_max = array( |
| 471 | 'processing_first' => '-', |
| 472 | 'processing_last' => '-', |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | return array_merge( $min_max, $processing_min_max ); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Get order details by gateway. |
| 481 | * |
| 482 | * @return array |
| 483 | */ |
| 484 | private static function get_orders_by_gateway() { |
| 485 | global $wpdb; |
| 486 | |
| 487 | $orders_by_gateway = $wpdb->get_results( |
| 488 | " |
| 489 | SELECT |
| 490 | gateway, currency, SUM(total) AS totals, COUNT(order_id) AS counts |
| 491 | FROM ( |
| 492 | SELECT |
| 493 | orders.id AS order_id, |
| 494 | MAX(CASE WHEN meta_key = '_payment_method' THEN meta_value END) gateway, |
| 495 | MAX(CASE WHEN meta_key = '_order_total' THEN meta_value END) total, |
| 496 | MAX(CASE WHEN meta_key = '_order_currency' THEN meta_value END) currency |
| 497 | FROM |
| 498 | {$wpdb->prefix}posts orders |
| 499 | LEFT JOIN |
| 500 | {$wpdb->prefix}postmeta order_meta ON order_meta.post_id = orders.id |
| 501 | WHERE orders.post_type = 'shop_order' |
| 502 | AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' ) |
| 503 | AND meta_key in( '_payment_method','_order_total','_order_currency') |
| 504 | GROUP BY orders.id |
| 505 | ) order_gateways |
| 506 | GROUP BY gateway, currency |
| 507 | " |
| 508 | ); |
| 509 | |
| 510 | $orders_by_gateway_currency = array(); |
| 511 | foreach ( $orders_by_gateway as $orders_details ) { |
| 512 | $gateway = 'gateway_' . $orders_details->gateway; |
| 513 | $currency = $orders_details->currency; |
| 514 | $count = $gateway . '_' . $currency . '_count'; |
| 515 | $total = $gateway . '_' . $currency . '_total'; |
| 516 | |
| 517 | $orders_by_gateway_currency[ $count ] = $orders_details->counts; |
| 518 | $orders_by_gateway_currency[ $total ] = $orders_details->totals; |
| 519 | } |
| 520 | |
| 521 | return $orders_by_gateway_currency; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * Get review counts for different statuses. |
| 526 | * |
| 527 | * @return array |
| 528 | */ |
| 529 | private static function get_review_counts() { |
| 530 | global $wpdb; |
| 531 | $review_count = array( 'total' => 0 ); |
| 532 | $status_map = array( |
| 533 | '0' => 'pending', |
| 534 | '1' => 'approved', |
| 535 | 'trash' => 'trash', |
| 536 | 'spam' => 'spam', |
| 537 | ); |
| 538 | $counts = $wpdb->get_results( |
| 539 | " |
| 540 | SELECT comment_approved, COUNT(*) AS num_reviews |
| 541 | FROM {$wpdb->comments} |
| 542 | WHERE comment_type = 'review' |
| 543 | GROUP BY comment_approved |
| 544 | ", |
| 545 | ARRAY_A |
| 546 | ); |
| 547 | |
| 548 | if ( ! $counts ) { |
| 549 | return $review_count; |
| 550 | } |
| 551 | |
| 552 | foreach ( $counts as $count ) { |
| 553 | $status = $count['comment_approved']; |
| 554 | if ( array_key_exists( $status, $status_map ) ) { |
| 555 | $review_count[ $status_map[ $status ] ] = $count['num_reviews']; |
| 556 | } |
| 557 | $review_count['total'] += $count['num_reviews']; |
| 558 | } |
| 559 | |
| 560 | return $review_count; |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Get the number of product categories. |
| 565 | * |
| 566 | * @return int |
| 567 | */ |
| 568 | private static function get_category_counts() { |
| 569 | return wp_count_terms( 'product_cat' ); |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * Get a list of all active payment gateways. |
| 574 | * |
| 575 | * @return array |
| 576 | */ |
| 577 | private static function get_active_payment_gateways() { |
| 578 | $active_gateways = array(); |
| 579 | $gateways = WC()->payment_gateways->payment_gateways(); |
| 580 | foreach ( $gateways as $id => $gateway ) { |
| 581 | if ( isset( $gateway->enabled ) && 'yes' === $gateway->enabled ) { |
| 582 | $active_gateways[ $id ] = array( |
| 583 | 'title' => $gateway->title, |
| 584 | 'supports' => $gateway->supports, |
| 585 | ); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return $active_gateways; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Get a list of all active shipping methods. |
| 594 | * |
| 595 | * @return array |
| 596 | */ |
| 597 | private static function get_active_shipping_methods() { |
| 598 | $active_methods = array(); |
| 599 | $shipping_methods = WC()->shipping()->get_shipping_methods(); |
| 600 | foreach ( $shipping_methods as $id => $shipping_method ) { |
| 601 | if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) { |
| 602 | $active_methods[ $id ] = array( |
| 603 | 'title' => $shipping_method->title, |
| 604 | 'tax_status' => $shipping_method->tax_status, |
| 605 | ); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | return $active_methods; |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Get all options starting with woocommerce_ prefix. |
| 614 | * |
| 615 | * @return array |
| 616 | */ |
| 617 | private static function get_all_woocommerce_options_values() { |
| 618 | return array( |
| 619 | 'version' => WC()->version, |
| 620 | 'currency' => get_woocommerce_currency(), |
| 621 | 'base_location' => WC()->countries->get_base_country(), |
| 622 | 'selling_locations' => WC()->countries->get_allowed_countries(), |
| 623 | 'api_enabled' => get_option( 'woocommerce_api_enabled' ), |
| 624 | 'weight_unit' => get_option( 'woocommerce_weight_unit' ), |
| 625 | 'dimension_unit' => get_option( 'woocommerce_dimension_unit' ), |
| 626 | 'download_method' => get_option( 'woocommerce_file_download_method' ), |
| 627 | 'download_require_login' => get_option( 'woocommerce_downloads_require_login' ), |
| 628 | 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ), |
| 629 | 'coupons_enabled' => get_option( 'woocommerce_enable_coupons' ), |
| 630 | 'guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ), |
| 631 | 'secure_checkout' => get_option( 'woocommerce_force_ssl_checkout' ), |
| 632 | 'enable_signup_and_login_from_checkout' => get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), |
| 633 | 'enable_myaccount_registration' => get_option( 'woocommerce_enable_myaccount_registration' ), |
| 634 | 'registration_generate_username' => get_option( 'woocommerce_registration_generate_username' ), |
| 635 | 'registration_generate_password' => get_option( 'woocommerce_registration_generate_password' ), |
| 636 | ); |
| 637 | } |
| 638 | |
| 639 | /** |
| 640 | * Look for any template override and return filenames. |
| 641 | * |
| 642 | * @return array |
| 643 | */ |
| 644 | private static function get_all_template_overrides() { |
| 645 | $override_data = array(); |
| 646 | $template_paths = apply_filters( 'woocommerce_template_overrides_scan_paths', array( 'WooCommerce' => WC()->plugin_path() . '/templates/' ) ); |
| 647 | $scanned_files = array(); |
| 648 | |
| 649 | require_once WC()->plugin_path() . '/includes/admin/class-wc-admin-status.php'; |
| 650 | |
| 651 | foreach ( $template_paths as $plugin_name => $template_path ) { |
| 652 | $scanned_files[ $plugin_name ] = WC_Admin_Status::scan_template_files( $template_path ); |
| 653 | } |
| 654 | |
| 655 | foreach ( $scanned_files as $plugin_name => $files ) { |
| 656 | foreach ( $files as $file ) { |
| 657 | if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { |
| 658 | $theme_file = get_stylesheet_directory() . '/' . $file; |
| 659 | } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) { |
| 660 | $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file; |
| 661 | } elseif ( file_exists( get_template_directory() . '/' . $file ) ) { |
| 662 | $theme_file = get_template_directory() . '/' . $file; |
| 663 | } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) { |
| 664 | $theme_file = get_template_directory() . '/' . WC()->template_path() . $file; |
| 665 | } else { |
| 666 | $theme_file = false; |
| 667 | } |
| 668 | |
| 669 | if ( false !== $theme_file ) { |
| 670 | $override_data[] = basename( $theme_file ); |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | return $override_data; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * When an admin user logs in, there user agent is tracked in user meta and collected here. |
| 679 | * |
| 680 | * @return array |
| 681 | */ |
| 682 | private static function get_admin_user_agents() { |
| 683 | return array_filter( (array) get_option( 'woocommerce_tracker_ua', array() ) ); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Search a specific post for text content. |
| 688 | * |
| 689 | * @param integer $post_id The id of the post to search. |
| 690 | * @param string $text The text to search for. |
| 691 | * @return string 'Yes' if post contains $text (otherwise 'No'). |
| 692 | */ |
| 693 | public static function post_contains_text( $post_id, $text ) { |
| 694 | global $wpdb; |
| 695 | |
| 696 | // Search for the text anywhere in the post. |
| 697 | $wildcarded = "%{$text}%"; |
| 698 | |
| 699 | $result = $wpdb->get_var( |
| 700 | $wpdb->prepare( |
| 701 | " |
| 702 | SELECT COUNT( * ) FROM {$wpdb->prefix}posts |
| 703 | WHERE ID=%d |
| 704 | AND {$wpdb->prefix}posts.post_content LIKE %s |
| 705 | ", |
| 706 | array( $post_id, $wildcarded ) |
| 707 | ) |
| 708 | ); |
| 709 | |
| 710 | return ( '0' !== $result ) ? 'Yes' : 'No'; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | /** |
| 715 | * Get tracker data for a specific block type on a woocommerce page. |
| 716 | * |
| 717 | * @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`. |
| 718 | * @param string $woo_page_name The woo page to search, e.g. `cart`. |
| 719 | * @return array Associative array of tracker data with keys: |
| 720 | * - page_contains_block |
| 721 | * - block_attributes |
| 722 | */ |
| 723 | public static function get_block_tracker_data( $block_name, $woo_page_name ) { |
| 724 | $blocks = WC_Blocks_Utils::get_blocks_from_page( $block_name, $woo_page_name ); |
| 725 | |
| 726 | $block_present = false; |
| 727 | $attributes = array(); |
| 728 | if ( $blocks && count( $blocks ) ) { |
| 729 | // Return any customised attributes from the first block. |
| 730 | $block_present = true; |
| 731 | $attributes = $blocks[0]['attrs']; |
| 732 | } |
| 733 | |
| 734 | return array( |
| 735 | 'page_contains_block' => $block_present ? 'Yes' : 'No', |
| 736 | 'block_attributes' => $attributes, |
| 737 | ); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Get info about the cart & checkout pages. |
| 742 | * |
| 743 | * @return array |
| 744 | */ |
| 745 | public static function get_cart_checkout_info() { |
| 746 | $cart_page_id = wc_get_page_id( 'cart' ); |
| 747 | $checkout_page_id = wc_get_page_id( 'checkout' ); |
| 748 | |
| 749 | $cart_block_data = self::get_block_tracker_data( 'woocommerce/cart', 'cart' ); |
| 750 | $checkout_block_data = self::get_block_tracker_data( 'woocommerce/checkout', 'checkout' ); |
| 751 | |
| 752 | return array( |
| 753 | 'cart_page_contains_cart_shortcode' => self::post_contains_text( |
| 754 | $cart_page_id, |
| 755 | '[woocommerce_cart]' |
| 756 | ), |
| 757 | 'checkout_page_contains_checkout_shortcode' => self::post_contains_text( |
| 758 | $checkout_page_id, |
| 759 | '[woocommerce_checkout]' |
| 760 | ), |
| 761 | |
| 762 | 'cart_page_contains_cart_block' => $cart_block_data['page_contains_block'], |
| 763 | 'cart_block_attributes' => $cart_block_data['block_attributes'], |
| 764 | 'checkout_page_contains_checkout_block' => $checkout_block_data['page_contains_block'], |
| 765 | 'checkout_block_attributes' => $checkout_block_data['block_attributes'], |
| 766 | ); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | WC_Tracker::init(); |
| 771 |