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