options = $container->get( Options::class ); add_action( 'rest_api_init', array( $this, 'routes' ) ); } public function routes(): void { register_rest_route( self::NAMESPACE, '/packs', array( 'methods' => 'GET', 'callback' => array( $this, 'catalog' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/packs/apply', array( 'methods' => 'POST', 'callback' => array( $this, 'apply' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), 'args' => array( 'pack' => array( 'type' => 'string', 'required' => true, 'enum' => Packs::ids(), ), 'region' => array( 'type' => 'string', 'required' => true, 'enum' => Regions::ids(), ), 'city' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'prompts' => array( 'type' => 'boolean' ), ), ) ); } public function catalog(): WP_REST_Response { $applied = get_option( self::OPTION ); return new WP_REST_Response( array( 'packs' => Packs::for_client(), 'regions' => Regions::for_client(), 'applied' => is_array( $applied ) ? $applied : null, ) ); } /** * Fill the blanks. Never argue with a decision the customer already made. */ public function apply( WP_REST_Request $request ): WP_REST_Response { $pack_id = (string) $request->get_param( 'pack' ); $region_id = (string) $request->get_param( 'region' ); $city = trim( (string) ( $request->get_param( 'city' ) ?: '' ) ); $want_prompts = null === $request->get_param( 'prompts' ) ? true : (bool) $request->get_param( 'prompts' ); $pack = Packs::all()[ $pack_id ]; $region = Regions::all()[ $region_id ]; $options = $this->options ?? \AiRankSuite\Kernel::service( Options::class ); $changed = array(); $kept = array(); /* ------------------------------------------------ schema + identity */ $schema = $options->get( 'schema', SchemaModule::defaults() ); $identity = array_merge( Identity::defaults(), (array) ( $schema['identity'] ?? array() ) ); if ( 'Organization' === (string) $schema['org_type'] || '' === (string) $schema['org_type'] ) { $schema['org_type'] = (string) $pack['type']; $changed[] = sprintf( /* translators: %s: schema.org type name. */ __( 'Organization type set to %s', 'ai-rank-suite' ), (string) $pack['type'] ); } else { $kept[] = sprintf( /* translators: %s: schema.org type name. */ __( 'Organization type left as %s', 'ai-rank-suite' ), (string) $schema['org_type'] ); } $added_subjects = $this->merge_list( $identity['knows_about'], (array) $pack['knows'] ); if ( $added_subjects > 0 ) { $changed[] = sprintf( /* translators: %d: number of subjects. */ _n( '%d subject added to knowsAbout', '%d subjects added to knowsAbout', $added_subjects, 'ai-rank-suite' ), $added_subjects ); } $added_areas = $this->merge_list( $identity['area_served'], (array) $region['countries'] ); if ( $added_areas > 0 ) { $changed[] = sprintf( /* translators: %d: number of countries. */ _n( '%d country added to areaServed', '%d countries added to areaServed', $added_areas, 'ai-rank-suite' ), $added_areas ); } $added_langs = $this->merge_list( $identity['languages'], (array) $region['languages'] ); if ( $added_langs > 0 ) { $changed[] = sprintf( /* translators: %d: number of languages. */ _n( '%d language declared', '%d languages declared', $added_langs, 'ai-rank-suite' ), $added_langs ); } $added_pay = $this->merge_list( $identity['payments'], (array) $region['payments'] ); if ( $added_pay > 0 ) { $changed[] = sprintf( /* translators: %d: number of payment methods. */ _n( '%d payment method listed', '%d payment methods listed', $added_pay, 'ai-rank-suite' ), $added_pay ); } if ( empty( array_filter( (array) $identity['currencies'] ) ) ) { $identity['currencies'] = array( (string) $region['currency'] ); $changed[] = sprintf( /* translators: %s: currency code. */ __( 'Currency set to %s', 'ai-rank-suite' ), (string) $region['currency'] ); } else { $kept[] = __( 'Currency left as configured', 'ai-rank-suite' ); } $schema['identity'] = $identity; $options->set( 'schema', $schema, SchemaModule::defaults() ); /* -------------------------------------------------------- local SEO */ $local = $options->get( 'local_seo', LocalSeoModule::defaults() ); if ( '' === trim( (string) $local['business_type'] ) || 'LocalBusiness' === $local['business_type'] ) { $local['business_type'] = (string) $pack['type']; } if ( '' !== $city && '' === trim( (string) $local['city'] ) ) { $local['city'] = $city; $changed[] = sprintf( /* translators: %s: city name. */ __( 'City set to %s', 'ai-rank-suite' ), $city ); } if ( '' === trim( (string) $local['country'] ) && ! empty( $region['countries'] ) ) { $local['country'] = (string) $region['countries'][0]; } $options->set( 'local_seo', $local, LocalSeoModule::defaults() ); /* ------------------------------------------------ monitoring prompts */ if ( $want_prompts && class_exists( AiVisibilityModule::class ) ) { $visibility = $options->get( 'ai_visibility', AiVisibilityModule::defaults() ); $existing = array_values( array_filter( (array) $visibility['prompts'] ) ); $brand = (string) ( $schema['org_name'] ?: get_bloginfo( 'name' ) ); $added = 0; foreach ( (array) $pack['prompts'] as $template ) { $prompt = $this->fill( (string) $template, $brand, $city, (string) $pack['noun'] ); // A template that still carries an unresolved placeholder would // be monitored literally, which measures nothing. if ( false !== strpos( $prompt, '%' ) ) { continue; } if ( in_array( $prompt, $existing, true ) ) { continue; } $existing[] = $prompt; ++$added; } if ( $added > 0 ) { $visibility['prompts'] = array_slice( $existing, 0, 50 ); $options->set( 'ai_visibility', $visibility, AiVisibilityModule::defaults() ); $changed[] = sprintf( /* translators: %d: number of prompts. */ _n( '%d monitoring prompt added', '%d monitoring prompts added', $added, 'ai-rank-suite' ), $added ); } else { $kept[] = __( 'Monitoring prompts already cover this industry', 'ai-rank-suite' ); } } update_option( self::OPTION, array( 'pack' => $pack_id, 'region' => $region_id, 'city' => $city, 'applied' => time(), ), false ); return new WP_REST_Response( array( 'ok' => true, 'pack' => $pack_id, 'region' => $region_id, 'changed' => $changed, 'kept' => $kept, // Labels, not keys: "vat_id, reg_id" is a database schema, not an // instruction a customer can act on. 'identifiers' => $this->identifier_labels( (array) $region['identifiers'] ), 'directories' => array_values( (array) $region['directories'] ), 'notes' => array_values( (array) $region['notes'] ), 'faq' => array_values( (array) $pack['faq'] ), 'schema' => array_values( (array) $pack['schema'] ), ) ); } /** * Turn identifier keys into the labels the Identity screen shows. * * @param list $keys Identifier keys. * @return list */ private function identifier_labels( array $keys ): array { $known = Identity::identifiers(); $out = array(); foreach ( $keys as $key ) { $out[] = isset( $known[ $key ] ) ? (string) $known[ $key ]['label'] : (string) $key; } return $out; } /** * Append the items that are not already present. Returns how many landed. * * @param array $target Modified in place. * @param array $items Candidates. */ private function merge_list( array &$target, array $items ): int { $target = array_values( array_filter( array_map( 'strval', $target ) ) ); $lowered = array_map( 'mb_strtolower', $target ); $added = 0; foreach ( $items as $item ) { $item = trim( (string) $item ); if ( '' === $item || in_array( mb_strtolower( $item ), $lowered, true ) ) { continue; } $target[] = $item; $lowered[] = mb_strtolower( $item ); ++$added; } return $added; } /** * Resolve the placeholders a prompt template carries. * * Anything still unresolved is the caller's signal to drop the prompt * rather than monitor a literal "%city%". */ private function fill( string $template, string $brand, string $city, string $noun ): string { return str_replace( array( '%brand%', '%city%', '%type%' ), array( $brand, $city, $noun ), $template ); } }