abstracts
1 year ago
admin
1 year ago
blocks
1 year ago
cli
1 year ago
customizer
2 years ago
data-stores
1 year ago
emails
1 year ago
export
1 year ago
gateways
1 year ago
import
1 year ago
integrations
2 years ago
interfaces
1 year ago
legacy
1 year ago
libraries
1 year ago
log-handlers
2 years ago
payment-tokens
5 years ago
product-usage
1 year ago
queue
4 years ago
react-admin
1 year ago
rest-api
1 year ago
shipping
1 year ago
shortcodes
1 year ago
theme-support
2 years ago
tracks
1 year ago
traits
5 years ago
walkers
5 years ago
wccom-site
1 year ago
widgets
1 year ago
class-wc-ajax.php
2 years ago
class-wc-auth.php
1 year ago
class-wc-autoloader.php
1 year ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-brands-brand-settings-manager.php
1 year ago
class-wc-brands-coupons.php
1 year ago
class-wc-brands.php
1 year ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
1 year ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
1 year ago
class-wc-cart-totals.php
2 years ago
class-wc-cart.php
1 year ago
class-wc-checkout.php
1 year ago
class-wc-cli.php
1 year ago
class-wc-comments.php
3 years ago
class-wc-countries.php
1 year ago
class-wc-coupon.php
1 year ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
1 year ago
class-wc-customer.php
2 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
3 years ago
class-wc-datetime.php
4 years ago
class-wc-deprecated-action-hooks.php
2 years ago
class-wc-deprecated-filter-hooks.php
3 years ago
class-wc-discounts.php
1 year ago
class-wc-download-handler.php
1 year ago
class-wc-emails.php
1 year ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
1 year ago
class-wc-frontend-scripts.php
1 year ago
class-wc-geo-ip.php
1 year ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
1 year ago
class-wc-https.php
2 years ago
class-wc-install.php
1 year ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
1 year ago
class-wc-meta-data.php
4 years ago
class-wc-order-factory.php
2 years ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
1 year ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
1 year ago
class-wc-order-item-shipping.php
1 year ago
class-wc-order-item-tax.php
4 years ago
class-wc-order-item.php
1 year ago
class-wc-order-query.php
4 years ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 year ago
class-wc-payment-gateways.php
1 year ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
1 year ago
class-wc-post-types.php
1 year ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
1 year ago
class-wc-privacy-exporters.php
4 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
4 years ago
class-wc-product-download.php
2 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
2 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
1 year ago
class-wc-product-variable.php
1 year ago
class-wc-product-variation.php
1 year ago
class-wc-query.php
1 year ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
1 year ago
class-wc-register-wp-admin-settings.php
4 years ago
class-wc-rest-authentication.php
1 year ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
2 years ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
4 years ago
class-wc-shortcodes.php
2 years ago
class-wc-structured-data.php
1 year ago
class-wc-tax.php
2 years ago
class-wc-template-loader.php
2 years ago
class-wc-tracker.php
1 year ago
class-wc-validation.php
2 years ago
class-wc-webhook.php
1 year ago
class-woocommerce.php
3 months ago
wc-account-functions.php
1 year ago
wc-attribute-functions.php
1 year ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
1 year ago
wc-conditional-functions.php
2 years ago
wc-core-functions.php
1 year ago
wc-coupon-functions.php
3 years ago
wc-deprecated-functions.php
2 years ago
wc-formatting-functions.php
1 year ago
wc-notice-functions.php
2 years ago
wc-order-functions.php
1 year ago
wc-order-item-functions.php
3 years ago
wc-page-functions.php
1 year ago
wc-product-functions.php
1 year ago
wc-rest-functions.php
1 year ago
wc-stock-functions.php
1 year ago
wc-template-functions.php
1 year ago
wc-template-hooks.php
1 year ago
wc-term-functions.php
3 years ago
wc-update-functions.php
1 year ago
wc-user-functions.php
1 year ago
wc-webhook-functions.php
1 year ago
wc-widget-functions.php
5 years ago
class-wc-tracker.php
1433 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 | use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; |
| 15 | use Automattic\WooCommerce\Utilities\{ FeaturesUtil, OrderUtil, PluginUtil }; |
| 16 | use Automattic\WooCommerce\Internal\Utilities\BlocksUtil; |
| 17 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 18 | use Automattic\WooCommerce\Blocks\Package; |
| 19 | use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields; |
| 20 | |
| 21 | defined( 'ABSPATH' ) || exit; |
| 22 | |
| 23 | // phpcs:disable Squiz.Classes.ClassFileName.NoMatch, Squiz.Classes.ValidClassName.NotCamelCaps -- Backwards compatibility. |
| 24 | /** |
| 25 | * WooCommerce Tracker Class |
| 26 | */ |
| 27 | class WC_Tracker { |
| 28 | |
| 29 | // phpcs:enable |
| 30 | /** |
| 31 | * URL to the WooThemes Tracker API endpoint. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | private static $api_url = 'https://tracking.woocommerce.com/v1/'; |
| 36 | |
| 37 | /** |
| 38 | * Hook into cron event. |
| 39 | */ |
| 40 | public static function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod.MissingFinal, WooCommerce.Functions.InternalInjectionMethod.MissingInternalTag -- Not an injection. |
| 41 | add_action( 'woocommerce_tracker_send_event', array( __CLASS__, 'send_tracking_data' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Decide whether to send tracking data or not. |
| 46 | * |
| 47 | * @param boolean $override Should override?. |
| 48 | */ |
| 49 | public static function send_tracking_data( $override = false ) { |
| 50 | // Don't trigger this on AJAX Requests. |
| 51 | if ( Constants::is_true( 'DOING_AJAX' ) ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Filter whether to send tracking data or not. |
| 57 | * |
| 58 | * @since 2.3.0 |
| 59 | */ |
| 60 | if ( ! apply_filters( 'woocommerce_tracker_send_override', $override ) ) { |
| 61 | // Send a maximum of once per week by default. |
| 62 | $last_send = self::get_last_send_time(); |
| 63 | if ( $last_send && $last_send > apply_filters( 'woocommerce_tracker_last_send_interval', strtotime( '-1 week' ) ) ) { // phpcs:ignore |
| 64 | return; |
| 65 | } |
| 66 | } else { |
| 67 | // Make sure there is at least a 1 hour delay between override sends, we don't want duplicate calls due to double clicking links. |
| 68 | $last_send = self::get_last_send_time(); |
| 69 | if ( $last_send && $last_send > strtotime( '-1 hours' ) ) { |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Update time first before sending to ensure it is set. |
| 75 | update_option( 'woocommerce_tracker_last_send', time() ); |
| 76 | |
| 77 | $params = self::get_tracking_data(); |
| 78 | wp_safe_remote_post( |
| 79 | self::$api_url, |
| 80 | array( |
| 81 | 'method' => 'POST', |
| 82 | 'timeout' => 45, |
| 83 | 'redirection' => 5, |
| 84 | 'httpversion' => '1.0', |
| 85 | 'blocking' => false, |
| 86 | 'headers' => array( 'user-agent' => 'WooCommerceTracker/' . md5( esc_url_raw( home_url( '/' ) ) ) . ';' ), |
| 87 | 'body' => wp_json_encode( $params ), |
| 88 | 'cookies' => array(), |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the last time tracking data was sent. |
| 95 | * |
| 96 | * @return int|bool |
| 97 | */ |
| 98 | private static function get_last_send_time() { |
| 99 | /** |
| 100 | * Filter the last time tracking data was sent. |
| 101 | * |
| 102 | * @since 2.3.0 |
| 103 | */ |
| 104 | return apply_filters( 'woocommerce_tracker_last_send_time', get_option( 'woocommerce_tracker_last_send', false ) ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Test whether this site is a staging site according to the Jetpack criteria. |
| 109 | * |
| 110 | * With Jetpack 8.1+, Jetpack::is_staging_site has been deprecated. |
| 111 | * \Automattic\Jetpack\Status::is_staging_site is the replacement. |
| 112 | * However, there are version of JP where \Automattic\Jetpack\Status exists, but does *not* contain is_staging_site method, |
| 113 | * so with those, code still needs to use the previous check as a fallback. |
| 114 | * |
| 115 | * After upgrading Jetpack Status to v3.3.2 is_staging_site is also deprecated and in_safe_mode is the new replacement. |
| 116 | * So we check this first of all. |
| 117 | * |
| 118 | * @return bool |
| 119 | */ |
| 120 | private static function is_jetpack_staging_site() { |
| 121 | if ( class_exists( '\Automattic\Jetpack\Status' ) ) { |
| 122 | |
| 123 | $jp_status = new \Automattic\Jetpack\Status(); |
| 124 | |
| 125 | if ( is_callable( array( $jp_status, 'in_safe_mode' ) ) ) { |
| 126 | return $jp_status->in_safe_mode(); |
| 127 | } elseif ( is_callable( array( $jp_status, 'is_staging_site' ) ) ) { |
| 128 | // Preferred way of checking with Jetpack 8.1+. |
| 129 | return $jp_status->is_staging_site(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Get all the tracking data. |
| 138 | * |
| 139 | * @return array |
| 140 | */ |
| 141 | public static function get_tracking_data() { |
| 142 | $data = array(); |
| 143 | $start_time = microtime( true ); |
| 144 | |
| 145 | // General site info. |
| 146 | $data['url'] = home_url(); |
| 147 | $data['store_id'] = get_option( \WC_Install::STORE_ID_OPTION, null ); |
| 148 | $data['blog_id'] = class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null; |
| 149 | |
| 150 | /** |
| 151 | * Filter the admin email that's sent with data. |
| 152 | * |
| 153 | * @since 2.3.0 |
| 154 | */ |
| 155 | $data['email'] = apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) ); |
| 156 | $data['theme'] = self::get_theme_info(); |
| 157 | |
| 158 | // WordPress Info. |
| 159 | $data['wp'] = self::get_wordpress_info(); |
| 160 | |
| 161 | // Server Info. |
| 162 | $data['server'] = self::get_server_info(); |
| 163 | |
| 164 | // Plugin info. |
| 165 | $all_plugins = self::get_all_plugins(); |
| 166 | $data['active_plugins'] = $all_plugins['active_plugins']; |
| 167 | $data['inactive_plugins'] = $all_plugins['inactive_plugins']; |
| 168 | |
| 169 | // Jetpack & WooCommerce Connect. |
| 170 | $data['jetpack_version'] = Constants::is_defined( 'JETPACK__VERSION' ) ? Constants::get_constant( 'JETPACK__VERSION' ) : 'none'; |
| 171 | $data['jetpack_connected'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active() ) ? 'yes' : 'no'; |
| 172 | $data['jetpack_is_staging'] = self::is_jetpack_staging_site() ? 'yes' : 'no'; |
| 173 | $data['connect_installed'] = class_exists( 'WC_Connect_Loader' ) ? 'yes' : 'no'; |
| 174 | $data['connect_active'] = ( class_exists( 'WC_Connect_Loader' ) && wp_next_scheduled( 'wc_connect_fetch_service_schemas' ) ) ? 'yes' : 'no'; |
| 175 | $data['helper_connected'] = self::get_helper_connected(); |
| 176 | |
| 177 | // Store count info. |
| 178 | $data['users'] = self::get_user_counts(); |
| 179 | $data['products'] = self::get_product_counts(); |
| 180 | $data['orders'] = self::get_orders(); |
| 181 | $data['reviews'] = self::get_review_counts(); |
| 182 | $data['categories'] = self::get_category_counts(); |
| 183 | $data['brands'] = self::get_brands_counts(); |
| 184 | |
| 185 | // Get order snapshot. |
| 186 | $data['order_snapshot'] = self::get_order_snapshot(); |
| 187 | |
| 188 | // Payment gateway info. |
| 189 | $data['gateways'] = self::get_active_payment_gateways(); |
| 190 | |
| 191 | // WcPay settings info. |
| 192 | $data['wcpay_settings'] = self::get_wcpay_settings(); |
| 193 | |
| 194 | // Shipping method info. |
| 195 | $data['shipping_methods'] = self::get_active_shipping_methods(); |
| 196 | |
| 197 | // Features. |
| 198 | $data['enabled_features'] = self::get_enabled_features(); |
| 199 | |
| 200 | // Get all WooCommerce options info. |
| 201 | $data['settings'] = self::get_all_woocommerce_options_values(); |
| 202 | |
| 203 | // Template overrides. |
| 204 | $data['template_overrides'] = self::get_all_template_overrides(); |
| 205 | |
| 206 | // Cart & checkout tech (blocks or shortcodes). |
| 207 | $data['cart_checkout'] = self::get_cart_checkout_info(); |
| 208 | |
| 209 | // Mini Cart block, which only exists since wp 5.9. |
| 210 | if ( version_compare( get_bloginfo( 'version' ), '5.9', '>=' ) ) { |
| 211 | $data['mini_cart_block'] = self::get_mini_cart_info(); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Filter whether to disable admin tracking. |
| 216 | * |
| 217 | * @since 5.2.0 |
| 218 | */ |
| 219 | $data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no'; |
| 220 | |
| 221 | // Mobile info. |
| 222 | $data['wc_mobile_usage'] = self::get_woocommerce_mobile_usage(); |
| 223 | |
| 224 | /** |
| 225 | * Filter the data that's sent with the tracker. |
| 226 | * |
| 227 | * @since 2.3.0 |
| 228 | */ |
| 229 | $data = apply_filters( 'woocommerce_tracker_data', $data ); |
| 230 | |
| 231 | // Total seconds taken to generate snapshot (including filtered data). |
| 232 | $data['snapshot_generation_time'] = microtime( true ) - $start_time; |
| 233 | |
| 234 | return $data; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Get the current theme info, theme name and version. |
| 239 | * |
| 240 | * @return array |
| 241 | */ |
| 242 | public static function get_theme_info() { |
| 243 | $theme_data = wp_get_theme(); |
| 244 | $theme_child_theme = wc_bool_to_string( is_child_theme() ); |
| 245 | $theme_wc_support = wc_bool_to_string( current_theme_supports( 'woocommerce' ) ); |
| 246 | $theme_is_block_theme = wc_bool_to_string( wc_current_theme_is_fse_theme() ); |
| 247 | |
| 248 | return array( |
| 249 | 'name' => $theme_data->Name, // @phpcs:ignore |
| 250 | 'version' => $theme_data->Version, // @phpcs:ignore |
| 251 | 'child_theme' => $theme_child_theme, |
| 252 | 'wc_support' => $theme_wc_support, |
| 253 | 'block_theme' => $theme_is_block_theme, |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get WordPress related data. |
| 259 | * |
| 260 | * @return array |
| 261 | */ |
| 262 | private static function get_wordpress_info() { |
| 263 | $wp_data = array(); |
| 264 | |
| 265 | $memory = wc_let_to_num( WP_MEMORY_LIMIT ); |
| 266 | |
| 267 | if ( function_exists( 'memory_get_usage' ) ) { |
| 268 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- False positive. |
| 269 | $system_memory = wc_let_to_num( @ini_get( 'memory_limit' ) ); |
| 270 | $memory = max( $memory, $system_memory ); |
| 271 | } |
| 272 | |
| 273 | // WordPress 5.5+ environment type specification. |
| 274 | // 'production' is the default in WP, thus using it as a default here, too. |
| 275 | $environment_type = 'production'; |
| 276 | if ( function_exists( 'wp_get_environment_type' ) ) { |
| 277 | $environment_type = wp_get_environment_type(); |
| 278 | } |
| 279 | |
| 280 | $wp_data['memory_limit'] = size_format( $memory ); |
| 281 | $wp_data['debug_mode'] = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No'; |
| 282 | $wp_data['locale'] = get_locale(); |
| 283 | $wp_data['version'] = get_bloginfo( 'version' ); |
| 284 | $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No'; |
| 285 | $wp_data['env_type'] = $environment_type; |
| 286 | $wp_data['dropins'] = array_keys( get_dropins() ); |
| 287 | |
| 288 | return $wp_data; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get server related info. |
| 293 | * |
| 294 | * @return array |
| 295 | */ |
| 296 | private static function get_server_info() { |
| 297 | $server_data = array(); |
| 298 | |
| 299 | if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) { |
| 300 | $server_data['software'] = $_SERVER['SERVER_SOFTWARE']; // @phpcs:ignore |
| 301 | } |
| 302 | |
| 303 | if ( function_exists( 'phpversion' ) ) { |
| 304 | $server_data['php_version'] = phpversion(); |
| 305 | } |
| 306 | |
| 307 | if ( function_exists( 'ini_get' ) ) { |
| 308 | $server_data['php_post_max_size'] = size_format( wc_let_to_num( ini_get( 'post_max_size' ) ) ); |
| 309 | $server_data['php_time_limt'] = ini_get( 'max_execution_time' ); |
| 310 | $server_data['php_max_input_vars'] = ini_get( 'max_input_vars' ); |
| 311 | $server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No'; |
| 312 | } |
| 313 | |
| 314 | $database_version = wc_get_server_database_version(); |
| 315 | $server_data['mysql_version'] = $database_version['number']; |
| 316 | |
| 317 | $server_data['php_max_upload_size'] = size_format( wp_max_upload_size() ); |
| 318 | $server_data['php_default_timezone'] = date_default_timezone_get(); |
| 319 | $server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No'; |
| 320 | $server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No'; |
| 321 | $server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No'; |
| 322 | |
| 323 | return $server_data; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get all plugins grouped into activated or not. |
| 328 | * |
| 329 | * @return array |
| 330 | */ |
| 331 | private static function get_all_plugins() { |
| 332 | // Ensure get_plugins function is loaded. |
| 333 | if ( ! function_exists( 'get_plugins' ) ) { |
| 334 | include ABSPATH . '/wp-admin/includes/plugin.php'; |
| 335 | } |
| 336 | |
| 337 | $plugins = wc_get_container()->get( LegacyProxy::class )->call_function( 'get_plugins' ); |
| 338 | $active_plugins_keys = get_option( 'active_plugins', array() ); |
| 339 | $active_plugins = array(); |
| 340 | |
| 341 | foreach ( $plugins as $k => $v ) { |
| 342 | // Take care of formatting the data how we want it. |
| 343 | $formatted = array(); |
| 344 | $formatted['name'] = wp_strip_all_tags( $v['Name'] ); |
| 345 | if ( isset( $v['Version'] ) ) { |
| 346 | $formatted['version'] = wp_strip_all_tags( $v['Version'] ); |
| 347 | } |
| 348 | if ( isset( $v['Author'] ) ) { |
| 349 | $formatted['author'] = wp_strip_all_tags( $v['Author'] ); |
| 350 | } |
| 351 | if ( isset( $v['Network'] ) ) { |
| 352 | $formatted['network'] = wp_strip_all_tags( $v['Network'] ); |
| 353 | } |
| 354 | if ( isset( $v['PluginURI'] ) ) { |
| 355 | $formatted['plugin_uri'] = wp_strip_all_tags( $v['PluginURI'] ); |
| 356 | } |
| 357 | $formatted['feature_compatibility'] = array(); |
| 358 | if ( wc_get_container()->get( PluginUtil::class )->is_woocommerce_aware_plugin( $k ) ) { |
| 359 | $formatted['feature_compatibility'] = array_filter( FeaturesUtil::get_compatible_features_for_plugin( $k ) ); |
| 360 | } |
| 361 | if ( in_array( $k, $active_plugins_keys, true ) ) { |
| 362 | // Remove active plugins from list so we can show active and inactive separately. |
| 363 | unset( $plugins[ $k ] ); |
| 364 | $active_plugins[ $k ] = $formatted; |
| 365 | } else { |
| 366 | $plugins[ $k ] = $formatted; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return array( |
| 371 | 'active_plugins' => $active_plugins, |
| 372 | 'inactive_plugins' => $plugins, |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Get the settings of WooCommerce Payments plugin |
| 378 | * |
| 379 | * @return array |
| 380 | */ |
| 381 | private static function get_wcpay_settings() { |
| 382 | return get_option( 'woocommerce_woocommerce_payments_settings' ); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Check to see if the helper is connected to WooCommerce.com |
| 387 | * |
| 388 | * @return string |
| 389 | */ |
| 390 | private static function get_helper_connected() { |
| 391 | if ( class_exists( 'WC_Helper_Options' ) && is_callable( 'WC_Helper_Options::get' ) ) { |
| 392 | $authenticated = WC_Helper_Options::get( 'auth' ); |
| 393 | } else { |
| 394 | $authenticated = ''; |
| 395 | } |
| 396 | return ( ! empty( $authenticated ) ) ? 'yes' : 'no'; |
| 397 | } |
| 398 | |
| 399 | |
| 400 | /** |
| 401 | * Get user totals based on user role. |
| 402 | * |
| 403 | * @return array |
| 404 | */ |
| 405 | private static function get_user_counts() { |
| 406 | $user_count = array(); |
| 407 | $user_count_data = count_users(); |
| 408 | $user_count['total'] = $user_count_data['total_users']; |
| 409 | |
| 410 | // Get user count based on user role. |
| 411 | foreach ( $user_count_data['avail_roles'] as $role => $count ) { |
| 412 | $user_count[ $role ] = $count; |
| 413 | } |
| 414 | |
| 415 | return $user_count; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Get product totals based on product type. |
| 420 | * |
| 421 | * @return array |
| 422 | */ |
| 423 | public static function get_product_counts() { |
| 424 | $product_count = array(); |
| 425 | $product_count_data = wp_count_posts( 'product' ); |
| 426 | $product_count['total'] = $product_count_data->publish; |
| 427 | |
| 428 | $product_statuses = get_terms( 'product_type', array( 'hide_empty' => 0 ) ); |
| 429 | foreach ( $product_statuses as $product_status ) { |
| 430 | $product_count[ $product_status->name ] = $product_status->count; |
| 431 | } |
| 432 | |
| 433 | return $product_count; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * Get order counts. |
| 438 | * |
| 439 | * @return array |
| 440 | */ |
| 441 | private static function get_order_counts() { |
| 442 | $order_count = array(); |
| 443 | foreach ( wc_get_order_statuses() as $status_slug => $status_name ) { |
| 444 | $order_count[ $status_slug ] = wc_orders_count( $status_slug ); |
| 445 | } |
| 446 | return $order_count; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Combine all order data. |
| 451 | * |
| 452 | * @return array |
| 453 | */ |
| 454 | private static function get_orders() { |
| 455 | $order_dates = self::get_order_dates(); |
| 456 | $order_counts = self::get_order_counts(); |
| 457 | $order_totals = self::get_order_totals(); |
| 458 | $order_gateways = self::get_orders_by_gateway(); |
| 459 | $order_origin = self::get_orders_origins(); |
| 460 | |
| 461 | return array_merge( $order_dates, $order_counts, $order_totals, $order_gateways, $order_origin ); |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * Get order totals. |
| 466 | * |
| 467 | * Keeping the internal statuses names as strings to avoid regression issues (not referencing Automattic\WooCommerce\Enums\OrderInternalStatus class). |
| 468 | * |
| 469 | * @since 5.4.0 |
| 470 | * @return array |
| 471 | */ |
| 472 | private static function get_order_totals() { |
| 473 | global $wpdb; |
| 474 | |
| 475 | $orders_table = OrdersTableDataStore::get_orders_table_name(); |
| 476 | |
| 477 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 478 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 479 | $gross_total = $wpdb->get_var( |
| 480 | " |
| 481 | SELECT SUM(total_amount) AS 'gross_total' |
| 482 | FROM $orders_table |
| 483 | WHERE status in ('wc-completed', 'wc-refunded'); |
| 484 | " |
| 485 | ); |
| 486 | // phpcs:enable |
| 487 | } else { |
| 488 | $gross_total = $wpdb->get_var( |
| 489 | " |
| 490 | SELECT |
| 491 | SUM( order_meta.meta_value ) AS 'gross_total' |
| 492 | FROM {$wpdb->prefix}posts AS orders |
| 493 | LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID |
| 494 | WHERE order_meta.meta_key = '_order_total' |
| 495 | AND orders.post_status in ( 'wc-completed', 'wc-refunded' ) |
| 496 | GROUP BY order_meta.meta_key |
| 497 | " |
| 498 | ); |
| 499 | } |
| 500 | |
| 501 | if ( is_null( $gross_total ) ) { |
| 502 | $gross_total = 0; |
| 503 | } |
| 504 | |
| 505 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 506 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 507 | $processing_gross_total = $wpdb->get_var( |
| 508 | " |
| 509 | SELECT SUM(total_amount) AS 'gross_total' |
| 510 | FROM $orders_table |
| 511 | WHERE status = 'wc-processing'; |
| 512 | " |
| 513 | ); |
| 514 | // phpcs:enable |
| 515 | } else { |
| 516 | $processing_gross_total = $wpdb->get_var( |
| 517 | " |
| 518 | SELECT |
| 519 | SUM( order_meta.meta_value ) AS 'gross_total' |
| 520 | FROM {$wpdb->prefix}posts AS orders |
| 521 | LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID |
| 522 | WHERE order_meta.meta_key = '_order_total' |
| 523 | AND orders.post_status = 'wc-processing' |
| 524 | GROUP BY order_meta.meta_key |
| 525 | " |
| 526 | ); |
| 527 | } |
| 528 | |
| 529 | if ( is_null( $processing_gross_total ) ) { |
| 530 | $processing_gross_total = 0; |
| 531 | } |
| 532 | |
| 533 | return array( |
| 534 | 'gross' => $gross_total, |
| 535 | 'processing_gross' => $processing_gross_total, |
| 536 | ); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Get last order date. |
| 541 | * |
| 542 | * @return string |
| 543 | */ |
| 544 | private static function get_order_dates() { |
| 545 | global $wpdb; |
| 546 | |
| 547 | $orders_table = OrdersTableDataStore::get_orders_table_name(); |
| 548 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 549 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 550 | $min_max = $wpdb->get_row( |
| 551 | " |
| 552 | SELECT |
| 553 | MIN( date_created_gmt ) as 'first', MAX( date_created_gmt ) as 'last' |
| 554 | FROM $orders_table |
| 555 | WHERE status = 'wc-completed'; |
| 556 | ", |
| 557 | ARRAY_A |
| 558 | ); |
| 559 | // phpcs:enable |
| 560 | } else { |
| 561 | $min_max = $wpdb->get_row( |
| 562 | " |
| 563 | SELECT |
| 564 | MIN( post_date_gmt ) as 'first', MAX( post_date_gmt ) as 'last' |
| 565 | FROM {$wpdb->prefix}posts |
| 566 | WHERE post_type = 'shop_order' |
| 567 | AND post_status = 'wc-completed' |
| 568 | ", |
| 569 | ARRAY_A |
| 570 | ); |
| 571 | } |
| 572 | |
| 573 | if ( is_null( $min_max ) ) { |
| 574 | $min_max = array( |
| 575 | 'first' => '-', |
| 576 | 'last' => '-', |
| 577 | ); |
| 578 | } |
| 579 | |
| 580 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 581 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 582 | $processing_min_max = $wpdb->get_row( |
| 583 | " |
| 584 | SELECT |
| 585 | MIN( date_created_gmt ) as 'processing_first', MAX( date_created_gmt ) as 'processing_last' |
| 586 | FROM $orders_table |
| 587 | WHERE status = 'wc-processing'; |
| 588 | ", |
| 589 | ARRAY_A |
| 590 | ); |
| 591 | // phpcs:enable |
| 592 | } else { |
| 593 | $processing_min_max = $wpdb->get_row( |
| 594 | " |
| 595 | SELECT |
| 596 | MIN( post_date_gmt ) as 'processing_first', MAX( post_date_gmt ) as 'processing_last' |
| 597 | FROM {$wpdb->prefix}posts |
| 598 | WHERE post_type = 'shop_order' |
| 599 | AND post_status = 'wc-processing' |
| 600 | ", |
| 601 | ARRAY_A |
| 602 | ); |
| 603 | } |
| 604 | |
| 605 | if ( is_null( $processing_min_max ) ) { |
| 606 | $processing_min_max = array( |
| 607 | 'processing_first' => '-', |
| 608 | 'processing_last' => '-', |
| 609 | ); |
| 610 | } |
| 611 | |
| 612 | return array_merge( $min_max, $processing_min_max ); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Extract the group key for an associative array of objects which have unique ids in the key. |
| 617 | * A 'group_key' property is introduced in the object. |
| 618 | * For example, two objects with keys like 'WooDataPay ** #123' and 'WooDataPay ** #78' would |
| 619 | * both have a group_key of 'WooDataPay **' after this function call. |
| 620 | * |
| 621 | * @param array $objects The array of objects that need to be grouped. |
| 622 | * @param string $default_key The property that will be the default group_key. |
| 623 | * @return array Contains the objects with a group_key property. |
| 624 | */ |
| 625 | private static function extract_group_key( $objects, $default_key ) { |
| 626 | $keys = array_keys( $objects ); |
| 627 | |
| 628 | // Sort keys by length and then by characters within the same length keys. |
| 629 | usort( |
| 630 | $keys, |
| 631 | function ( $a, $b ) { |
| 632 | if ( strlen( $a ) === strlen( $b ) ) { |
| 633 | return strcmp( $a, $b ); |
| 634 | } |
| 635 | return ( strlen( $a ) < strlen( $b ) ) ? -1 : 1; |
| 636 | } |
| 637 | ); |
| 638 | |
| 639 | // Look for common tokens in every pair of adjacent keys. |
| 640 | $prev = ''; |
| 641 | foreach ( $keys as $key ) { |
| 642 | if ( $prev ) { |
| 643 | $comm_tokens = array(); |
| 644 | |
| 645 | // Tokenize the current and previous gateway names. |
| 646 | $curr_tokens = preg_split( '/[ :,\-_]+/', $key ); |
| 647 | $prev_tokens = preg_split( '/[ :,\-_]+/', $prev ); |
| 648 | |
| 649 | $len_curr = is_array( $curr_tokens ) ? count( $curr_tokens ) : 0; |
| 650 | $len_prev = is_array( $prev_tokens ) ? count( $prev_tokens ) : 0; |
| 651 | |
| 652 | $index_unique = -1; |
| 653 | // Gather the common tokens. |
| 654 | // Let us allow for the unique reference id to be anywhere in the name. |
| 655 | for ( $i = 0; $i < $len_curr && $i < $len_prev; $i++ ) { |
| 656 | if ( $curr_tokens[ $i ] === $prev_tokens[ $i ] ) { |
| 657 | $comm_tokens[] = $curr_tokens[ $i ]; |
| 658 | } elseif ( preg_match( '/\d/', $curr_tokens[ $i ] ) && preg_match( '/\d/', $prev_tokens[ $i ] ) ) { |
| 659 | $index_unique = $i; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | // If only one token is different, and those tokens contain digits, then that could be the unique id. |
| 664 | if ( $len_curr - count( $comm_tokens ) <= 1 && count( $comm_tokens ) > 0 && $index_unique > -1 ) { |
| 665 | $objects[ $key ]->group_key = implode( ' ', $comm_tokens ); |
| 666 | $objects[ $prev ]->group_key = implode( ' ', $comm_tokens ); |
| 667 | } else { |
| 668 | $objects[ $key ]->group_key = $objects[ $key ]->$default_key; |
| 669 | } |
| 670 | } else { |
| 671 | $objects[ $key ]->group_key = $objects[ $key ]->$default_key; |
| 672 | } |
| 673 | $prev = $key; |
| 674 | } |
| 675 | return $objects; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * Get order details by gateway. |
| 680 | * |
| 681 | * @return array |
| 682 | */ |
| 683 | private static function get_orders_by_gateway() { |
| 684 | global $wpdb; |
| 685 | |
| 686 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 687 | $orders_table = OrdersTableDataStore::get_orders_table_name(); |
| 688 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 689 | $orders_and_gateway_details = $wpdb->get_results( |
| 690 | " |
| 691 | SELECT payment_method AS gateway, currency AS currency, SUM( total_amount ) AS totals, count( id ) AS counts |
| 692 | FROM $orders_table |
| 693 | WHERE status IN ( 'wc-completed', 'wc-processing', 'wc-refunded' ) |
| 694 | GROUP BY gateway, currency; |
| 695 | " |
| 696 | ); |
| 697 | // phpcs:enable |
| 698 | } else { |
| 699 | $orders_and_gateway_details = $wpdb->get_results( |
| 700 | " |
| 701 | SELECT |
| 702 | gateway, currency, SUM(total) AS totals, COUNT(order_id) AS counts |
| 703 | FROM ( |
| 704 | SELECT |
| 705 | orders.id AS order_id, |
| 706 | MAX(CASE WHEN meta_key = '_payment_method' THEN meta_value END) gateway, |
| 707 | MAX(CASE WHEN meta_key = '_order_total' THEN meta_value END) total, |
| 708 | MAX(CASE WHEN meta_key = '_order_currency' THEN meta_value END) currency |
| 709 | FROM |
| 710 | {$wpdb->prefix}posts orders |
| 711 | LEFT JOIN |
| 712 | {$wpdb->prefix}postmeta order_meta ON order_meta.post_id = orders.id |
| 713 | WHERE orders.post_type = 'shop_order' |
| 714 | AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' ) |
| 715 | AND meta_key in( '_payment_method','_order_total','_order_currency') |
| 716 | GROUP BY orders.id |
| 717 | ) order_gateways |
| 718 | GROUP BY gateway, currency |
| 719 | " |
| 720 | ); |
| 721 | } |
| 722 | |
| 723 | $orders_by_gateway_currency = array(); |
| 724 | |
| 725 | // The associative array that is created as the result of array_reduce is passed to extract_group_key() |
| 726 | // This function has the logic that will remove specific transaction identifiers that may sometimes be part of a |
| 727 | // payment method. For example, two payments methods like 'WooDataPay ** #123' and 'WooDataPay ** #78' would |
| 728 | // both have the same group_key 'WooDataPay **'. |
| 729 | $orders_by_gateway = self::extract_group_key( |
| 730 | // Convert into an associative array with a combination of currency and gateway as key. |
| 731 | array_reduce( |
| 732 | $orders_and_gateway_details, |
| 733 | function ( $result, $item ) { |
| 734 | $item->gateway = preg_replace( '/\s+/', ' ', $item->gateway ); |
| 735 | |
| 736 | // Introduce currency as a prefix for the key. |
| 737 | $key = $item->currency . '==' . $item->gateway; |
| 738 | |
| 739 | $result[ $key ] = $item; |
| 740 | return $result; |
| 741 | }, |
| 742 | array() |
| 743 | ), |
| 744 | 'gateway' |
| 745 | ); |
| 746 | |
| 747 | // Aggregate using group_key. |
| 748 | foreach ( $orders_by_gateway as $orders_details ) { |
| 749 | $gkey = $orders_details->group_key; |
| 750 | |
| 751 | // Remove currency as prefix of key for backward compatibility. |
| 752 | if ( str_contains( $gkey, '==' ) ) { |
| 753 | $tokens = preg_split( '/==/', $gkey ); |
| 754 | $key = $tokens[1]; |
| 755 | } else { |
| 756 | $key = $gkey; |
| 757 | } |
| 758 | |
| 759 | $key = str_replace( array( 'payment method', 'payment gateway', 'gateway' ), '', strtolower( $key ) ); |
| 760 | $key = trim( preg_replace( '/[: ,#*\-_]+/', ' ', $key ) ); |
| 761 | |
| 762 | // Add currency as postfix of gateway for backward compatibility. |
| 763 | $key = 'gateway_' . $key . '_' . $orders_details->currency; |
| 764 | $count_key = $key . '_count'; |
| 765 | $total_key = $key . '_total'; |
| 766 | |
| 767 | if ( array_key_exists( $count_key, $orders_by_gateway_currency ) || array_key_exists( $total_key, $orders_by_gateway_currency ) ) { |
| 768 | $orders_by_gateway_currency[ $count_key ] = $orders_by_gateway_currency[ $count_key ] + $orders_details->counts; |
| 769 | $orders_by_gateway_currency[ $total_key ] = $orders_by_gateway_currency[ $total_key ] + $orders_details->totals; |
| 770 | } else { |
| 771 | $orders_by_gateway_currency[ $count_key ] = $orders_details->counts; |
| 772 | $orders_by_gateway_currency[ $total_key ] = $orders_details->totals; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | return $orders_by_gateway_currency; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Get orders origin details. |
| 781 | * |
| 782 | * @return array |
| 783 | */ |
| 784 | private static function get_orders_origins() { |
| 785 | global $wpdb; |
| 786 | |
| 787 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 788 | $op_table_name = OrdersTableDataStore::get_operational_data_table_name(); |
| 789 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 790 | $orders_origin = $wpdb->get_results( |
| 791 | " |
| 792 | SELECT created_via as origin, COUNT( order_id ) as count |
| 793 | FROM $op_table_name |
| 794 | GROUP BY created_via; |
| 795 | " |
| 796 | ); |
| 797 | // phpcs:enable |
| 798 | } else { |
| 799 | $orders_origin = $wpdb->get_results( |
| 800 | " |
| 801 | SELECT |
| 802 | meta_value as origin, COUNT( DISTINCT ( orders.id ) ) as count |
| 803 | FROM |
| 804 | $wpdb->posts orders |
| 805 | LEFT JOIN |
| 806 | $wpdb->postmeta order_meta ON order_meta.post_id = orders.id |
| 807 | WHERE |
| 808 | meta_key = '_created_via' |
| 809 | GROUP BY |
| 810 | meta_value; |
| 811 | " |
| 812 | ); |
| 813 | } |
| 814 | |
| 815 | // The associative array that is created as the result of array_reduce is passed to extract_group_key() |
| 816 | // This function has the logic that will remove specific identifiers that may sometimes be part of an origin. |
| 817 | // For example, two origins like 'Import #123' and 'Import ** #78' would both have a group_key 'Import **'. |
| 818 | $orders_and_origins = self::extract_group_key( |
| 819 | // Convert into an associative array with the origin as key. |
| 820 | array_reduce( |
| 821 | $orders_origin, |
| 822 | function ( $result, $item ) { |
| 823 | $key = $item->origin; |
| 824 | |
| 825 | $result[ $key ] = $item; |
| 826 | return $result; |
| 827 | }, |
| 828 | array() |
| 829 | ), |
| 830 | 'origin' |
| 831 | ); |
| 832 | |
| 833 | $orders_by_origin = array(); |
| 834 | |
| 835 | // Aggregate using group_key. |
| 836 | foreach ( $orders_and_origins as $origin ) { |
| 837 | $key = strtolower( $origin->group_key ); |
| 838 | |
| 839 | if ( array_key_exists( $key, $orders_by_origin ) ) { |
| 840 | $orders_by_origin[ $key ] = $orders_by_origin[ $key ] + (int) $origin->count; |
| 841 | } else { |
| 842 | $orders_by_origin[ $key ] = (int) $origin->count; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | return array( 'created_via' => $orders_by_origin ); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Get review counts for different statuses. |
| 851 | * |
| 852 | * @return array |
| 853 | */ |
| 854 | private static function get_review_counts() { |
| 855 | global $wpdb; |
| 856 | $review_count = array( 'total' => 0 ); |
| 857 | $status_map = array( |
| 858 | '0' => 'pending', |
| 859 | '1' => 'approved', |
| 860 | 'trash' => 'trash', |
| 861 | 'spam' => 'spam', |
| 862 | ); |
| 863 | $counts = $wpdb->get_results( |
| 864 | " |
| 865 | SELECT comment_approved, COUNT(*) AS num_reviews |
| 866 | FROM {$wpdb->comments} |
| 867 | WHERE comment_type = 'review' |
| 868 | GROUP BY comment_approved |
| 869 | ", |
| 870 | ARRAY_A |
| 871 | ); |
| 872 | |
| 873 | if ( ! $counts ) { |
| 874 | return $review_count; |
| 875 | } |
| 876 | |
| 877 | foreach ( $counts as $count ) { |
| 878 | $status = $count['comment_approved']; |
| 879 | if ( array_key_exists( $status, $status_map ) ) { |
| 880 | $review_count[ $status_map[ $status ] ] = $count['num_reviews']; |
| 881 | } |
| 882 | $review_count['total'] += $count['num_reviews']; |
| 883 | } |
| 884 | |
| 885 | return $review_count; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * Get the number of product categories. |
| 890 | * |
| 891 | * @return int |
| 892 | */ |
| 893 | private static function get_category_counts() { |
| 894 | return wp_count_terms( 'product_cat' ); |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * Get the number of product brands. |
| 899 | * |
| 900 | * @return int |
| 901 | */ |
| 902 | private static function get_brands_counts() { |
| 903 | if ( ! taxonomy_exists( 'product_brand' ) ) { |
| 904 | return 0; |
| 905 | } |
| 906 | return wp_count_terms( 'product_brand' ); |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * Get a list of all active payment gateways. |
| 911 | * |
| 912 | * @return array |
| 913 | */ |
| 914 | private static function get_active_payment_gateways() { |
| 915 | $active_gateways = array(); |
| 916 | $gateways = WC()->payment_gateways->payment_gateways(); |
| 917 | foreach ( $gateways as $id => $gateway ) { |
| 918 | if ( isset( $gateway->enabled ) && 'yes' === $gateway->enabled ) { |
| 919 | $active_gateways[ $id ] = array( |
| 920 | 'title' => $gateway->title, |
| 921 | 'supports' => $gateway->supports, |
| 922 | ); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | return $active_gateways; |
| 927 | } |
| 928 | |
| 929 | |
| 930 | /** |
| 931 | * Get a list of all active shipping methods. |
| 932 | * |
| 933 | * @return array |
| 934 | */ |
| 935 | private static function get_active_shipping_methods() { |
| 936 | $active_methods = array(); |
| 937 | $shipping_methods = WC()->shipping()->get_shipping_methods(); |
| 938 | foreach ( $shipping_methods as $id => $shipping_method ) { |
| 939 | if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) { |
| 940 | $active_methods[ $id ] = array( |
| 941 | 'title' => $shipping_method->title, |
| 942 | 'tax_status' => $shipping_method->tax_status, |
| 943 | ); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | return $active_methods; |
| 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Get an array of slugs for WC features that are enabled on the site. |
| 952 | * |
| 953 | * @return string[] |
| 954 | */ |
| 955 | private static function get_enabled_features() { |
| 956 | $all_features = FeaturesUtil::get_features( true, true ); |
| 957 | $enabled_features = array_filter( |
| 958 | $all_features, |
| 959 | function ( $feature ) { |
| 960 | return $feature['is_enabled']; |
| 961 | } |
| 962 | ); |
| 963 | |
| 964 | return array_keys( $enabled_features ); |
| 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Get all options starting with woocommerce_ prefix. |
| 969 | * |
| 970 | * @return array |
| 971 | */ |
| 972 | private static function get_all_woocommerce_options_values() { |
| 973 | return array( |
| 974 | 'version' => WC()->version, |
| 975 | 'currency' => get_woocommerce_currency(), |
| 976 | 'base_location' => WC()->countries->get_base_country(), |
| 977 | 'base_state' => WC()->countries->get_base_state(), |
| 978 | 'base_postcode' => WC()->countries->get_base_postcode(), |
| 979 | 'selling_locations' => WC()->countries->get_allowed_countries(), |
| 980 | 'api_enabled' => get_option( 'woocommerce_api_enabled', 'no' ), |
| 981 | 'weight_unit' => get_option( 'woocommerce_weight_unit' ), |
| 982 | 'dimension_unit' => get_option( 'woocommerce_dimension_unit' ), |
| 983 | 'download_method' => get_option( 'woocommerce_file_download_method' ), |
| 984 | 'download_require_login' => get_option( 'woocommerce_downloads_require_login' ), |
| 985 | 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ), |
| 986 | 'coupons_enabled' => get_option( 'woocommerce_enable_coupons' ), |
| 987 | 'guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ), |
| 988 | 'delayed_account_creation' => get_option( 'woocommerce_enable_delayed_account_creation' ), |
| 989 | 'checkout_login_reminder' => get_option( 'woocommerce_enable_checkout_login_reminder' ), |
| 990 | 'secure_checkout' => get_option( 'woocommerce_force_ssl_checkout' ), |
| 991 | 'enable_signup_and_login_from_checkout' => get_option( 'woocommerce_enable_signup_and_login_from_checkout' ), |
| 992 | 'enable_myaccount_registration' => get_option( 'woocommerce_enable_myaccount_registration' ), |
| 993 | 'registration_generate_username' => get_option( 'woocommerce_registration_generate_username' ), |
| 994 | 'registration_generate_password' => get_option( 'woocommerce_registration_generate_password' ), |
| 995 | 'hpos_sync_enabled' => get_option( 'woocommerce_custom_orders_table_data_sync_enabled' ), |
| 996 | 'hpos_cot_authoritative' => get_option( 'woocommerce_custom_orders_table_enabled' ), |
| 997 | 'hpos_transactions_enabled' => get_option( 'woocommerce_use_db_transactions_for_custom_orders_table_data_sync' ), |
| 998 | 'hpos_transactions_level' => get_option( 'woocommerce_db_transactions_isolation_level_for_custom_orders_table_data_sync' ), |
| 999 | 'show_marketplace_suggestions' => get_option( 'woocommerce_show_marketplace_suggestions' ), |
| 1000 | 'admin_install_timestamp' => get_option( 'woocommerce_admin_install_timestamp' ), |
| 1001 | ); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Look for any template override and return filenames. |
| 1006 | * |
| 1007 | * @return array |
| 1008 | */ |
| 1009 | private static function get_all_template_overrides() { |
| 1010 | $override_data = array(); |
| 1011 | /** |
| 1012 | * Filter the paths to scan for template overrides. |
| 1013 | * |
| 1014 | * @since 2.3.0 |
| 1015 | */ |
| 1016 | $template_paths = apply_filters( 'woocommerce_template_overrides_scan_paths', array( 'WooCommerce' => WC()->plugin_path() . '/templates/' ) ); |
| 1017 | $scanned_files = array(); |
| 1018 | |
| 1019 | require_once WC()->plugin_path() . '/includes/admin/class-wc-admin-status.php'; |
| 1020 | |
| 1021 | foreach ( $template_paths as $plugin_name => $template_path ) { |
| 1022 | $scanned_files[ $plugin_name ] = WC_Admin_Status::scan_template_files( $template_path ); |
| 1023 | } |
| 1024 | |
| 1025 | foreach ( $scanned_files as $plugin_name => $files ) { |
| 1026 | foreach ( $files as $file ) { |
| 1027 | if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) { |
| 1028 | $theme_file = get_stylesheet_directory() . '/' . $file; |
| 1029 | } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) { |
| 1030 | $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file; |
| 1031 | } elseif ( file_exists( get_template_directory() . '/' . $file ) ) { |
| 1032 | $theme_file = get_template_directory() . '/' . $file; |
| 1033 | } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) { |
| 1034 | $theme_file = get_template_directory() . '/' . WC()->template_path() . $file; |
| 1035 | } else { |
| 1036 | $theme_file = false; |
| 1037 | } |
| 1038 | |
| 1039 | if ( false !== $theme_file ) { |
| 1040 | $override_data[] = basename( $theme_file ); |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | return $override_data; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * Search a specific post for text content. |
| 1049 | * |
| 1050 | * @param integer $post_id The id of the post to search. |
| 1051 | * @param string $text The text to search for. |
| 1052 | * @return string 'Yes' if post contains $text (otherwise 'No'). |
| 1053 | */ |
| 1054 | public static function post_contains_text( $post_id, $text ) { |
| 1055 | global $wpdb; |
| 1056 | |
| 1057 | // Search for the text anywhere in the post. |
| 1058 | $wildcarded = "%{$text}%"; |
| 1059 | |
| 1060 | $result = $wpdb->get_var( |
| 1061 | $wpdb->prepare( |
| 1062 | " |
| 1063 | SELECT COUNT( * ) FROM {$wpdb->prefix}posts |
| 1064 | WHERE ID=%d |
| 1065 | AND {$wpdb->prefix}posts.post_content LIKE %s |
| 1066 | ", |
| 1067 | array( $post_id, $wildcarded ) |
| 1068 | ) |
| 1069 | ); |
| 1070 | |
| 1071 | return ( '0' !== $result ) ? 'Yes' : 'No'; |
| 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | /** |
| 1076 | * Get tracker data for a specific block type on a woocommerce page. |
| 1077 | * |
| 1078 | * @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`. |
| 1079 | * @param string $woo_page_name The woo page to search, e.g. `cart`. |
| 1080 | * @return array Associative array of tracker data with keys: |
| 1081 | * - page_contains_block |
| 1082 | * - block_attributes |
| 1083 | */ |
| 1084 | public static function get_block_tracker_data( $block_name, $woo_page_name ) { |
| 1085 | $blocks = WC_Blocks_Utils::get_blocks_from_page( $block_name, $woo_page_name ); |
| 1086 | |
| 1087 | $block_present = false; |
| 1088 | $attributes = array(); |
| 1089 | if ( $blocks && count( $blocks ) ) { |
| 1090 | // Return any customised attributes from the first block. |
| 1091 | $block_present = true; |
| 1092 | $attributes = $blocks[0]['attrs']; |
| 1093 | } |
| 1094 | |
| 1095 | return array( |
| 1096 | 'page_contains_block' => $block_present ? 'Yes' : 'No', |
| 1097 | 'block_attributes' => $attributes, |
| 1098 | ); |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * Get tracker data for a pickup location method. |
| 1103 | * |
| 1104 | * @return array Associative array of tracker data with keys: |
| 1105 | * - pickup_location_enabled |
| 1106 | * - pickup_locations_count |
| 1107 | */ |
| 1108 | public static function get_pickup_location_data() { |
| 1109 | $pickup_location_enabled = false; |
| 1110 | $pickup_location_pickup_locations = get_option( 'pickup_location_pickup_locations', array() ); |
| 1111 | $pickup_locations_count = is_countable( $pickup_location_pickup_locations ) ? count( $pickup_location_pickup_locations ) : 0; |
| 1112 | |
| 1113 | // Get the available shipping methods. |
| 1114 | $shipping_methods = WC()->shipping()->get_shipping_methods(); |
| 1115 | |
| 1116 | // Check if the desired shipping method is enabled. |
| 1117 | if ( isset( $shipping_methods['pickup_location'] ) && $shipping_methods['pickup_location']->is_enabled() ) { |
| 1118 | $pickup_location_enabled = true; |
| 1119 | } |
| 1120 | |
| 1121 | return array( |
| 1122 | 'pickup_location_enabled' => $pickup_location_enabled, |
| 1123 | 'pickup_locations_count' => $pickup_locations_count, |
| 1124 | ); |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * Get tracker data for additional fields on the checkout page. |
| 1129 | * |
| 1130 | * @return array Array of fields count and names. |
| 1131 | */ |
| 1132 | public static function get_checkout_additional_fields_data() { |
| 1133 | $additional_fields_controller = Package::container()->get( CheckoutFields::class ); |
| 1134 | |
| 1135 | return array( |
| 1136 | 'fields_count' => count( $additional_fields_controller->get_additional_fields() ), |
| 1137 | 'fields_names' => array_keys( $additional_fields_controller->get_additional_fields() ), |
| 1138 | ); |
| 1139 | } |
| 1140 | /** |
| 1141 | * Get info about the cart & checkout pages. |
| 1142 | * |
| 1143 | * @return array |
| 1144 | */ |
| 1145 | public static function get_cart_checkout_info() { |
| 1146 | $cart_page_id = wc_get_page_id( 'cart' ); |
| 1147 | $checkout_page_id = wc_get_page_id( 'checkout' ); |
| 1148 | |
| 1149 | $cart_block_data = self::get_block_tracker_data( 'woocommerce/cart', 'cart' ); |
| 1150 | $checkout_block_data = self::get_block_tracker_data( 'woocommerce/checkout', 'checkout' ); |
| 1151 | |
| 1152 | $pickup_location_data = self::get_pickup_location_data(); |
| 1153 | |
| 1154 | $additional_fields_data = self::get_checkout_additional_fields_data(); |
| 1155 | |
| 1156 | return array( |
| 1157 | 'cart_page_contains_cart_shortcode' => self::post_contains_text( |
| 1158 | $cart_page_id, |
| 1159 | '[woocommerce_cart]' |
| 1160 | ), |
| 1161 | 'checkout_page_contains_checkout_shortcode' => self::post_contains_text( |
| 1162 | $checkout_page_id, |
| 1163 | '[woocommerce_checkout]' |
| 1164 | ), |
| 1165 | |
| 1166 | 'cart_page_contains_cart_block' => $cart_block_data['page_contains_block'], |
| 1167 | 'cart_block_attributes' => $cart_block_data['block_attributes'], |
| 1168 | 'checkout_page_contains_checkout_block' => $checkout_block_data['page_contains_block'], |
| 1169 | 'checkout_block_attributes' => $checkout_block_data['block_attributes'], |
| 1170 | 'pickup_location' => $pickup_location_data, |
| 1171 | 'additional_fields' => $additional_fields_data, |
| 1172 | ); |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * Get info about the Mini Cart Block. |
| 1177 | * |
| 1178 | * @return array |
| 1179 | */ |
| 1180 | private static function get_mini_cart_info() { |
| 1181 | $mini_cart_block_name = 'woocommerce/mini-cart'; |
| 1182 | $mini_cart_block_data = wc_current_theme_is_fse_theme() ? BlocksUtil::get_block_from_template_part( $mini_cart_block_name, 'header' ) : BlocksUtil::get_blocks_from_widget_area( $mini_cart_block_name ); |
| 1183 | return array( |
| 1184 | 'mini_cart_used' => empty( $mini_cart_block_data[0] ) ? 'No' : 'Yes', |
| 1185 | 'mini_cart_block_attributes' => empty( $mini_cart_block_data[0] ) ? array() : $mini_cart_block_data[0]['attrs'], |
| 1186 | ); |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * Get info about WooCommerce Mobile App usage |
| 1191 | * |
| 1192 | * @return array |
| 1193 | */ |
| 1194 | public static function get_woocommerce_mobile_usage() { |
| 1195 | return get_option( 'woocommerce_mobile_app_usage' ); |
| 1196 | } |
| 1197 | |
| 1198 | /** |
| 1199 | * Map legacy order meta keys to a column name. |
| 1200 | * |
| 1201 | * @param string $meta_key Legacy meta key name. |
| 1202 | * @return string Mapped column name. |
| 1203 | */ |
| 1204 | private static function map_legacy_meta_key_name( $meta_key ) { |
| 1205 | switch ( $meta_key ) { |
| 1206 | case '_order_currency': |
| 1207 | return 'currency'; |
| 1208 | case '_order_total': |
| 1209 | return 'total_amount'; |
| 1210 | case '_payment_method': |
| 1211 | return 'payment_method'; |
| 1212 | case '_payment_method_title': |
| 1213 | return 'payment_method_title'; |
| 1214 | case '_recorded_sales': |
| 1215 | return 'recorded_sales'; |
| 1216 | case '_order_version': |
| 1217 | return 'woocommerce_version'; |
| 1218 | default: |
| 1219 | return $meta_key; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * Fetch main order data. |
| 1225 | * |
| 1226 | * @param string $sort_order Date sort order (ASC or DESC). |
| 1227 | * @param integer $limit Limit the amount of orders to return (default 20). |
| 1228 | * @return array Found orders indexed by ID. |
| 1229 | */ |
| 1230 | private static function get_order_data( $sort_order = 'ASC', $limit = 20 ) { |
| 1231 | global $wpdb; |
| 1232 | |
| 1233 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 1234 | $order_table_name = OrdersTableDataStore::get_orders_table_name(); |
| 1235 | |
| 1236 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1237 | $orders = $wpdb->get_results( |
| 1238 | "SELECT |
| 1239 | id, |
| 1240 | row_number() OVER (ORDER BY date_created_gmt) AS order_rank, |
| 1241 | date_created_gmt AS order_created_at, |
| 1242 | date_updated_gmt AS order_updated_at, |
| 1243 | currency, |
| 1244 | total_amount, |
| 1245 | payment_method, |
| 1246 | payment_method_title |
| 1247 | FROM $order_table_name |
| 1248 | WHERE |
| 1249 | type = 'shop_order' |
| 1250 | AND status IN ('wc-completed', 'wc-refunded') |
| 1251 | ORDER BY date_created_gmt $sort_order |
| 1252 | LIMIT $limit;", |
| 1253 | ARRAY_A |
| 1254 | ); |
| 1255 | // phpcs:enable |
| 1256 | } else { |
| 1257 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1258 | $orders = $wpdb->get_results( |
| 1259 | "SELECT |
| 1260 | ID AS id, |
| 1261 | row_number() OVER (ORDER BY post_date_gmt) AS order_rank, |
| 1262 | post_date_gmt AS order_created_at, |
| 1263 | post_modified_gmt AS order_updated_at |
| 1264 | FROM $wpdb->posts |
| 1265 | WHERE |
| 1266 | post_type = 'shop_order' |
| 1267 | AND post_status IN ('wc-completed', 'wc-refunded') |
| 1268 | ORDER BY post_date_gmt $sort_order |
| 1269 | LIMIT $limit;", |
| 1270 | ARRAY_A |
| 1271 | ); |
| 1272 | // phpcs:enable |
| 1273 | } |
| 1274 | |
| 1275 | return array_column( $orders, null, 'id' ); |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * Fetch additional data for a specific set of orders. |
| 1280 | * |
| 1281 | * @param array $order_ids List of order ID's to fetch data for. |
| 1282 | * @return array Additional data, indexed by order ID. |
| 1283 | */ |
| 1284 | private static function get_additional_order_data( $order_ids ) { |
| 1285 | global $wpdb; |
| 1286 | |
| 1287 | if ( empty( $order_ids ) || ! is_array( $order_ids ) ) { |
| 1288 | return array(); |
| 1289 | } |
| 1290 | $joined_ids = implode( ',', $order_ids ); |
| 1291 | $additional_data = array(); |
| 1292 | |
| 1293 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 1294 | $op_table_name = OrdersTableDataStore::get_operational_data_table_name(); |
| 1295 | |
| 1296 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1297 | $data = $wpdb->get_results( |
| 1298 | "SELECT order_id, woocommerce_version, recorded_sales |
| 1299 | FROM $op_table_name |
| 1300 | WHERE order_id IN ($joined_ids)", |
| 1301 | ARRAY_A |
| 1302 | ); |
| 1303 | // phpcs:enable |
| 1304 | |
| 1305 | foreach ( $data as $row ) { |
| 1306 | $additional_data[ $row['order_id'] ] = array( |
| 1307 | 'woocommerce_version' => $row['woocommerce_version'], |
| 1308 | 'recorded_sales' => $row['recorded_sales'] ? 'yes' : 'no', |
| 1309 | ); |
| 1310 | } |
| 1311 | } else { |
| 1312 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1313 | $data = $wpdb->get_results( |
| 1314 | "SELECT post_id AS order_id, meta_key, meta_value |
| 1315 | FROM $wpdb->postmeta |
| 1316 | WHERE |
| 1317 | meta_key IN ( |
| 1318 | '_order_currency', |
| 1319 | '_order_total', |
| 1320 | '_payment_method', |
| 1321 | '_payment_method_title', |
| 1322 | '_recorded_sales', |
| 1323 | '_order_version' |
| 1324 | ) |
| 1325 | AND post_id IN ($joined_ids) |
| 1326 | ", |
| 1327 | ARRAY_A |
| 1328 | ); |
| 1329 | // phpcs:enable |
| 1330 | |
| 1331 | foreach ( $data as $row ) { |
| 1332 | $meta_key = self::map_legacy_meta_key_name( $row['meta_key'] ); |
| 1333 | $additional_data[ $row['order_id'] ][ $meta_key ] = $row['meta_value']; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | return $additional_data; |
| 1338 | } |
| 1339 | |
| 1340 | /** |
| 1341 | * Fetch refund data for a specific set of orders. |
| 1342 | * |
| 1343 | * @param array $order_ids List of order ID's to fetch data for. |
| 1344 | * @return array Refund data, indexed by order ID. |
| 1345 | */ |
| 1346 | private static function get_refund_order_data( $order_ids ) { |
| 1347 | global $wpdb; |
| 1348 | |
| 1349 | if ( empty( $order_ids ) || ! is_array( $order_ids ) ) { |
| 1350 | return array(); |
| 1351 | } |
| 1352 | $joined_ids = implode( ',', $order_ids ); |
| 1353 | $refund_data = array(); |
| 1354 | |
| 1355 | if ( OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 1356 | $order_table_name = OrdersTableDataStore::get_orders_table_name(); |
| 1357 | |
| 1358 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1359 | $data = $wpdb->get_results( |
| 1360 | "SELECT |
| 1361 | parent_order_id AS order_id, |
| 1362 | SUM(total_amount) AS refund_amount |
| 1363 | FROM $order_table_name |
| 1364 | WHERE |
| 1365 | type = 'shop_order_refund' |
| 1366 | AND status = 'wc-completed' |
| 1367 | AND parent_order_id IN ($joined_ids) |
| 1368 | GROUP BY parent_order_id", |
| 1369 | ARRAY_A |
| 1370 | ); |
| 1371 | // phpcs:enable |
| 1372 | } else { |
| 1373 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1374 | $data = $wpdb->get_results( |
| 1375 | "SELECT |
| 1376 | refunds.post_parent AS order_id, |
| 1377 | SUM(amount.meta_value) AS refund_amount |
| 1378 | FROM $wpdb->posts AS refunds |
| 1379 | LEFT JOIN $wpdb->postmeta AS amount ON amount.post_id = refunds.ID AND amount.meta_key = '_order_total' |
| 1380 | WHERE |
| 1381 | refunds.post_type = 'shop_order_refund' |
| 1382 | AND refunds.post_status = 'wc-completed' |
| 1383 | AND refunds.post_parent IN ($joined_ids) |
| 1384 | GROUP BY refunds.post_parent", |
| 1385 | ARRAY_A |
| 1386 | ); |
| 1387 | // phpcs:enable |
| 1388 | } |
| 1389 | |
| 1390 | foreach ( $data as $row ) { |
| 1391 | $refund_data[ $row['order_id'] ] = array( |
| 1392 | 'refund_amount' => $row['refund_amount'], |
| 1393 | ); |
| 1394 | } |
| 1395 | |
| 1396 | return $refund_data; |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * Get a snapshot of the first 20 orders and the last 20 orders. |
| 1401 | * |
| 1402 | * @return array |
| 1403 | */ |
| 1404 | private static function get_order_snapshot() { |
| 1405 | $first_20 = self::get_order_data( 'ASC', 20 ); |
| 1406 | $last_20 = self::get_order_data( 'DESC', 20 ); |
| 1407 | $order_ids = array_unique( array_merge( array_keys( $first_20 ), array_keys( $last_20 ) ) ); |
| 1408 | |
| 1409 | foreach ( self::get_additional_order_data( $order_ids ) as $order_id => $data ) { |
| 1410 | if ( isset( $first_20[ $order_id ] ) ) { |
| 1411 | $first_20[ $order_id ] = array_merge( $first_20[ $order_id ], $data ); |
| 1412 | } |
| 1413 | if ( isset( $last_20[ $order_id ] ) ) { |
| 1414 | $last_20[ $order_id ] = array_merge( $last_20[ $order_id ], $data ); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | foreach ( self::get_refund_order_data( $order_ids ) as $order_id => $data ) { |
| 1419 | if ( isset( $first_20[ $order_id ] ) ) { |
| 1420 | $first_20[ $order_id ] = array_merge( $first_20[ $order_id ], $data ); |
| 1421 | } |
| 1422 | if ( isset( $last_20[ $order_id ] ) ) { |
| 1423 | $last_20[ $order_id ] = array_merge( $last_20[ $order_id ], $data ); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | return array( |
| 1428 | 'first_20_orders' => $first_20, |
| 1429 | 'last_20_orders' => $last_20, |
| 1430 | ); |
| 1431 | } |
| 1432 | } |
| 1433 |