get_global_unique_id() : ''; $imgs = array(); if ( $p->get_image_id() ) { $imgs[] = wp_get_attachment_url( $p->get_image_id() ); } foreach ( array_slice( $p->get_gallery_image_ids(), 0, 9 ) as $gid ) { $imgs[] = wp_get_attachment_url( $gid ); } $cats = array(); foreach ( $p->get_category_ids() as $cid ) { $t = get_term( $cid, 'product_cat' ); if ( $t && ! is_wp_error( $t ) ) { $cats[] = $t->name; } } $attrs = array(); foreach ( $p->get_attributes() as $attr ) { if ( is_object( $attr ) ) { $name = wc_attribute_label( $attr->get_name() ); $vals = $attr->is_taxonomy() ? wc_get_product_terms( $p->get_id(), $attr->get_name(), array( 'fields' => 'names' ) ) : $attr->get_options(); $attrs[ $name ] = array_values( array_map( 'strval', (array) $vals ) ); } } $brand = ''; $brands = wp_get_post_terms( $p->get_id(), 'product_brand', array( 'fields' => 'names' ) ); if ( ! is_wp_error( $brands ) && ! empty( $brands ) ) { $brand = $brands[0]; } return array( 'id' => (string) $p->get_id(), 'sku' => (string) $p->get_sku(), 'gtin' => $gtin, 'title' => $p->get_name(), 'description' => self::best_description( $p ), 'summary' => trim( wp_strip_all_tags( (string) $p->get_short_description() ) ), 'url' => get_permalink( $p->get_id() ), 'images' => array_values( array_filter( $imgs ) ), 'price' => (string) wc_get_price_to_display( $p ), 'regular_price' => (string) $p->get_regular_price(), 'currency' => get_woocommerce_currency(), 'availability' => $p->is_in_stock() ? 'in_stock' : 'out_of_stock', 'stock_quantity' => $p->managing_stock() ? (int) $p->get_stock_quantity() : null, 'categories' => $cats, 'brand' => $brand, 'attributes' => $attrs, 'weight' => $p->get_weight() ? $p->get_weight() . ' ' . get_option( 'woocommerce_weight_unit' ) : '', 'type' => $p->get_type(), 'rating' => $p->get_review_count() ? array( 'average' => (float) $p->get_average_rating(), 'count' => (int) $p->get_review_count() ) : null, 'updated_at' => $p->get_date_modified() ? $p->get_date_modified()->date( DATE_ATOM ) : null, ); } public static function best_description( $p ) { foreach ( array( $p->get_description(), $p->get_short_description() ) as $text ) { $text = trim( wp_strip_all_tags( (string) $text ) ); if ( '' !== $text ) { return $text; } } return $p->get_name(); } public static function query_products( $page = 1, $per_page = 100 ) { return wc_get_products( array( 'status' => 'publish', 'visibility' => 'visible', 'limit' => $per_page, 'page' => $page, 'paginate' => true, 'orderby' => 'ID', 'order' => 'ASC', ) ); } /* ---------------------------- REST -------------------------------- */ public static function register_rest() { register_rest_route( 'agentrue/v1', '/products', array( 'methods' => 'GET', 'permission_callback' => '__return_true', 'callback' => array( __CLASS__, 'rest_products' ), 'args' => array( 'page' => array( 'default' => 1, 'sanitize_callback' => 'absint' ), 'per_page' => array( 'default' => 50, 'sanitize_callback' => 'absint' ), 'q' => array( 'default' => '', 'sanitize_callback' => 'sanitize_text_field' ), ), ) ); register_rest_route( 'agentrue/v1', '/products/(?P\d+)', array( 'methods' => 'GET', 'permission_callback' => '__return_true', 'callback' => array( __CLASS__, 'rest_product' ), ) ); register_rest_route( 'agentrue/v1', '/store', array( 'methods' => 'GET', 'permission_callback' => '__return_true', 'callback' => array( __CLASS__, 'rest_store' ), ) ); } public static function rest_products( WP_REST_Request $req ) { if ( ! Agentrue_Settings::on( 'feed_enabled' ) ) { return new WP_Error( 'agentrue_disabled', 'Feed disabled', array( 'status' => 404 ) ); } $per = min( 100, max( 1, (int) $req['per_page'] ) ); $page = max( 1, (int) $req['page'] ); $args = array( 'status' => 'publish', 'visibility' => 'visible', 'limit' => $per, 'page' => $page, 'paginate' => true, ); if ( '' !== $req['q'] ) { $args['s'] = $req['q']; } $res = wc_get_products( $args ); $items = array_map( array( __CLASS__, 'product_record' ), $res->products ); return rest_ensure_response( array( 'store' => get_bloginfo( 'name' ), 'page' => $page, 'pages' => (int) $res->max_num_pages, 'total' => (int) $res->total, 'products' => $items, ) ); } public static function rest_product( WP_REST_Request $req ) { $p = wc_get_product( (int) $req['id'] ); if ( ! $p || 'publish' !== $p->get_status() || ! Agentrue_Settings::on( 'feed_enabled' ) ) { return new WP_Error( 'not_found', 'Product not found', array( 'status' => 404 ) ); } return rest_ensure_response( self::product_record( $p ) ); } public static function rest_store() { $rr = (int) get_option( 'woocommerce_refund_returns_page_id' ); return rest_ensure_response( array( 'name' => get_bloginfo( 'name' ), 'description' => get_bloginfo( 'description' ), 'url' => home_url( '/' ), 'currency' => get_woocommerce_currency(), 'country' => WC()->countries ? WC()->countries->get_base_country() : '', 'returns_policy' => ( $rr && 'publish' === get_post_status( $rr ) ) ? get_permalink( $rr ) : null, 'guest_checkout' => 'yes' === get_option( 'woocommerce_enable_guest_checkout' ), 'catalog' => rest_url( 'agentrue/v1/products' ), 'feed' => home_url( '/agentrue-feed.xml' ), ) ); } /* ---------------------------- XML --------------------------------- */ public static function xml_feed() { $out = array(); $out[] = ''; $out[] = ''; $out[] = ''; $out[] = '' . esc_xml( get_bloginfo( 'name' ) ) . ''; $out[] = '' . esc_xml( home_url( '/' ) ) . ''; $out[] = '' . esc_xml( get_bloginfo( 'description' ) ) . ''; $page = 1; do { $res = self::query_products( $page, 100 ); foreach ( $res->products as $p ) { if ( '' === (string) $p->get_price() ) { continue; } $r = self::product_record( $p ); $out[] = ''; $out[] = '' . esc_xml( $r['sku'] ? $r['sku'] : $r['id'] ) . ''; $out[] = '' . esc_xml( mb_substr( $r['title'], 0, 150 ) ) . ''; $out[] = '' . esc_xml( mb_substr( $r['description'], 0, 5000 ) ) . ''; $out[] = '' . esc_xml( $r['url'] ) . ''; if ( ! empty( $r['images'] ) ) { $out[] = '' . esc_xml( $r['images'][0] ) . ''; foreach ( array_slice( $r['images'], 1 ) as $img ) { $out[] = '' . esc_xml( $img ) . ''; } } $out[] = '' . ( 'in_stock' === $r['availability'] ? 'in_stock' : 'out_of_stock' ) . ''; $reg = $r['regular_price'] ? $r['regular_price'] : $r['price']; $out[] = '' . esc_xml( wc_format_decimal( $reg, 2 ) . ' ' . $r['currency'] ) . ''; if ( $p->is_on_sale() && $r['price'] !== $reg ) { $out[] = '' . esc_xml( wc_format_decimal( $r['price'], 2 ) . ' ' . $r['currency'] ) . ''; } $out[] = 'new'; if ( $r['gtin'] ) { $out[] = '' . esc_xml( $r['gtin'] ) . ''; } if ( $r['sku'] ) { $out[] = '' . esc_xml( $r['sku'] ) . ''; } if ( $r['brand'] ) { $out[] = '' . esc_xml( $r['brand'] ) . ''; } elseif ( ! $r['gtin'] ) { $out[] = 'no'; } if ( ! empty( $r['categories'] ) ) { $out[] = '' . esc_xml( implode( ' > ', $r['categories'] ) ) . ''; } if ( $r['weight'] ) { $out[] = '' . esc_xml( $r['weight'] ) . ''; } $out[] = ''; } $page++; } while ( $page <= (int) $res->max_num_pages && $page <= 100 ); $out[] = ''; $out[] = ''; return implode( "\n", $out ); } /* --------------------------- llms.txt ------------------------------ */ public static function llms_txt() { $lines = array(); $lines[] = '# ' . get_bloginfo( 'name' ); $lines[] = ''; $desc = get_bloginfo( 'description' ); if ( $desc ) { $lines[] = '> ' . $desc; $lines[] = ''; } $lines[] = 'This is an online store. AI agents may browse the catalog and help customers buy products.'; $lines[] = ''; $lines[] = '## Machine-readable data'; $lines[] = '- [Product catalog (JSON)](' . rest_url( 'agentrue/v1/products' ) . '): paginated, supports ?q= search'; $lines[] = '- [Store info (JSON)](' . rest_url( 'agentrue/v1/store' ) . '): currency, policies, capabilities'; $lines[] = '- [Product feed (XML)](' . home_url( '/agentrue-feed.xml' ) . ')'; $lines[] = ''; $lines[] = '## Policies'; $rr = (int) get_option( 'woocommerce_refund_returns_page_id' ); if ( $rr && 'publish' === get_post_status( $rr ) ) { $lines[] = '- [Refunds & returns](' . get_permalink( $rr ) . ')'; } $terms = function_exists( 'wc_terms_and_conditions_page_id' ) ? wc_terms_and_conditions_page_id() : 0; if ( $terms && 'publish' === get_post_status( $terms ) ) { $lines[] = '- [Terms & conditions](' . get_permalink( $terms ) . ')'; } $lines[] = ''; $lines[] = '## Product categories'; $terms_list = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => true, 'number' => 30 ) ); if ( ! is_wp_error( $terms_list ) ) { foreach ( $terms_list as $t ) { $link = get_term_link( $t ); if ( ! is_wp_error( $link ) ) { $lines[] = '- [' . $t->name . '](' . $link . ')'; } } } return implode( "\n", $lines ) . "\n"; } /* --------------------------- robots -------------------------------- */ public static function robots( $output, $public ) { if ( ! $public || ! Agentrue_Settings::on( 'allow_ai_bots' ) ) { return $output; } $add = "\n# Agentrue: allow AI shopping agents\n"; foreach ( Agentrue_Scanner::ai_bots() as $bot ) { $add .= "User-agent: {$bot}\nAllow: /\nDisallow: /wp-admin/\n\n"; } if ( Agentrue_Settings::on( 'llms_txt_enabled' ) ) { $add .= '# llms.txt: ' . home_url( '/llms.txt' ) . "\n"; } return $output . $add; } }