options_cache = $options_cache; } /** * Build the `contentStructure` block. * * @return array{ * canManage: bool, * canManageSettings: bool, * storeLanguage: string, * multilingual: string|null, * siteName: string, * generatorUrls: array, * settingsAdvancedUrl?: string, * reviewEnabled: bool, * entitlements: array{customFields: bool, additionalPrompts: array, faqModes: bool, taxonomyMaxSamples: int, lockedSourceRefs: array, titleKeywords: bool, seoTitleMax: int, seoDescMax: int, seoKeywordAi: bool, imageAltAttributes: bool, taxonomyCustomSection: bool, taxonomyCommonAttributes: bool, taxonomyProductDescriptions: bool, voiceSamples: bool, extraSlots: bool, webSources: bool, voiceLimit: int, storeSections: bool, catalogWatch: bool}, * currentUserId?: int, * structurePrefs: array, * structureSignatures: array>,order:array}> * } */ public function build(): array { $can_manage_settings = (bool) current_user_can( 'manage_aipt_settings' ); $policy = new EntitlementPolicy(); $seo_ceilings = $policy->seoMetaLengthCeilings(); $data = array( 'canManage' => (bool) current_user_can( 'manage_woocommerce' ), 'canManageSettings' => $can_manage_settings, 'storeLanguage' => (string) $this->options_cache->get( 'aipt_language', \AIPT\Core\Activator::get_default_value( 'language' ) ), 'multilingual' => self::detectMultilingual(), 'siteName' => (string) get_bloginfo( 'name' ), 'generatorUrls' => $this->buildGeneratorUrls(), 'reviewEnabled' => (bool) apply_filters( 'aipt_content_review_enabled_cache_only', false ), 'entitlements' => array( 'customFields' => (bool) apply_filters( 'aipt_custom_variables_can_edit', false ), 'additionalPrompts' => $this->buildAdditionalPromptEntitlements( $policy ), 'bannedPhrases' => $policy->bannedPhrasesAllowed(), 'faqModes' => $policy->faqAdvancedModeAllowed(), 'taxonomyMaxSamples' => $policy->taxonomyMaxSamples(), 'lockedSourceRefs' => $policy->lockedSourceRefs(), 'titleKeywords' => $policy->titleKeywordsAllowed(), 'seoTitleMax' => $seo_ceilings['title'], 'seoDescMax' => $seo_ceilings['description'], 'seoKeywordAi' => $policy->seoMetaKeywordAiAllowed(), 'imageAltAttributes' => $policy->imageAltAttributesAllowed(), 'taxonomyCustomSection' => $policy->taxonomyCustomPurposeAllowed(), 'taxonomyCommonAttributes' => $policy->taxonomyCommonAttributesAllowed(), 'taxonomyProductDescriptions' => $policy->taxonomyProductDescriptionsAllowed(), 'voiceSamples' => $policy->voiceSamplesAllowed(), 'extraSlots' => $policy->extraSlotsAllowed(), 'webSources' => $policy->webSourcesAllowed(), 'voiceLimit' => $policy->voiceLimit(), 'storeSections' => $policy->storeSectionsAllowed(), 'catalogWatch' => (bool) apply_filters( 'aipt_catalog_watch_enabled', false ), ), ); if ( $can_manage_settings ) { $data['settingsAdvancedUrl'] = admin_url( 'admin.php?page=aipt-settings&tab=studio' ); } $user_id = (int) get_current_user_id(); if ( $user_id > 0 ) { $data['currentUserId'] = $user_id; } $data['structurePrefs'] = ( new StructurePrefsStore() )->getAll( $user_id ); $data['structureSignatures'] = $this->buildStructureSignatures(); return $data; } /** * Every generator's StructureSignature: the opening tab (Content * structure vs. Generation) is decided on first paint from this, with * no flash while a GET structures request is in flight. * Built without the expensive one-pass counts scan — never more than * one assignment-rows query per generator. * * @return array> */ private function buildStructureSignatures(): array { $assignments = new AssignmentStore(); $builder = new StructureStateBuilder(); $signatures = array(); foreach ( Generators::all() as $generator ) { $rows = $assignments->listForGenerator( $generator ); $state = $builder->build( $generator, $rows, false ); $signatures[ $generator ] = StructureStateBuilder::toSignature( $state ); } return $signatures; } /** * Which multilingual plugin, if any, manages this site's own * per-product language. Detected directly rather than through a * capability or option, matching how the rest of the codebase already * tells these two plugins apart: a WPML (SitePress) install always * defines its version constant on load, and Polylang always registers * this language-lookup function once active. Neither plugin needs to be * installed for this check to be safe; it simply reports null. Public so * every admin page's localized data can carry the same answer. * * @return string|null 'wpml' | 'polylang' | null when neither is active. */ public static function detectMultilingual(): ?string { if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return 'wpml'; } if ( function_exists( 'pll_get_post_language' ) ) { return 'polylang'; } return null; } /** * @return array Generator key => admin.php URL. */ private function buildGeneratorUrls(): array { $urls = array(); foreach ( Generators::all() as $generator ) { $urls[ $generator ] = admin_url( 'admin.php?page=' . Generators::adminSlug( $generator ) ); } return $urls; } /** * Every generator's additional-instructions entitlement, decided by the * same `EntitlementPolicy::instructionsAllowed()` a run asks. * * @param EntitlementPolicy $policy Plan decisions. * @return array Generator key => additional-prompt entitlement. */ private function buildAdditionalPromptEntitlements( EntitlementPolicy $policy ): array { $entitlements = array(); foreach ( Generators::all() as $generator ) { $entitlements[ $generator ] = $policy->instructionsAllowed( $generator ); } return $entitlements; } }