'https://api.feedtrix.com', 'shop_id' => '', 'api_key' => '', 'indexnow_key' => '', 'llms_txt' => 1, 'bot_tracking' => 1, 'attribution' => 1, 'json_ld' => 1, 'faq_tab' => 0, 'product_cache_ttl' => DAY_IN_SECONDS, // set from FeedTrix (POST /wp-json/feedtrix/settings) 'llms_cache_ttl' => DAY_IN_SECONDS, ); /** Feature switches that can also be changed from the FeedTrix dashboard. */ public const TOGGLES = array( 'llms_txt', 'bot_tracking', 'attribution', 'json_ld', 'faq_tab' ); private const TTL_MIN = 5 * MINUTE_IN_SECONDS; private const TTL_MAX = 30 * DAY_IN_SECONDS; private const CONNECTION_KEYS = array( 'endpoint', 'shop_id', 'api_key', 'indexnow_key' ); /** @var array|null */ private ?array $cache = null; public function register(): void { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'plugin_action_links_' . plugin_basename( FEEDTRIX_FILE ), array( $this, 'action_links' ) ); } /** * Precedence: defaults < feedtrix-config.php (from the .zip package from the dashboard) < settings saved in WP. * Empty connection fields in the settings do not override values from the config file. * * @return array */ public function all(): array { if ( null !== $this->cache ) { return $this->cache; } $defaults = self::DEFAULTS; if ( defined( 'FEEDTRIX_ENDPOINT' ) ) { $defaults['endpoint'] = (string) FEEDTRIX_ENDPOINT; // e.g. a test environment: define( 'FEEDTRIX_ENDPOINT', 'http://feedtrix-api:81' ); } $values = array_merge( $defaults, self::bundled_config() ); $saved = get_option( self::OPTION, array() ); foreach ( is_array( $saved ) ? $saved : array() as $key => $value ) { if ( in_array( $key, self::CONNECTION_KEYS, true ) && '' === (string) $value ) { continue; } $values[ $key ] = $value; } return $this->cache = $values; } /** @return array */ private static function bundled_config(): array { $file = FEEDTRIX_DIR . 'feedtrix-config.php'; if ( ! is_readable( $file ) ) { return array(); } $config = include $file; return is_array( $config ) ? array_intersect_key( array_map( 'strval', $config ), array_flip( self::CONNECTION_KEYS ) ) : array(); } public function has_bundled_config(): bool { return array() !== self::bundled_config(); } /** * Saves the access data received from FeedTrix (connection with keys from the dashboard). * * @param array $connection endpoint, shop_id, api_key, indexnow_key * @param array $account email, verified, panel_url, account */ public function save_connection( array $connection, array $account ): void { $saved = get_option( self::OPTION, array() ); $saved = array_merge( self::DEFAULTS, is_array( $saved ) ? $saved : array() ); foreach ( self::CONNECTION_KEYS as $key ) { $saved[ $key ] = (string) ( $connection[ $key ] ?? '' ); } if ( '' === $saved['endpoint'] ) { $saved['endpoint'] = $this->endpoint(); // disconnecting: the FeedTrix URL stays unchanged } update_option( self::OPTION, $saved ); if ( array() === $account ) { delete_option( self::ACCOUNT_OPTION ); } else { update_option( self::ACCOUNT_OPTION, $account, false ); } $this->flush_cache(); } /** @return array */ public function account(): array { $account = get_option( self::ACCOUNT_OPTION, array() ); return is_array( $account ) ? $account : array(); } /** * FeedTrix dashboard home. Older API versions returned the store page URL (/shops/{id}), * which gives a 404 when another account is logged in in the browser, so only the dashboard URL is kept. */ public function panel_url(): string { $url = (string) ( $this->account()['panel_url'] ?? '' ); $parts = wp_parse_url( $url ); if ( empty( $parts['scheme'] ) || empty( $parts['host'] ) ) { return ''; } return $parts['scheme'] . '://' . $parts['host'] . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) . '/'; } public static function page_url(): string { return admin_url( 'admin.php?page=' . self::PAGE ); } public function flush_cache(): void { $this->cache = null; } /** FeedTrix API URL: the FEEDTRIX_ENDPOINT constant (local tests), otherwise the URL saved at connection. */ public function endpoint(): string { if ( defined( 'FEEDTRIX_ENDPOINT' ) ) { return untrailingslashit( (string) FEEDTRIX_ENDPOINT ); } return untrailingslashit( (string) $this->all()['endpoint'] ); } public function shop_id(): string { return (string) $this->all()['shop_id']; } public function api_key(): string { return (string) $this->all()['api_key']; } public function indexnow_key(): string { $key = strtolower( (string) $this->all()['indexnow_key'] ); return 1 === preg_match( '/^[a-f0-9]{32}$/', $key ) ? $key : ''; } public function is_connected(): bool { return '' !== $this->shop_id() && '' !== $this->api_key(); } public function llms_txt_enabled(): bool { return (bool) $this->all()['llms_txt']; } public function bot_tracking_enabled(): bool { return (bool) $this->all()['bot_tracking']; } public function attribution_enabled(): bool { return (bool) $this->all()['attribution']; } public function json_ld_enabled(): bool { return (bool) $this->all()['json_ld']; } public function faq_tab_enabled(): bool { return (bool) $this->all()['faq_tab']; } public function product_cache_ttl(): int { return self::clamp_ttl( $this->all()['product_cache_ttl'] ?? DAY_IN_SECONDS ); } public function llms_cache_ttl(): int { return self::clamp_ttl( $this->all()['llms_cache_ttl'] ?? DAY_IN_SECONDS ); } /** @param mixed $seconds */ private static function clamp_ttl( $seconds ): int { return max( self::TTL_MIN, min( self::TTL_MAX, (int) $seconds ) ); } /** * Changes from FeedTrix: only the given switches and cache times, the rest stays as it is. * * @param array $toggles * @param array $cache product_ttl, llms_ttl */ public function update_remote( array $toggles, array $cache ): void { $saved = get_option( self::OPTION, array() ); $saved = array_merge( self::DEFAULTS, is_array( $saved ) ? $saved : array() ); foreach ( self::TOGGLES as $key ) { if ( array_key_exists( $key, $toggles ) ) { $saved[ $key ] = empty( $toggles[ $key ] ) ? 0 : 1; } } foreach ( array( 'product_ttl' => 'product_cache_ttl', 'llms_ttl' => 'llms_cache_ttl' ) as $from => $key ) { if ( isset( $cache[ $from ] ) && is_numeric( $cache[ $from ] ) ) { $saved[ $key ] = self::clamp_ttl( $cache[ $from ] ); } } $product_ttl_changed = $saved['product_cache_ttl'] !== $this->product_cache_ttl(); update_option( self::OPTION, $saved ); // also drops the local /llms.txt copy (FeedTrix_Llms_Txt::purge_local) $this->flush_cache(); if ( $product_ttl_changed ) { FeedTrix_Json_Ld::purge_all(); // copies saved with the old time } } /** @return array{settings: array, cache: array{product_ttl: int, llms_ttl: int}} */ public function remote_state(): array { $settings = array(); foreach ( self::TOGGLES as $key ) { $settings[ $key ] = (bool) $this->all()[ $key ]; } return array( 'settings' => $settings, 'cache' => array( 'product_ttl' => $this->product_cache_ttl(), 'llms_ttl' => $this->llms_cache_ttl(), ), ); } public function add_menu(): void { $parent = class_exists( 'WooCommerce' ) ? 'woocommerce' : 'options-general.php'; add_submenu_page( $parent, 'FeedTrix: ' . __( 'AI search optimization (AEO) for your store', 'feedtrix' ), 'FeedTrix', 'manage_options', self::PAGE, array( $this, 'render_page' ) ); } /** @param string[] $links */ public function action_links( array $links ): array { $url = admin_url( ( class_exists( 'WooCommerce' ) ? 'admin.php' : 'options-general.php' ) . '?page=' . self::PAGE ); array_unshift( $links, '' . esc_html__( 'Settings', 'feedtrix' ) . '' ); return $links; } public function register_settings(): void { register_setting( self::PAGE, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( $this, 'sanitize' ), 'default' => self::DEFAULTS, ) ); } public function enqueue_assets( string $hook ): void { if ( '_page_' . self::PAGE !== substr( $hook, -strlen( '_page_' . self::PAGE ) ) ) { return; } wp_enqueue_style( 'feedtrix-admin', plugins_url( 'assets/admin.css', FEEDTRIX_FILE ), array(), FEEDTRIX_VERSION ); } /** * @param mixed $input * @return array */ public function sanitize( $input ): array { $input = is_array( $input ) ? $input : array(); $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); // connection data is saved by onboarding (save_connection); the settings form does not change it $values = array( // the API URL is not editable in the dashboard (local tests: FEEDTRIX_ENDPOINT constant) 'endpoint' => esc_url_raw( trim( (string) ( $saved['endpoint'] ?? self::DEFAULTS['endpoint'] ) ), array( 'https', 'http' ) ), 'llms_txt' => empty( $input['llms_txt'] ) ? 0 : 1, 'bot_tracking' => empty( $input['bot_tracking'] ) ? 0 : 1, 'attribution' => empty( $input['attribution'] ) ? 0 : 1, 'json_ld' => empty( $input['json_ld'] ) ? 0 : 1, 'faq_tab' => empty( $input['faq_tab'] ) ? 0 : 1, ); // cache times are set from FeedTrix only; the settings form keeps them foreach ( array( 'product_cache_ttl', 'llms_cache_ttl' ) as $key ) { $values[ $key ] = self::clamp_ttl( $input[ $key ] ?? $saved[ $key ] ?? self::DEFAULTS[ $key ] ); } foreach ( array( 'shop_id', 'api_key', 'indexnow_key' ) as $key ) { $values[ $key ] = array_key_exists( $key, $input ) ? sanitize_text_field( (string) $input[ $key ] ) : (string) ( $saved[ $key ] ?? '' ); } if ( '' === $values['endpoint'] ) { $values['endpoint'] = self::DEFAULTS['endpoint']; } return $values; } private ?FeedTrix_Onboarding $onboarding = null; public function set_onboarding( FeedTrix_Onboarding $onboarding ): void { $this->onboarding = $onboarding; } public static function page_tab_url( string $tab ): string { return 'settings' === $tab ? add_query_arg( 'tab', 'settings', self::page_url() ) : self::page_url(); } /** * Sign-up in the FeedTrix dashboard. Dashboard URL: the FEEDTRIX_PANEL_URL constant (test environment), * otherwise the API host with "api." replaced by "panel." (api.feedtrix.com -> panel.feedtrix.com). */ public function signup_url(): string { if ( defined( 'FEEDTRIX_PANEL_URL' ) ) { $panel = untrailingslashit( (string) FEEDTRIX_PANEL_URL ); } else { $parts = wp_parse_url( $this->endpoint() ); $host = (string) ( $parts['host'] ?? '' ); $panel = 0 === strpos( $host, 'api.' ) ? ( $parts['scheme'] ?? 'https' ) . '://panel.' . substr( $host, 4 ) . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) : 'https://panel.feedtrix.com'; } return add_query_arg( array( 'utm_source' => 'woocommerce-plugin', 'utm_medium' => 'plugin' ), $panel . '/register' ); } /** Masked key: only the start and end are visible (ftx_504b••••••ed12). */ private static function mask( string $value, int $head = 4, int $tail = 4 ): string { if ( strlen( $value ) <= $head + $tail ) { return str_repeat( '•', 8 ); } return substr( $value, 0, $head ) . str_repeat( '•', 10 ) . substr( $value, -$tail ); } public function render_page(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $notice = FeedTrix_Onboarding::take_notice(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- tab selection only $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : (string) ( $notice['tab'] ?? '' ); $tab = 'settings' === $tab ? 'settings' : 'overview'; ?>

FeedTrix

render_settings( is_array( $notice ) ? $notice : array() ); } elseif ( $this->is_connected() ) { $this->render_connected(); $this->render_features_status(); $this->render_chat_preview(); } else { $this->render_intro(); $this->render_chat_preview(); } ?>
''; $steps = array( array( $icon( '' ), __( 'Your store', 'feedtrix' ), __( 'WooCommerce products, prices and stock', 'feedtrix' ), false ), array( $icon( '' ), 'FeedTrix', __( 'Writes /llms.txt, structured data and AI descriptions', 'feedtrix' ), true ), array( $icon( '' ), __( 'AI assistants', 'feedtrix' ), 'ChatGPT, Perplexity, Claude, Gemini', false ), array( $icon( '' ), __( 'New customers', 'feedtrix' ), __( 'AI recommends your products and links to your store', 'feedtrix' ), false ), ); ?>

$step ) : ?> 0 ) : ?>
account(); ?>

✉

panel_url() ) : ?>
plugin features (settings and overview tiles) */ private function features(): array { return array( 'llms_txt' => array( 'title' => __( 'Serve /llms.txt', 'feedtrix' ), 'description' => __( 'A file for AI bots with your catalog, served from your domain.', 'feedtrix' ), 'icon' => '', ), 'bot_tracking' => array( 'title' => __( 'AI bot statistics', 'feedtrix' ), 'description' => __( 'Counts visits of AI bots (no personal data) and sends them to FeedTrix every hour.', 'feedtrix' ), 'icon' => '', ), 'attribution' => array( 'title' => __( 'Sales from AI (order attribution)', 'feedtrix' ), 'description' => __( 'Marks paid orders from customers who came from AI assistants in the last 30 days.', 'feedtrix' ), 'icon' => '', ), 'json_ld' => array( 'title' => __( 'FeedTrix structured data (JSON-LD) on product pages', 'feedtrix' ), 'description' => __( 'Adds AI descriptions, attributes and FAQ to the product data read by AI and search engines.', 'feedtrix' ), 'icon' => '', ), 'faq_tab' => array( 'title' => __( 'Show the questions and answers tab on product pages', 'feedtrix' ), 'description' => __( 'A visible tab with questions and answers prepared by FeedTrix.', 'feedtrix' ), 'icon' => '', ), ); } private static function icon( string $paths, int $size = 22 ): string { return ''; } /** After connecting: what FeedTrix does in the store (tiles with icons and on / off state). */ private function render_features_status(): void { $values = $this->all(); ?>

features() as $key => $feature ) : ?>

→

'publish', 'visibility' => 'catalog', 'limit' => 20, 'orderby' => 'rand', 'stock_status' => 'instock' ) ); $priced = array_values( array_filter( $candidates, static fn ( $p ) => $p instanceof WC_Product && '' !== $p->get_price() ) ); $with_image = array_values( array_filter( $priced, static fn ( WC_Product $p ) => (int) $p->get_image_id() > 0 ) ); $product = $with_image[0] ?? $priced[0] ?? null; } if ( $product instanceof WC_Product ) { $image = wp_get_attachment_image_url( (int) $product->get_image_id(), 'thumbnail' ); return array( 'name' => $product->get_name(), 'price' => function_exists( 'wc_price' ) && '' !== $product->get_price() ? wp_strip_all_tags( wc_price( (float) $product->get_price() ) ) : '', 'image' => $image ? (string) $image : '', ); } return array( 'name' => __( 'Your bestseller', 'feedtrix' ), 'price' => '', 'image' => '' ); } /** Chat mockup: an AI assistant recommends a product from this store and links to it. */ private function render_chat_preview(): void { $product = $this->preview_product(); $domain = (string) wp_parse_url( home_url(), PHP_URL_HOST ); ?>

$notice */ private function render_settings( array $notice ): void { $this->render_connection( is_array( $notice['old'] ?? null ) ? $notice['old'] : array() ); $values = $this->all(); $features = $this->features(); ?>

$feature ) : ?>
$old form values after an error */ private function render_connection( array $old ): void { $post = admin_url( 'admin-post.php' ); ?>
is_connected() ) : ?>

has_bundled_config() ) : ?>

shop_id(), 8 ) ); ?>
api_key(), 0 ) ); ?>
has_bundled_config() ) : ?>