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