few large files"); this class has no * REST concerns of its own. * * @package AIPT\Core\ContentStructure\Rest * @since 2.8.0 */ namespace AIPT\Core\ContentStructure\Rest; use AIPT\Api\ModelCatalog; use AIPT\Api\ProductEditor\ContentDefaultsService; use AIPT\Core\ContentStructure\Brand\BrandProfileSchema; use AIPT\Core\ContentStructure\Domain\DefinitionPrimitives; use AIPT\Core\ContentStructure\Domain\FieldCatalog; use AIPT\Core\ContentStructure\Domain\Generators; use AIPT\Core\ContentStructure\Domain\SectionCatalog; defined( 'ABSPATH' ) || exit; /** * Stateless builder for the content structure catalog payload. */ final class CatalogBuilder { /** * @param FieldCatalog $fields Field catalog. * @param bool $can_use Whether the current user holds the USE capability. * @param bool $can_manage Whether the current user holds the MANAGE capability. * @return array ContentStructureCatalog. */ public static function build( FieldCatalog $fields, bool $can_use, bool $can_manage ): array { $generators = array(); foreach ( Generators::all() as $key ) { $generators[] = array( 'key' => $key, 'label' => Generators::label( $key ), 'subjectType' => Generators::subjectType( $key ), 'appBarKey' => Generators::appBarKey( $key ), ); } $section_types = array(); foreach ( array( Generators::DESCRIPTION, Generators::TAXONOMY ) as $key ) { $section_types[ $key ] = array_values( SectionCatalog::typesFor( $key ) ); } return array( 'generators' => $generators, 'sectionTypes' => $section_types, 'fields' => array( 'product' => self::distinctFieldLabels( $fields->forScope( 'product' ) ), 'term' => $fields->forScope( 'term' ), 'image' => $fields->forScope( 'image' ), ), 'titlePartKeys' => SectionCatalog::titlePartKeys(), 'seoTokenKeys' => SectionCatalog::seoTokenKeys(), 'languages' => self::languages(), 'limits' => self::limits(), 'traits' => BrandProfileSchema::traits(), 'expressionLabels' => BrandProfileSchema::expressionLabels(), 'lockedClaims' => BrandProfileSchema::lockedClaims(), 'capabilities' => array( 'canUse' => $can_use, 'canManage' => $can_manage ), 'faqSchema' => array( 'enabled' => self::faqSchemaEnabled() ), 'vision' => array( 'available' => self::visionAvailable() ), ); } /** * The editor's product field list with labels it can tell apart: an * attribute or custom field named like a product field ("Brand" next to * WooCommerce Brands) says which kind it is. Display only: a run's prompt * and product tables keep each field's own label * ({@see FieldCatalog::label()}). * * @param array $fields Product scope fields. * @return array */ public static function distinctFieldLabels( array $fields ): array { $core_labels = array(); foreach ( $fields as $field ) { if ( 'core' === $field['group'] ) { $core_labels[ self::labelKey( $field['label'] ) ] = true; } } $distinct = array(); foreach ( $fields as $field ) { if ( 'core' !== $field['group'] && isset( $core_labels[ self::labelKey( $field['label'] ) ] ) ) { if ( 'custom' === $field['group'] ) { /* translators: %s: a custom field's own label, e.g. "Brand". */ $field['label'] = sprintf( __( '%s (custom field)', 'ai-product-tools' ), trim( $field['label'] ) ); } else { /* translators: %s: a product attribute's own label, e.g. "Brand". */ $field['label'] = sprintf( __( '%s (attribute)', 'ai-product-tools' ), trim( $field['label'] ) ); } } $distinct[] = $field; } return $distinct; } /** * A label as compared for sameness: trimmed, case-folded. * * @param string $label Field label. * @return string */ private static function labelKey( string $label ): string { $label = trim( $label ); return function_exists( 'mb_strtolower' ) ? mb_strtolower( $label ) : strtolower( $label ); } /** * @return array */ private static function languages(): array { $store_language = (string) get_option( 'aipt_language', '' ); $languages = array( array( 'code' => 'store', 'label' => '' !== $store_language ? sprintf( /* translators: %s: store language name. */ __( 'Store language (%s)', 'ai-product-tools' ), $store_language ) : __( 'Store language', 'ai-product-tools' ), ), ); foreach ( ContentDefaultsService::SUPPORTED_LANGUAGES as $name ) { $languages[] = array( 'code' => $name, 'label' => $name ); } return $languages; } /** * Flat numeric limits map from `DefinitionPrimitives`/`BrandProfileSchema` * constants, for the editor's inspector to enforce the same bounds the * server validates. * * @return array */ private static function limits(): array { return array( 'aiItemsMax' => DefinitionPrimitives::AI_ITEMS_MAX, 'aiItemsMin' => DefinitionPrimitives::AI_ITEMS_MIN, 'dataItemsMax' => DefinitionPrimitives::DATA_ITEMS_MAX, 'dataItemsMin' => DefinitionPrimitives::DATA_ITEMS_MIN, 'titlePartsMax' => DefinitionPrimitives::TITLE_PARTS_MAX, 'titlePartsMin' => DefinitionPrimitives::TITLE_PARTS_MIN, 'titleMaxCharsMin' => DefinitionPrimitives::TITLE_MAX_CHARS_MIN, 'titleMaxCharsMax' => DefinitionPrimitives::TITLE_MAX_CHARS_MAX, 'faqGroupCountMin' => DefinitionPrimitives::FAQ_GROUP_COUNT_MIN, 'faqGroupCountMax' => DefinitionPrimitives::FAQ_GROUP_COUNT_MAX, 'faqMaxQuestionsMin' => DefinitionPrimitives::FAQ_MAX_QUESTIONS_MIN, 'faqMaxQuestionsMax' => DefinitionPrimitives::FAQ_MAX_QUESTIONS_MAX, 'imageAltMaxCharsMin' => DefinitionPrimitives::IMAGE_ALT_MAX_CHARS_MIN, 'imageAltMaxCharsMax' => DefinitionPrimitives::IMAGE_ALT_MAX_CHARS_MAX, 'nameMaxLen' => DefinitionPrimitives::NAME_MAX_LEN, 'headingTextMaxLen' => DefinitionPrimitives::HEADING_TEXT_MAX_LEN, 'focusMaxLen' => DefinitionPrimitives::FOCUS_MAX_LEN, 'fixedTextMaxLen' => DefinitionPrimitives::FIXED_TEXT_MAX_LEN, 'fallbackTextMaxLen' => DefinitionPrimitives::FALLBACK_TEXT_MAX_LEN, 'instructionMaxLen' => DefinitionPrimitives::INSTRUCTION_MAX_LEN, 'ruleInstructionTextMaxLen' => DefinitionPrimitives::RULE_INSTRUCTION_TEXT_MAX_LEN, 'ruleInstructionsMax' => DefinitionPrimitives::RULE_INSTRUCTIONS_MAX, 'titlePartTextMaxLen' => DefinitionPrimitives::TITLE_PART_TEXT_MAX_LEN, 'sectionsMax' => DefinitionPrimitives::SECTIONS_MAX, 'bannedPhraseMaxLen' => DefinitionPrimitives::BANNED_PHRASE_MAX_LEN, 'bannedPhrasesMax' => DefinitionPrimitives::BANNED_PHRASES_MAX, 'allowedDomainsMax' => DefinitionPrimitives::ALLOWED_DOMAINS_MAX, 'sourcePolicyItemsMax' => DefinitionPrimitives::SOURCE_POLICY_ITEMS_MAX, 'productFieldsMax' => DefinitionPrimitives::PRODUCT_FIELDS_MAX, 'wordTargetMax' => DefinitionPrimitives::WORD_TARGET_MAX, 'hardWordLimitMax' => DefinitionPrimitives::HARD_WORD_LIMIT_MAX, 'partMaxCharsMax' => DefinitionPrimitives::PART_MAX_CHARS_MAX, 'tokenTextMaxLen' => DefinitionPrimitives::TOKEN_TEXT_MAX_LEN, 'seoTitleCharsMax' => DefinitionPrimitives::SEO_TITLE_CHARS_MAX, 'seoDescCharsMax' => DefinitionPrimitives::SEO_DESC_CHARS_MAX, 'faqAnswerTargetMax' => DefinitionPrimitives::FAQ_ANSWER_TARGET_MAX, 'faqAnswerHardMax' => DefinitionPrimitives::FAQ_ANSWER_HARD_MAX, 'groupTopicMaxLen' => DefinitionPrimitives::GROUP_TOPIC_MAX_LEN, 'brandDescriptionMaxLen' => BrandProfileSchema::DESCRIPTION_MAX_LEN, 'brandAudienceMaxLen' => BrandProfileSchema::AUDIENCE_MAX_LEN, 'writingRulesMax' => BrandProfileSchema::WRITING_RULES_MAX, 'writingRuleMaxLen' => BrandProfileSchema::WRITING_RULE_MAX_LEN, 'preferredTermsMax' => BrandProfileSchema::PREFERRED_TERMS_MAX, 'preferredTermMaxLen' => BrandProfileSchema::PREFERRED_TERM_MAX_LEN, 'avoidPhrasesMax' => BrandProfileSchema::AVOID_PHRASES_MAX, 'avoidPhraseMaxLen' => BrandProfileSchema::AVOID_PHRASE_MAX_LEN, 'approvedSamplesMax' => BrandProfileSchema::APPROVED_SAMPLES_MAX, 'approvedSampleMaxLen' => BrandProfileSchema::APPROVED_SAMPLE_MAX_LEN, 'claimRulesMax' => BrandProfileSchema::CLAIM_RULES_MAX, 'claimRuleMaxLen' => BrandProfileSchema::CLAIM_RULE_MAX_LEN, ); } /** * Whether the FAQPage schema switch (owned by FAQ display settings) * is currently enabled. * * @return bool */ private static function faqSchemaEnabled(): bool { if ( ! class_exists( '\AIPT\Api\BulkGenerator\FAQ\FAQDisplaySettings' ) ) { return false; } $settings = ( new \AIPT\Api\BulkGenerator\FAQ\FAQDisplaySettings() )->getAll(); return (bool) ( $settings['schema_enabled'] ?? false ); } /** * Whether the currently configured AI provider/model supports vision, * via {@see ModelCatalog::get_capabilities()}. * * @return bool */ private static function visionAvailable(): bool { $provider = (string) get_option( 'aipt_api_provider', '' ); $model = (string) get_option( 'aipt_model', '' ); if ( '' === $provider || ! class_exists( ModelCatalog::class ) ) { return false; } $model = ModelCatalog::normalize_model( $provider, $model ); $capabilities = ModelCatalog::get_capabilities( $provider, $model ); return (bool) ( $capabilities['vision'] ?? false ); } }