valid_types(), true ) ) { $type = $configured; } else { $type = $this->detect(); } /** * Filters the resolved website type. * * @param string $type The resolved type. * @param SiteTypeDetector $detector The detector instance. */ return (string) apply_filters( 'aicommerce_site_type', $type, $this ); } /** * Auto-detect the site type from content signals (cached). * * @return string */ public function detect(): string { $cached = get_transient( self::CACHE_KEY ); if ( is_string( $cached ) && '' !== $cached ) { return $cached; } $type = $this->compute(); set_transient( self::CACHE_KEY, $type, DAY_IN_SECONDS ); return $type; } /** * Compute the site type from signals. * * @return string */ private function compute(): string { // A configured business type is a strong signal of a local business. $business_type = (string) get_option( 'aicommerce_business_type', '' ); $has_business = '' !== trim( $business_type ); // Ecommerce: WooCommerce active with at least one published product. if ( function_exists( 'aicommerce_has_woocommerce' ) && aicommerce_has_woocommerce() ) { $product_counts = wp_count_posts( 'product' ); if ( $product_counts && (int) $product_counts->publish > 0 ) { return self::TYPE_ECOMMERCE; } } if ( $has_business ) { return self::TYPE_LOCAL_BUSINESS; } // Blog/publisher: substantially more posts than pages. $posts = wp_count_posts( 'post' ); $pages = wp_count_posts( 'page' ); $post_count = $posts ? (int) $posts->publish : 0; $page_count = $pages ? (int) $pages->publish : 0; if ( $post_count >= 5 && $post_count > $page_count ) { return self::TYPE_BLOG; } return self::TYPE_ORGANIZATION; } /** * Get the configured schema.org LocalBusiness type (or subtype). * * @return string A schema.org type such as `LocalBusiness`, `Restaurant`, `Store`. */ public function get_local_business_type(): string { $type = (string) get_option( 'aicommerce_business_type', '' ); $type = '' !== trim( $type ) ? trim( $type ) : 'LocalBusiness'; /** * Filters the schema.org LocalBusiness type used for site markup. * * @param string $type The LocalBusiness type or subtype. */ return (string) apply_filters( 'aicommerce_local_business_type', $type ); } /** * Whether the site is an ecommerce store. * * @return bool */ public function is_ecommerce(): bool { return self::TYPE_ECOMMERCE === $this->get_type(); } /** * Clear the cached detection (call when relevant settings/content change). * * @return void */ public function clear_cache(): void { delete_transient( self::CACHE_KEY ); } /** * List of valid explicit types. * * @return array */ public function valid_types(): array { return array( self::TYPE_ECOMMERCE, self::TYPE_LOCAL_BUSINESS, self::TYPE_BLOG, self::TYPE_ORGANIZATION, ); } }