*/ public function detect(): array { $base_location = $this->base_location(); $checkout_id = $this->wc_page_id( 'checkout' ); $terms = $this->terms_page_resolution(); $terms_id = $terms['canonical_id']; $terms_url = ''; if ( $terms_id > 0 && function_exists( 'get_permalink' ) ) { $permalink = get_permalink( $terms_id ); $terms_url = false === $permalink ? '' : (string) $permalink; } return array( 'wordpress_version' => $this->wordpress_version(), 'woocommerce_version' => defined( 'WC_VERSION' ) ? (string) WC_VERSION : '', 'php_version' => PHP_VERSION, 'site_locale' => $this->site_locale(), 'currency' => function_exists( 'get_woocommerce_currency' ) ? (string) get_woocommerce_currency() : '', 'base_country' => $base_location['country'], 'base_state' => $base_location['state'], 'hpos_enabled' => $this->hpos_enabled(), 'checkout_page_id' => $checkout_id, 'checkout_mode' => $this->checkout_mode( $checkout_id ), 'terms_page_id' => $terms_id, 'terms_page_option_id' => $terms['option_id'], 'terms_page_wc_id' => $terms['wc_id'], 'terms_page_source' => $terms['source'], 'terms_page_id_mismatch' => $terms['option_id'] !== $terms['wc_id'], 'terms_page_filter_hook' => 'woocommerce_get_terms_page_id', 'terms_page_filter_active' => function_exists( 'has_filter' ) && false !== has_filter( 'woocommerce_get_terms_page_id' ), 'terms_page_checked_at' => gmdate( 'c' ), 'terms_page_cache_state' => 'fresh_request', 'terms_page_status' => $terms_id > 0 && function_exists( 'get_post_status' ) ? (string) get_post_status( $terms_id ) : '', 'terms_page_url_present' => '' !== $terms_url, 'terms_page_title' => $terms_id > 0 && function_exists( 'get_the_title' ) ? (string) get_the_title( $terms_id ) : '', 'terms_page_url' => $terms_url, 'published_pages' => $this->published_pages(), 'order_statuses' => $this->order_statuses(), 'payment_gateways' => $this->payment_gateways(), 'shipping_methods' => $this->shipping_methods(), 'customer_emails' => $this->customer_emails(), 'pro_active' => defined( 'LMCC_PRO_VERSION' ), 'pro_version' => defined( 'LMCC_PRO_VERSION' ) ? (string) LMCC_PRO_VERSION : '', 'product_scope' => $this->product_scope(), 'store_address' => $this->option_text( 'woocommerce_store_address' ), 'store_city' => $this->option_text( 'woocommerce_store_city' ), 'store_postcode' => $this->option_text( 'woocommerce_store_postcode' ), 'admin_email' => function_exists( 'get_option' ) ? (string) get_option( 'admin_email', '' ) : '', 'site_url' => function_exists( 'home_url' ) ? (string) home_url( '/' ) : '', ); } /** @return array{country:string,state:string} */ private function base_location(): array { if ( function_exists( 'wc_get_base_location' ) ) { $location = wc_get_base_location(); if ( is_array( $location ) ) { return array( 'country' => isset( $location['country'] ) ? (string) $location['country'] : '', 'state' => isset( $location['state'] ) ? (string) $location['state'] : '', ); } } $raw = function_exists( 'get_option' ) ? (string) get_option( 'woocommerce_default_country', '' ) : ''; $parts = explode( ':', $raw, 2 ); return array( 'country' => $parts[0] ?? '', 'state' => $parts[1] ?? '', ); } private function wordpress_version(): string { global $wp_version; return isset( $wp_version ) ? (string) $wp_version : ''; } private function site_locale(): string { if ( function_exists( 'determine_locale' ) ) { return (string) determine_locale(); } if ( function_exists( 'get_locale' ) ) { return (string) get_locale(); } return ''; } private function hpos_enabled(): bool { $class_name = 'Automattic\\WooCommerce\\Utilities\\OrderUtil'; return class_exists( $class_name ) && method_exists( $class_name, 'custom_orders_table_usage_is_enabled' ) ? (bool) $class_name::custom_orders_table_usage_is_enabled() : false; } private function wc_page_id( string $page ): int { if ( function_exists( 'wc_get_page_id' ) ) { return max( 0, (int) wc_get_page_id( $page ) ); } return 0; } /** @return array{canonical_id:int,option_id:int,wc_id:int,source:string} */ private function terms_page_resolution(): array { $option_id = function_exists( 'get_option' ) ? max( 0, (int) get_option( 'woocommerce_terms_page_id', 0 ) ) : 0; $wc_id = function_exists( 'wc_get_page_id' ) ? max( 0, (int) wc_get_page_id( 'terms' ) ) : $option_id; return array( 'canonical_id' => $wc_id, 'option_id' => $option_id, 'wc_id' => $wc_id, 'source' => function_exists( 'wc_get_page_id' ) ? 'wc_get_page_id' : 'woocommerce_terms_page_id', ); } /** @return array */ private function published_pages(): array { if ( ! function_exists( 'get_pages' ) ) { return array(); } $pages = get_pages( array( 'post_status' => 'publish', 'sort_column' => 'post_title', 'sort_order' => 'ASC', ) ); $results = array(); foreach ( is_array( $pages ) ? $pages : array() as $page ) { if ( ! is_object( $page ) || empty( $page->ID ) ) { continue; } $results[] = array( 'id' => (int) $page->ID, 'title' => isset( $page->post_title ) ? wp_strip_all_tags( (string) $page->post_title ) : '#' . (int) $page->ID, ); } return $results; } private function checkout_mode( int $checkout_id ): string { if ( $checkout_id <= 0 ) { return 'missing'; } if ( function_exists( 'get_post_field' ) && function_exists( 'has_block' ) ) { $content = (string) get_post_field( 'post_content', $checkout_id ); if ( '' !== $content && has_block( 'woocommerce/checkout', $content ) ) { return 'blocks'; } } return 'classic'; } /** @return array */ private function payment_gateways(): array { $results = array(); try { if ( ! function_exists( 'WC' ) || ! WC() || ! method_exists( WC(), 'payment_gateways' ) ) { return $results; } $manager = WC()->payment_gateways(); if ( ! is_object( $manager ) || ! method_exists( $manager, 'payment_gateways' ) ) { return $results; } foreach ( $manager->payment_gateways() as $gateway ) { if ( ! is_object( $gateway ) ) { continue; } $id = isset( $gateway->id ) ? (string) $gateway->id : ''; $title = method_exists( $gateway, 'get_title' ) ? (string) $gateway->get_title() : $id; $enabled = isset( $gateway->enabled ) && 'yes' === (string) $gateway->enabled; $results[] = array( 'id' => $id, 'title' => $title, 'enabled' => $enabled, ); } } catch ( \Throwable $exception ) { unset( $exception ); } return $results; } /** @return array */ private function shipping_methods(): array { $results = array(); $class = 'WC_Shipping_Zones'; if ( ! class_exists( $class ) ) { return $results; } try { $zones = $class::get_zones(); $zones = is_array( $zones ) ? $zones : array(); $zones[] = array( 'zone_id' => 0 ); foreach ( $zones as $zone_data ) { $zone_id = isset( $zone_data['zone_id'] ) ? (int) $zone_data['zone_id'] : 0; $zone = new \WC_Shipping_Zone( $zone_id ); foreach ( $zone->get_shipping_methods( false ) as $method ) { $id = isset( $method->id ) ? (string) $method->id : ''; $title = method_exists( $method, 'get_title' ) ? (string) $method->get_title() : $id; $enabled = method_exists( $method, 'is_enabled' ) ? (bool) $method->is_enabled() : ( ! isset( $method->enabled ) || 'yes' === (string) $method->enabled ); $results[] = array( 'id' => $id, 'title' => $title, 'enabled' => $enabled, ); } } } catch ( \Throwable $exception ) { unset( $exception ); } return $this->unique_by_id( $results ); } /** @return array */ private function customer_emails(): array { $results = array(); try { if ( ! function_exists( 'WC' ) || ! WC() || ! method_exists( WC(), 'mailer' ) ) { return $results; } $mailer = WC()->mailer(); if ( ! is_object( $mailer ) || ! method_exists( $mailer, 'get_emails' ) ) { return $results; } foreach ( $mailer->get_emails() as $email ) { if ( ! is_object( $email ) || ! isset( $email->id ) ) { continue; } $id = (string) $email->id; if ( 0 !== strpos( $id, 'customer_' ) ) { continue; } $title = method_exists( $email, 'get_title' ) ? (string) $email->get_title() : $id; $enabled = method_exists( $email, 'is_enabled' ) ? (bool) $email->is_enabled() : true; $description = method_exists( $email, 'get_description' ) ? wp_strip_all_tags( (string) $email->get_description() ) : ''; $eligibility = WooEmailEligibility::inspect( $email ); $results[] = array( 'id' => $id, 'title' => $title, 'enabled' => $enabled, 'description' => $description, 'customer_facing' => $eligibility['customer_facing'], 'order_document_supported' => $eligibility['order_document_supported'], 'eligible' => $enabled && $eligibility['customer_facing'] && $eligibility['order_document_supported'], 'eligibility_reason' => $eligibility['reason'], ); } } catch ( \Throwable $exception ) { unset( $exception ); } return $results; } /** @return array Status slugs without the wc- prefix. */ private function order_statuses(): array { if ( ! function_exists( 'wc_get_order_statuses' ) ) { return array(); } $statuses = array(); foreach ( (array) wc_get_order_statuses() as $key => $label ) { $slug = 0 === strpos( (string) $key, 'wc-' ) ? substr( (string) $key, 3 ) : (string) $key; if ( '' !== $slug ) { $statuses[ $slug ] = wp_strip_all_tags( (string) $label ); } } return $statuses; } /** @return array{has_physical:bool,has_virtual:bool,has_downloadable:bool,detection_error:bool} */ private function product_scope(): array { $scope = array( 'has_physical' => false, 'has_virtual' => false, 'has_downloadable' => false, 'detection_error' => false, ); if ( ! function_exists( 'wc_get_products' ) ) { $scope['detection_error'] = true; return $scope; } try { $scope['has_virtual'] = ! empty( wc_get_products( array( 'limit' => 1, 'return' => 'ids', 'status' => 'publish', 'virtual' => true, ) ) ); $scope['has_downloadable'] = ! empty( wc_get_products( array( 'limit' => 1, 'return' => 'ids', 'status' => 'publish', 'downloadable' => true, ) ) ); $scope['has_physical'] = ! empty( wc_get_products( array( 'limit' => 1, 'return' => 'ids', 'status' => 'publish', 'virtual' => false, 'downloadable' => false, ) ) ); } catch ( \Throwable $exception ) { unset( $exception ); $scope['detection_error'] = true; } return $scope; } private function option_text( string $key ): string { return function_exists( 'get_option' ) ? (string) get_option( $key, '' ) : ''; } /** @param array $items */ private function unique_by_id( array $items ): array { $unique = array(); foreach ( $items as $item ) { $unique[ $item['id'] ] = $item; } return array_values( $unique ); } }